Example #1
0
 /**
  * Read WKT string into geometry objects
  *
  * @param string $WKT A WKT string
  *
  * @return Geometry
  */
 public function read($wkt)
 {
     $wkt = trim($wkt);
     // If it contains a ';', then it contains additional SRID data
     if (strpos($wkt, ';')) {
         $parts = explode(';', $wkt);
         $wkt = $parts[1];
         $eparts = explode('=', $parts[0]);
         $srid = $eparts[1];
     } else {
         $srid = NULL;
     }
     // If geos is installed, then we take a shortcut and let it parse the WKT
     if (geoPHP::geosInstalled()) {
         $reader = new GEOSWKTReader();
         if ($srid) {
             $geom = geoPHP::geosToGeometry($reader->read($wkt));
             $geom->setSRID($srid);
             return $geom;
         } 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
     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, $wkt_geom);
             $method = 'parse' . $geom_type;
             if ($srid) {
                 $geom = $this->{$method}($data_string);
                 $geom->setSRID($srid);
                 return $geom;
             } else {
                 return $this->{$method}($data_string);
             }
         }
     }
 }
Example #2
0
File: KML.php Project: kitbs/geoPHP
 protected function parseGeometryCollection($xml)
 {
     $components = array();
     $geom_types = geoPHP::geometryList();
     foreach ($xml->childNodes as $child) {
         $nodeName = $child->nodeName == 'linearring' ? 'linestring' : $child->nodeName;
         if (array_key_exists($nodeName, $geom_types)) {
             $function = 'parse' . $geom_types[$nodeName];
             $components[] = $this->{$function}($child);
         }
     }
     return new GeometryCollection($components);
 }