Skip to content

Commit 91a2bcd

Browse files
committed
1 parent 33e2848 commit 91a2bcd

14 files changed

+824
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
composer.lock

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Smiley <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 |

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "chillerlan/php-geojson-helpers",
3+
"description": " PHP 7.2+",
4+
"homepage": "https://github.com/chillerlan/php-geojson-helpers",
5+
"license": "MIT",
6+
"keywords": [
7+
"geojson", "helper"
8+
],
9+
"authors": [
10+
{
11+
"name": "Smiley",
12+
"email": "[email protected]",
13+
"homepage": "https://github.com/codemasher"
14+
}
15+
],
16+
"require": {
17+
"php": "^7.2",
18+
"ext-json": "*"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "^7.5"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"chillerlan\\GeoJSON\\": "src/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"chillerlan\\GeoJSONTest\\": "tests/",
31+
"chillerlan\\GeoJSONExamples\\": "examples/"
32+
}
33+
}
34+
}

phpmd.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="chillerlan/php-geojson-helpers PMD ruleset"
3+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
6+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
7+
<description>chillerlan/php-geojson-helpers PMD ruleset</description>
8+
<exclude-pattern>*/examples/*</exclude-pattern>
9+
<exclude-pattern>*/tests/*</exclude-pattern>
10+
<rule ref="rulesets/cleancode.xml"/>
11+
<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
12+
<priority>1</priority>
13+
<properties>
14+
<property name="maximum" value="100" />
15+
</properties>
16+
</rule>
17+
<rule ref="rulesets/controversial.xml">
18+
<exclude name="CamelCaseMethodName"/>
19+
<exclude name="CamelCasePropertyName"/>
20+
<exclude name="CamelCaseParameterName"/>
21+
<exclude name="CamelCaseVariableName"/>
22+
</rule>
23+
<rule ref="rulesets/design.xml"/>
24+
<rule ref="rulesets/naming.xml">
25+
<exclude name="LongVariable"/>
26+
<exclude name="ShortVariable"/>
27+
</rule>
28+
<rule ref="rulesets/unusedcode.xml"/>
29+
</ruleset>

phpunit.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
>
12+
<filter>
13+
<whitelist processUncoveredFilesFromWhitelist="true">
14+
<directory suffix=".php">./src</directory>
15+
</whitelist>
16+
</filter>
17+
<testsuites>
18+
<testsuite name="php-geojson-helpers test suite">
19+
<directory suffix=".php">./tests</directory>
20+
</testsuite>
21+
</testsuites>
22+
</phpunit>

src/ContinentRect.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Class ContinentRect
4+
*
5+
* @filesource ContinentRect.php
6+
* @created 25.06.2018
7+
* @package chillerlan\GeoJSON
8+
* @author smiley <[email protected]>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\GeoJSON;
14+
15+
class ContinentRect{
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $rect;
21+
22+
/**
23+
* ContinentRect constructor.
24+
*
25+
* @param array $continent_rect (NW/SE corners [[nw_x, nw_y],[se_x, se_y]])
26+
*/
27+
public function __construct(array $continent_rect){
28+
$this->rect = $continent_rect;
29+
}
30+
31+
/**
32+
* returns bounds for L.LatLngBounds() (NE/SW corners)
33+
*
34+
* @return array
35+
*/
36+
public function getBounds():array{
37+
return [
38+
[$this->rect[0][0], $this->rect[1][1]],
39+
[$this->rect[1][0], $this->rect[0][1]],
40+
];
41+
}
42+
43+
/**
44+
* returns the center of the rectangle
45+
*
46+
* @return array
47+
*/
48+
public function getCenter():array{
49+
return [
50+
($this->rect[0][0] + $this->rect[1][0]) / 2,
51+
($this->rect[0][1] + $this->rect[1][1]) / 2,
52+
];
53+
}
54+
55+
/**
56+
* returns a polygon made of the rectangles corners
57+
*
58+
* @return array
59+
*/
60+
public function getPoly():array{
61+
return [[
62+
[$this->rect[0][0], $this->rect[0][1]],
63+
[$this->rect[1][0], $this->rect[0][1]],
64+
[$this->rect[1][0], $this->rect[1][1]],
65+
[$this->rect[0][0], $this->rect[1][1]]
66+
]];
67+
}
68+
69+
}

0 commit comments

Comments
 (0)