Skip to content

chillerlan/php-geojson-helpers

Repository files navigation

chillerlan/php-geojson-helpers

PHP Version Support version license Continuous Integration Coverage Scrunitizer Packagist downloads

Documentation

Requirements

Installation

requires composer

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

{
	"require": {
		"php": "^7.4 || ^8.0",
		"chillerlan/php-geojson-helpers": "dev-master"
	}
}

Profit!

Usage

FeatureCollection

$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();

// as of v2.x via JsonSerializable
$json = json_encode($featureCollection);
{
    "type":"FeatureCollection",
    "bbox":[0, 0, 1024, 1024],
    "features":[
        {
            "type":"Feature",
            "geometry":{
                "type":"Point",
                "coordinates":[512, 512]
            },
            "properties":{
                "id":1
            }
        }
    ]
}

PolylineSimplifyer

$polylineCoords = [[11, 12], [21, 22], [31, 32], /* ... */];

$ps             = new PolylineSimplifyer($polylineCoords);
$simplified     = $ps->simplify(5, true);

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
jsonSerialize() array from JsonSerializable

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

PolylineSimplifyer

method return description
__construct(array $polylineCoords) - an array of polyline coordiantes: [[x1, y1], [x2, y2], ...]
simplify(float $tolerance = 1, bool $highestQuality = false) array