| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- Lib::loadClass('Api_WfsException');
- class Api_WfsGeomTypeConverter {
- public function getType($geomType) {
- }
- public function convertGmlCoordinatesToWkt($gmlCoordinates) {
- $DBG = (V::get('DBG_GML_CONV', '', $_GET) > 0);// TODO: Profiler
- $wkt = null;
- if($DBG){echo "\ngmlCoordinates L." . __LINE__ . ": " . str_replace(' ', "\n", $gmlCoordinates) . "\n";}
- $gmlFormats = array();
- $gmlFormats[] = array('cs'=>',', 'ts'=>' ');// QGIS request
- $gmlFormats[] = array('cs'=>' ', 'ts'=>',', 'decimal'=>'.');// WKT
- $wktTypesMap = array();
- $wktTypesMap['Point'] = 'POINT';
- $wktTypesMap['LineString'] = 'LINESTRING';
- $wktTypesMap['Polygon'] = 'POLYGON';
- //$wktTypesMap[''] = 'MULTIPOINT';
- //$wktTypesMap[''] = 'MULTILINESTRING';
- //$wktTypesMap[''] = 'MULTIPOLYGON';
- /*
- <extension base="string">
- <attribute name="decimal" type="string" use="optional" default="."/>
- <attribute name="cs" type="string" use="optional" default=","/>
- <attribute name="ts" type="string" use="optional" default=" "/>
- </extension>
- */
- foreach ($gmlFormats as $i => $gmlFormat) {
- $gmlPoint = '((\-?\d+\.?\d*)' .$gmlFormat['cs'] . '(\-?\d+\.?\d*))';
- //$gmlPoints = $gmlPoint . '(' .$gmlFormat['ts'] . $gmlPoint . ')+';
- $gmlPoints = $gmlPoint . '(' .$gmlFormat['ts'] . $gmlPoint . ')*';
- $wktPolygonPattern = '/^([a-zA-Z]+)\(\(?' . $gmlPoints . '\)?\)$/';
- if($DBG){echo 'wktPolygonPattern:';print_r($wktPolygonPattern);echo "\n";}
- $matches = array();
- if (preg_match($wktPolygonPattern, $gmlCoordinates, $matches)) {
- if($DBG){echo "[{$i}]matches:";print_r($matches);echo "\n";}
- if (count($matches) > 2) {
- $wktType = $matches[1];
- if($DBG){echo 'wktType:';print_r($wktType);echo "\n";}
- if (!array_key_exists($wktType, $wktTypesMap)) {
- throw new Api_WfsException("Gml type '{$wktType}' not supported", 501);
- }
- $wkt = $gmlCoordinates;
- $wkt = str_replace(array($gmlFormat['cs'], $gmlFormat['ts']), array('#cs#', '#ts#'), $wkt);
- $wkt = str_replace(array('#cs#', '#ts#'), array(' ', ','), $wkt);
- $wkt = str_replace($wktType, $wktTypesMap[$wktType], $wkt);
- if($DBG){echo 'wkt:';print_r($wkt);echo "\n";}
- }
- break;
- }
- }
- if (!$wkt) {
- throw new Api_WfsException("Gml coordinates not supported", 501);
- }
- return $wkt;
- }
- public function createGmlFromWkt($geomAsWkt, $dom) {
- /* @see http://en.wikipedia.org/wiki/Geography_Markup_Language
- *
- * LineString:
- <gml:LineString xmlns:gml="http://www.opengis.net/gml" srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
- <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs=" " ts=",">
- 18.5142937987997 54.3142300873962,18.5142730427068 54.3142435923024
- </gml:coordinates>
- </gml:LineString>
- *
- * Polygon:
- <gml:Polygon xmlns:gml="http://www.opengis.net/gml" srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
- <gml:outerBoundaryIs>
- <gml:LinearRing>
- <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs=" " ts=",">18.4252094051732 54.377797436489,18.4150883433964 54.3729569286827,18.4425912286593 54.3696565824512,18.4379707439351 54.3780174595711,18.4252094051732 54.377797436489</gml:coordinates>
- </gml:LinearRing>
- </gml:outerBoundaryIs>
- </gml:Polygon>
- *
- * Point:
- <gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
- <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs=" " ts=",">100 200</gml:coordinates>
- </gml:Point>
- *
- */
- $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
- if (empty($geomAsWkt)) return null;
- // WKT: http://en.wikipedia.org/wiki/Well-known_text
- if($DBG){echo 'wkt:';print_r($geomAsWkt);echo "\n";}
- $gmlCoordinatesFromWkt = '';
- $bbox = null;
- $bboxPolygon = null;
- $wktType = substr($geomAsWkt, 0, strpos($geomAsWkt, '('));
- $sql = "select
- -- GeometryType(GeomFromText('{$geomAsWkt}')) as type,
- AsText(Envelope(GeomFromText('{$geomAsWkt}'))) as bbox
- ";
- $db = DB::getDB();
- $res = $db->query($sql);
- if ($r = $db->fetch($res)) {
- $bboxPolygon = $r->bbox;
- }
- if($DBG){echo 'wktType:';print_r($wktType);echo "\n";}
- if($DBG){echo 'bboxPolygon:';print_r($bboxPolygon);echo "\n";}
- if ($bboxPolygon) {// eg. POLYGON((18.550927583884 54.3124269117497,18.6401914998997 54.3124269117497,18.6401914998997 54.3608667397837,18.550927583884 54.3608667397837,18.550927583884 54.3124269117497))
- $bbox = '';
- $wktPoint = '(\d+\.?\d*) (\d+\.?\d*)';
- $wktPolygonPattern = '/^POLYGON\(\(' . $wktPoint . ',' . $wktPoint . ',' . $wktPoint . ',' . $wktPoint . ',' . $wktPoint . '\)\)$/';
- if($DBG){echo 'wktPolygonPattern:';print_r($wktPolygonPattern);echo "\n";}
- if (preg_match($wktPolygonPattern, $bboxPolygon, $matches)) {
- $bbox = $matches;
- }
- }
- if($DBG){echo 'bbox:';print_r($bbox);echo "\n";}
- if ('POLYGON' === $wktType) {
- $gmlCoordinatesFromWkt = $geomAsWkt;
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, strlen('POLYGON(('));
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, 0, -1 * strlen('))'));
- $gmlType = 'Polygon';
- }
- else if ('LINESTRING' === $wktType) {
- $gmlCoordinatesFromWkt = $geomAsWkt;
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, strlen('LINESTRING('));
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, 0, -1 * strlen(')'));
- $gmlType = 'LineString';
- }
- else if ('POINT' === $wktType) {
- $gmlCoordinatesFromWkt = $geomAsWkt;
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, strlen('POINT('));
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, 0, -1 * strlen(')'));
- $gmlCoordinatesFromWkt = str_replace(' ', ',', $gmlCoordinatesFromWkt);
- $gmlType = 'Point';
- }
- else {
- return null;
- }
- if($DBG){echo 'gmlCoordinatesFromWkt:';print_r($gmlCoordinatesFromWkt);echo "\n";}
- $geomNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:' . $gmlType);
- $geomNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
- if ('Point' === $gmlType) {
- $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
- $geomNode->appendChild($coordinatesNode);
- //$coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
- //$coordinatesNode->setAttribute('decimal', '.');
- //$coordinatesNode->setAttribute('cs', ' ');
- //$coordinatesNode->setAttribute('ts', ',');
- $coordinatesNode->nodeValue = $gmlCoordinatesFromWkt;
- } else if ('LineString' === $gmlType) {
- $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
- $geomNode->appendChild($coordinatesNode);
- $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
- $coordinatesNode->setAttribute('decimal', '.');
- $coordinatesNode->setAttribute('cs', ' ');
- $coordinatesNode->setAttribute('ts', ',');
- $coordinatesNode->nodeValue = $gmlCoordinatesFromWkt;
- } else {
- $outerBoundsNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:outerBoundaryIs');
- $geomNode->appendChild($outerBoundsNode);
- $linearRingNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:LinearRing');
- $outerBoundsNode->appendChild($linearRingNode);
- $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
- $linearRingNode->appendChild($coordinatesNode);
- $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
- $coordinatesNode->setAttribute('decimal', '.');
- $coordinatesNode->setAttribute('cs', ' ');
- $coordinatesNode->setAttribute('ts', ',');
- $coordinatesNode->nodeValue = $gmlCoordinatesFromWkt;
- }
- return $geomNode;
- }
- public function createGmlFromWkt_xmlWriter($geomAsWkt, $xmlWriter) {
- $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
- if (empty($geomAsWkt)) return null;
- // WKT: http://en.wikipedia.org/wiki/Well-known_text
- if($DBG){echo 'wkt:';print_r($geomAsWkt);echo "\n";}
- $gmlCoordinatesFromWkt = '';
- $bbox = null;
- $bboxPolygon = null;
- $wktType = substr($geomAsWkt, 0, strpos($geomAsWkt, '('));
- $sql = "select
- -- GeometryType(GeomFromText('{$geomAsWkt}')) as type,
- AsText(Envelope(GeomFromText('{$geomAsWkt}'))) as bbox
- ";
- $db = DB::getDB();
- $res = $db->query($sql);
- if ($r = $db->fetch($res)) {
- $bboxPolygon = $r->bbox;
- }
- if($DBG){echo 'wktType:';print_r($wktType);echo "\n";}
- if($DBG){echo 'bboxPolygon:';print_r($bboxPolygon);echo "\n";}
- if ($bboxPolygon) {// eg. POLYGON((18.550927583884 54.3124269117497,18.6401914998997 54.3124269117497,18.6401914998997 54.3608667397837,18.550927583884 54.3608667397837,18.550927583884 54.3124269117497))
- $bbox = '';
- $wktPoint = '(\d+\.?\d*) (\d+\.?\d*)';
- $wktPolygonPattern = '/^POLYGON\(\(' . $wktPoint . ',' . $wktPoint . ',' . $wktPoint . ',' . $wktPoint . ',' . $wktPoint . '\)\)$/';
- if($DBG){echo 'wktPolygonPattern:';print_r($wktPolygonPattern);echo "\n";}
- if (preg_match($wktPolygonPattern, $bboxPolygon, $matches)) {
- $bbox = $matches;
- }
- }
- if($DBG){echo 'bbox:';print_r($bbox);echo "\n";}
- if ('POLYGON' === $wktType) {
- $gmlCoordinatesFromWkt = $geomAsWkt;
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, strlen('POLYGON(('));
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, 0, -1 * strlen('))'));
- $gmlType = 'Polygon';
- }
- else if ('LINESTRING' === $wktType) {
- $gmlCoordinatesFromWkt = $geomAsWkt;
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, strlen('LINESTRING('));
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, 0, -1 * strlen(')'));
- $gmlType = 'LineString';
- }
- else if ('POINT' === $wktType) {
- $gmlCoordinatesFromWkt = $geomAsWkt;
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, strlen('POINT('));
- $gmlCoordinatesFromWkt = substr($gmlCoordinatesFromWkt, 0, -1 * strlen(')'));
- $gmlCoordinatesFromWkt = str_replace(' ', ',', $gmlCoordinatesFromWkt);
- $gmlType = 'Point';
- }
- else {
- return null;
- }
- if($DBG){echo 'gmlCoordinatesFromWkt:';print_r($gmlCoordinatesFromWkt);echo "\n";}
- //$xmlWriter->startElement("gml:{$gmlType}");
- $xmlWriter->startElementNS('gml', $gmlType, 'http://www.opengis.net/gml');
- $xmlWriter->writeAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
- if ('Point' === $gmlType) {
- $xmlWriter->startElement("gml:coordinates");
- //$coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
- //$coordinatesNode->setAttribute('decimal', '.');
- //$coordinatesNode->setAttribute('cs', ' ');
- //$coordinatesNode->setAttribute('ts', ',');
- $xmlWriter->text($gmlCoordinatesFromWkt);
- $xmlWriter->endElement();// gml:coordinates
- } else if ('LineString' === $gmlType) {
- $xmlWriter->startElement("gml:coordinates");
- $xmlWriter->writeAttribute('decimal', '.');
- $xmlWriter->writeAttribute('cs', ' ');
- $xmlWriter->writeAttribute('ts', ',');
- $xmlWriter->text($gmlCoordinatesFromWkt);
- $xmlWriter->endElement();// gml:coordinates
- } else {
- $xmlWriter->startElement('gml:outerBoundaryIs');
- $xmlWriter->startElement('gml:LinearRing');
- $xmlWriter->startElement('gml:coordinates');
- $xmlWriter->writeAttribute('decimal', '.');
- $xmlWriter->writeAttribute('cs', ' ');
- $xmlWriter->writeAttribute('ts', ',');
- $xmlWriter->text($gmlCoordinatesFromWkt);
- $xmlWriter->endElement();// gml:coordinates
- $xmlWriter->endElement();// gml:LinearRing
- $xmlWriter->endElement();// gml:outerBoundaryIs
- }
- $xmlWriter->endElement();// gml:{$gmlType}
- }
- }
|