Use GeoJSON data as a PHP Class Object, including handy object methods.
GeoJSON is a great format for sharing mapping data across different systems and
programming languages, especially JavaScript. But on its own, GeoJSON is just a
block of data. This GeoJSON
Object adds handy reference and utility functions
to the data, without changing how you would access the data were it an ordinary
JavaScript Object.
$featureRaw = [
"type" => "Feature",
"geometry" => [
"type" => "Polygon",
"coordinates" => [[[10,0],[50,10],[40,50],[0,40]]]
]
];
$featureObject = new \GeoJSON\Feature( $featureRaw );
echo json_encode( $featureObject ), PHP_EOL;
// {"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[10,0],[50,10],[40,50],[0,40],[10,0]]]}}
// Note that the polygon coordinate ring was automatically "closed" so the first and last points are the same.
echo json_encode( $featureObject->bbox ), PHP_EOL;
// [0,0,50,50]
$fcRaw = [
"type" => "FeatureCollection",
"features" => [
[
"type" => "Feature",
"geometry" => [
"type" => "Point",
"coordinates" => [50,10]
],
"properties" => [
"id" => 1,
"label" => "First Base"
]
],
[
"type" => "Feature",
"geometry" => [
"type" => "Point",
"coordinates" => [40,50]
],
"properties" => [
"id" => 2,
"label" => "Second Base"
]
],
]
];
$myBases = new \GeoJSON\FeatureCollection( $fcRaw );
echo json_encode( $fcObj->bbox ), PHP_EOL;
// [40,10,50,50]
- Download or clone this repo.
- Copy the
src/GeoJSON
folder into the directory you keep your PHP classes. include
the GeoJSON class files, or use the autoloader of your choice.
There are currently four object classes at your disposal in this script, each starts with the standard GeoJSON data format, then adds a few convenience methods.
- FeatureCollection
- Feature
- Geometry
- Bbox
All class object constructors in GeoJSON-php expect input in the the standard GeoJSON format. See RFC7946 for details.
$fc = new \GeoJSON\FeatureCollection( $featureCollectionData );
$f = new \GeoJSON\Feature( $featureData );
$g = new \GeoJSON\Geometry( $featureGeometryData );
For all classes, the bbox
property is automatically calculated when accessed.
$bbox = $g->bbox; // Object property access,
$southernmostLatitude = $f['bbox'][0]; // or array access.
Class methods and housekeeping properties are automatically stripped out by the
jsonSerialize()
method and when using json_encode( $myGeoJsonObj )
.
$jsonString = json_encode( $myFeatureClassObject );
You can turn any GeoJSON Class Object back into a simple associative array with
the toArray()
method.
$plainObject = $myGeoJsonClassObject->toArray();