Luzula Luzuloides 36.4 45.5 Spain MNCN Luzula Luzuloides var. lala 38.4 42.5 Spain MNCN */ function simple_xml_parser($xml,$datasetId) { //The function will return: // -number of records // -the SQL statement necessary to insert the data in PostGIS $sql=""; $featureCollection = simplexml_load_string($xml); //check if there are records in the document to process: $featureCount=count($featureCollection->feature); if ($featureCount) { //properties of the feature or columns in the temporary table $feature_properties=array(); $feature_properties[] = "fid"; //create temporary table //find the elements inside features $sql.= "CREATE TABLE $datasetId (fid int8 PRIMARY KEY,"; foreach($featureCollection->feature-> children() as $name => $node){ //If the element is the point we take care later if ($name!="point") { $sql.= "\"".$name."\" varchar(400), "; $feature_properties[] = $name; } } $sql.="created_when timestamp DEFAULT now()) WITHOUT OIDS;"; $sql.="\nALTER TABLE $datasetId OWNER TO postgres; \n"; //Add the geometry column to the table $sql.="SELECT AddGeometryColumn('','$datasetId','point',4326,'POINT',2);\n"; $feature_properties[] = "point"; foreach ($featureCollection->feature as $feature) { $data=$feature['fid'].","; foreach($feature_properties as $value) { if ($value!="point" && $value!="fid") { $data.="'".$feature->$value."',"; } } $data.="GeomFromText('POINT(".$feature->point.")',4326)"; $sql.="INSERT INTO $datasetId (\"".implode("\",\"", $feature_properties) ."\") VALUES ($data);\n"; } $sql.="create index \"fi_$datasetId\" on \"$datasetId\" using GIST (\"point\" GIST_GEOMETRY_OPS);\n"; $sql.="VACUUM ANALYZE \"$datasetId\";\n"; } else { $sql=""; } return array ($featureCount,$sql); } ?>