Skip to content

Commit 1c914d9

Browse files
committed
Merge pull request #132 from ddproxy/develop
To GeoJSON and tests
2 parents bfd98cb + 937a48b commit 1c914d9

File tree

12 files changed

+129
-5
lines changed

12 files changed

+129
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require-dev": {
2020
"phpunit/phpunit": "<5.0",
2121
"phpunit/phpcov": "*",
22-
"satooshi/php-coveralls": "dev-master"
22+
"satooshi/php-coveralls": "~1.0"
2323
},
2424
"autoload": {
2525
"psr-0": {

lib/CrEOF/Spatial/PHP/Types/AbstractGeometry.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public function __toString()
5555
return $this->$method($this->toArray());
5656
}
5757

58+
/**
59+
* @return string
60+
*/
61+
public function toJson()
62+
{
63+
$json['type'] = $this->getType();
64+
$json['coordinates'] = $this->toArray();
65+
return json_encode($json);
66+
}
67+
5868
/**
5969
* @return null|int
6070
*/

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/LineStringTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function testBadLineString()
157157

158158
public function testLineStringFromArraysToString()
159159
{
160-
$expected = '0 0,0 5,5 0,0 0';
160+
$expected = '0 0,0 5,5 0,0 0';
161161
$lineString = new LineString(
162162
array(
163163
array(0, 0),
@@ -169,4 +169,19 @@ public function testLineStringFromArraysToString()
169169

170170
$this->assertEquals($expected, (string) $lineString);
171171
}
172+
173+
public function testJson()
174+
{
175+
$expected = "{\"type\":\"LineString\",\"coordinates\":[[0,0],[0,5],[5,0],[0,0]]}";
176+
177+
$lineString = new LineString(
178+
array(
179+
array(0, 0),
180+
array(0, 5),
181+
array(5, 0),
182+
array(0, 0)
183+
)
184+
);
185+
$this->assertEquals($expected, $lineString->toJson());
186+
}
172187
}

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiLineStringTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,28 @@ public function testMultiLineStringFromArraysToString()
205205

206206
$this->assertEquals($expected, $result);
207207
}
208+
209+
public function testJson()
210+
{
211+
$expected = '{"type":"MultiLineString","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]]}';
212+
$lineStrings = array(
213+
array(
214+
array(0, 0),
215+
array(10, 0),
216+
array(10, 10),
217+
array(0, 10),
218+
array(0, 0)
219+
),
220+
array(
221+
array(0, 0),
222+
array(10, 0),
223+
array(10, 10),
224+
array(0, 10),
225+
array(0, 0)
226+
)
227+
);
228+
$multiLineString = new MultiLineString($lineStrings);
229+
230+
$this->assertEquals($expected, $multiLineString->toJson());
231+
}
208232
}

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPointTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,19 @@ public function testMultiPointFromArraysToString()
141141

142142
$this->assertEquals($expected, (string) $multiPoint);
143143
}
144+
145+
public function testJson()
146+
{
147+
$expected = '{"type":"MultiPoint","coordinates":[[0,0],[0,5],[5,0],[0,0]]}';
148+
$multiPoint = new MultiPoint(
149+
array(
150+
array(0, 0),
151+
array(0, 5),
152+
array(5, 0),
153+
array(0, 0)
154+
)
155+
);
156+
157+
$this->assertEquals($expected, $multiPoint->toJson());
158+
}
144159
}

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPolygonTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,32 @@ public function testSolidMultiPolygonFromArraysToString()
233233

234234
$this->assertEquals($expected, $result);
235235
}
236+
237+
public function testJson()
238+
{
239+
$expected = '{"type":"MultiPolygon","coordinates":[[[[0,0],[10,0],[10,10],[0,10],[0,0]]],[[[5,5],[7,5],[7,7],[5,7],[5,5]]]]}';
240+
$polygons = array(
241+
array(
242+
array(
243+
array(0, 0),
244+
array(10, 0),
245+
array(10, 10),
246+
array(0, 10),
247+
array(0, 0)
248+
)
249+
),
250+
array(
251+
array(
252+
array(5, 5),
253+
array(7, 5),
254+
array(7, 7),
255+
array(5, 7),
256+
array(5, 5)
257+
)
258+
)
259+
);
260+
$multiPolygon = new MultiPolygon($polygons);
261+
262+
$this->assertEquals($expected, $multiPolygon->toJson());
263+
}
236264
}

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PointTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,12 @@ public function testPointWrongArgumentTypes()
219219
{
220220
new Point(array(), array(), '1234');
221221
}
222+
223+
public function testJson()
224+
{
225+
$expected = '{"type":"Point","coordinates":[5,5]}';
226+
$point = new Point(array(5, 5));
227+
228+
$this->assertEquals($expected, $point->toJson());
229+
}
222230
}

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PolygonTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,28 @@ public function testSolidPolygonFromArraysToString()
193193

194194
$this->assertEquals($expected, $result);
195195
}
196+
197+
public function testJson()
198+
{
199+
$expected = '{"type":"Polygon","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]]}';
200+
$rings = array(
201+
array(
202+
array(0, 0),
203+
array(10, 0),
204+
array(10, 10),
205+
array(0, 10),
206+
array(0, 0)
207+
),
208+
array(
209+
array(0, 0),
210+
array(10, 0),
211+
array(10, 10),
212+
array(0, 10),
213+
array(0, 0)
214+
)
215+
);
216+
$polygon = new Polygon($rings);
217+
218+
$this->assertEquals($expected, $polygon->toJson());
219+
}
196220
}

tests/travis/composer.orm2.3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require-dev": {
2020
"phpunit/phpunit": "<5.0",
2121
"phpunit/phpcov": "*",
22-
"satooshi/php-coveralls": "dev-master"
22+
"satooshi/php-coveralls": "~1.0"
2323
},
2424
"autoload": {
2525
"psr-0": {

0 commit comments

Comments
 (0)