function TranslateLayer($hSrcDS, $hSrcLayer, $hDstDS)
{
    /* -------------------------------------------------------------------- */
    /*      Create the layer.                                               */
    /* -------------------------------------------------------------------- */
    if (!OGR_DS_TestCapability($hDstDS, ODsCCreateLayer)) {
        printf("%s data source does not support layer creation.\n", OGR_DS_GetName($hDstDS));
        return OGRERR_FAILURE;
    }
    $hFDefn = OGR_L_GetLayerDefn($hSrcLayer);
    /* 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. */
    $hDstLayer = OGR_DS_GetLayer($hDstDS, 0);
    if ($hDstLayer == NULL) {
        return FALSE;
    }
    /* -------------------------------------------------------------------- */
    /*      Add fields.                                                     */
    /* -------------------------------------------------------------------- */
    for ($iField = 0; $iField < OGR_FD_GetFieldCount($hFDefn); $iField++) {
        if (OGR_L_CreateField($hDstLayer, OGR_FD_GetFieldDefn($hFDefn, $iField), 0) != OGRERR_NONE) {
            return FALSE;
        }
    }
    /* -------------------------------------------------------------------- */
    /*      Transfer features.                                              */
    /* -------------------------------------------------------------------- */
    OGR_L_ResetReading($hSrcLayer);
    while (($hFeature = OGR_L_GetNextFeature($hSrcLayer)) != NULL) {
        $hDstFeature = OGR_F_Create(OGR_L_GetLayerDefn($hDstLayer));
        if (OGR_F_SetFrom($hDstFeature, $hFeature, FALSE) != OGRERR_NONE) {
            OGR_F_Destroy($hFeature);
            printf("Unable to translate feature %d from layer %s.\n", OGR_F_GetFID($hFeature), OGR_FD_GetName($hFDefn));
            return FALSE;
        }
        OGR_F_Destroy($hFeature);
        if (OGR_L_CreateFeature($hDstLayer, $hDstFeature) != OGRERR_NONE) {
            OGR_F_Destroy($hDstFeature);
            return FALSE;
        }
        OGR_F_Destroy($hDstFeature);
    }
    return TRUE;
}
function TranslateLayer($hSrcDS, $hSrcLayer, $hDstDS, $amAttributeIn)
{
    /* -------------------------------------------------------------------- */
    /*      Create the layer.                                               */
    /* -------------------------------------------------------------------- */
    if (!OGR_DS_TestCapability($hDstDS, ODsCCreateLayer)) {
        printf("%s data source does not support layer creation.\n", OGR_DS_GetName($hDstDS));
        return OGRERR_FAILURE;
    }
    $hFDefn = OGR_L_GetLayerDefn($hSrcLayer);
    /* 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. */
    $hDstLayer = OGR_DS_GetLayer($hDstDS, 0);
    if ($hDstLayer == NULL) {
        return FALSE;
    }
    $iFieldCount = OGR_FD_GetFieldCount($hFDefn);
    printf("field count =%d\n", $iFieldCount);
    /* -------------------------------------------------------------------- */
    /*      Add fields.                                                     */
    /* -------------------------------------------------------------------- */
    for ($iField = 0; $iField < $iFieldCount; $iField++) {
        if (OGR_L_CreateField($hDstLayer, OGR_FD_GetFieldDefn($hFDefn, $iField), 0) != OGRERR_NONE) {
            return FALSE;
        }
    }
    /* -------------------------------------------------------------------- */
    /*      Transfer features.                                              */
    /* -------------------------------------------------------------------- */
    OGR_L_ResetReading($hSrcLayer);
    while (($hFeature = OGR_L_GetNextFeature($hSrcLayer)) != NULL) {
        $hDstFeature = OGR_F_Create(OGR_L_GetLayerDefn($hDstLayer));
        /* Verify if the current feature is corresponding to the feature
           to update the attribute to. If yes, the value corresponding to
           the attribute name selected is substituted. All other attributes
           and geometry are copied as is.  If not, the source feature
           is copied to the destination feature as is.*/
        if ($amAttributeIn && OGR_F_GetFID($hFeature) == $amAttributeIn[0]) {
            for ($i = 0; $i < $iFieldCount; $i++) {
                $hCurrentFieldDefn = OGR_F_GetFieldDefnRef($hFeature, $i);
                $strCurrentFieldName = OGR_FLD_GetNameRef($hCurrentFieldDefn);
                if ($strCurrentFieldName == $amAttributeIn[1]) {
                    $value = $amAttributeIn[2];
                } else {
                    $value = OGR_F_GetFieldAsString($hFeature, $i);
                }
                OGR_F_SetFieldString($hDstFeature, $i, $value);
            }
            $hGeometry = OGR_F_GetGeometryRef($hFeature);
            OGR_F_SetGeometry($hDstFeature, $hGeometry);
        } else {
            if (OGR_F_SetFrom($hDstFeature, $hFeature, FALSE) != OGRERR_NONE) {
                OGR_F_Destroy($hFeature);
                printf("Unable to translate feature %d from layer %s.\n", OGR_F_GetFID($hFeature), OGR_FD_GetName($hFDefn));
                return FALSE;
            }
        }
        OGR_F_Destroy($hFeature);
        if (OGR_L_CreateFeature($hDstLayer, $hDstFeature) != OGRERR_NONE) {
            OGR_F_Destroy($hDstFeature);
            return FALSE;
        }
        OGR_F_Destroy($hDstFeature);
    }
    return TRUE;
}
 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;
 }
