From c49cd838c41cf7813fe1b3a1656be6839845208f Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Fri, 22 Apr 2011 18:09:49 -0700 Subject: [PATCH] Fixing some bugs --- lib/adapters/GPX.class.php | 2 +- lib/adapters/KML.class.php | 33 +++++++++++++++++++++++---------- lib/geometry/Geometry.class.php | 12 ++++++------ lib/geometry/Polygon.class.php | 4 ++-- tests/test.php | 31 +++++++++++++++++++++---------- 5 files changed, 53 insertions(+), 29 deletions(-) diff --git a/lib/adapters/GPX.class.php b/lib/adapters/GPX.class.php index 4a3cdb1e..139f1706 100644 --- a/lib/adapters/GPX.class.php +++ b/lib/adapters/GPX.class.php @@ -32,7 +32,7 @@ public function read($gpx) { * @return string The KML string representation of the input geometries */ public function write(Geometry $geometry) { - return $this->geometryToGPX($geometry); + return ''.$this->geometryToGPX($geometry).''; } public function geomFromText($text) { diff --git a/lib/adapters/KML.class.php b/lib/adapters/KML.class.php index 63b19bdb..e4e1c9e1 100644 --- a/lib/adapters/KML.class.php +++ b/lib/adapters/KML.class.php @@ -71,16 +71,26 @@ protected function geomFromXML() { $geometries = array(); $geom_types = geoPHP::geometryList(); $placemark_elements = $this->xmlobj->getElementsByTagName('placemark'); - foreach ($placemark_elements as $placemark) { - foreach ($placemark->childNodes as $child) { - // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection - $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName; - if (array_key_exists($node_name, $geom_types)) { - $function = 'parse'.$geom_types[$node_name]; - $geometries[] = $this->$function($child); + if ($placemark_elements->length) { + foreach ($placemark_elements as $placemark) { + foreach ($placemark->childNodes as $child) { + // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection + $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName; + if (array_key_exists($node_name, $geom_types)) { + $function = 'parse'.$geom_types[$node_name]; + $geometries[] = $this->$function($child); + } } } } + else { + // The document does not have a placemark, try to create a valid geometry from the root element + $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; + if (array_key_exists($node_name, $geom_types)) { + $function = 'parse'.$geom_types[$node_name]; + $geometries[] = $this->$function($this->xmlobj->documentElement); + } + } return geoPHP::geometryReduce($geometries); } @@ -188,8 +198,11 @@ private function pointToKML($geom) { return "".$geom->getX().",".$geom->getY().""; } - private function linestringToKML($geom) { - $type = $geom->getGeomType(); + private function linestringToKML($geom, $type = FALSE) { + if (!$type) { + $type = $geom->getGeomType(); + } + $str = '<'. $type .'>'; $i=0; foreach ($geom->getComponents() as $comp) { @@ -203,7 +216,7 @@ private function linestringToKML($geom) { public function polygonToKML($geom) { $components = $geom->getComponents(); - $str = '' . $this->linestringToKML($components[0]) . ''; + $str = '' . $this->linestringToKML($components[0], 'LinearRing') . ''; foreach (array_slice($components, 1) as $comp) { $str .= '' . $this->linestringToKML($comp) . ''; } diff --git a/lib/geometry/Geometry.class.php b/lib/geometry/Geometry.class.php index 679140b5..6d48756d 100644 --- a/lib/geometry/Geometry.class.php +++ b/lib/geometry/Geometry.class.php @@ -71,12 +71,13 @@ public function envelope() { $bbox = $this->getBBox(); $points = array ( - new Point($bbox['maxy'],$bbox['minx']), - new Point($bbox['maxy'],$bbox['maxx']), - new Point($bbox['miny'],$bbox['maxx']), - new Point($bbox['miny'],$bbox['minx']), - new Point($bbox['maxy'],$bbox['minx']), + new Point($bbox['maxx'],$bbox['miny']), + new Point($bbox['maxx'],$bbox['maxy']), + new Point($bbox['minx'],$bbox['maxy']), + new Point($bbox['minx'],$bbox['miny']), + new Point($bbox['maxx'],$bbox['miny']), ); + $outer_boundary = new LineString($points); return new Polygon(array($outer_boundary)); } @@ -85,7 +86,6 @@ public function geometryType() { return $this->geom_type; } - // Public: Non-Standard -- Common to all geometries // ------------------------------------------------ public function getGeoInterface() { diff --git a/lib/geometry/Polygon.class.php b/lib/geometry/Polygon.class.php index dbe45bf0..49a4a4ff 100644 --- a/lib/geometry/Polygon.class.php +++ b/lib/geometry/Polygon.class.php @@ -88,8 +88,8 @@ public function centroid() { $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; } - $cn['x'] = - $cn['x'] / ( 6 * $a); - $cn['y'] = - $cn['y'] / ( 6 * $a); + $cn['x'] = $cn['x'] / ( 6 * $a); + $cn['y'] = $cn['y'] / ( 6 * $a); $centroid = new Point($cn['x'], $cn['y']); return $centroid; diff --git a/tests/test.php b/tests/test.php index 9496bb88..f00c814a 100644 --- a/tests/test.php +++ b/tests/test.php @@ -2,6 +2,13 @@ include_once('../geoPHP.inc'); +if (geoPHP::geosInstalled()) { + print "GEOS is installed. "; +} +else { + print "GEOS is not installed. "; +} + foreach (scandir('./input') as $file) { $parts = explode('.',$file); if ($parts[0]) { @@ -12,16 +19,7 @@ } } -function test_geometry($geometry) { - // Test adapter output - $geometry->out('wkt'); - $geometry->out('wkb'); - $geometry->out('kml'); - $geometry->out('gpx'); - $geometry->out('json'); - - //Don't test google geocoder regularily. Uncomment to test - #$geometry->out('google_geocode'); +function test_geometry($geometry, $test_adapters = TRUE) { // Test common functions $geometry->area(); @@ -98,6 +96,19 @@ function test_geometry($geometry) { $geometry->coordinateDimension(); $geometry->z(); $geometry->m(); + + // Test adapter output and input. Do a round-trip and re-test + if ($test_adapters) { + foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) { + if ($adapter != 'google_geocode') { //Don't test google geocoder regularily. Uncomment to test + $format = $geometry->out($adapter_key); + $adapter_loader = new $adapter_class(); + $translated_geometry = $adapter_loader->read($format); + #test_geometry($translated_geometry, FALSE); + } + } + } + } print "Done! Test passes!"; \ No newline at end of file