Skip to content

Commit

Permalink
Fixing some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Apr 23, 2011
1 parent bfa3ef7 commit c49cd83
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/adapters/GPX.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<gpx creator="geoPHP" version="1.0">'.$this->geometryToGPX($geometry).'</gpx>';
}

public function geomFromText($text) {
Expand Down
33 changes: 23 additions & 10 deletions lib/adapters/KML.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -188,8 +198,11 @@ private function pointToKML($geom) {
return "<Point><coordinates>".$geom->getX().",".$geom->getY()."</coordinates></Point>";
}

private function linestringToKML($geom) {
$type = $geom->getGeomType();
private function linestringToKML($geom, $type = FALSE) {
if (!$type) {
$type = $geom->getGeomType();
}

$str = '<'. $type .'><coordinates>';
$i=0;
foreach ($geom->getComponents() as $comp) {
Expand All @@ -203,7 +216,7 @@ private function linestringToKML($geom) {

public function polygonToKML($geom) {
$components = $geom->getComponents();
$str = '<outerBoundaryIs>' . $this->linestringToKML($components[0]) . '</outerBoundaryIs>';
$str = '<outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</outerBoundaryIs>';
foreach (array_slice($components, 1) as $comp) {
$str .= '<innerBoundaryIs>' . $this->linestringToKML($comp) . '</innerBoundaryIs>';
}
Expand Down
12 changes: 6 additions & 6 deletions lib/geometry/Geometry.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -85,7 +86,6 @@ public function geometryType() {
return $this->geom_type;
}


// Public: Non-Standard -- Common to all geometries
// ------------------------------------------------
public function getGeoInterface() {
Expand Down
4 changes: 2 additions & 2 deletions lib/geometry/Polygon.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 21 additions & 10 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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();
Expand Down Expand Up @@ -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!";

0 comments on commit c49cd83

Please sign in to comment.