chillerlan/php-geojson-helpers

PHP 7.2+

Maintainers

👁 codemasher

Package info

github.com/chillerlan/php-geojson-helpers

pkg:composer/chillerlan/php-geojson-helpers

Statistics

Installs: 2 251

Dependents: 1

Suggesters: 0

Stars: 2

Open Issues: 0

2.0.0 2023-01-08 15:13 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 21bbb1354d4452fab3462433eca39d7285488193

  • Smiley <smiley.woop@chillerlan.net>

helpergeojson

This package is auto-updated.

Last update: 2026-06-08 22:52:54 UTC


README

👁 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