1.0.1 2024-01-27 17:25 UTC

This package is auto-updated.

Last update: 2024-04-27 18:04:33 UTC


README

Installation

The library is published as a package and is installable via Composer:

composer require nikopeikrishvili/geojson

Creating GeoJson Object

You can create Feature object and extract GeoJson string from it, you need just provide Coordinates, for example :

Polygon

$feature = new GeoJSON\FeatureTypes\Polygon([/**coordinates array**/]);

Multi Polygon

$feature = new GeoJSON\FeatureTypes\MultiPolygon([/**coordinates array**/]);

Point

$feature = new GeoJSON\FeatureTypes\Point([/**coordinates array**/]);

Multi Point

$feature = new GeoJSON\FeatureTypes\MultiPoint([/**coordinates array**/]);

LineString

$feature = new GeoJSON\FeatureTypes\LineString([/**coordinates array**/]);

MultiLineString

$feature = new GeoJSON\FeatureTypes\MultiLineString([/**coordinates array**/]);

Generate GeoJson String

$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
            ]
          ]
        ]
      }
    }
  ]
}

Examples :

Polygon - Create Polygon Feature
Point - Create Point Feature
MultiPolygon - Create MultiPolygon Feature
MultiPoint - Create MultiPoint Feature
LineString - Create LineString Feature
MultiLineString - Create MultiLineString Feature

GeoJson as Object

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