function OGR_F_DumpReadable($hFeature)
{
    $hFeatureDefn = OGR_F_GetDefnRef($hFeature);
    printf("OGRFeature(%s):%ld\n", OGR_FD_GetName($hFeatureDefn), OGR_F_GetFID($hFeature));
    $numFields = OGR_FD_GetFieldCount($hFeatureDefn);
    for ($iField = 0; $iField < $numFields; $iField++) {
        $hFieldDefn = OGR_FD_GetFieldDefn($hFeatureDefn, $iField);
        printf("  %s (%s) = ", OGR_FLD_GetNameRef($hFieldDefn), OGR_GetFieldTypeName(OGR_FLD_GetType($hFieldDefn)));
        if (OGR_F_IsFieldSet($hFeature, $iField)) {
            printf("%s\n", OGR_F_GetFieldAsString($hFeature, $iField));
        } else {
            printf("(null)\n");
        }
    }
    if (OGR_F_GetStyleString($hFeature) != NULL) {
        printf("  Style = %s\n", OGR_F_GetStyleString($hFeature));
    }
    if (OGR_F_GetGeometryRef($hFeature) != NULL) {
        OGR_G_DumpReadable(OGR_F_GetGeometryRef($hFeature));
    }
    printf("\n");
}
function TranslateLayer($hSrcDS, $hSrcLayer, $hDstDS)
{
    /* -------------------------------------------------------------------- */
    /*      Create the layer.                                               */
    /* -------------------------------------------------------------------- */
    if (!OGR_DS_TestCapability($hDstDS, ODsCCreateLayer)) {
        printf("%s data source does not support layer creation.\n", OGR_DS_GetName($hDstDS));
        return OGRERR_FAILURE;
    }
    $hFDefn = OGR_L_GetLayerDefn($hSrcLayer);
    $hDstLayer = OGR_DS_CreateLayer($hDstDS, OGR_FD_GetName($hFDefn), OGR_L_GetSpatialRef($hSrcLayer), OGR_FD_GetGeomType($hFDefn), NULL);
    if ($hDstLayer == NULL) {
        return FALSE;
    }
    /* -------------------------------------------------------------------- */
    /*      Add fields.                                                     */
    /* -------------------------------------------------------------------- */
    for ($iField = 0; $iField < OGR_FD_GetFieldCount($hFDefn); $iField++) {
        if (OGR_L_CreateField($hDstLayer, OGR_FD_GetFieldDefn($hFDefn, $iField), 0) != OGRERR_NONE) {
            return FALSE;
        }
    }
    /* -------------------------------------------------------------------- */
    /*      Transfer features.                                              */
    /* -------------------------------------------------------------------- */
    OGR_L_ResetReading($hSrcLayer);
    while (($hFeature = OGR_L_GetNextFeature($hSrcLayer)) != NULL) {
        $hDstFeature = OGR_F_Create(OGR_L_GetLayerDefn($hDstLayer));
        if (OGR_F_SetFrom($hDstFeature, $hFeature, FALSE) != OGRERR_NONE) {
            OGR_F_Destroy($hFeature);
            printf("Unable to translate feature %d from layer %s.\n", OGR_F_GetFID($hFeature), OGR_FD_GetName($hFDefn));
            return FALSE;
        }
        OGR_F_Destroy($hFeature);
        if (OGR_L_CreateFeature($hDstLayer, $hDstFeature) != OGRERR_NONE) {
            OGR_F_Destroy($hDstFeature);
            return FALSE;
        }
        OGR_F_Destroy($hDstFeature);
    }
    return TRUE;
}
예제 #6
0
 /**
  *
  * processas as camadas do arquivo importado
  *
  * @param string $hSrcDS
  * @param array  $hSrcLayer
  * @param string $hDstDS
  */
 public function translateLayer($hSrcDS, $hSrcLayer, $hDstDS)
 {
     // cria layer
     if (!OGR_DS_TestCapability($hDstDS, ODsCCreateLayer)) {
         printf(STATIC::GEO_UNABLE_CREATE_LAYER, OGR_DS_GetName($hDstDS));
         return OGRERR_FAILURE;
     }
     $hFDefn = OGR_L_GetLayerDefn($hSrcLayer);
     $hDstLayer = OGR_DS_CreateLayer($hDstDS, OGR_FD_GetName($hFDefn), OGR_L_GetSpatialRef($hSrcLayer), OGR_FD_GetGeomType($hFDefn), NULL);
     if ($hDstLayer == NULL) {
         return FALSE;
     }
     // adiciona campos
     for ($iField = 0; $iField < OGR_FD_GetFieldCount($hFDefn); $iField++) {
         if (OGR_L_CreateField($hDstLayer, OGR_FD_GetFieldDefn($hFDefn, $iField), 0) != OGRERR_NONE) {
             return FALSE;
         }
     }
     // processa os features do layer
     OGR_L_ResetReading($hSrcLayer);
     while (($hFeature = OGR_L_GetNextFeature($hSrcLayer)) != NULL) {
         $hDstFeature = OGR_F_Create(OGR_L_GetLayerDefn($hDstLayer));
         if (OGR_F_SetFrom($hDstFeature, $hFeature, FALSE) != OGRERR_NONE) {
             OGR_F_Destroy($hFeature);
             printf(static::GEO_UNABLE_TRANSLATE_FEATURE_LAYER, OGR_F_GetFID($hFeature), OGR_FD_GetName($hFDefn));
             return FALSE;
         }
         OGR_F_Destroy($hFeature);
         if (OGR_L_CreateFeature($hDstLayer, $hDstFeature) != OGRERR_NONE) {
             OGR_F_Destroy($hDstFeature);
             return FALSE;
         }
         OGR_F_Destroy($hDstFeature);
     }
     return TRUE;
 }
