/** * @param WKBBuffer $buffer * @param integer $srid * * @return Geometry * * @throws GeometryIOException */ protected function readGeometry(WKBBuffer $buffer, $srid) { $buffer->readByteOrder(); $this->readGeometryHeader($buffer, $geometryType, $hasZ, $hasM, $srid); $cs = new CoordinateSystem($hasZ, $hasM, $srid); switch ($geometryType) { case Geometry::POINT: return $this->readPoint($buffer, $cs); case Geometry::LINESTRING: return $this->readLineString($buffer, $cs); case Geometry::CIRCULARSTRING: return $this->readCircularString($buffer, $cs); case Geometry::COMPOUNDCURVE: return $this->readCompoundCurve($buffer, $cs); case Geometry::POLYGON: return $this->readPolygon($buffer, $cs); case Geometry::CURVEPOLYGON: return $this->readCurvePolygon($buffer, $cs); case Geometry::MULTIPOINT: return $this->readMultiPoint($buffer, $cs); case Geometry::MULTILINESTRING: return $this->readMultiLineString($buffer, $cs); case Geometry::MULTIPOLYGON: return $this->readMultiPolygon($buffer, $cs); case Geometry::GEOMETRYCOLLECTION: return $this->readGeometryCollection($buffer, $cs); case Geometry::POLYHEDRALSURFACE: return $this->readPolyhedralSurface($buffer, $cs); case Geometry::TIN: return $this->readTIN($buffer, $cs); case Geometry::TRIANGLE: return $this->readTriangle($buffer, $cs); } throw GeometryIOException::unsupportedGeometryType($geometryType); }
/** * @param string $wkt The WKT to read. * @param integer $srid The optional SRID of the geometry. * * @return Geometry * * @throws GeometryIOException */ public function read($wkt, $srid = 0) { $parser = new WKTParser(strtoupper($wkt)); $geometry = $this->readGeometry($parser, $srid); if (!$parser->isEndOfStream()) { throw GeometryIOException::invalidWKT(); } return $geometry; }
/** * @param string $ewkt The EWKT to read. * * @return Geometry * * @throws GeometryIOException */ public function read($ewkt) { $parser = new EWKTParser(strtoupper($ewkt)); $srid = $parser->getOptionalSRID(); $geometry = $this->readGeometry($parser, $srid); if (!$parser->isEndOfStream()) { throw GeometryIOException::invalidEWKT(); } return $geometry; }
/** * {@inheritdoc} */ protected function readGeometryHeader(WKBBuffer $buffer, &$geometryType, &$hasZ, &$hasM, &$srid) { $wkbType = $buffer->readUnsignedLong(); $geometryType = $wkbType % 1000; $dimension = ($wkbType - $geometryType) / 1000; if ($dimension < 0 || $dimension > 3) { throw GeometryIOException::unsupportedWKBType($wkbType); } $hasZ = $dimension === 1 || $dimension === 3; $hasM = $dimension === 2 || $dimension === 3; }
/** * Detects the machine byte order (big endian or little endian). * * @return integer * * @throws GeometryIOException */ public static function getMachineByteOrder() { static $byteOrder; if ($byteOrder === null) { self::checkDoubleIs64Bit(); switch (pack('L', 0x61626364)) { case 'abcd': $byteOrder = self::BIG_ENDIAN; break; case 'dcba': $byteOrder = self::LITTLE_ENDIAN; break; default: throw GeometryIOException::unsupportedEndianness(); } } return $byteOrder; }
/** * {@inheritdoc} */ protected function readGeometryHeader(WKBBuffer $buffer, &$geometryType, &$hasZ, &$hasM, &$srid) { $header = $buffer->readUnsignedLong(); if ($header >= 0 && $header < 4000) { $geometryType = $header % 1000; $dimension = ($header - $geometryType) / 1000; if ($dimension < 0 || $dimension > 3) { throw GeometryIOException::unsupportedWKBType($header); } $hasZ = $dimension === 1 || $dimension === 3; $hasM = $dimension === 2 || $dimension === 3; } else { $geometryType = $header & 0xfff; $hasZ = ($header & EWKBTools::Z) !== 0; $hasM = ($header & EWKBTools::M) !== 0; $hasSRID = ($header & EWKBTools::S) !== 0; if ($hasSRID) { $srid = $buffer->readUnsignedLong(); } } }
/** * Reads the machine byte order from the buffer and stores the result to act accordingly. * * @throws GeometryIOException */ public function readByteOrder() { $byteOrder = $this->readUnsignedChar(); if ($byteOrder !== WKBTools::BIG_ENDIAN && $byteOrder !== WKBTools::LITTLE_ENDIAN) { throw GeometryIOException::invalidWKB('unknown byte order: ' . $byteOrder); } $this->invert = $byteOrder !== $this->machineByteOrder; }
/** * @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 . ')'; }
/** * @param Geometry $geometry The geometry export as WKB write. * @param boolean $outer False if the geometry is nested in another geometry, true otherwise. * * @return string The WKB representation of the given geometry. * * @throws GeometryIOException If the given geometry cannot be exported as WKT. */ protected function doWrite(Geometry $geometry, $outer) { if ($geometry instanceof Point) { return $this->writePoint($geometry, $outer); } if ($geometry instanceof LineString) { return $this->writeCurve($geometry, $outer); } if ($geometry instanceof CircularString) { return $this->writeCurve($geometry, $outer); } if ($geometry instanceof Polygon) { return $this->writePolygon($geometry, $outer); } if ($geometry instanceof CompoundCurve) { return $this->writeComposedGeometry($geometry, $outer); } if ($geometry instanceof CurvePolygon) { return $this->writeComposedGeometry($geometry, $outer); } if ($geometry instanceof GeometryCollection) { return $this->writeComposedGeometry($geometry, $outer); } if ($geometry instanceof PolyhedralSurface) { return $this->writeComposedGeometry($geometry, $outer); } throw GeometryIOException::unsupportedGeometryType($geometry->geometryType()); }