Validate GeoJson to RFC 7946 standard
The library is published as a package and is installable via Composer:
composer require nikopeikrishvili/geojson
You can create Feature object and extract GeoJson string from it, you need just provide Coordinates, for example :
$feature = new GeoJSON\FeatureTypes\Polygon([/**coordinates array**/]);
$feature = new GeoJSON\FeatureTypes\MultiPolygon([/**coordinates array**/]);
$feature = new GeoJSON\FeatureTypes\Point([/**coordinates array**/]);
$feature = new GeoJSON\FeatureTypes\MultiPoint([/**coordinates array**/]);
$feature = new GeoJSON\FeatureTypes\LineString([/**coordinates array**/]);
$feature = new GeoJSON\FeatureTypes\MultiLineString([/**coordinates array**/]);
$geoJson = $feature->asGeoJson();
$geoJsonString = $geoJson->asString();
echo $geoJsonString.PHP_EOL;
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-72.04315136683675,
29.451187895050822
],
[
-72.04315136683675,
27.849712263460177
],
[
-68.23275413868745,
27.849712263460177
],
[
-68.23275413868745,
29.451187895050822
],
[
-72.04315136683675,
29.451187895050822
]
]
]
}
}
]
}
Polygon - Create Polygon Feature
Point - Create Point Feature
MultiPolygon - Create MultiPolygon Feature
MultiPoint - Create MultiPoint Feature
LineString - Create LineString Feature
MultiLineString - Create MultiLineString Feature
You can use this library to validate GeoJson to RFC 7946 standard
Currently, it supports following GeoJson types:
Polygon - Polygon Feature
Point - Point Feature
MultiPolygon - MultiPolygon Feature
MultiPoint - MultiPoint Feature
LineString - LineString Feature
MultiLineString - MultiLineString Feature
Package supports stdclass, string and array as an input
// String Based
$point = '{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[-90,180]}}';
$geojson = new GeoJSON($point);
// Array Based
$geojson = new GeoJSON([
'type' => 'Feature',
'properties' => [],
'geometry' => [
'type' => 'Point',
'coordinates' => [-90, 180]
]
]);
// StdClass Based
$point = '{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[-90,180]}}';
$geojson = new GeoJSON(json_decode($point));
Note: if your geojson starts with Feature instead of FeatureCollection, package will wrap it with FeatureCollection