You are here

public function WKT::read in geoPHP 8

Same name and namespace in other branches
  1. 7 geoPHP/lib/adapters/WKT.class.php \WKT::read()

Read WKT string into geometry objects

Parameters

string $WKT A WKT string:

Return value

Geometry

Overrides GeoAdapter::read

1 call to WKT::read()
WKT::parseGeometryCollection in geoPHP/lib/adapters/WKT.class.php

File

geoPHP/lib/adapters/WKT.class.php, line 15

Class

WKT
WKT (Well Known Text) Adapter

Code

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);
      }
    }
  }
}