Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function write(Geometry $geometry)
 {
     $srid = $geometry->SRID();
     if ($srid === 0) {
         return $this->doWrite($geometry);
     }
     return 'SRID=' . $geometry->SRID() . ';' . $this->prettyPrintSpace . $this->doWrite($geometry);
 }
Beispiel #2
0
 /**
  * @param Geometry $geometry The geometry to export as WKT.
  *
  * @return string The WKT representation of the given geometry.
  *
  * @throws GeometryIOException If the given geometry cannot be exported as WKT.
  */
 protected function doWrite(Geometry $geometry)
 {
     $type = strtoupper($geometry->geometryType());
     $cs = $geometry->coordinateSystem();
     $hasZ = $cs->hasZ();
     $hasM = $cs->hasM();
     $dimensionality = '';
     if ($hasZ || $hasM) {
         $dimensionality .= ' ';
         if ($hasZ) {
             $dimensionality .= 'Z';
         }
         if ($hasM) {
             $dimensionality .= 'M';
         }
     }
     if ($geometry instanceof GeometryCollection) {
         $isEmpty = $geometry->numGeometries() === 0;
     } else {
         $isEmpty = $geometry->isEmpty();
     }
     if ($isEmpty) {
         return $type . $dimensionality . ' EMPTY';
     }
     if ($geometry instanceof Point) {
         $data = $this->writePoint($geometry);
     } elseif ($geometry instanceof LineString) {
         $data = $this->writeLineString($geometry);
     } elseif ($geometry instanceof CircularString) {
         $data = $this->writeCircularString($geometry);
     } elseif ($geometry instanceof CompoundCurve) {
         $data = $this->writeCompoundCurve($geometry);
     } elseif ($geometry instanceof Triangle) {
         $data = $this->writePolygon($geometry);
     } elseif ($geometry instanceof Polygon) {
         $data = $this->writePolygon($geometry);
     } elseif ($geometry instanceof CurvePolygon) {
         $data = $this->writeCurvePolygon($geometry);
     } elseif ($geometry instanceof MultiPoint) {
         $data = $this->writeMultiPoint($geometry);
     } elseif ($geometry instanceof MultiLineString) {
         $data = $this->writeMultiLineString($geometry);
     } elseif ($geometry instanceof MultiPolygon) {
         $data = $this->writeMultiPolygon($geometry);
     } elseif ($geometry instanceof GeometryCollection) {
         $data = $this->writeGeometryCollection($geometry);
     } elseif ($geometry instanceof TIN) {
         $data = $this->writePolyhedralSurface($geometry);
     } elseif ($geometry instanceof PolyhedralSurface) {
         $data = $this->writePolyhedralSurface($geometry);
     } else {
         throw GeometryIOException::unsupportedGeometryType($geometry->geometryType());
     }
     return $type . $dimensionality . $this->prettyPrintSpace . '(' . $data . ')';
 }
Beispiel #3
0
 /**
  * {@inheritdoc}
  */
 protected function packHeader(Geometry $geometry, $outer)
 {
     $geometryType = $geometry->geometryTypeBinary();
     $cs = $geometry->coordinateSystem();
     if ($cs->hasZ()) {
         $geometryType += 1000;
     }
     if ($cs->hasM()) {
         $geometryType += 2000;
     }
     return $this->packUnsignedInteger($geometryType);
 }
Beispiel #4
0
 /**
  * {@inheritdoc}
  */
 public function getIterator()
 {
     if ($this->proxyGeometry === null) {
         $this->load();
     }
     return $this->proxyGeometry->getIterator();
 }
Beispiel #5
0
 /**
  * @param \GEOSGeometry $geometry
  *
  * @return Geometry
  */
 private function fromGEOS(\GEOSGeometry $geometry)
 {
     if ($geometry->isEmpty()) {
         return Geometry::fromText($this->wktWriter->write($geometry), $geometry->getSRID());
     }
     if ($this->hasBinaryReadWrite) {
         return $this->ewkbReader->read($this->wkbWriter->write($geometry));
     }
     return $this->ewkbReader->read(hex2bin($this->wkbWriter->writeHEX($geometry)));
 }
Beispiel #6
0
 /**
  * {@inheritdoc}
  */
 protected function packHeader(Geometry $geometry, $outer)
 {
     $geometryType = $geometry->geometryTypeBinary();
     $cs = $geometry->coordinateSystem();
     if ($cs->hasZ()) {
         $geometryType |= EWKBTools::Z;
     }
     if ($cs->hasM()) {
         $geometryType |= EWKBTools::M;
     }
     $srid = $cs->SRID();
     if ($srid !== 0 && $outer) {
         $geometryType |= EWKBTools::S;
     }
     $header = $this->packUnsignedInteger($geometryType);
     if ($srid !== 0 && $outer) {
         $header .= $this->packUnsignedInteger($srid);
     }
     return $header;
 }
