Example #1
0
 protected function createNewLayer($map, $driver, $layerName, $geoType, $bSearchSpatialRef)
 {
     // if layer contains point objects
     if ($geoType == wkbPoint) {
         // before creating layer "entities" we create layer "blocks"
         $layerBlocks = OGR_DS_CreateLayer($driver, 'blocks', NULL, wkbMultiLineString, NULL);
         // in this layer we export representaion of all point objects (it's a square)
         $wktLine = 'MULTILINESTRING((-0.5 -0.5, 0.5 -0.5, 0.5 0.5, -0.5 0.5, -0.5 -0.5))';
         $geoOGR = ogr_g_createfromwkt($wktLine, NULL, NULL);
         $dfnLayerBlocks = OGR_L_GetLayerDefn($layerBlocks);
         $dataLine = OGR_F_Create($dfnLayerBlocks);
         OGR_F_SetGeometry($dataLine, $geoOGR);
         for ($i = 0; $i < OGR_FD_GetFieldCount($dfnLayerBlocks); $i++) {
             $fieldDfn = OGR_FD_GetFieldDefn($dfnLayerBlocks, $i);
             if (OGR_Fld_GetNameRef($fieldDfn) == 'BlockName') {
                 OGR_F_SetFieldString($dataLine, $i, 'SQUARE');
             }
         }
         OGR_L_CreateFeature($layerBlocks, $dataLine);
         OGR_F_Destroy($dataLine);
         OGR_G_DestroyGeometry($geoOGR);
     }
     return parent::createNewLayer($map, $driver, 'entities', $geoType, false);
 }
