function get_dst_layer($form_submission_id, $geom_type) { OGRRegisterAll(); $dst_layer_name = ""; $dst_layer = NULL; $sql_str = "SELECT " . "tng_spatial_data.table_name, " . "tng_spatial_data.pk_col_name " . "FROM " . "tng_form_submission " . "INNER JOIN tng_form_spatial_data " . "ON tng_form_submission.form_id = tng_form_spatial_data.form_id " . "INNER JOIN tng_spatial_attribute_table " . "ON tng_form_spatial_data.attr_table_id = tng_spatial_attribute_table.attr_table_id " . "INNER JOIN tng_spatial_data " . "ON tng_spatial_attribute_table.spatial_table_id = tng_spatial_data.spatial_table_id " . "WHERE " . "tng_form_submission.form_submission_id = " . $form_submission_id . " " . "AND " . "POSITION(tng_spatial_data.geometry_type IN '" . $geom_type . "') > 0 "; // note: if geom_type is "multilinestring", then doing a straight // comparison (without the LIKE clause) will return no // results, because tng_spatial_data.geometry_type // is one of: polygon, linestring, point. $this->dbconn->connect(); $result = pg_query($this->dbconn->conn, $sql_str); if (!$result) { echo "An error occurred while executing the query " . pg_last_error($this->dbconn->conn); $this->dbconn->disconnect(); } else { $this->dst_layer_name = pg_fetch_result($result, 0, 0); $this->pk_col_name = pg_fetch_result($result, 0, 1); $this->dbconn->disconnect(); // now attempt to create an OGR layer // from the spatial table name // use the same connection string as the // one in the dbconn object $dst_driver = NULL; $this->ogr_dst_ds = OGROpen("PG:" . $this->dbconn->conn_str, FALSE, $dst_driver); if ($this->ogr_dst_ds != NULL) { $dst_layer = OGR_DS_GetLayerByName($this->ogr_dst_ds, $this->dst_layer_name); } } return $dst_layer; }
/** * Return layer definition string * reads either *.msl file with same base name as data file * or default polygon/line/point.lyr file from plugin dir * @return string $layerString */ protected function getLayerDefinition($connParams, $layerSpatialType) { $ogrDBConn = false; if ($connParams['type'] == "generic") { $ogrConnectionStr = $this->data_path_prefix . $connParams['layer']; //error_log($ogrConnectionStr); // $layerDefFile = dirname(__FILE__) . "/" . $connParams['layer'] . ".msl"; ## swap comment if layer definition file in plugin dir $layerDefFile = $ogrConnectionStr . ".msl"; } else { $ogrConnectionStr = "PG: " . $connParams['connection']; $layerDefFile = dirname(__FILE__) . "/layerdefinition/" . $connParams['schema'] . "." . $connParams['layer'] . ".msl"; //$layerDefFile = dirname(__FILE__) . "/layerdefinition/" . $connParams['layer'] . ".msl"; } if ($layerSpatialType == "vector") { if (!file_exists($ogrConnectionStr) && $connParams['type'] == "generic") { error_log($ogrConnectionStr . " does not exist."); return null; } // Register all drivers OGRRegisterAll(); // Open data source $hSFDriver = NULL; $hDatasource = OGROpen($ogrConnectionStr, 0, $hSFDriver); if (!$hDatasource) { error_log("Unable to open %s\n" . $ogrConnectionStr); return 0; } if ($connParams['type'] == "generic") { $hLayer = OGR_DS_GetLayer($hDatasource, 0); } else { $hLayer = OGR_DS_GetLayerByName($hDatasource, $connParams['schema'] . "." . $connParams['layer']); } /* Dump info about this layer */ $hLayerDefn = OGR_L_GetLayerDefn($hLayer); $hFeature = OGR_L_GetNextFeature($hLayer); if (OGR_F_GetGeometryRef($hFeature) != NULL) { $hGeom = OGR_F_GetGeometryRef($hFeature); //$geomType = OGR_G_GetGeometryType($hGeom); $geomNameOGR = OGR_G_GetGeometryName($hGeom); } /* Close data source */ OGR_DS_Destroy($hDatasource); /*$geomList[1] = "point"; $geomList[2] = "line"; $geomList[3] = "polygon"; $geomList[6] = "polygon";*/ $geomList["POINT"] = "point"; $geomList["MULTIPOINT"] = "point"; $geomList["LINESTRING"] = "line"; $geomList["MULTILINESTRING"] = "line"; $geomList["POLYGON"] = "polygon"; $geomList["MULTIPOLYGON"] = "polygon"; // $geomName = $geomList[$geomType]; $geomName = $geomList[$geomNameOGR]; } else { $geomName = "raster"; } if (is_file($layerDefFile)) { //if (is_file($layerDefFile = $layerFilename . ".msl")) { $layerString = file_get_contents($layerDefFile); } else { $layerPath = dirname(__FILE__) . "/layerdefinition/{$geomName}.lyr"; $layerString = file_get_contents($layerPath); } //error_log("layerDefFile: $layerDefFile"); return $layerString; }
$sql_str = "SELECT " . "tng_spatial_data.table_name, " . "tng_spatial_attribute_table.attr_table_name " . "FROM " . "tng_form_spatial_data " . "INNER JOIN tng_spatial_data ON tng_form_spatial_data.spatial_table_id = tng_spatial_data.spatial_table_id " . "INNER JOIN tng_spatial_attribute_table on tng_form_spatial_data.spatial_table_id = tng_spatial_attribute_table.spatial_table_id " . "WHERE " . "tng_form_spatial_data.form_id = 1 " . "AND " . "tng_spatial_data.geometry_type = '" . $geom_type . "'"; $dbconn = new DBConn(); $dbconn->connect(); $result = pg_query($dbconn->conn, $sql_str); if (!$result) { echo "An error occurred while executing the query - class_form.php:47 " . pg_last_error($dbconn->conn); $dbconn->disconnect(); } else { $dst_layer_name = pg_fetch_result($result, 0, 0); $dst_attr_table_name = pg_fetch_result($result, 0, 1); $dbconn->disconnect(); } /* end find geometry type */ /*---------------------------------------------------------------------------------------*/ /* get the handle for the destination layer */ $dst_layer = OGR_DS_GetLayerByName($dst_ds, $dst_layer_name); if ($dst_layer == NULL) { echo "could not create a handle for " . $dst_layer_name; exit; } echo "<br> dst layer : " . $dst_layer_name . "<br>"; OGR_L_ResetReading($src_layer); //for( $i = 0; $i < OGR_FD_GetFieldCount($feature_defn); $i++ ) while (($feature = OGR_L_GetNextFeature($src_layer)) != NULL) { // create a blank feature in the // destination layer. $dst_feature_defn = OGR_L_GetLayerDefn($dst_layer); $dst_feature = OGR_F_Create($dst_feature_defn); // now copy the feature from the source // layer to the dest. layer // the last value, bForgiving must be set to