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 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; }
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; }
/** * * 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; }