Example #2
0
 function __construct($driverName, $exportExt, $json, $map, $bExportAtt = false, $optDataSourceCreation = NULL, $bSearchSpatialRef = false)
 {
     $bOk = false;
     $msVersion = null;
     // MapServer version
     $groups = null;
     // Layers list that will be exported
     parent::__construct($json);
     OGRRegisterAll();
     // looking for destination driver
     for ($iDriver = 0; $iDriver < OGRGetDriverCount() && !$bOk; $iDriver++) {
         if (!strcasecmp(OGR_DR_GetName(OGRGetDriver($iDriver)), $driverName)) {
             $this->driverOGRDest = OGRGetDriver($iDriver);
             $bOk = true;
             break;
         }
     }
     if ($bOk) {
         // is it possible to create new data source with current driver
         if (!OGR_Dr_TestCapability($this->driverOGRDest, ODrCCreateDataSource)) {
             $this->driverOGRDest = null;
             $bOk = false;
             return;
         }
     }
     if ($bOk) {
         $msVersion = $_SESSION['MS_VERSION'];
         $groups = (array) $this->jsonList[0];
         // Create the destination directory
         @mkdir($this->tempFilePath, 0700);
         // for each layers that will be exported
         foreach ($groups as $grp) {
             $baseDest = null;
             $layerList = array();
             $attNameList = $grp->header;
             $ObjList = $grp->values;
             if (!$ObjList[0][0]->shplink) {
                 continue;
             }
             // if we need to export attributs
             if ($bExportAtt) {
                 include_once '../common/pluginsMapUtils.inc.php';
                 for ($iAtt = 0; $iAtt < count($attNameList); $iAtt++) {
                     if ($attNameList[$iAtt] == '@') {
                         array_splice($attNameList, $iAtt, 1);
                         $iAtt--;
                         continue;
                     }
                     // remove accents and spaces:
                     $newAttName = PluginsMapUtils::decodeMapFileVal($map, $attNameList[$iAtt], 'UTF-8');
                     $newAttName = str_replace($this->bad, $this->good, $attNameList[$iAtt]);
                     if (is_numeric($newAttName[0])) {
                         $newAttName = 'z' . $newAttName;
                     }
                     $attNameList[$iAtt] = $newAttName;
                 }
             }
             // for each objects that we want to export
             foreach ($ObjList as $Obj) {
                 $layerSrc = null;
                 // Source layer
                 $layerDest = null;
                 // Destination layer
                 $baseDest = null;
                 // Destination DataBase
                 $geoType = 0;
                 $shpLink = $Obj[0]->shplink;
                 $layerName = $shpLink[0];
                 $IdObj = $shpLink[1];
                 // if it's the first time we found current layer
                 if (!isset($layerList[$layerName])) {
                     // getting mapserver layer
                     $layerSrc = $map->getLayerByName($layerName);
                     // use layers with complex queries that are too long to select results
                     $newdata = $layerSrc->getMetaData('PM_RESULT_DATASUBSTITION');
                     if ($newdata != '') {
                         $layerSrc->set('data', $newdata);
                     }
                     // create destination data base
                     $output = $this->tempFilePath . "\\{$layerName}.{$exportExt}";
                     $output = str_replace('/', '\\', $output);
                     $baseDest = OGR_Dr_CreateDataSource($this->driverOGRDest, $output, $optDataSourceCreation);
                     if (!$baseDest) {
                         continue;
                     }
                     // is it possible to create new layers in current data source ?
                     if (!OGR_DS_TestCapability($baseDest, ODsCCreateLayer)) {
                         if ($baseDest) {
                             OGR_DS_Destroy($baseDest);
                         }
                         continue;
                     }
                     // create new layer in the destination data base
                     $geoType = $this->typeList[$layerSrc->type];
                     $layerDest = $this->createNewLayer($map, $baseDest, $layerName, $geoType, $bSearchSpatialRef);
                     // add new attribut in destination layer
                     if ($bExportAtt) {
                         $idAtt = 0;
                         foreach ($attNameList as $attName) {
                             $att = OGR_Fld_Create($attName, OFTString);
                             OGR_L_CreateField($layerDest, $att, $idAtt);
                             $idAtt++;
                         }
                     }
                     // saving all in layerList
                     $layerList[$layerName] = array();
                     $layerList[$layerName][] = $layerSrc;
                     $layerList[$layerName][] = $baseDest;
                     $layerList[$layerName][] = $layerDest;
                     $layerList[$layerName][] = $geoType;
                     // add file to files list will be exorted
                     $this->addFileToFileList($output);
                 } else {
                     $layerSrc = $layerList[$layerName][0];
                     $baseDest = $layerList[$layerName][1];
                     $layerDest = $layerList[$layerName][2];
                     $geoType = $layerList[$layerName][3];
                 }
                 // gettint shape object
                 $srcShp = PMCommon::resultGetShape($msVersion, $layerSrc, null, $IdObj, -1);
                 // export geometry of the object in WKT
                 $geoWKT = $srcShp->toWkt();
                 // create new geometry OGR object from WKT geometry
                 $geoOGR = $this->createNewOGRGeometry($geoWKT);
                 // create new data line on destination layer
                 $layerDestDefn = OGR_L_GetLayerDefn($layerDest);
                 $Data = OGR_F_Create($layerDestDefn);
                 // add geometry in data line
                 OGR_F_SetGeometry($Data, $geoOGR);
                 // if we need to export attributs
                 if ($bExportAtt) {
                     // add attributs values in data line
                     for ($iAtt = 1; $iAtt < count($Obj); $iAtt++) {
                         $newAttVal = PluginsMapUtils::decodeLayerVal($map, $layerName, $Obj[$iAtt], 'UTF-8');
                         OGR_F_SetFieldString($Data, $iAtt - 1 + $this->nbFieldBase, $newAttVal);
                     }
                 }
                 $this->insertSpecialFields($geoType, $layerDestDefn, $Data);
                 // add data line in destination layer
                 OGR_L_CreateFeature($layerDest, $Data);
                 OGR_F_Destroy($Data);
                 OGR_G_DestroyGeometry($geoOGR);
             }
             foreach ($layerList as $l) {
                 if (isset($l[1]) && $l[1]) {
                     OGR_DS_Destroy($l[1]);
                 }
             }
         }
         // files compression
         $this->zipFiles();
         // remove directory
         rmdir($this->tempFilePath);
     }
 }
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 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 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 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;
 }
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;
}
Example #8
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;
 }
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;
}
 // 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] : ";
 //$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);