Skip to content

Commit

Permalink
fix parsing of EMPTY wkt geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
mprins committed Aug 29, 2014
1 parent 5235994 commit dd363b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
62 changes: 34 additions & 28 deletions lib/adapters/WKT.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
class WKT extends GeoAdapter
{

/**
* Read WKT string into geometry objects
*
Expand All @@ -14,7 +14,7 @@ class WKT extends GeoAdapter
*/
public function read($wkt) {
$wkt = trim($wkt);

// If it contains a ';', then it contains additional SRID data
if (strpos($wkt,';')) {
$parts = explode(';', $wkt);
Expand All @@ -25,7 +25,7 @@ public function read($wkt) {
else {
$srid = NULL;
}

// If geos is installed, then we take a shortcut and let it parse the WKT
if (geoPHP::geosInstalled()) {
$reader = new GEOSWKTReader();
Expand All @@ -34,35 +34,39 @@ public function read($wkt) {
$geom->setSRID($srid);
return $geom;
}
else {
else {
return geoPHP::geosToGeometry($reader->read($wkt));
}
}
$wkt = str_replace(', ', ',', $wkt);

// For each geometry type, check to see if we have a match at the
// beggining of the string. If we do, then parse using that type
// beginning of the string. If we do, then parse using that type
foreach (geoPHP::geometryList() as $geom_type) {
$wkt_geom = strtoupper($geom_type);
if (strtoupper(substr($wkt, 0, strlen($wkt_geom))) == $wkt_geom) {
$data_string = $this->getDataString($wkt);
$method = 'parse'.$geom_type;

if ($srid) {
$geom = $this->$method($data_string);
$geom->setSRID($srid);
return $geom;
}
else {
else {
return $this->$method($data_string);
}

}
}
}

private function parsePoint($data_string) {
$data_string = $this->trimParens($data_string);

// If it's marked as empty, then return an empty point
if ($data_string == 'EMPTY') return new Point();

$parts = explode(' ',$data_string);
return new Point($parts[0], $parts[1]);
}
Expand All @@ -72,7 +76,7 @@ private function parseLineString($data_string) {

// If it's marked as empty, then return an empty line
if ($data_string == 'EMPTY') return new LineString();

$parts = explode(',',$data_string);
$points = array();
foreach ($parts as $part) {
Expand All @@ -83,10 +87,10 @@ private function parseLineString($data_string) {

private function parsePolygon($data_string) {
$data_string = $this->trimParens($data_string);

// If it's marked as empty, then return an empty polygon
if ($data_string == 'EMPTY') return new Polygon();

$parts = explode('),(',$data_string);
$lines = array();
foreach ($parts as $part) {
Expand All @@ -99,24 +103,24 @@ private function parsePolygon($data_string) {

private function parseMultiPoint($data_string) {
$data_string = $this->trimParens($data_string);

// If it's marked as empty, then return an empty MutiPoint
if ($data_string == 'EMPTY') return new MultiPoint();

$parts = explode(',',$data_string);
$points = array();
foreach ($parts as $part) {
$points[] = $this->parsePoint($part);
}
return new MultiPoint($points);
}

private function parseMultiLineString($data_string) {
$data_string = $this->trimParens($data_string);

// If it's marked as empty, then return an empty multi-linestring
if ($data_string == 'EMPTY') return new MultiLineString();

$parts = explode('),(',$data_string);
$lines = array();
foreach ($parts as $part) {
Expand All @@ -133,7 +137,7 @@ private function parseMultiPolygon($data_string) {

// If it's marked as empty, then return an empty multi-polygon
if ($data_string == 'EMPTY') return new MultiPolygon();

$parts = explode(')),((',$data_string);
$polys = array();
foreach ($parts as $part) {
Expand All @@ -150,12 +154,12 @@ private function parseGeometryCollection($data_string) {

// If it's marked as empty, then return an empty geom-collection
if ($data_string == 'EMPTY') return new GeometryCollection();

$geometries = array();
$matches = array();
$str = preg_replace('/,\s*([A-Za-z])/', '|$1', $data_string);
$components = explode('|', trim($str));

foreach ($components as $component) {
$geometries[] = $this->read($component);
}
Expand All @@ -167,23 +171,25 @@ protected function getDataString($wkt) {

if ($first_paren !== FALSE) {
return substr($wkt, $first_paren);
}
return FALSE;
} elseif (strstr($wkt,'EMPTY')) {
return 'EMPTY';
} else
return FALSE;
}

/**
* Trim the parenthesis and spaces
*/
protected function trimParens($str) {
$str = trim($str);

// We want to only strip off one set of parenthesis
if ($this->beginsWith($str, '(')) {
return substr($str,1,-1);
}
else return $str;
}

protected function beginsWith($str, $char) {
if (substr($str,0,strlen($char)) == $char) return TRUE;
else return FALSE;
Expand All @@ -193,7 +199,7 @@ protected function endsWith($str, $char) {
if (substr($str,(0 - strlen($char))) == $char) return TRUE;
else return FALSE;
}

/**
* Serialize geometries into a WKT string.
*
Expand All @@ -208,15 +214,15 @@ public function write(Geometry $geometry) {
$writer->setTrim(TRUE);
return $writer->write($geometry->geos());
}

if ($geometry->isEmpty()) {
return strtoupper($geometry->geometryType()).' EMPTY';
}
else if ($data = $this->extractData($geometry)) {
return strtoupper($geometry->geometryType()).' ('.$data.')';
}
}

/**
* Extract geometry to a WKT string
*
Expand Down
1 change: 1 addition & 0 deletions tests/input/empty_point.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POINT EMPTY

0 comments on commit dd363b4

Please sign in to comment.