function OGRSetGetInteger($hFeatureIn, $iField, $value) { /*Assigning an integer value to the field to the field "0".*/ OGR_F_SetFieldInteger($hFeatureIn, $iField, $value); /*Retrieving the field value. */ $value = OGR_F_GetFieldAsInteger($hFeatureIn, $iField); printf("Integer value = %d\n", $value); return OGRERR_NONE; }
function OGRCCreate($strFname) { /* Register all OGR drivers */ OGRRegisterAll(); /* Fetch MITAB driver - we want to create a TAB file */ $numDrivers = OGRGetDriverCount(); printf("nombre drivers = %d\n", $numDrivers); for ($i = 0; $i < $numDrivers; $i++) { $hdriver = OGRGetDriver($i); if (OGR_Dr_GetName($hdriver) == "MapInfo File") { printf("Driver name = %s driver number = %d\n", OGR_Dr_GetName($hdriver), $i); break; /* Found it! */ } $hdriver = NULL; } if (!$hdriver) { printf("Driver not found!\n"); return -1; } /* Create new file using this driver */ /*Uncomment and add options here. */ /* $aoptions[0] = 'option1'; $aoptions[1] = 'option2'; $hdatasource = OGR_Dr_CreateDataSource($hdriver, $strFname, $aoptions); */ /* Or use no option.*/ $hdatasource = OGR_Dr_CreateDataSource($hdriver, $strFname, NULL); if ($hdatasource == NULL) { printf("Unable to create %s\n", $strFname); return -1; } /* MapInfo data sources are created with one empty layer corresponding to the $strFname that was passed to the OGR_Dr_CreateDataSource() call. Fetch this layer handle now. */ $hlayer = OGR_DS_GetLayer($hdatasource, 0); if ($hlayer == NULL) { printf("Unable to create new layer in %s\n", $strFname); return -1; } /* Add a few fields to the layer defn */ $hfieldDefn = OGR_Fld_Create("id", OFTInteger); $eErr = OGR_L_CreateField($hlayer, $hfieldDefn, 0); if ($eErr != OGRERR_NONE) { return $eErr; } $hfieldDefn = OGR_Fld_Create("area", OFTReal); $eErr = OGR_L_CreateField($hlayer, $hfieldDefn, 0); if ($eErr != OGRERR_NONE) { return $eErr; } $hfieldDefn = OGR_Fld_Create("name", OFTString); $eErr = OGR_L_CreateField($hlayer, $hfieldDefn, 0); if ($eErr != OGRERR_NONE) { return $eErr; } /* We'll need the layerDefn handle to create new features in this layer */ $hlayerDefn = OGR_L_GetLayerDefn($hlayer); /* Create a new point */ $hfeature = OGR_F_Create($hlayerDefn); OGR_F_SetFieldInteger($hfeature, 0, 1); OGR_F_SetFieldDouble($hfeature, 1, 123.45); OGR_F_SetFieldString($hfeature, 2, "Feature #1"); $hgeometry = OGR_G_CreateGeometry(wkbPoint); OGR_G_SetPoint($hgeometry, 0, 123.45, 456.78, 0); $eErr = OGR_F_SetGeometry($hfeature, $hgeometry); if ($eErr != OGRERR_NONE) { return $eErr; } $eErr = OGR_L_CreateFeature($hlayer, $hfeature); if ($eErr != OGRERR_NONE) { printf("Error trapped"); return $eErr; } /* Create a new line */ $hfeature = OGR_F_Create($hlayerDefn); OGR_F_SetFieldInteger($hfeature, 0, 2); OGR_F_SetFieldDouble($hfeature, 1, 42.45); OGR_F_SetFieldString($hfeature, 2, "Feature #2"); $hgeometry = OGR_G_CreateGeometry(wkbLineString); OGR_G_AddPoint($hgeometry, 123.45, 456.78, 0); OGR_G_AddPoint($hgeometry, 12.34, 45.67, 0); $eErr = OGR_F_SetGeometry($hfeature, $hgeometry); if ($eErr != OGRERR_NONE) { return $eErr; } $eErr = OGR_L_CreateFeature($hlayer, $hfeature); if ($eErr != OGRERR_NONE) { return $eErr; } /* Create a new polygon (square) */ $hfeature = OGR_F_Create($hlayerDefn); OGR_F_SetFieldInteger($hfeature, 0, 3); OGR_F_SetFieldDouble($hfeature, 1, 49.71); OGR_F_SetFieldString($hfeature, 2, "Feature #3"); $hgeometry = OGR_G_CreateGeometry(wkbPolygon); $hring = OGR_G_CreateGeometry(wkbLinearRing); OGR_G_AddPoint($hring, 123.45, 456.78, 0); OGR_G_AddPoint($hring, 12.34, 456.78, 0); OGR_G_AddPoint($hring, 12.34, 45.67, 0); OGR_G_AddPoint($hring, 123.45, 45.67, 0); OGR_G_AddPoint($hring, 123.45, 456.78, 0); $eErr = OGR_G_AddGeometry($hgeometry, $hring); if ($eErr != OGRERR_NONE) { return $eErr; } $eErr = OGR_F_SetGeometry($hfeature, $hgeometry); if ($eErr != OGRERR_NONE) { return $eErr; } $eErr = OGR_L_CreateFeature($hlayer, $hfeature); if ($eErr != OGRERR_NONE) { return $eErr; } /* Close data source */ OGR_DS_Destroy($hdatasource); return OGRERR_NONE; }
function add_layer_to_db($dst_layer_name) { OGRRegisterAll(); $success = false; // create layer record so that all // these geometries can be linked to // one layer $layer_id = $this->create_layer_record($this->submission_id, $dst_layer_name); if ($layer_id != -1) { // loop through source layer, copy // each feature to the destination // layer OGR_L_ResetReading($this->ogr_src_layer); $dst_feature_defn = OGR_L_GetLayerDefn($this->ogr_dst_layer); while (($src_feature = OGR_L_GetNextFeature($this->ogr_src_layer)) != NULL) { // create a blank feature // based on the destination // feature definition $dst_feature = OGR_F_Create($dst_feature_defn); // now copy the feature object // from the source to the destination. // the last value, bForgiving must be set to // true because our source and destination // schemas dont match. if bForgiving is // false, OGR_F_SetFrom quits when it // finds a feature that doesnt match both // schemas. if (OGR_F_SetFrom($dst_feature, $src_feature, TRUE) != OGRERR_NONE) { echo "could not set destination feature from source feature"; return false; } // set the layer_id (fk) of the feature // to the layer id created earlier in // this method $layerid_f_index = OGR_F_GetFieldIndex($dst_feature, "layer_id"); OGR_F_SetFieldInteger($dst_feature, $layerid_f_index, $layer_id); // ------------------------------------------ // begin locked section // ------------------------------------------ // since the feature creation does not automatically // query the ID of the feature from the db, we // need to query it ourselves. to prevent any // interleaving of threads that are executing this // method, we add a semaphore lock before the // feature is created and release the lock once // the ID of the feature has been queried. // see php docs for sem_get(), sem_acquire() // and sem_release // note the second arg to sem_get must be "1" // so that only one process at a time can // acquire the semaphore $sem_num = sem_get(5001, 1); // block until semaphore is available sem_acquire($sem_num); // destination feature successfully set. // now "create" the dst feature within the // destination layer. if (OGR_L_CreateFeature($this->ogr_dst_layer, $dst_feature) != OGRERR_NONE) { echo "could not create feature in destination layer "; return false; } // now that the feature has been // created within the db, we need to // get its ID so that we can use it // as a FK when inserting the attributes // of the feature. $pk_col_name = ""; /* unused. see comments in get_max_id() */ $feature_id = $this->get_max_id($this->dst_layer_name, $pk_col_name); // release the semaphore sem_release($sem_num); // ------------------------------------------ // end locked section // ------------------------------------------ if ($feature_id == -1) { echo "could not obtain new ID for feature "; return false; } // if the value of the ID was successfully // obtained, attempt to create // records for the attributes in the attribute // table if (!$this->add_attributes_to_db($src_feature, $feature_id, $this->pk_col_name)) { echo "could not insert attributes for feature ID " . $feature_id; return false; } OGR_F_Destroy($src_feature); OGR_F_Destroy($dst_feature); } } return true; }
// now copy the feature from the source // layer to the dest. layer // the last value, bForgiving must be set to // true because our source and destination // schemas dont match if (OGR_F_SetFrom($dst_feature, $feature, TRUE) != OGRERR_NONE) { echo "could not set destination feature from source feature"; exit; } // otherwise the feature was set // from the source feature. // set the layer_id (fk) of the feature // to the layer id created at the time // of submission. $layerid_f_index = OGR_F_GetFieldIndex($dst_feature, "layer_id"); OGR_F_SetFieldInteger($dst_feature, $layerid_f_index, -1); // now "create" this feature within the // destination layer. the method should // be called "add" rather than "create" if (OGR_L_CreateFeature($dst_layer, $dst_feature) != OGRERR_NONE) { echo "could not create feature in destination layer "; exit; } //$geometry = OGR_F_GetGeometryRef($feature); //$txt_buffer = ""; //$res = OGR_G_ExportToWkt($geometry, $txt_buffer); //$res = OGR_G_GetGeometryName($geometry); //OGR_G_ExportToWkb($geometry, $txt_buffer); //OGR_G_ExportToWkb //$len = OGR_G_WkbSize($geometry); echo "feature: " . $txt_buffer . ", " . $res . "<br> ----- [fields] : ";