예제 #7
0
파일: export.dxf.php 프로젝트: sukma279/GIS
 protected function insertSpecialFields($geoType, $dfnLayerDest, $dataLine)
 {
     // if "dataLine" contains point object
     if ($geoType == wkbPoint) {
         // we inform the name of representation
         for ($i = 0; $i < OGR_FD_GetFieldCount($dfnLayerDest); $i++) {
             $fieldDfn = OGR_FD_GetFieldDefn($dfnLayerDest, $i);
             if (OGR_Fld_GetNameRef($fieldDfn) == 'BlockName') {
                 OGR_F_SetFieldString($dataLine, $i, 'SQUARE');
             }
         }
     }
     return true;
 }
function ogr2tab_copy_srs()
{
    /*Corresponding MapInfo File driver number.*/
    $iDriver = 5;
    $strFormat = "MapInfo File";
    /*Path to existing data source to modify to fit user needs.*/
    $strSrcDataSource = "./phpunit_tests/data/tab/mpasabe200/Commune.TAB";
    /*Path to new data source to modify to fit user needs.*/
    $strDestDataSource = "../ogrtests/tmp/output";
    /*New layer name to modify to fit user needs.*/
    $strDestLayerName = "MyNewLayer";
    /*Layer selected to fetch a feature from.  If the data source name in
      $strSrcDataSource is a "tab" file, the layer number, specified by 
      $iLayer, to copy from must be set to zero.  Otherwise the layer 
      number might be any number between 0 and the layer count.*/
    //    $iLayer = 1;
    $iLayer = 0;
    /* -------------------------------------------------------------------- */
    /*      Register format(s).                                             */
    /* -------------------------------------------------------------------- */
    OGRRegisterAll();
    /* -------------------------------------------------------------------- */
    /*      Open the existing data source.                                  */
    /* -------------------------------------------------------------------- */
    $hSFDriver = NULL;
    $hSrcDS = OGROpen($strSrcDataSource, FALSE, $hSFDriver);
    /* -------------------------------------------------------------------- */
    /*      Report failure                                                  */
    /* -------------------------------------------------------------------- */
    if ($hSrcDS == NULL) {
        printf("FAILURE:\nUnable to open datasource `%s'\n", $strSrcDataSource);
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Find the output driver handle.                                  */
    /* -------------------------------------------------------------------- */
    if ($hSFDriver == NULL) {
        $hSFDriver = OGRGetDriver($iDriver);
    }
    if ($hSFDriver == NULL) {
        printf("Unable to find driver `%s'.\n", $strFormat);
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Create the destination data source.                             */
    /* -------------------------------------------------------------------- */
    $hDestDS = OGR_Dr_CreateDataSource($hSFDriver, $strDestDataSource, NULL);
    if ($hDestDS == NULL) {
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Select an existing layer from the existing data source to get   */
    /*      the projection information from.  To modify accordingly to user */
    /*      needs.                                                          */
    /* -------------------------------------------------------------------- */
    if (OGR_DS_GetLayerCount($hSrcDS) > 0) {
        $hSrcLayer = OGR_DS_GetLayer($hSrcDS, $iLayer);
        if ($hSrcLayer == NULL) {
            printf("FAILURE: Couldn't fetch advertised layer %d!\n", $iLayer);
            return OGRERR_FAILURE;
        }
        $hSrcSRS = OGR_L_GetSpatialRef($hSrcLayer);
    }
    /* -------------------------------------------------------------------- */
    /*      Create a new layer based on the existing data source            */
    /*      information.                                                    */
    /* -------------------------------------------------------------------- */
    $hSrcFDefn = OGR_L_GetLayerDefn($hSrcLayer);
    $eType = OGR_FD_GetGeomType($hSrcFDefn);
    $hDestLayer = OGR_DS_CreateLayer($hDestDS, $strDestLayerName, $hSrcSRS, $eType, NULL);
    if ($hDestLayer == NULL) {
        return FALSE;
    }
    /* -------------------------------------------------------------------- */
    /*      Create the field definitions for the new layer based on the     */
    /*      existing field definitions of the source layer.                 */
    /* -------------------------------------------------------------------- */
    for ($iField = 0; $iField < OGR_FD_GetFieldCount($hSrcFDefn); $iField++) {
        if (OGR_L_CreateField($hDestLayer, OGR_FD_GetFieldDefn($hSrcFDefn, $iField), 0) != OGRERR_NONE) {
            return OGRERR_FAILURE;
        }
    }
    /* -------------------------------------------------------------------- */
    /*      Get only one feature on the existing layer and copy it to the   */
    /*      new layer.                                                      */
    /* -------------------------------------------------------------------- */
    OGR_L_ResetReading($hSrcLayer);
    $hSrcFeature = OGR_L_GetNextFeature($hSrcLayer);
    $hDestFeature = OGR_F_Create(OGR_L_GetLayerDefn($hDestLayer));
    if (OGR_F_SetFrom($hDestFeature, $hSrcFeature, FALSE) != OGRERR_NONE) {
        OGR_F_Destroy($hDestFeature);
        printf("Unable to copy feature %d from layer %s.\n", OGR_F_GetFID($hSrcFeature), OGR_FD_GetName($hSrcFDefn));
        return OGRERR_FAILURE;
    }
    OGR_F_Destroy($hSrcFeature);
    if (OGR_L_CreateFeature($hDestLayer, $hDestFeature) != OGRERR_NONE) {
        OGR_F_Destroy($hDestFeature);
        return OGRERR_FAILURE;
    }
    OGR_F_Destroy($hDestFeature);
    /* -------------------------------------------------------------------- */
    /*      Close down.                                                     */
    /* -------------------------------------------------------------------- */
    OGR_DS_Destroy($hSrcDS);
    OGR_DS_Destroy($hDestDS);
    return OGRERR_NONE;
}
예제 #9
0
     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";
             }
         }
     }