Beispiel #7
0
 /**
  * {@inheritdoc}
  */
 public function expand($className, $value, array $options = [])
 {
     if ($className == Geometry::class || is_subclass_of($className, Geometry::class)) {
         try {
             $geometry = Geometry::fromText($value);
             if (!$geometry instanceof $className) {
                 throw new ObjectNotConvertibleException(sprintf('Expected instance of %s, got instance of %s.', $className, get_class($geometry)));
             }
             return $geometry;
         } catch (GeometryException $e) {
             throw new ObjectNotConvertibleException($e->getMessage(), $e->getCode(), $e);
         }
     }
     return null;
 }
Beispiel #8
0
 /**
  * @param CoordinateSystem $cs        The coordinate system.
  * @param float            ...$coords The point coordinates; can be empty for an empty point.
  *
  * @return Point
  *
  * @throws InvalidGeometryException If the number of coordinates does not match the coordinate system.
  */
 public function __construct(CoordinateSystem $cs, ...$coords)
 {
     parent::__construct($cs, !$coords);
     if ($coords) {
         if (count($coords) !== $cs->coordinateDimension()) {
             throw new InvalidGeometryException(sprintf('Expected %d coordinates for Point %s, got %d.', $cs->coordinateDimension(), $cs->coordinateName(), count($coords)));
         }
         $this->x = (double) $coords[0];
         $this->y = (double) $coords[1];
         $hasZ = $cs->hasZ();
         $hasM = $cs->hasM();
         if ($hasZ) {
             $this->z = (double) $coords[2];
         }
         if ($hasM) {
             $this->m = (double) $coords[$hasZ ? 3 : 2];
         }
     }
 }
Beispiel #9
0
 /**
  * Creates a non-empty GeometryCollection composed of the given geometries.
  *
  * @param Geometry    $geometry1 The first geometry.
  * @param Geometry ...$geometryN The subsequent geometries, if any.
  *
  * @return static
  *
  * @throws CoordinateSystemException   If the geometries use different coordinate systems.
  * @throws UnexpectedGeometryException If a geometry is not a valid type for a sub-class of GeometryCollection.
  */
 public static function of(Geometry $geometry1, Geometry ...$geometryN)
 {
     return new static($geometry1->coordinateSystem(), $geometry1, ...$geometryN);
 }
Beispiel #10
0
 /**
  * @param Geometry $reference
  * @param Geometry $culprit
  *
  * @return CoordinateSystemException
  */
 public static function dimensionalityMix(Geometry $reference, Geometry $culprit)
 {
     return new CoordinateSystemException(sprintf('Dimensionality mix: %s %s cannot contain %s %s.', $reference->geometryType(), $reference->coordinateSystem()->coordinateName(), $culprit->geometryType(), $culprit->coordinateSystem()->coordinateName()));
 }
Beispiel #11
0
 /**
  * @param Geometry $g      The Geometry to test.
  * @param array    $coords The expected raw coordinates of the geometry.
  * @param boolean  $hasZ   Whether the geometry is expected to contain Z coordinates.
  * @param boolean  $hasM   Whether the geometry is expected to contain M coordinates.
  * @param integer  $srid   The expected SRID of the geometry.
  */
 protected final function assertGeometryContents(Geometry $g, array $coords, $hasZ = false, $hasM = false, $srid = 0)
 {
     $this->castToFloat($coords);
     $this->assertSame($coords, $g->toArray());
     $this->assertSame($hasZ, $g->is3D());
     $this->assertSame($hasM, $g->isMeasured());
     $this->assertSame($srid, $g->SRID());
 }
Beispiel #12
0
 /**
  * @param Geometry    $reference  The geometry holding the reference coordinate system.
  * @param Geometry ...$geometries The geometries to check against this coordinate system.
  *
  * @return void
  *
  * @throws CoordinateSystemException If the coordinate systems differ.
  */
 public static function check(Geometry $reference, Geometry ...$geometries)
 {
     $referenceCS = $reference->coordinateSystem();
     foreach ($geometries as $geometry) {
         $geometryCS = $geometry->coordinateSystem();
         if ($geometryCS == $referenceCS) {
             // by-value comparison.
             continue;
         }
         if ($geometryCS->srid !== $referenceCS->srid) {
             throw CoordinateSystemException::sridMix($reference, $geometry);
         }
         throw CoordinateSystemException::dimensionalityMix($reference, $geometry);
     }
 }
Beispiel #13
0
 /**
  * @dataProvider providerToArray
  *
  * @param string $geometry The WKT of the geometry to test.
  * @param array  $array    The expected result array.
  */
 public function testToArray($geometry, $array)
 {
     $this->castToFloat($array);
     $this->assertSame($array, Geometry::fromText($geometry)->toArray());
 }