getSRID, setSRID
[geos.git] / php / test / test.php
blob0b86d7779352a539c6067f19ce4dc1ce2902de75
1 <?php
3 # Run with:
4 # php -n -d enable_dl=On -d extension_dir=.. test.php
6 dl("geos.so");
8 require_once 'PHPUnit/Framework.php';
11 class test extends PHPUnit_Framework_TestCase
13 public function testGEOSVersion()
15 $this->assertContains('-CAPI-', GEOSVersion());
18 public function testConstants()
20 $this->assertEquals(1, GEOSBUF_CAP_ROUND);
21 $this->assertEquals(2, GEOSBUF_CAP_FLAT);
22 $this->assertEquals(3, GEOSBUF_CAP_SQUARE);
24 $this->assertEquals(1, GEOSBUF_JOIN_ROUND);
25 $this->assertEquals(2, GEOSBUF_JOIN_MITRE);
26 $this->assertEquals(3, GEOSBUF_JOIN_BEVEL);
28 $this->assertEquals(0, GEOS_POINT);
29 $this->assertEquals(1, GEOS_LINESTRING);
30 $this->assertEquals(2, GEOS_LINEARRING);
31 $this->assertEquals(3, GEOS_POLYGON);
32 $this->assertEquals(4, GEOS_MULTIPOINT);
33 $this->assertEquals(5, GEOS_MULTILINESTRING);
34 $this->assertEquals(6, GEOS_MULTIPOLYGON);
35 $this->assertEquals(7, GEOS_GEOMETRYCOLLECTION);
38 public function testWKTReader__construct()
40 $reader = new GEOSWKTReader();
41 $this->assertNotNull($reader);
44 public function testWKTReader_read()
46 $reader = new GEOSWKTReader();
48 /* Good WKT */
49 $geom = $reader->read('POINT(0 0)');
50 $this->assertNotNull($geom);
51 $geom = $reader->read('POINT EMPTY');
52 $this->assertNotNull($geom);
53 $geom = $reader->read('MULTIPOINT(0 0 1, 2 3 4)');
54 $this->assertNotNull($geom);
55 $geom = $reader->read('MULTIPOINT((0 0), (2 3))');
56 $this->assertNotNull($geom);
57 $geom = $reader->read('MULTIPOINT EMPTY');
58 $this->assertNotNull($geom);
59 $geom = $reader->read('LINESTRING(0 0 1, 2 3 4)');
60 $this->assertNotNull($geom);
61 $geom = $reader->read('LINESTRING EMPTY');
62 $this->assertNotNull($geom);
63 $geom = $reader->read('MULTILINESTRING((0 0 1, 2 3 4),
64 (10 10 2, 3 4 5))');
65 $this->assertNotNull($geom);
66 $geom = $reader->read('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))');
67 $this->assertNotNull($geom);
68 $geom = $reader->read('POLYGON EMPTY');
69 $this->assertNotNull($geom);
70 $geom = $reader->read('MULTIPOLYGON(
71 ((0 0, 1 0, 1 1, 0 1, 0 0)),
72 ((10 10, 10 14, 14 14, 14 10, 10 10),
73 (11 11, 11 12, 12 12, 12 11, 11 11))
74 )');
75 $this->assertNotNull($geom);
76 $geom = $reader->read('MULTIPOLYGON EMPTY');
77 $this->assertNotNull($geom);
78 $geom = $reader->read('GEOMETRYCOLLECTION(
79 MULTIPOLYGON(
80 ((0 0, 1 0, 1 1, 0 1, 0 0)),
81 ((10 10, 10 14, 14 14, 14 10, 10 10),
82 (11 11, 11 12, 12 12, 12 11, 11 11))
84 POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)),
85 MULTILINESTRING((0 0, 2 3), (10 10, 3 4)),
86 LINESTRING(0 0, 2 3),
87 MULTIPOINT(0 0, 2 3),
88 POINT(9 0)
89 )');
90 $this->assertNotNull($geom);
91 $geom = $reader->read('GEOMETRYCOLLECTION EMPTY');
92 $this->assertNotNull($geom);
94 /* BOGUS WKT */
95 try {
96 $reader->read("MULTIDOT(0 1 2 3)");
97 $this->assertTrue(FALSE); # this is just to fail if we get here
98 } catch (Exception $e) {
99 $this->assertContains('ParseException', $e->getMessage());
103 public function testWKTWriter__construct()
105 $writer = new GEOSWKTWriter();
106 $this->assertNotNull($writer);
109 public function testWKTWriter_write()
111 $writer = new GEOSWKTWriter();
112 $reader = new GEOSWKTReader();
114 try {
115 $writer->write(1);
116 $this->assertTrue(FALSE); # this is just to fail if we get here
117 } catch (Exception $e) {
118 $this->assertContains('expects parameter 1', $e->getMessage());
121 $g = $reader->read('POINT(6 7)');
123 $this->assertEquals('POINT (6.0000000000000000 7.0000000000000000)',
124 $writer->write($g));
127 public function testWKTWriter_setTrim()
129 $writer = new GEOSWKTWriter();
130 $reader = new GEOSWKTReader();
132 $g = $reader->read('POINT(6 7)');
133 $this->assertNotNull($g);
135 $writer->setTrim(TRUE);
136 $this->assertEquals('POINT (6 7)',
137 $writer->write($g));
139 $writer->setTrim(FALSE);
140 $this->assertEquals('POINT (6.0000000000000000 7.0000000000000000)',
141 $writer->write($g));
145 public function testWKT_roundTrip()
147 $r = new GEOSWKTReader();
148 $w = new GEOSWKTWriter();
149 $w->setTrim(TRUE);
151 $in[] = 'POINT (0 0)';
152 $in[] = 'POINT EMPTY';
153 $in[] = 'MULTIPOINT (0 1, 2 3)';
154 $in[] = 'MULTIPOINT EMPTY';
155 $in[] = 'LINESTRING (0 0, 2 3)';
156 $in[] = 'LINESTRING EMPTY';
157 $in[] = 'MULTILINESTRING ((0 1, 2 3), (10 10, 3 4))';
158 $in[] = 'MULTILINESTRING EMPTY';
159 $in[] = 'POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))';
160 $in[] = 'POLYGON EMPTY';
161 $in[] = 'MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11)))';
162 $in[] = 'MULTIPOLYGON EMPTY';
163 $in[] = 'GEOMETRYCOLLECTION (MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11))), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)), MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)), LINESTRING (0 0, 2 3), MULTIPOINT (0 0, 2 3), POINT (9 0))';
164 $in[] = 'GEOMETRYCOLLECTION EMPTY';
166 foreach ($in as $i) {
167 $this->assertEquals($i, $w->write($r->read($i)));
172 public function testWKTWriter_setRoundingPrecision()
174 $writer = new GEOSWKTWriter();
175 $reader = new GEOSWKTReader();
177 $g = $reader->read('POINT(6.123456 7.123456)');
179 $this->assertEquals('POINT (6.1234560000000000 7.1234560000000000)',
180 $writer->write($g));
182 $writer->setRoundingPrecision(2);
183 $this->assertEquals('POINT (6.12 7.12)', $writer->write($g));
185 $writer->setRoundingPrecision(5); /* rounds */
186 $this->assertEquals('POINT (6.12346 7.12346)', $writer->write($g));
188 $writer->setRoundingPrecision(1);
189 $this->assertEquals('POINT (6.1 7.1)', $writer->write($g));
191 $writer->setRoundingPrecision(0);
192 $this->assertEquals('POINT (6 7)', $writer->write($g));
196 public function testWKTWriter_setOutputDimension()
198 $reader = new GEOSWKTReader();
199 $g3d = $reader->read('POINT(1 2 3)');
200 $g2d = $reader->read('POINT(3 2)');
202 $writer = new GEOSWKTWriter();
203 $writer->setTrim(TRUE);
205 # Only 2d by default
206 $this->assertEquals('POINT (1 2)', $writer->write($g3d));
208 # 3d if requested _and_ available
209 $writer->setOutputDimension(3);
210 $this->assertEquals('POINT Z (1 2 3)', $writer->write($g3d));
211 $this->assertEquals('POINT (3 2)', $writer->write($g2d));
215 public function testWKTWriter_setOld3D()
217 $reader = new GEOSWKTReader();
218 $g3d = $reader->read('POINT(1 2 3)');
220 $writer = new GEOSWKTWriter();
221 $writer->setTrim(TRUE);
223 # New 3d WKT by default
224 $writer->setOutputDimension(3);
225 $this->assertEquals('POINT Z (1 2 3)', $writer->write($g3d));
227 # Switch to old
228 $writer->setOld3D(TRUE);
229 $this->assertEquals('POINT (1 2 3)', $writer->write($g3d));
231 # Old3d flag is not reset when changing dimensions
232 $writer->setOutputDimension(2);
233 $this->assertEquals('POINT (1 2)', $writer->write($g3d));
234 $writer->setOutputDimension(3);
235 $this->assertEquals('POINT (1 2 3)', $writer->write($g3d));
237 # Likewise, dimensions spec is not reset when changing old3d flag
238 $writer->setOld3D(FALSE);
239 $this->assertEquals('POINT Z (1 2 3)', $writer->write($g3d));
243 public function testGeometry_project()
245 $reader = new GEOSWKTReader();
247 $g = $reader->read('POINT(1 2)');
248 $g2 = $reader->read('POINT(3 4)');
250 /* The method only accept lineal geometries */
251 try {
252 $prj = $g->project($g2);
253 $this->assertTrue(FALSE); # this is just to fail if we get here
254 } catch (Exception $e) {
255 $this->assertContains('lineal', $e->getMessage());
258 $g = $reader->read('LINESTRING(0 0, 10 0)');
260 $g2 = $reader->read('POINT(0 0)');
261 $prj = $g->project($g2);
262 $this->assertEquals(0, $prj);
263 $prj = $g->project($g2, TRUE);
264 $this->assertEquals(0, $prj);
266 $g2 = $reader->read('POINT(10 0)');
267 $prj = $g->project($g2);
268 $this->assertEquals(10, $prj);
269 $prj = $g->project($g2, TRUE);
270 $this->assertEquals(1, $prj);
272 $g2 = $reader->read('POINT(5 0)');
273 $prj = $g->project($g2);
274 $this->assertEquals(5, $prj);
275 $prj = $g->project($g2, TRUE);
276 $this->assertEquals(0.5, $prj);
278 $g = $reader->read('MULTILINESTRING((0 0, 10 0),(20 10, 20 20))');
280 $g2 = $reader->read('POINT(20 0)');
281 $prj = $g->project($g2);
282 $this->assertEquals(10, $prj);
283 $prj = $g->project($g2, TRUE);
284 $this->assertEquals(0.5, $prj);
286 $g2 = $reader->read('POINT(20 5)');
287 $prj = $g->project($g2);
288 $this->assertEquals(10, $prj);
289 $prj = $g->project($g2, TRUE);
290 $this->assertEquals(0.5, $prj);
295 public function testGeometry_interpolate()
297 $reader = new GEOSWKTReader();
298 $writer = new GEOSWKTWriter();
299 $writer->setTrim(TRUE);
301 /* The method only accept LineString geometries */
302 $g = $reader->read('POINT(1 2)');
303 try {
304 $prj = $g->interpolate(0);
305 $this->assertTrue(FALSE); # this is just to fail if we get here
306 } catch (Exception $e) {
307 $this->assertContains('LineString', $e->getMessage());
310 $g = $reader->read('LINESTRING(0 0, 10 0)');
312 $prj = $g->interpolate(0);
313 $this->assertNotNull($prj);
314 $this->assertEquals('POINT (0 0)', $writer->write($prj));
315 $prj = $g->interpolate(0, TRUE);
316 $this->assertNotNull($prj);
317 $this->assertEquals('POINT (0 0)', $writer->write($prj));
319 $prj = $g->interpolate(5);
320 $this->assertNotNull($prj);
321 $this->assertEquals('POINT (5 0)', $writer->write($prj));
322 $prj = $g->interpolate(0.5, TRUE);
323 $this->assertNotNull($prj);
324 $this->assertEquals('POINT (5 0)', $writer->write($prj));
326 /* return closest on longer distance */
327 $prj = $g->interpolate(20);
328 $this->assertNotNull($prj);
329 $this->assertEquals('POINT (10 0)', $writer->write($prj));
330 $prj = $g->interpolate(2, TRUE);
331 $this->assertNotNull($prj);
332 $this->assertEquals('POINT (10 0)', $writer->write($prj));
336 public function testGeometry_buffer()
338 $reader = new GEOSWKTReader();
339 $writer = new GEOSWKTWriter();
340 $writer->setRoundingPrecision(0);
342 $g = $reader->read('POINT(0 0)');
343 $b = $g->buffer(0);
344 $this->assertEquals('POLYGON EMPTY', $writer->write($b));
346 $b = $g->buffer(10);
347 $this->assertEquals(
348 'POLYGON ((10 0, 10 -2, 9 -4, 8 -6, 7 -7, 6 -8, 4 -9, 2 -10, 0 -10, -2 -10, -4 -9, -6 -8, -7 -7, -8 -6, -9 -4, -10 -2, -10 -0, -10 2, -9 4, -8 6, -7 7, -6 8, -4 9, -2 10, -0 10, 2 10, 4 9, 6 8, 7 7, 8 6, 9 4, 10 2, 10 0))'
349 , $writer->write($b));
351 # One segment per quadrant
352 $b = $g->buffer(10, array('quad_segs' => 1));
353 $this->assertEquals(
354 'POLYGON ((10 0, 0 -10, -10 -0, -0 10, 10 0))'
355 , $writer->write($b));
357 /* End cap styles */
359 $g = $reader->read('LINESTRING(0 0, 100 0)');
361 $b = $g->buffer(10, array(
362 'quad_segs' => 1,
363 'endcap' => GEOSBUF_CAP_ROUND
365 $this->assertEquals(
366 'POLYGON ((100 10, 110 0, 100 -10, 0 -10, -10 0, 0 10, 100 10))'
367 , $writer->write($b));
369 $b = $g->buffer(10, array(
370 'quad_segs' => 1,
371 'endcap' => GEOSBUF_CAP_FLAT
373 $this->assertEquals(
374 'POLYGON ((100 10, 100 -10, 0 -10, 0 10, 100 10))'
375 , $writer->write($b));
377 $b = $g->buffer(10, array(
378 'quad_segs' => 1,
379 'endcap' => GEOSBUF_CAP_SQUARE
381 $this->assertEquals(
382 'POLYGON ((100 10, 110 10, 110 -10, 0 -10, -10 -10, -10 10, 100 10))'
383 , $writer->write($b));
385 /* Join styles */
387 $g = $reader->read('LINESTRING(0 0, 100 0, 100 100)');
389 $b = $g->buffer(10, array(
390 'quad_segs' => 2,
391 'join' => GEOSBUF_JOIN_ROUND
393 $this->assertEquals(
394 'POLYGON ((90 10, 90 100, 93 107, 100 110, 107 107, 110 100, 110 0, 107 -7, 100 -10, 0 -10, -7 -7, -10 0, -7 7, 0 10, 90 10))'
395 , $writer->write($b));
397 $b = $g->buffer(10, array(
398 'quad_segs' => 2,
399 'join' => GEOSBUF_JOIN_BEVEL
401 $this->assertEquals(
402 'POLYGON ((90 10, 90 100, 93 107, 100 110, 107 107, 110 100, 110 0, 100 -10, 0 -10, -7 -7, -10 0, -7 7, 0 10, 90 10))'
403 , $writer->write($b));
405 $b = $g->buffer(10, array(
406 'quad_segs' => 2,
407 'join' => GEOSBUF_JOIN_MITRE
409 $this->assertEquals(
410 'POLYGON ((90 10, 90 100, 93 107, 100 110, 107 107, 110 100, 110 -10, 0 -10, -7 -7, -10 0, -7 7, 0 10, 90 10))'
411 , $writer->write($b));
413 $b = $g->buffer(10, array(
414 'quad_segs' => 2,
415 'join' => GEOSBUF_JOIN_MITRE,
416 'mitre_limit' => 1.0
418 $this->assertEquals(
419 'POLYGON ((90 10, 90 100, 93 107, 100 110, 107 107, 110 100, 109 -5, 105 -9, 0 -10, -7 -7, -10 0, -7 7, 0 10, 90 10))'
420 , $writer->write($b));
422 /* Check that elements of the passed style array are not
423 * type-converted (buffer op will need to type-convert
424 * internally)
426 $ary = array('a' => 1);
428 $myStyle = array(
429 'quad_segs' => "a string",
430 'join' => "1",
431 'endcap' => $ary,
432 'mitre_limit' => 2 /* an int.. */
434 $this->assertEquals('string', gettype($myStyle['quad_segs']));
435 $this->assertEquals('string', gettype($myStyle['join']));
436 $this->assertEquals('array', gettype($myStyle['endcap']));
437 $this->assertEquals('integer', gettype($myStyle['mitre_limit']));
438 $b = $g->buffer(10, $myStyle);
439 $this->assertEquals('string', gettype($myStyle['quad_segs']));
440 $this->assertEquals('string', gettype($myStyle['join']));
441 $this->assertEquals('array', gettype($myStyle['endcap']));
442 $this->assertEquals('integer', gettype($myStyle['mitre_limit']));
447 public function testGeometry_envelope()
449 $reader = new GEOSWKTReader();
450 $writer = new GEOSWKTWriter();
451 $writer->setRoundingPrecision(0);
453 $g = $reader->read('POINT(0 0)');
454 $b = $g->envelope();
455 $this->assertEquals(
456 'POINT (0 0)'
457 , $writer->write($b));
459 $g = $reader->read('LINESTRING(0 0, 10 10)');
460 $b = $g->envelope();
461 $this->assertEquals(
462 'POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'
463 , $writer->write($b));
467 public function testGeometry_intersection()
469 $reader = new GEOSWKTReader();
470 $writer = new GEOSWKTWriter();
471 $writer->setRoundingPrecision(0);
473 /* POINT - POINT */
474 $g = $reader->read('POINT(0 0)');
475 $g2 = $reader->read('POINT(0 0)');
476 $gi = $g->intersection($g2);
477 $this->assertEquals( 'POINT (0 0)'
478 , $writer->write($gi));
479 $g2 = $reader->read('POINT(1 0)');
480 $gi = $g->intersection($g2);
481 $this->assertEquals( 'GEOMETRYCOLLECTION EMPTY'
482 , $writer->write($gi));
484 /* POINT - LINE */
485 $g = $reader->read('LINESTRING(0 0, 10 0)');
486 $g2 = $reader->read('POINT(5 0)');
487 $gi = $g->intersection($g2);
488 $this->assertEquals( 'POINT (5 0)'
489 , $writer->write($gi));
490 $g2 = $reader->read('POINT(12 0)');
491 $gi = $g->intersection($g2);
492 $this->assertEquals( 'GEOMETRYCOLLECTION EMPTY'
493 , $writer->write($gi));
495 /* LINE - LINE */
496 $g = $reader->read('LINESTRING(0 0, 10 0)');
497 $g2 = $reader->read('LINESTRING(5 -10, 5 10)');
498 $gi = $g->intersection($g2);
499 $this->assertEquals( 'POINT (5 0)'
500 , $writer->write($gi));
501 $g2 = $reader->read('LINESTRING(5 0, 20 0)');
502 $gi = $g->intersection($g2);
503 $this->assertEquals( 'LINESTRING (5 0, 10 0)'
504 , $writer->write($gi));
506 /* LINE - POLY */
507 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
508 $g2 = $reader->read('LINESTRING(5 -10, 5 10)');
509 $gi = $g->intersection($g2);
510 $this->assertEquals( 'LINESTRING (5 0, 5 10)'
511 , $writer->write($gi));
512 $g2 = $reader->read('LINESTRING(10 0, 20 0)');
513 $gi = $g->intersection($g2);
514 $this->assertEquals( 'POINT (10 0)'
515 , $writer->write($gi));
517 /* POLY - POLY */
518 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
519 $g2 = $reader->read('POLYGON((5 -5, 5 5, 15 5, 15 -5, 5 -5))');
520 $gi = $g->intersection($g2);
521 $this->assertEquals(
522 'POLYGON ((10 5, 10 0, 5 0, 5 5, 10 5))'
523 , $writer->write($gi));
524 $g2 = $reader->read('POLYGON((10 0, 20 0, 20 -5, 10 -5, 10 0))');
525 $gi = $g->intersection($g2);
526 $this->assertEquals( 'POINT (10 0)'
527 , $writer->write($gi));
528 $g2 = $reader->read('POLYGON((8 0, 20 0, 20 -5, 10 -5, 8 0))');
529 $gi = $g->intersection($g2);
530 $this->assertEquals( 'LINESTRING (8 0, 10 0)'
531 , $writer->write($gi));
534 public function testGeometry_convexHull()
536 $reader = new GEOSWKTReader();
537 $writer = new GEOSWKTWriter();
538 $writer->setRoundingPrecision(0);
540 $g = $reader->read('POINT(0 0)');
541 $b = $g->convexHull();
542 $this->assertEquals(
543 'POINT (0 0)'
544 , $writer->write($b));
546 $g = $reader->read('LINESTRING(0 0, 10 10)');
547 $b = $g->convexHull();
548 $this->assertEquals(
549 'LINESTRING (0 0, 10 10)'
550 , $writer->write($b));
552 $g = $reader->read('POLYGON((0 0, 0 10, 5 5, 10 10, 10 0, 0 0))');
553 $b = $g->convexHull();
554 $this->assertEquals(
555 'POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))'
556 , $writer->write($b));
560 public function testGeometry_difference()
562 $reader = new GEOSWKTReader();
563 $writer = new GEOSWKTWriter();
564 $writer->setRoundingPrecision(0);
566 /* POINT - POINT */
567 $g = $reader->read('POINT(0 0)');
568 $g2 = $reader->read('POINT(0 0)');
569 $gi = $g->difference($g2);
570 $this->assertEquals( 'GEOMETRYCOLLECTION EMPTY'
571 , $writer->write($gi));
572 $g2 = $reader->read('POINT(1 0)');
573 $gi = $g->difference($g2);
574 $this->assertEquals( 'POINT (0 0)'
575 , $writer->write($gi));
577 /* LINE - POINT */
578 $g = $reader->read('LINESTRING(0 0, 10 0)');
579 $g2 = $reader->read('POINT(5 0)');
580 $gi = $g->difference($g2);
581 $this->assertEquals('LINESTRING (0 0, 10 0)'
582 , $writer->write($gi));
584 /* POINT - LINE */
585 $g = $reader->read('POINT(5 0)');
586 $g2 = $reader->read('LINESTRING(0 0, 10 0)');
587 $gi = $g->difference($g2);
588 $this->assertEquals('GEOMETRYCOLLECTION EMPTY'
589 , $writer->write($gi));
590 $g2 = $reader->read('LINESTRING(0 1, 10 1)');
591 $gi = $g->difference($g2);
592 $this->assertEquals( 'POINT (5 0)'
593 , $writer->write($gi));
595 /* LINE - LINE */
596 $g = $reader->read('LINESTRING(0 0, 10 0)');
597 $g2 = $reader->read('LINESTRING(5 -10, 5 10)');
598 $gi = $g->difference($g2);
599 $this->assertEquals( 'MULTILINESTRING ((0 0, 5 0), (5 0, 10 0))'
600 , $writer->write($gi));
601 $g2 = $reader->read('LINESTRING(5 0, 20 0)');
602 $gi = $g->difference($g2);
603 $this->assertEquals( 'LINESTRING (0 0, 5 0)'
604 , $writer->write($gi));
606 /* POLY - LINE */
607 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
608 $g2 = $reader->read('LINESTRING(5 -10, 5 10)');
609 $gi = $g->difference($g2);
610 $this->assertEquals(
611 'POLYGON ((5 0, 0 0, 0 10, 5 10, 10 10, 10 0, 5 0))'
612 , $writer->write($gi));
613 $g2 = $reader->read('LINESTRING(10 0, 20 0)');
614 $gi = $g->difference($g2);
615 $this->assertEquals(
616 'POLYGON ((10 0, 0 0, 0 10, 10 10, 10 0))'
617 , $writer->write($gi));
619 /* POLY - POLY */
620 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
621 $g2 = $reader->read('POLYGON((5 -5, 5 5, 15 5, 15 -5, 5 -5))');
622 $gi = $g->difference($g2);
623 $this->assertEquals(
624 'POLYGON ((5 0, 0 0, 0 10, 10 10, 10 5, 5 5, 5 0))'
625 , $writer->write($gi));
628 public function testGeometry_symdifference()
630 $reader = new GEOSWKTReader();
631 $writer = new GEOSWKTWriter();
632 $writer->setRoundingPrecision(0);
634 /* POINT - POINT */
635 $g = $reader->read('POINT(0 0)');
636 $g2 = $reader->read('POINT(0 0)');
637 $gi = $g->symDifference($g2);
638 $this->assertEquals( 'GEOMETRYCOLLECTION EMPTY'
639 , $writer->write($gi));
640 $g2 = $reader->read('POINT(1 0)');
641 $gi = $g->symDifference($g2);
642 $this->assertEquals( 'MULTIPOINT (0 0, 1 0)'
643 , $writer->write($gi));
645 /* LINE - POINT */
646 $g = $reader->read('LINESTRING(0 0, 10 0)');
647 $g2 = $reader->read('POINT(5 0)');
648 $gi = $g->symDifference($g2);
649 $this->assertEquals('LINESTRING (0 0, 10 0)'
650 , $writer->write($gi));
652 /* POINT - LINE */
653 $g = $reader->read('POINT(5 0)');
654 $g2 = $reader->read('LINESTRING(0 0, 10 0)');
655 $gi = $g->symDifference($g2);
656 $this->assertEquals( 'LINESTRING (0 0, 10 0)'
657 , $writer->write($gi));
658 $g2 = $reader->read('LINESTRING(0 1, 10 1)');
659 $gi = $g->symDifference($g2);
660 $this->assertEquals(
661 'GEOMETRYCOLLECTION (POINT (5 0), LINESTRING (0 1, 10 1))'
662 , $writer->write($gi));
664 /* LINE - LINE */
665 $g = $reader->read('LINESTRING(0 0, 10 0)');
666 $g2 = $reader->read('LINESTRING(5 -10, 5 10)');
667 $gi = $g->symDifference($g2);
668 $this->assertEquals(
669 'MULTILINESTRING ((0 0, 5 0), (5 0, 10 0), (5 -10, 5 0), (5 0, 5 10))'
670 , $writer->write($gi));
671 $g2 = $reader->read('LINESTRING(5 0, 20 0)');
672 $gi = $g->symDifference($g2);
673 $this->assertEquals(
674 'MULTILINESTRING ((0 0, 5 0), (10 0, 20 0))'
675 , $writer->write($gi));
677 /* POLY - LINE */
678 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
679 $g2 = $reader->read('LINESTRING(5 -10, 5 10)');
680 $gi = $g->symDifference($g2);
681 $this->assertEquals(
682 'GEOMETRYCOLLECTION (LINESTRING (5 -10, 5 0), POLYGON ((5 0, 0 0, 0 10, 5 10, 10 10, 10 0, 5 0)))'
683 , $writer->write($gi));
684 $g2 = $reader->read('LINESTRING(10 0, 20 0)');
685 $gi = $g->symDifference($g2);
686 $this->assertEquals(
687 'GEOMETRYCOLLECTION (LINESTRING (10 0, 20 0), POLYGON ((10 0, 0 0, 0 10, 10 10, 10 0)))'
688 , $writer->write($gi));
690 /* POLY - POLY */
691 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
692 $g2 = $reader->read('POLYGON((5 -5, 5 5, 15 5, 15 -5, 5 -5))');
693 $gi = $g->symDifference($g2);
694 $this->assertEquals(
695 'MULTIPOLYGON (((5 0, 0 0, 0 10, 10 10, 10 5, 5 5, 5 0)), ((5 0, 10 0, 10 5, 15 5, 15 -5, 5 -5, 5 0)))'
696 , $writer->write($gi));
699 public function testGeometry_boundary()
701 $reader = new GEOSWKTReader();
702 $writer = new GEOSWKTWriter();
703 $writer->setRoundingPrecision(0);
705 $g = $reader->read('POINT(0 0)');
706 $b = $g->boundary();
707 $this->assertEquals(
708 'GEOMETRYCOLLECTION EMPTY'
709 , $writer->write($b));
711 $g = $reader->read('LINESTRING(0 0, 10 10)');
712 $b = $g->boundary();
713 $this->assertEquals(
714 'MULTIPOINT (0 0, 10 10)'
715 , $writer->write($b));
717 $g = $reader->read(
718 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),( 5 5, 5 6, 6 6, 6 5, 5 5))');
719 $b = $g->boundary();
720 $this->assertEquals(
721 'MULTILINESTRING ((0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 5 6, 6 6, 6 5, 5 5))'
722 , $writer->write($b));
726 public function testGeometry_union()
728 $reader = new GEOSWKTReader();
729 $writer = new GEOSWKTWriter();
730 $writer->setRoundingPrecision(0);
732 /* POINT - POINT */
733 $g = $reader->read('POINT(0 0)');
734 $g2 = $reader->read('POINT(0 0)');
735 $gi = $g->union($g2);
736 $this->assertEquals( 'POINT (0 0)'
737 , $writer->write($gi));
738 $g2 = $reader->read('POINT(1 0)');
739 $gi = $g->union($g2);
740 $this->assertEquals( 'MULTIPOINT (0 0, 1 0)'
741 , $writer->write($gi));
743 /* LINE - POINT */
744 $g = $reader->read('LINESTRING(0 0, 10 0)');
745 $g2 = $reader->read('POINT(5 0)');
746 $gi = $g->union($g2);
747 $this->assertEquals('LINESTRING (0 0, 10 0)'
748 , $writer->write($gi));
750 /* POINT - LINE */
751 $g = $reader->read('POINT(5 0)');
752 $g2 = $reader->read('LINESTRING(0 0, 10 0)');
753 $gi = $g->union($g2);
754 $this->assertEquals( 'LINESTRING (0 0, 10 0)'
755 , $writer->write($gi));
756 $g2 = $reader->read('LINESTRING(0 1, 10 1)');
757 $gi = $g->union($g2);
758 $this->assertEquals(
759 'GEOMETRYCOLLECTION (POINT (5 0), LINESTRING (0 1, 10 1))'
760 , $writer->write($gi));
762 /* LINE - LINE */
763 $g = $reader->read('LINESTRING(0 0, 10 0)');
764 $g2 = $reader->read('LINESTRING(5 -10, 5 10)');
765 $gi = $g->union($g2);
766 $this->assertEquals(
767 'MULTILINESTRING ((0 0, 5 0), (5 0, 10 0), (5 -10, 5 0), (5 0, 5 10))'
768 , $writer->write($gi));
769 $g2 = $reader->read('LINESTRING(5 0, 20 0)');
770 $gi = $g->union($g2);
771 $this->assertEquals(
772 'MULTILINESTRING ((0 0, 5 0), (5 0, 10 0), (10 0, 20 0))'
773 , $writer->write($gi));
775 /* POLY - LINE */
776 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
777 $g2 = $reader->read('LINESTRING(5 -10, 5 10)');
778 $gi = $g->union($g2);
779 $this->assertEquals(
780 'GEOMETRYCOLLECTION (LINESTRING (5 -10, 5 0), POLYGON ((5 0, 0 0, 0 10, 5 10, 10 10, 10 0, 5 0)))'
781 , $writer->write($gi));
782 $g2 = $reader->read('LINESTRING(10 0, 20 0)');
783 $gi = $g->union($g2);
784 $this->assertEquals(
785 'GEOMETRYCOLLECTION (LINESTRING (10 0, 20 0), POLYGON ((10 0, 0 0, 0 10, 10 10, 10 0)))'
786 , $writer->write($gi));
788 /* POLY - POLY */
789 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
790 $g2 = $reader->read('POLYGON((5 -5, 5 5, 15 5, 15 -5, 5 -5))');
791 $gi = $g->union($g2);
792 $this->assertEquals(
793 'POLYGON ((5 0, 0 0, 0 10, 10 10, 10 5, 15 5, 15 -5, 5 -5, 5 0))'
794 , $writer->write($gi));
797 public function testGeometry_unionCascaded()
799 $reader = new GEOSWKTReader();
800 $writer = new GEOSWKTWriter();
801 $writer->setRoundingPrecision(0);
803 $g = $reader->read('MULTIPOLYGON(
804 ((0 0, 1 0, 1 1, 0 1, 0 0)),
805 ((10 10, 10 14, 14 14, 14 10, 10 10),
806 (11 11, 11 12, 12 12, 12 11, 11 11)),
807 ((0 0, 11 0, 11 11, 0 11, 0 0))
808 ))');
810 $gu = $g->union();
811 $this->assertEquals(
812 'POLYGON ((1 0, 0 0, 0 1, 0 11, 10 11, 10 14, 14 14, 14 10, 11 10, 11 0, 1 0), (11 11, 12 11, 12 12, 11 12, 11 11))'
813 , $writer->write($gu));
817 public function testGeometry_pointOnSurface()
819 $reader = new GEOSWKTReader();
820 $writer = new GEOSWKTWriter();
821 $writer->setRoundingPrecision(0);
823 $g = $reader->read('POINT(0 0)');
824 $b = $g->pointOnSurface();
825 $this->assertEquals(
826 'POINT (0 0)'
827 , $writer->write($b));
829 $g = $reader->read('LINESTRING(0 0, 5 5, 10 10)');
830 $b = $g->pointOnSurface();
831 $this->assertEquals(
832 'POINT (5 5)'
833 , $writer->write($b));
835 $g = $reader->read('POLYGON((0 0, 0 10, 5 5, 10 10, 10 0, 0 0))');
836 $b = $g->pointOnSurface();
837 $this->assertEquals(
838 'POINT (2 5)'
839 , $writer->write($b));
842 public function testGeometry_centroid()
844 $reader = new GEOSWKTReader();
845 $writer = new GEOSWKTWriter();
846 $writer->setRoundingPrecision(0);
848 $g = $reader->read('POINT(0 0)');
849 $b = $g->centroid();
850 $this->assertEquals(
851 'POINT (0 0)'
852 , $writer->write($b));
854 $g = $reader->read('LINESTRING(0 0, 10 10)');
855 $b = $g->centroid();
856 $this->assertEquals(
857 'POINT (5 5)'
858 , $writer->write($b));
860 $g = $reader->read('POLYGON((0 0, 0 10, 5 5, 10 10, 10 0, 0 0))');
861 $b = $g->centroid();
862 $this->assertEquals(
863 'POINT (5 4)'
864 , $writer->write($b));
867 public function testGeometry_relate()
869 $reader = new GEOSWKTReader();
870 $writer = new GEOSWKTWriter();
871 $writer->setRoundingPrecision(0);
873 $g = $reader->read('POINT(0 0)');
874 $g2 = $reader->read('POINT(0 0)');
875 $ret = $g->relate($g2);
876 $this->assertEquals('0FFFFFFF2', $ret);
877 $ret = $g->relate($g2, '0FFFFFFF2');
878 $this->assertEquals(TRUE, $ret);
879 $ret = $g->relate($g2, '0*******T');
880 $this->assertEquals(TRUE, $ret);
881 $ret = $g->relate($g2, '0*******1');
882 $this->assertEquals(FALSE, $ret);
884 $g = $reader->read('POINT(0 0)');
885 $g2 = $reader->read('POINT(1 0)');
886 $ret = $g->relate($g2);
887 $this->assertEquals('FF0FFF0F2', $ret);
888 $ret = $g->relate($g2, 'FF0FFF0F2');
889 $this->assertEquals(TRUE, $ret);
890 $ret = $g->relate($g2, 'F*******2');
891 $this->assertEquals(TRUE, $ret);
892 $ret = $g->relate($g2, 'T*******2');
893 $this->assertEquals(FALSE, $ret);
895 $g = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
896 $g2 = $reader->read('POINT(1 0)');
897 $ret = $g->relate($g2);
898 $this->assertEquals('FF20F1FF2', $ret);
899 $ret = $g->relate($g2, 'FF20F1FF2');
900 $this->assertEquals(TRUE, $ret);
901 $ret = $g->relate($g2, 'F****T**T');
902 $this->assertEquals(TRUE, $ret);
903 $ret = $g->relate($g2, 'T*******2');
904 $this->assertEquals(FALSE, $ret);
908 public function testGeometry_polygonize()
910 $reader = new GEOSWKTReader();
911 $writer = new GEOSWKTWriter();
912 $writer->setRoundingPrecision(0);
914 $g = $reader->read('GEOMETRYCOLLECTION(
915 LINESTRING(0 0, 10 10),
916 LINESTRING(185 221, 100 100),
917 LINESTRING(185 221, 88 275, 180 316),
918 LINESTRING(185 221, 292 281, 180 316),
919 LINESTRING(189 98, 83 187, 185 221),
920 LINESTRING(189 98, 325 168, 185 221))
921 )');
923 $g2 = $reader->read('POINT(0 0)');
924 $g = $g->union($g2); /* Make sure linestrings are noded */
926 $ret = GEOSPolygonize($g);
929 * NOTE: the following expected results are suspicious
930 * due to the duplicated dangle and lack of a cut edge
933 //var_dump($ret);
935 $this->assertEquals('array', gettype($ret));
936 $this->assertEquals('array', gettype($ret['rings']));
937 $this->assertEquals('array', gettype($ret['cut_edges']));
938 $this->assertEquals('array', gettype($ret['dangles']));
939 $this->assertEquals('array', gettype($ret['invalid_rings']));
941 $this->assertEquals(3, count($ret['rings']));
942 $this->assertEquals(
943 'POLYGON ((185 221, 132 146, 83 187, 185 221))'
944 , $writer->write($ret['rings'][0]));
945 $this->assertEquals(
946 'POLYGON ((132 146, 185 221, 325 168, 189 98, 132 146))'
947 , $writer->write($ret['rings'][1]));
948 $this->assertEquals(
949 'POLYGON ((185 221, 88 275, 180 316, 292 281, 185 221))'
950 , $writer->write($ret['rings'][2]));
952 $this->assertEquals(0, count($ret['cut_edges']));
954 $this->assertEquals(3, count($ret['dangles']));
955 $this->assertEquals(
956 'LINESTRING (132 146, 100 100)'
957 , $writer->write($ret['dangles'][0]));
958 $this->assertEquals(
959 'LINESTRING (0 0, 10 10)'
960 , $writer->write($ret['dangles'][1]));
961 $this->assertEquals(
962 'LINESTRING (0 0, 10 10)'
963 , $writer->write($ret['dangles'][2]));
965 $this->assertEquals(0, count($ret['invalid_rings']));
969 public function testGeometry_lineMerge()
971 $reader = new GEOSWKTReader();
972 $writer = new GEOSWKTWriter();
973 $writer->setRoundingPrecision(0);
975 $g = $reader->read('MULTILINESTRING(
976 (0 0, 10 10),
977 (10 10, 10 0),
978 (5 0, 10 0),
979 (5 -5, 5 0)
980 )');
982 $ret = GEOSLineMerge($g);
984 $this->assertEquals('array', gettype($ret));
985 $this->assertEquals('1', count($ret));
987 $this->assertEquals(
988 'LINESTRING (0 0, 10 10, 10 0, 5 0, 5 -5)'
989 , $writer->write($ret[0]));
993 public function testGeometry_simplify()
995 $reader = new GEOSWKTReader();
996 $writer = new GEOSWKTWriter();
997 $writer->setRoundingPrecision(0);
999 $g = $reader->read('LINESTRING(0 0, 3 4, 5 10, 10 0, 10 9, 5 11, 0 9)');
1000 $gs = $g->simplify(2);
1001 $this->assertEquals( 'LINESTRING (0 0, 5 10, 10 0, 10 9, 0 9)'
1002 , $writer->write($gs));
1003 $gs = $g->simplify(2, TRUE);
1004 $this->assertEquals( 'LINESTRING (0 0, 5 10, 10 0, 10 9, 5 11, 0 9)'
1005 , $writer->write($gs));
1008 public function testGeometry_extractUniquePoints()
1010 $reader = new GEOSWKTReader();
1011 $writer = new GEOSWKTWriter();
1012 $writer->setRoundingPrecision(0);
1014 $g = $reader->read(
1015 'GEOMETRYCOLLECTION (
1016 MULTIPOLYGON (
1017 ((0 0, 1 0, 1 1, 0 1, 0 0)),
1018 ((10 10, 10 14, 14 14, 14 10, 10 10),
1019 (11 11, 11 12, 12 12, 12 11, 11 11))
1021 POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)),
1022 MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)),
1023 LINESTRING (0 0, 2 3),
1024 MULTIPOINT (0 0, 2 3),
1025 POINT (9 0),
1026 POINT(1 0)),
1027 LINESTRING EMPTY
1030 $gs = $g->extractUniquePoints();
1031 if ( ! $gs ) RETURN_NULL(); /* should get an exception before */
1033 $this->assertEquals(
1034 'MULTIPOINT (0 0, 1 0, 1 1, 0 1, 10 10, 10 14, 14 14, 14 10, 11 11, 11 12, 12 12, 12 11, 2 3, 3 4, 9 0)'
1035 , $writer->write($gs));
1038 public function testGeometry_relationalOps()
1040 $reader = new GEOSWKTReader();
1041 $writer = new GEOSWKTWriter();
1042 $writer->setRoundingPrecision(0);
1044 $g1 = $reader->read('POINT(0 0)');
1045 $g2 = $reader->read('POINT(0 0)');
1047 $this->assertFalse( $g1->disjoint($g2) );
1048 $this->assertFalse( $g1->touches($g2) ); /* no bounds, can't touch */
1049 $this->assertTrue( $g1->intersects($g2) );
1050 $this->assertFalse( $g1->crosses($g2) );
1051 $this->assertTrue( $g1->within($g2) );
1052 $this->assertTrue( $g1->contains($g2) );
1053 $this->assertFalse( $g1->overlaps($g2) );
1054 $this->assertTrue( $g1->equals($g2) );
1055 $this->assertTrue( $g1->equalsExact($g2) );
1057 $g1 = $reader->read('POINT(0 0)');
1058 $g2 = $reader->read('LINESTRING(0 0, 10 0)');
1060 $this->assertFalse( $g1->disjoint($g2) );
1061 $this->assertTrue( $g1->touches($g2) );
1062 $this->assertTrue( $g1->intersects($g2) );
1063 $this->assertFalse( $g1->crosses($g2) );
1064 $this->assertFalse( $g1->within($g2) );
1065 $this->assertFalse( $g1->contains($g2) );
1066 $this->assertFalse( $g1->overlaps($g2) );
1067 $this->assertFalse( $g1->equals($g2) );
1068 $this->assertFalse( $g1->equalsExact($g2, 10) );
1070 $g1 = $reader->read('POINT(5 0)');
1071 $g2 = $reader->read('LINESTRING(0 0, 10 0)');
1073 $this->assertFalse( $g1->disjoint($g2) );
1074 $this->assertFalse( $g1->touches($g2) );
1075 $this->assertTrue( $g1->intersects($g2) );
1076 $this->assertFalse( $g1->crosses($g2) );
1077 $this->assertTrue( $g1->within($g2) );
1078 $this->assertFalse( $g1->contains($g2) );
1079 $this->assertFalse( $g1->overlaps($g2) );
1080 $this->assertFalse( $g1->equals($g2) );
1081 $this->assertFalse( $g1->equalsExact($g2, 10) );
1083 $g1 = $reader->read('LINESTRING(5 -5, 5 5)');
1084 $g2 = $reader->read('LINESTRING(0 0, 10 0)');
1086 $this->assertFalse( $g1->disjoint($g2) );
1087 $this->assertFalse( $g1->touches($g2) );
1088 $this->assertTrue( $g1->intersects($g2) );
1089 $this->assertTrue( $g1->crosses($g2) );
1090 $this->assertFalse( $g1->within($g2) );
1091 $this->assertFalse( $g1->contains($g2) );
1092 $this->assertFalse( $g1->overlaps($g2) );
1093 $this->assertFalse( $g1->equals($g2) );
1094 $this->assertFalse( $g1->equalsExact($g2, 1) );
1096 $g1 = $reader->read('LINESTRING(5 0, 15 0)');
1097 $g2 = $reader->read('LINESTRING(0 0, 10 0)');
1099 $this->assertFalse( $g1->disjoint($g2) );
1100 $this->assertFalse( $g1->touches($g2) );
1101 $this->assertTrue( $g1->intersects($g2) );
1102 $this->assertFalse( $g1->crosses($g2) );
1103 $this->assertFalse( $g1->within($g2) );
1104 $this->assertFalse( $g1->contains($g2) );
1105 $this->assertTrue( $g1->overlaps($g2) );
1106 $this->assertFalse( $g1->equals($g2) );
1107 $this->assertFalse( $g1->equalsExact($g2, 1) );
1109 $g1 = $reader->read('LINESTRING(0 0, 5 0, 10 0)');
1110 $g2 = $reader->read('LINESTRING(0 0, 10 0)');
1112 $this->assertFalse( $g1->disjoint($g2) );
1113 $this->assertFalse( $g1->touches($g2) );
1114 $this->assertTrue( $g1->intersects($g2) );
1115 $this->assertFalse( $g1->crosses($g2) );
1116 $this->assertTrue( $g1->within($g2) );
1117 $this->assertTrue( $g1->contains($g2) );
1118 $this->assertFalse( $g1->overlaps($g2) );
1119 $this->assertTrue( $g1->equals($g2) );
1120 $this->assertFalse( $g1->equalsExact($g2, 1) );
1122 $g1 = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
1123 $g2 = $reader->read('POLYGON((5 -5, 5 5, 15 5, 15 -5, 5 -5))');
1125 $this->assertFalse( $g1->disjoint($g2) );
1126 $this->assertFalse( $g1->touches($g2) );
1127 $this->assertTrue( $g1->intersects($g2) );
1128 $this->assertFalse( $g1->crosses($g2) );
1129 $this->assertFalse( $g1->within($g2) );
1130 $this->assertFalse( $g1->contains($g2) );
1131 $this->assertTrue( $g1->overlaps($g2) );
1132 $this->assertFalse( $g1->equals($g2) );
1133 $this->assertFalse( $g1->equalsExact($g2, 1) );
1135 $g1 = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
1136 $g2 = $reader->read('POINT(15 15)');
1138 $this->assertTrue( $g1->disjoint($g2) );
1139 $this->assertFalse( $g1->touches($g2) );
1140 $this->assertFalse( $g1->intersects($g2) );
1141 $this->assertFalse( $g1->crosses($g2) );
1142 $this->assertFalse( $g1->within($g2) );
1143 $this->assertFalse( $g1->contains($g2) );
1144 $this->assertFalse( $g1->overlaps($g2) );
1145 $this->assertFalse( $g1->equals($g2) );
1146 $this->assertFalse( $g1->equalsExact($g2, 1) );
1150 public function testGeometry_isEmpty()
1152 $reader = new GEOSWKTReader();
1153 $writer = new GEOSWKTWriter();
1154 $writer->setRoundingPrecision(0);
1156 $g1 = $reader->read('POINT(0 0)');
1157 $this->assertFalse( $g1->isEmpty() );
1159 $g1 = $reader->read('POINT EMPTY');
1160 $this->assertTrue( $g1->isEmpty() );
1162 $g1 = $reader->read('LINESTRING(0 0, 10 0)');
1163 $this->assertFalse( $g1->isEmpty() );
1165 $g1 = $reader->read('LINESTRING EMPTY');
1166 $this->assertTrue( $g1->isEmpty() );
1168 $g1 = $reader->read('POLYGON((0 0, 10 0, 10 10, 0 0))');
1169 $this->assertFalse( $g1->isEmpty() );
1171 $g1 = $reader->read('POLYGON EMPTY');
1172 $this->assertTrue( $g1->isEmpty() );
1174 $g1 = $reader->read('GEOMETRYCOLLECTION(POINT(0 0))');
1175 $this->assertFalse( $g1->isEmpty() );
1177 $g1 = $reader->read('GEOMETRYCOLLECTION EMPTY');
1178 $this->assertTrue( $g1->isEmpty() );
1181 public function testGeometry_checkValidity()
1183 $reader = new GEOSWKTReader();
1184 $writer = new GEOSWKTWriter();
1185 $writer->setRoundingPrecision(0);
1187 $g = $reader->read('POINT(0 0)');
1188 $val = $g->checkValidity();
1189 $this->assertType( 'array', $val );
1190 $this->assertTrue( $val['valid'] );
1191 $this->assertNull( $val['reason'] );
1192 $this->assertNull( $val['location'] );
1194 $g = $reader->read('POINT(0 NaN)');
1195 $val = $g->checkValidity();
1196 $this->assertType( 'array', $val );
1197 $this->assertFalse( $val['valid'] );
1198 $this->assertEquals( 'Invalid Coordinate', $val['reason'] );
1199 $this->assertEquals( 'POINT (0 nan)',
1200 $writer->write($val['location']) );
1203 public function testGeometry_isSimple()
1205 $reader = new GEOSWKTReader();
1207 $g = $reader->read('POINT(0 0)');
1208 $this->assertTrue( $g->isSimple() );
1210 $g = $reader->read('LINESTRING(0 0, 10 0)');
1211 $this->assertTrue( $g->isSimple() );
1213 $g = $reader->read('LINESTRING(0 0, 10 0, 5 5, 5 -5)');
1214 $this->assertFalse( $g->isSimple() );
1217 public function testGeometry_isRing()
1219 $reader = new GEOSWKTReader();
1221 $g = $reader->read('POINT(0 0)');
1222 $this->assertFalse( $g->isRing() );
1224 $g = $reader->read('LINESTRING(0 0, 10 0, 5 5, 5 -5)');
1225 $this->assertFalse( $g->isRing() );
1227 $g = $reader->read('LINESTRING(0 0, 10 0, 5 5, 0 0)');
1228 $this->assertTrue( $g->isRing() );
1231 public function testGeometry_hasZ()
1233 $reader = new GEOSWKTReader();
1235 $g = $reader->read('POINT(0 0)');
1236 $this->assertFalse( $g->hasZ() );
1238 $g = $reader->read('POINT(0 0 0)');
1239 $this->assertTrue( $g->hasZ() );
1243 public function testGeometry_isClosed()
1245 $reader = new GEOSWKTReader();
1247 $g = $reader->read('POINT(0 0)');
1248 try {
1249 $this->assertFalse( $g->isClosed() );
1250 $this->assertTrue(FALSE);
1251 } catch (Exception $e) {
1252 $this->assertContains('LineString', $e->getMessage());
1255 $g = $reader->read('LINESTRING(0 0, 10 0, 5 5, 5 -5)');
1256 $this->assertFalse( $g->isClosed() );
1258 $g = $reader->read('LINESTRING(0 0, 10 0, 5 5, 0 0)');
1259 $this->assertTrue( $g->isClosed() );
1262 public function testGeometry_type()
1264 $reader = new GEOSWKTReader();
1266 $g = $reader->read('POINT(0 0)');
1267 $this->assertEquals('Point', $g->typeName());
1268 $this->assertEquals(GEOS_POINT, $g->typeId());
1270 $g = $reader->read('MULTIPOINT (0 1, 2 3)');
1271 $this->assertEquals('MultiPoint', $g->typeName());
1272 $this->assertEquals(GEOS_MULTIPOINT, $g->typeId());
1274 $g = $reader->read('LINESTRING (0 0, 2 3)');
1275 $this->assertEquals('LineString', $g->typeName());
1276 $this->assertEquals(GEOS_LINESTRING, $g->typeId());
1278 $g = $reader->read('MULTILINESTRING ((0 1, 2 3), (10 10, 3 4))');
1279 $this->assertEquals('MultiLineString', $g->typeName());
1280 $this->assertEquals(GEOS_MULTILINESTRING, $g->typeId());
1282 $g = $reader->read('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))');
1283 $this->assertEquals('Polygon', $g->typeName());
1284 $this->assertEquals(GEOS_POLYGON, $g->typeId());
1286 $g = $reader->read('MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11)))');
1287 $this->assertEquals('MultiPolygon', $g->typeName());
1288 $this->assertEquals(GEOS_MULTIPOLYGON, $g->typeId());
1290 $g = $reader->read('GEOMETRYCOLLECTION (MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11))), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)), MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)), LINESTRING (0 0, 2 3), MULTIPOINT (0 0, 2 3), POINT (9 0))');
1291 $this->assertEquals('GeometryCollection', $g->typeName());
1292 $this->assertEquals(GEOS_GEOMETRYCOLLECTION, $g->typeId());
1295 public function testGeometry_srid()
1297 $reader = new GEOSWKTReader();
1299 $g = $reader->read('POINT(0 0)');
1300 $this->assertEquals(0, $g->getSRID());
1301 $g->setSRID(2);
1302 $this->assertEquals(2, $g->getSRID());