|
| 1 | +# chillerlan/php-geojson-helpers |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +[![version][packagist-badge]][packagist] |
| 6 | +[![license][license-badge]][license] |
| 7 | +[![Travis][travis-badge]][travis] |
| 8 | +[![Coverage][coverage-badge]][coverage] |
| 9 | +[![Scrunitizer][scrutinizer-badge]][scrutinizer] |
| 10 | +[![Packagist downloads][downloads-badge]][downloads] |
| 11 | +[![PayPal donate][donate-badge]][donate] |
| 12 | + |
| 13 | +[packagist-badge]: https://img.shields.io/packagist/v/chillerlan/php-geojson-helpers.svg?style=flat-square |
| 14 | +[packagist]: https://packagist.org/packages/chillerlan/php-geojson-helpers |
| 15 | +[license-badge]: https://img.shields.io/github/license/chillerlan/php-geojson-helpers.svg?style=flat-square |
| 16 | +[license]: https://github.com/chillerlan/php-geojson-helpers/blob/master/LICENSE |
| 17 | +[travis-badge]: https://img.shields.io/travis/chillerlan/php-geojson-helpers.svg?style=flat-square |
| 18 | +[travis]: https://travis-ci.org/chillerlan/php-geojson-helpers |
| 19 | +[coverage-badge]: https://img.shields.io/codecov/c/github/chillerlan/php-geojson-helpers.svg?style=flat-square |
| 20 | +[coverage]: https://codecov.io/github/chillerlan/php-geojson-helpers |
| 21 | +[scrutinizer-badge]: https://img.shields.io/scrutinizer/g/chillerlan/php-geojson-helpers.svg?style=flat-square |
| 22 | +[scrutinizer]: https://scrutinizer-ci.com/g/chillerlan/php-geojson-helpers |
| 23 | +[downloads-badge]: https://img.shields.io/packagist/dt/chillerlan/php-geojson-helpers.svg?style=flat-square |
| 24 | +[downloads]: https://packagist.org/packages/chillerlan/php-geojson-helpers/stats |
| 25 | +[donate-badge]: https://img.shields.io/badge/donate-paypal-ff33aa.svg?style=flat-square |
| 26 | +[donate]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WLYUNAT9ZTJZ4 |
| 27 | + |
| 28 | +# Documentation |
| 29 | + |
| 30 | +## Requirements |
| 31 | +- PHP 7.2+ |
| 32 | + |
| 33 | +## Installation |
| 34 | +**requires [composer](https://getcomposer.org)** |
| 35 | + |
| 36 | +*composer.json* (note: replace `dev-master` with a [version boundary](https://getcomposer.org/doc/articles/versions.md)) |
| 37 | +```json |
| 38 | +{ |
| 39 | + "require": { |
| 40 | + "php": "^7.2", |
| 41 | + "chillerlan/php-geojson-helpers": "dev-master" |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### Manual installation |
| 47 | +Download the desired version of the package from [master](https://github.com/chillerlan/php-geojson-helpers/archive/master.zip) or |
| 48 | +[release](https://github.com/chillerlan/php-geojson-helpers/releases) and extract the contents to your project folder. After that: |
| 49 | +- run `composer install` to install the required dependencies and generate `/vendor/autoload.php`. |
| 50 | +- if you use a custom autoloader, point the namespace `chillerlan\GeoJSON` to the folder `src` of the package |
| 51 | + |
| 52 | +Profit! |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +```php |
| 57 | +$featureCollection = (new FeatureCollection)->setBbox([0, 0, 1024, 1024]); |
| 58 | + |
| 59 | +// add a single feature |
| 60 | +$feature = new Feature([512, 512], 'Point', 1); |
| 61 | +$featureCollection->addFeature($feature); |
| 62 | + |
| 63 | +// add an iterable of features |
| 64 | +$featureCollection->addFeatures([$feature, /* ... more features ... */]); |
| 65 | + |
| 66 | +// create the GeoJSON, feed leaflet |
| 67 | +$json = $featureCollection->toJSON(); |
| 68 | +``` |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + "type":"FeatureCollection", |
| 73 | + "bbox":[0, 0, 1024, 1024], |
| 74 | + "features":[ |
| 75 | + { |
| 76 | + "type":"Feature", |
| 77 | + "geometry":{ |
| 78 | + "type":"Point", |
| 79 | + "coordinates":[512, 512] |
| 80 | + }, |
| 81 | + "properties":{ |
| 82 | + "id":1 |
| 83 | + } |
| 84 | + } |
| 85 | + ] |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## API |
| 90 | + |
| 91 | +### `Feature` methods |
| 92 | +method | return | description |
| 93 | +------ | ------ | ----------- |
| 94 | +`__construct(array $coords = null, string $type = null, $id = null)` | - | coords: `[x, y]` |
| 95 | +`setGeometry(array $coords, string $type)` | `Feature` | coords: `[x, y]`, type is one of `Feature::types` |
| 96 | +`setProperties(array $properties)` | `Feature` | |
| 97 | +`setID($id)` | `Feature` | |
| 98 | + |
| 99 | +### `FeatureCollection` methods |
| 100 | +method | return | description |
| 101 | +------ | ------ | ----------- |
| 102 | +`__construct(iterable $features = null)` | - | |
| 103 | +`addFeature(Feature $feature)` | `FeatureCollection` | |
| 104 | +`addFeatures(iterable $features)` | `FeatureCollection` | |
| 105 | +`clearFeatures()` | `FeatureCollection` | |
| 106 | + |
| 107 | +### common methods to `Feature` and `FeatureCollection` |
| 108 | + |
| 109 | +method | return | description |
| 110 | +------ | ------ | ----------- |
| 111 | +`setBbox(array $bbox)` | `Feature`/`FeatureCollection` | |
| 112 | +`toArray()` | array | |
| 113 | +`toJSON(int $options = null)` | string | |
| 114 | + |
| 115 | +### `ContinentRect` |
| 116 | + |
| 117 | +method | return | description |
| 118 | +------ | ------ | ----------- |
| 119 | +`__construct(array $continent_rect)` | - | NW/SE corners `[[nw_x, nw_y],[se_x, se_y]]` |
| 120 | +`getBounds()` | array | |
| 121 | +`getCenter()` | array | |
| 122 | +`getPoly()` | array | |
0 commit comments