$hfieldDefn = OGR_Fld_Create("Address", OFTInteger); OGR_FD_AddFieldDefn($hFeatureDefn, $hfieldDefn); $hfieldDefn = OGR_Fld_Create("Total earning", OFTReal); OGR_FD_AddFieldDefn($hFeatureDefn, $hfieldDefn); $hfieldDefn = OGR_Fld_Create("Street", OFTString); OGR_FD_AddFieldDefn($hFeatureDefn, $hfieldDefn); $hfieldDefn = OGR_Fld_Create("Age", OFTIntegerList); OGR_FD_AddFieldDefn($hFeatureDefn, $hfieldDefn); $hfieldDefn = OGR_Fld_Create("Individual earning", OFTRealList); OGR_FD_AddFieldDefn($hFeatureDefn, $hfieldDefn); $hfieldDefn = OGR_Fld_Create("First-Last Names", OFTStringList); OGR_FD_AddFieldDefn($hFeatureDefn, $hfieldDefn); /*Creating a new feature with the previous definition */ $hFeature = OGR_F_Create($hFeatureDefn); /*Testing if the field is created. */ $numFields = OGR_F_GetFieldCount($hFeature); printf("nombre de field = %d\n", $numFields); $fieldIndex = OGR_F_GetFieldIndex($hFeature, "First-Last Names"); printf("field index = %d\n", $fieldIndex); $fieldIndex = 0; $eErr = OGRSetGetInteger($hFeature, $fieldIndex, $intValue); $fieldIndex = 1; $eErr = OGRSetGetDouble($hFeature, $fieldIndex, $doubleValue); $fieldIndex = 2; $eErr = OGRSetGetString($hFeature, $fieldIndex, $stringValue); $fieldIndex = 3; $eErr = OGRSetGetIntegerList($hFeature, $fieldIndex, $numInteger, $intListValue); $fieldIndex = 4; $eErr = OGRSetGetDoubleList($hFeature, $fieldIndex, $numDouble, $doubleListValue); $fieldIndex = 5; $eErr = OGRSetGetStringList($hFeature, $fieldIndex, $stringListValue);
function add_attributes_to_db($src_feature, $feature_id, $fk_col_name) { $success = false; $num_fields = OGR_F_GetFieldCount($src_feature); $sql_str = "INSERT INTO " . $this->attr_table_name . "(" . $fk_col_name . ", "; // append all field names to the // sql string first for ($i = 0; $i < $num_fields; $i++) { $field_defn = OGR_FD_GetFieldDefn(OGR_L_GetLayerDefn($this->ogr_src_layer), $i); $f_name = strtolower(OGR_Fld_GetNameRef($field_defn)); $sql_str .= $f_name; if ($i < $num_fields - 1) { $sql_str .= ", "; } } $sql_str .= ") VALUES ( " . $feature_id . ", "; // append field values for ($i = 0; $i < $num_fields; $i++) { $field_defn = OGR_FD_GetFieldDefn(OGR_L_GetLayerDefn($this->ogr_src_layer), $i); $f_name = strtolower(OGR_Fld_GetNameRef($field_defn)); $f_type = OGR_Fld_GetType($field_defn); $f_value = OGR_F_GetFieldAsString($src_feature, $i); // surround the value with quotes if // the type is string if ($f_type == OFTString) { $sql_str .= "'" . str_replace("'", "''", $f_value) . "'"; } else { if ($f_value == "") { $sql_str .= "NULL"; } else { $sql_str .= $f_value; } } if ($i < $num_fields - 1) { $sql_str .= ", "; } } $sql_str .= ")"; $this->dbconn->connect(); $result = pg_query($this->dbconn->conn, $sql_str); if (!$result) { echo "An error occurred while executing the query: " . $sql_str . pg_last_error($this->dbconn->conn) . "\n\n"; } else { $success = true; } $this->dbconn->disconnect(); return $success; }
// 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] : "; //$field_defn = OGR_FD_GetFieldDefn( $feature, $i); $num_fields = OGR_F_GetFieldCount($dst_feature); for ($i = 0; $i < $num_fields; $i++) { //$field_defn = OGR_FD_GetFieldDefn( $feature_defn, $i); $field_defn = OGR_FD_GetFieldDefn($dst_feature_defn, $i); $f_name = strtolower(OGR_Fld_GetNameRef($field_defn)); $f_type = OGR_Fld_GetType($field_defn); if ($f_type == OFTString) { $f_type = "string"; } else { if ($f_type == OFTInteger) { $f_type = "int"; } else { if ($f_type == OFTReal) { $f_type = "double"; } else { $f_type = "unknown";