file; #$data = implode("",file($url)); if(!$data){ echo "Unable to open document: ".$url; die; } # for temporary wms a id has to be created... $this->wfs_id = "id_" . substr(md5(rand()),0,6); $parser = xml_parser_create(CHARSET); xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0); xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1); xml_parser_set_option($parser,XML_OPTION_TARGET_ENCODING,CHARSET); xml_parse_into_struct($parser,$data,$values,$tags); xml_parser_free($parser); $section = false; $request = false; $featuretype_name = false; $featuretype_title = false; $featuretype_srs = false; foreach ($values as $element) { if(strtoupper($element[tag]) == "WFS_CAPABILITIES" && $element[type] == "open"){ $this->wfs_version = $element[attributes][version]; } if(strtoupper($element[tag]) == "NAME" && $element[level] == '3'){ $this->wfs_name = $element[value]; } if(strtoupper($element[tag]) == "TITLE" && $element[level] == '3'){ $this->wfs_title = $element[value]; } if(strtoupper($element[tag]) == "ABSTRACT" && $element[level] == '3'){ $this->wfs_abstract = $element[value]; } /*capability section*/ if($this->wfs_version == "1.0.0"){ # getCapabilities if(strtoupper($element[tag]) == "GETCAPABILITIES" && $element[type] == "open"){ $section = "getcapabilities"; } if($section == "getcapabilities" && strtoupper($element[tag]) == "GET"){ $this->wfs_getcapabilities = $element[attributes][onlineResource]; } # descriptFeatureType if(strtoupper($element[tag]) == "DESCRIBEFEATURETYPE" && $element[type] == "open"){ $section = "describefeaturetype"; $this->wfs_describefeaturetype = $element[attributes][onlineResource]; } if($section == "describefeaturetype" && strtoupper($element[tag]) == "GET"){ $this->wfs_describefeaturetype = $element[attributes][onlineResource]; } # getFeature if(strtoupper($element[tag]) == "GETFEATURE" && $element[type] == "open"){ $section = "getfeature"; } if($section == "getfeature" && strtoupper($element[tag]) == "GET"){ $this->wfs_getfeature = $element[attributes][onlineResource]; } if(strtoupper($element[tag]) == "GETFEATURE" && $element[type] == "close"){ $section = ""; } # transaction if(strtoupper($element[tag]) == "TRANSACTION" && $element[type] == "open"){ $section = "transaction"; } if($section == "transaction" && strtoupper($element[tag]) == "GET"){ $this->wfs_transaction = $element[attributes][onlineResource]; } if(strtoupper($element[tag]) == "TRANSACTION" && $element[type] == "close"){ $section = ""; } } if(strtoupper($element[tag]) == "FEATURETYPE" && $element[type] == "open"){ $section = "featuretype"; } if($section == "featuretype" && strtoupper($element[tag]) == "NAME"){ $featuretype_name = $element[value]; } if($section == "featuretype" && strtoupper($element[tag]) == "TITLE"){ $featuretype_title = $element[value]; } if($section == "featuretype" && strtoupper($element[tag]) == "SRS"){ $featuretype_srs = $element[value]; $this->addFeaturetype($featuretype_name,$featuretype_title,$featuretype_srs,$this->wfs_describefeaturetype,$this->wfs_version); } } } function displayWFS(){ echo "id: " . $this->wfs_id . "
"; echo "version: " . $this->wfs_version . "
"; echo "name: " . $this->wfs_name . "
"; echo "title: " . $this->wfs_title . "
"; echo "abstract: " . $this->wfs_abstract . "
"; echo "capabilitiesrequest: " . $this->wfs_getcapabilities . "
"; echo "describefeaturetype: " . $this->wfs_describefeaturetype . "
"; echo "getfeature: " . $this->wfs_getfeature . "
"; echo "transaction: " . $this->wfs_transaction . "
"; for($i=0; $iwfs_featuretype); $i++){ echo "
"; echo "name: ". $this->wfs_featuretype[$i]->featuretype_name . "
"; echo "title: ". $this->wfs_featuretype[$i]->featuretype_title . "
"; echo "srs: ". $this->wfs_featuretype[$i]->featuretype_srs . "
"; for($j=0; $jwfs_featuretype[$i]->featuretype_element);$j++){ echo " element: " . $this->wfs_featuretype[$i]->featuretype_element[$j]["name"] ." - ".$this->wfs_featuretype[$i]->featuretype_element[$j]["type"]."
"; } for($j=0; $jwfs_featuretype[$i]->featuretype_namespace);$j++){ echo " namespace: " . $this->wfs_featuretype[$i]->featuretype_namespace[$j]["name"] ." - ".$this->wfs_featuretype[$i]->featuretype_namespace[$j]["value"]."
"; } } } function addFeaturetype($name,$title,$srs,$url,$version){ $this->wfs_featuretype[count($this->wfs_featuretype)] = new featuretype($name,$title,$srs,$url,$version); } function createJsObjFromWFS($parent){ if(!$this->wfs_title || $this->wfs_title == ""){ echo "alert('Error: no valid capabilities-document !!');"; die; exit; } if($parent){ echo "parent."; } print("add_wfs('". $this->wfs_id ."','". $this->wfs_version ."','". $this->wfs_title ."','". $this->wfs_abstract ."','". $this->wfs_getcapabilities ."','" . $this->wfs_describefeaturetype ."');"); for($i=0; $iwfs_featuretype); $i++){ if($parent){ echo "parent."; } print ("wfs_add_featuretype('". $this->wfs_featuretype[$i]->featuretype_name ."','". $this->wfs_featuretype[$i]->featuretype_title . "','". $this->wfs_featuretype[$i]->featuretype_srs ."','". $this->wfs_featuretype[$i]->featuretype_geomtype ."');"); for($j=0; $jwfs_featuretype[$i]->featuretype_element);$j++){ if($parent){ echo "parent."; } print("wfs_add_featuretype_element('".$this->wfs_featuretype[$i]->featuretype_element[$j]["name"]."', '".$this->wfs_featuretype[$i]->featuretype_element[$j]["type"]."', ".$j.", ".$i.");"); } for($j=0; $jwfs_featuretype[$i]->featuretype_namespace);$j++){ if($parent){ echo "parent."; } print("wfs_add_featuretype_namespace('".$this->wfs_featuretype[$i]->featuretype_namespace[$j]["name"]."', '".$this->wfs_featuretype[$i]->featuretype_namespace[$j]["value"]."', ".$j.", ".$i.");"); } } } /** * wfs2db * * this function exports the information from the xml to the mapbender database */ function wfs2db($gui_id){ global $DBSERVER,$DB,$OWNER,$PW; $con = db_connect($DBSERVER,$OWNER,$PW); db_select_db($DB,$con); # TABLE wfs $sql = "INSERT INTO wfs (wfs_version, wfs_name, wfs_title, wfs_abstract, wfs_getcapabilities, wfs_describefeaturetype, wfs_getfeature, wfs_transaction) "; $sql .= "VALUES("; $sql .= "'" . $this->wfs_version ."', "; $sql .= "'" . db_escape_string(str_replace("'","",$this->wfs_name)) ."', "; $sql .= "'" . db_escape_string(str_replace("'","",$this->wfs_title)) ."', "; $sql .= "'" . db_escape_string(str_replace("'","",$this->wfs_abstract)) . "', "; $sql .= "'" . $this->wfs_getcapabilities ."', "; $sql .= "'" . $this->wfs_describefeaturetype . "', "; $sql .= "'". $this->wfs_getfeature . "', "; $sql .= "'". $this->wfs_transaction . "'"; $sql .= ");"; #echo "sql wfs:
".$sql; $res = db_query($sql)or die(db_error()); $myWFS = db_insert_id($con,'wfs','wfs_id'); #echo "
myWFS: ".$myWFS; # TABLE wfs_featuretype for($i=0; $iwfs_featuretype); $i++){ $sql = "INSERT INTO wfs_featuretype(fkey_wfs_id, featuretype_name, featuretype_title, featuretype_srs) "; $sql .= "VALUES("; $sql .= $myWFS . ","; $sql .= "'".$this->wfs_featuretype[$i]->featuretype_name . "',"; $sql .= "'".$this->wfs_featuretype[$i]->featuretype_title."',"; $sql .= "'".$this->wfs_featuretype[$i]->featuretype_srs."'"; $sql .= ")"; #$res = mysql_query($sql) or $this->cleanDB($myWFS,$sql); $res = db_query($sql) or $this->cleanDB($myWFS,$sql); # save the id of each featuretype: $this->wfs_featuretype[$i]->mysql_id = db_insert_id($con,'wfs_featuretype','featuretype_id'); for($j=0; $jwfs_featuretype[$i]->featuretype_element);$j++){ $sql = "INSERT INTO wfs_element(fkey_featuretype_id, element_name,element_type) "; $sql .= "VALUES("; $sql .= "'" .$this->wfs_featuretype[$i]->mysql_id. "', "; $sql .= "'" .$this->wfs_featuretype[$i]->featuretype_element[$j]["name"]. "', "; $sql .= "'" .$this->wfs_featuretype[$i]->featuretype_element[$j]["type"]. "' "; $sql .= ")"; $res = db_query($sql) or $this->cleanDB($myWFS,$sql); } for($j=0; $jwfs_featuretype[$i]->featuretype_namespace);$j++){ $sql = "INSERT INTO wfs_featuretype_namespace(fkey_wfs_id, fkey_featuretype_id, namespace, namespace_location) "; $sql .= "VALUES("; $sql .= "'" .$myWFS. "',"; $sql .= "'" .$this->wfs_featuretype[$i]->mysql_id. "', "; $sql .= "'" .$this->wfs_featuretype[$i]->featuretype_namespace[$j]["name"]. "', "; $sql .= "'" .$this->wfs_featuretype[$i]->featuretype_namespace[$j]["value"]. "' "; $sql .= ")"; $res = db_query($sql) or $this->cleanDB($myWFS,$sql); } } # TABLE gui_wfs $sql ="INSERT INTO gui_wfs (fkey_gui_id, fkey_wfs_id)"; $sql .= "VALUES("; $sql .= "'" . $gui_id . "', "; $sql .= $myWFS; $sql .= ");"; $res = db_query($sql) or $this->cleanDB($myWFS,$sql); } function cleanDB($wfsid,$sql){ global $DBSERVER,$DB,$OWNER,$PW; $con = db_connect($DBSERVER,$OWNER,$PW); db_select_db($DB,$con); $s = "DELETE FROM wfs WHERE wfs_id = ".$wfsid; $res = db_query($s); echo "
Error in :".$sql."
"; echo "
Db cleaned.
"; die; } /** * creatObjfromDB * */ function createObjFromDB________($wfs_id){ global $server,$db,$owner,$pw; $con = mysql_connect($server,$owner,$pw); mysql_select_db($db, $con); $sql = "SELECT * FROM wfs WHERE wfs_id = ".$wfs_id.";"; $res = mysql_query($sql); $cnt = 0; while(mysql_fetch_row($res)){ $this->wfs_id = mysql_result($res, $cnt, "wfs_id"); $this->wfs_version = mysql_result($res, $cnt, "wfs_version"); $this->wfs_title = mysql_result($res, $cnt, "wfs_title"); $this->wfs_abstract = mysql_result($res, $cnt, "wfs_abstract"); $this->wfs_getcapabilities = mysql_result($res, $cnt, "wfs_getcapabilities"); $this->wfs_describefeaturetype = mysql_result($res, $cnt, "wfs_describefeaturetype"); $sql_fe = "SELECT * FROM wfs_featuretype WHERE fkey_wfs_id = " . $this->wfs_id . " ORDER BY featuretype_id"; $res_fe = mysql_query($sql_fe); $cnt_fe = 0; while(mysql_fetch_row($res_fe)){ $c = count($this->wfs_featuretype); $this->wfs_featuretype[$c]->featuretype_name = mysql_result($res_fe, $cnt_fe, "featuretype_name"); $this->wfs_featuretype[$c]->featuretype_title = mysql_result($res_fe, $cnt_fe, "featuretype_title"); $this->wfs_featuretype[$c]->featuretype_srs = mysql_result($res_fe, $cnt_fe, "featuretype_srs"); $this->wfs_featuretype[$c]->featuretype_geomtype = mysql_result($res_fe, $cnt_fe, "featuretype_geomtype"); $sql_el = "SELECT * FROM wfs_element WHERE fkey_featuretype_id = ". mysql_result($res_fe, $cnt_fe, "featuretype_id")." ORDER BY element_id"; $res_el = mysql_query($sql_el); $cnt_el = 0; while(mysql_fetch_row($res_el)){ $z = count($this->wfs_featuretype[$c]->featuretype_element); $this->wfs_featuretype[$c]->featuretype_element[$z]["name"] = mysql_result($res_el, $cnt_el, "element_name"); $this->wfs_featuretype[$c]->featuretype_element[$z]["type"] = mysql_result($res_el, $cnt_el, "element_type"); $cnt_el++; } $sql_el = "SELECT * FROM wfs_featuretype_namespace WHERE fkey_featuretype_id = ". mysql_result($res_fe, $cnt_fe, "featuretype_id")." ORDER BY namespace"; $res_el = mysql_query($sql_el); $cnt_el = 0; while(mysql_fetch_row($res_el)){ $z = count($this->wfs_featuretype[$c]->featuretype_namespace); $this->wfs_featuretype[$c]->featuretype_namespace[$z]["name"] = mysql_result($res_el, $cnt_el, "namespace"); $this->wfs_featuretype[$c]->featuretype_namespace[$z]["value"] = mysql_result($res_el, $cnt_el, "namespace_location"); $cnt_el++; } $cnt_fe++; } $cnt++; } } } /** end createObjfromDB **/ class featuretype extends wfs{ var $featuretype_element = array(); var $featuretype_namespace = array(); function featuretype($name,$title,$srs,$url,$version){ $url .= "&SERVICE=WFS&VERSION=".$version."&REQUEST=DescribeFeatureType&TYPENAME=".$name; $this->featuretype_name = $name; $this->featuretype_title = $title; $this->featuretype_srs = $srs; $data = implode("",file($url)); $parser = xml_parser_create(); xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0); xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1); xml_parse_into_struct($parser,$data,$values,$tags); xml_parser_free($parser); foreach ($values as $element) { if($this->sepNameSpace($element[tag]) == "schema" && $element[type] == "open"){ $section = "namespace"; echo "namespace
"; } if($section == "namespace"){ while (list($k, $val) = each ($element[attributes])) { if (substr($k, 0, 5) == "xmlns") { $cnt = count($this->featuretype_namespace); $match = false; for ($i = 0 ; $i < $cnt && $match == false ; $i++) { if ($this->sepNameSpace($k) == $this->featuretype_namespace[$i]["name"] && $val == $this->featuretype_namespace[$i]["value"]) { $match = true; } } if ($match == false) { $this->featuretype_namespace[$cnt]["name"] = $this->sepNameSpace($k); $this->featuretype_namespace[$cnt]["value"] = $val; echo "namespace: " . $this->sepNameSpace($k) . " -> " . $val . "
"; } } } } if($this->sepNameSpace($element[tag]) == "complexType" && $element[type] == "open"){ $section = ""; } if($this->sepNameSpace($element[tag]) == "complexContent" && $element[type] == "open"){ $section = "complexcontent"; echo "complexcontent
"; } if($section == "complexcontent" && $this->sepNameSpace($element[tag]) == "element" && $element[attributes][name]){ $cnt = count($this->featuretype_element); $this->featuretype_element[$cnt]["name"] = $element[attributes]["name"]; if (array_key_exists("type", $element[attributes])) { $this->featuretype_element[$cnt]["type"] = $this->sepNameSpace($element[attributes]["type"]); } echo "element: ".$this->featuretype_element[$cnt]["name"]."
"; } if($section == "complexcontent" && $this->sepNameSpace($element[tag]) == "restriction" && $element[attributes][base]){ $this->featuretype_element[$cnt]["type"] = $this->sepNameSpace($element[attributes]["base"]); } if($this->sepNameSpace($element[tag]) == "complexContent" && $element[type] == "close"){ $section = ""; } } } function sepNameSpace($s){ $c = strpos($s,":"); if($c>0){ return substr($s,$c+1); } else{ return $s; } } } ?>