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';
/*
*/
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:
18.5142937987997 54.3142300873962,18.5142730427068 54.3142435923024
*
* Polygon:
18.4252094051732 54.377797436489,18.4150883433964 54.3729569286827,18.4425912286593 54.3696565824512,18.4379707439351 54.3780174595711,18.4252094051732 54.377797436489
*
* Point:
100,200
*
*/
$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;
}
}