chillerlan / php-geojson-helpers
PHP 7.2+
                                    Fund package maintenance!
                                                                            
                                                                                                                                        Ko Fi
                                                                                    
                                                                            
                                                                                                                                        www.paypal.com/donate?hosted_button_id=WLYUNAT9ZTJZ4
                                                                                    
                                                                
Installs: 2 165
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/chillerlan/php-geojson-helpers
Requires
- php: ^7.4 || ^8.0
 - ext-json: *
 
Requires (Dev)
- phan/phan: ^5.4
 - phpunit/phpunit: ^9.5
 
This package is auto-updated.
Last update: 2025-10-08 21:26:30 UTC
README
Documentation
Requirements
- PHP 7.4+
 
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 |