Index: gml.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/wfs/includes/Attic/gml.inc,v
retrieving revision 1.1.2.4
diff -r1.1.2.4 gml.inc
3a4,5
> require('wkt2gml.inc');
> 
6d7
<  * Adapted from OpenLayers
9,11d9
< //TODO: namespace or encapsulate into a class
< 
< define('NS_GML', 'http://www.opengis.net/gml');
13,15c11,21
< /**
<  * @param $wkt string of WKT data
<  * @return $geometry array of parsed geometry
---
> /*
>  *
>  * WKT to GML converter
>  *   info: Sandro Santilli, strk@keybit.net
>  *   distributed under GNU GPL v2 license
>  *
>  * GML2 and primitives are from a script by Nedjo Rogers:
>  *   This preliminary script is part of OpenSVGMapServer project, see
>  *   http://www.carto.net/opensvgmapserver
>  *   distributed under GNU GPL license
>  *   info: Nedjo Rogers, nedjo@miningwatch.org
17,18d22
<  * This function is not intended to be tested: 
<  * it is parsing geometries into an internal format.
20,41c24,87
< function parse_wkt($wkt) {
<   $type_string = '/^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/';
<   $paren_comma = '/\)\s*,\s*\(/';
<   $trim_parens = '/^\s*\(?(.*?)\)?\s*$/';
< 
<   $geometry = array();
<   if (preg_match($type_string, $wkt, $matches)) {
<     $type = strtolower($matches[1]);
< 
<     if ($type == 'polygon') {
<       $content = $matches[2];
<       $rings = preg_split($paren_comma, $content);
<       $rings = preg_replace($trim_parens, '$1', $rings);
< 
<       foreach ($rings as $ring) {
<         $r = array();
<         $points = explode(',', $ring);
< 
<         foreach ($points as $point) {
<           $r[] = $point;
<         }
<         $geometry[] = $r;
---
> class WKTParser {
> 
>   // Entry point for GML conversion
>   //
>   // @param $wkt     WKT representation string of the geometry
>   // @param $ver     GML version
>   // @param $off     If not null, make output indentend and starting
>   //                 from given offset (mainly used for easier debugging)
>   //
>   public function toGML($wkt, $ver, $off=null) {
> 
>     if ( $off !== null) {
>       $this->doIndent = True;
>       $this->inds = $off;
>     }
>     else $this->doIndent = False;
> 
>     if ($ver < 2 || $ver > 3) throw new Exception("Unsupported GML version $ver");
>     $this->gml_version = $ver;
> 
>     $out = '';
>     $this->convertGeometry($wkt, $out); 
>     return $out;
>   }
> 
> 
>   private $doIndent = False;
>   private $inds = 0;
>   private $gml_version = 2;
> 
>   private function geometrytype($geom){
>     $pos = strpos($geom, "(");
>     $geomType = substr($geom, 0, $pos);
>     return $geomType;
>   }
> 
>   private function dropShapeName($str) {
>     return strchr($str,"(");
>   }
> 
>   private function dropFirstLastChrs($str) {
>     $strLen=strlen($str);
>     return (substr($str,1,($strLen)-2));
>   }
> 
>   private function addSpacesMulti($str) {
>     $str = str_replace(")),((", ")) , ((", $str);
>     return $str;
>   }
> 
>   private function addSpacesSingle($str) {
>     $str = str_replace("),(", ") , (", $str);
>     return $str;
>   }
> 
>   private function explodeGeom($str) {
>     $strs = explode(" , ", $str);
>     return $strs;
>   }
> 
>   private function indent(&$out){
>     if ( $this->doIndent ) {
>       for($i=0;$i<$this->inds;$i++){
>         $out .= "  ";
46,47c92,98
<   return $geometry;
< }
---
>   private function openTag(&$out, $tag,$n){
>     $this->indent($out);
>     $out .= "<".$tag.">";
>     if($n==True && $this->doIndent){
>       $out .= "\n";
>     }
>   }
49,62c100,114
< /**
<  * @param $geometry array of parsed geometry
<  * @return $gml string of GML XML
<  */
< function geometry_to_gml($geometry) {
<   $doc = new SimpleXMLElement(
<     "<gml:polygon xmlns:gml='http://www.opengis.net/gml'></gml:polygon>");
<   $gml = $doc->addChild('Polygon', '', NS_GML);
<   $outer = TRUE;
< 
<   foreach ($geometry as $ring) {
<     if ($outer) {
<       $boundary = $gml->addChild('exterior', '', NS_GML);
<       $outer = FALSE;
---
>   private function closeTag(&$out, $tag,$nl,$ind=True){
>     if ( $ind ) $this->indent($out);
>     $out .= "</".$tag.">";
>     if($nl==True && $this->doIndent){
>       $out .= "\n";
>     }
>   }
> 
>   private function convertCoordinatesToGML($str, &$out){
>     if ( $this->gml_version == 2 ) {
>       $str = str_replace(" ","&",$str);
>       $str = str_replace(","," ",$str);
>       $str = str_replace("&",",",$str);
>       $str = str_replace("(","",$str);
>       $str = str_replace(")","",$str);
64c116,156
<       $boundary = $gml->addChild('interior', '', NS_GML);
---
>       /* In GML3, inside <posList> or <pos>, coordinates are separated by a space separator
>        * NOTE: in GML3 lat/lon are reversed for geocentric data !! (TODO?)
>        */
>       $str = str_replace("(","",$str);
>       $str = str_replace(")","",$str);
>       //$str = str_replace(" ","&",$str);
>       //$str = str_replace(","," ",$str);
>       //$str = str_replace("&",",",$str);
>     } 
>     
>     $out .= $str;
>   }
> 
>   private function convertGeometry($geom, &$out) {
>     $geomType=strtoupper($this->geometrytype($geom));
>     $geom=$this->dropFirstLastChrs($this->dropShapeName($geom));
>     switch ($geomType) {
>       case "POINT":
>         $this->convertPoint($geom,$out);
>         break;
>       case "LINESTRING":
>         $this->convertLineString($geom,$out);
>         break;
>       case "POLYGON":
>         $this->convertPolygon($geom,$out);
>         break;
>       case "MULTILINESTRING":
>         $this->convertMultiLineString($geom,$out);
>         break;
>       case "MULTIPOINT":
>         $this->convertMultiPoint($geom,$out);
>         break;
>       case "MULTIPOLYGON":
>         $this->convertMultiPolygon($geom,$out);
>         break;
>       case "GEOMETRYCOLLECTION":
>         $this->convertGeometryCollection($geom,$out);
>         break;
>       default:
>         throw new Exception("Dunno how to convert $geomType");
>         break;
65a158
>   }
67,69c160,283
<     $linear_ring = $boundary->addChild('gml:LinearRing', '', NS_GML);
<     $pos = $linear_ring->addChild('gml:posList', implode(' ', $ring), NS_GML);
<     $pos->addAttribute('decimal', '.');
---
>   private function convertMultiPoint($geom, &$out) {
>     $geom=$this->addSpacesMulti($geom);
>     $this->openTag($out, "gml:MultiPoint",True);
>     $this->inds++;
> 
>     $points = explode(",", $geom); 
>     foreach ($points as $point) {
>       $this->openTag($out, "gml:pointMember",True);
>       $this->inds++;
>       $this->convertPoint($point,$out);
>       $this->inds--;
>       $this->closeTag($out, "gml:pointMember",True);
>     }
>     $this->inds--;
>     $this->closeTag($out, "gml:MultiPoint",True);
>   }
> 
>   private function convertPoint($geom, &$out){
>     // [GML2]
>     // <gml:Point>
>     //  <gml:coordinates>0,0</gml:coordinates>
>     // </gml:Point>
>     //
>     // [GML3]
>     // <gml:Point><gml:pos srsDimension="2">0 0</gml:pos></gml:Point>
>     //
>     $this->openTag($out, "gml:Point",True);
>     $this->inds++;
>     $this->openTag($out, "gml:coordinates",False);
>     $this->convertCoordinatesToGML($geom,$out);
>     $this->closeTag($out, "gml:coordinates",True,False);
>     $this->inds--;
>     $this->closeTag($out, "gml:Point",True);
>   }
> 
>   private function convertMultiLineString($geom, &$out) {
>     $geom=$this->addSpacesSingle($geom);
>     $this->openTag($out, "gml:MultiLineString",True);
>     $this->inds++;
>     $lines=$this->explodeGeom($geom);
>     foreach ($lines as $line) {
>       $this->openTag($out, "gml:lineStringMember",True);
>       $this->inds++;
>       $this->convertLineString($this->dropFirstLastChrs($line),$out);
>       $this->inds--;
>       $this->closeTag($out, "gml:lineStringMember",True);
>     }
>     $this->inds--;
>     $this->closeTag($out, "gml:MultiLineString",True);
>   }
> 
>   private function convertLineString($geom, &$out){
>     $this->openTag($out, "gml:LineString",True);
>     $this->inds++;
>     $this->openTag($out, "gml:coordinates",False);
>     $this->convertCoordinatesToGML($geom,$out);
>     $this->closeTag($out, "gml:coordinates",True,False);
>     $this->inds--;
>     $this->closeTag($out, "gml:LineString",True);
>   }
> 
>   private function convertMultiPolygon($geom, &$out) {
>     $geom=$this->addSpacesMulti($geom);
>     $this->openTag($out, "gml:MultiPolygon",True);
>     $this->inds++;
>     $polys=$this->explodeGeom($geom);
>     foreach ($polys as $poly) {
>       $this->openTag($out, "gml:polygonMember",True);
>       $this->inds++;
>       $this->convertPolygon($this->dropFirstLastChrs($poly),$out);
>       $this->inds--;
>       $this->closeTag($out, "gml:polygonMember",True);
>     }
>     $this->inds--;
>     $this->closeTag($out, "gml:MultiPolygon",True);
>   }
> 
>   private function convertPolygon($geom, &$out){
>     $geom=$this->addSpacesSingle($geom);
>     $rings=$this->explodeGeom($geom);
>     $this->openTag($out, "gml:Polygon",True);
>     $this->inds++;
>     $pass=0;
>     foreach ($rings as $ring) {
>       $ring=$this->dropFirstLastChrs($ring);
>       if($pass==0){
>         $boundTag="outer";
>       }
>       else{
>         $boundTag="inner";
>       }
>       $this->openTag($out, "gml:".$boundTag."BoundaryIs",True);
>       $this->inds++;
>       $this->openTag($out, "gml:LinearRing",True);
>       $this->inds++;
>       $this->openTag($out, "gml:coordinates",False);
>       $this->convertCoordinatesToGML($ring,$out);
>       $this->closeTag($out, "gml:coordinates",True,False);
>       $this->inds--;
>       $this->closeTag($out, "gml:LinearRing",True);
>       $this->inds--;
>       $this->closeTag($out, "gml:".$boundTag."BoundaryIs",True);
>       $pass++;
>     }
>     $this->inds--;
>     $this->closeTag($out, "gml:Polygon",True);
>   }
> 
>   private function convertGeometryCollection($geom, &$out){
>     $searchstr = array(",POINT",",LINESTRING",",POLYGON",",MULTIPOINT",",MULTILINESTRING",",MULTIPOLYGON");
>     $replacestr = array(" , POINT"," , LINESTRING"," , POLYGON"," , MULTIPOINT"," , MULTILINESTRING"," , MULTIPOLYGON");
>     $geom = str_replace($searchstr,$replacestr,$geom);
>     $this->openTag($out, "gml:MultiGeometry",True);
>     $this->inds++;
>     $geoms=$this->explodeGeom($geom);
>     foreach ($geoms as $geom) {
>       $this->openTag($out, "gml:geometryMember",True);
>       $this->inds++;
>       $this->convertGeometry($geom,$out);
>       $this->inds--;
>       $this->closeTag($out, "gml:geometryMember",True);
>     }
>     $this->inds--;
>     $this->closeTag($out, "gml:MultiGeometry",True);
72,75d285
<   // this workaround required by the fact that 
<   // ->asXML() on root elements will produce XML opening and 
<   // closing tags
<   return $doc->children(NS_GML)->asXML();
83a294
>  * TODO: namespace or encapsulate into a class
86,87c297,308
<   $geometry = parse_wkt($wkt);
<   return empty($geometry) ? FALSE : geometry_to_gml($geometry);
---
> 
>   $parser = new WKTParser();
> 
>   try {
>     $gml2 =  $parser->toGML($wkt, 2);
>     watchdog('WFS', 'GML2: '.htmlentities($gml2), array(), WATCHDOG_DEBUG);
>     return $gml2;
>   } catch (Exception $e) {
>     watchdog('WFS', 'wkt_to_gml: '.$e->getMessage(), array(), WATCHDOG_ERROR); 
>     return FALSE;
>   }
> 
