chillerlan/php-geojson-helpers

1.0.0 2019-02-14 16:26 UTC

README

version license Travis Coverage Scrunitizer Packagist downloads PayPal donate

Documentation

Requirements

  • PHP 7.2+

Installation

requires composer

composer.json (note: replace dev-master with a version boundary)

{
	"require": {
		"php": "^7.2",
		"chillerlan/php-geojson-helpers": "dev-master"
	}
}

Manual installation

Download the desired version of the package from master or release and extract the contents to your project folder. After that:

  • run composer install to install the required dependencies and generate /vendor/autoload.php.
  • if you use a custom autoloader, point the namespace chillerlan\GeoJSON to the folder src of the package

Profit!

Usage

$featureCollection = (new FeatureCollection)->setBbox([0, 0, 1024, 1024]);

// add a single feature
$feature = new Feature([512, 512], 'Point', 1);
$featureCollection->addFeature($feature);

// add an iterable of features
$featureCollection->addFeatures([$feature, /* ... more features ... */]);

// create the GeoJSON, feed leaflet
$json = $featureCollection->toJSON();
{
    "type":"FeatureCollection",
    "bbox":[0, 0, 1024, 1024],
    "features":[
        {
            "type":"Feature",
            "geometry":{
                "type":"Point",
                "coordinates":[512, 512]
            },
            "properties":{
                "id":1
            }
        }
    ]
}

API

Feature methods

method return description
__construct(array $coords = null, string $type = null, $id = null) - coords: [x, y]
setGeometry(array $coords, string $type) Feature coords: [x, y], type is one of Feature::types
setProperties(array $properties) Feature
setID($id) Feature

FeatureCollection methods

method return description
__construct(iterable $features = null) -
addFeature(Feature $feature) FeatureCollection
addFeatures(iterable $features) FeatureCollection
clearFeatures() FeatureCollection

common methods to Feature and FeatureCollection

method return description
setBbox(array $bbox) Feature/FeatureCollection
toArray() array
toJSON(int $options = null) string

ContinentRect

method return description
__construct(array $continent_rect) - NW/SE corners [[nw_x, nw_y],[se_x, se_y]]
getBounds() array
getCenter() array
getPoly() array