function OGR2Tab_Update_main()
{
    $strFormat = "MapInfo File";
    $strDataSource = NULL;
    $strDestDataSource = NULL;
    $amAttribute = NULL;
    /* -------------------------------------------------------------------- */
    /*      Register format(s).                                             */
    /* -------------------------------------------------------------------- */
    OGRRegisterAll();
    /* -------------------------------------------------------------------- */
    /*      Processing command line arguments.                              */
    /* -------------------------------------------------------------------- */
    $numArgs = count($_SERVER["argv"]);
    for ($iArg = 1; $iArg < $numArgs; $iArg++) {
        /* Get the feature id and attribute name to update to the new attribute
           value.*/
        if (!strcasecmp($_SERVER["argv"][$iArg], "-fid") && $iArg < $numArgs - 1) {
            $amAttribute[0] = $_SERVER["argv"][++$iArg];
            $amAttribute[1] = $_SERVER["argv"][++$iArg];
            $amAttribute[2] = $_SERVER["argv"][++$iArg];
            printf("FID = %d\n", $amAttribute[0]);
            printf("Attribute name  = %s\n", $amAttribute[1]);
            printf("Attribute value = %d\n", $amAttribute[2]);
        } else {
            if ($_SERVER["argv"][$iArg][0] == '-') {
                Usage();
            } else {
                if ($strDestDataSource == NULL) {
                    $strDestDataSource = $_SERVER["argv"][$iArg];
                    printf("DestDataSource = %s\n", $strDestDataSource);
                } else {
                    if ($strDataSource == NULL) {
                        $strDataSource = $_SERVER["argv"][$iArg];
                        printf("DataSource = %s\n", $strDataSource);
                    }
                }
            }
        }
    }
    if ($strDataSource == NULL) {
        Usage();
    }
    /* -------------------------------------------------------------------- */
    /*      Open data source.                                               */
    /* -------------------------------------------------------------------- */
    $hSFDriver = NULL;
    $hDS = OGROpen($strDataSource, FALSE, $hSFDriver);
    /* -------------------------------------------------------------------- */
    /*      Report failure                                                  */
    /* -------------------------------------------------------------------- */
    if ($hDS == NULL) {
        printf("FAILURE:\nUnable to open datasource `%s' with the following drivers:\n", $strDataSource);
        for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
            printf("  -> %s\n", OGR_DR_GetName(OGRGetDriver($iDriver)));
        }
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Find the output driver.                                         */
    /* -------------------------------------------------------------------- */
    for ($iDriver = 0; $iDriver < OGRGetDriverCount() && $hSFDriver == NULL; $iDriver++) {
        if (!strcasecmp(OGR_DR_GetName(OGRGetDriver($iDriver)), $strFormat)) {
            $hSFDriver = OGRGetDriver($iDriver);
        }
    }
    if ($hSFDriver == NULL) {
        printf("Unable to find driver `%s'.\n", $strFormat);
        printf("The following drivers are available:\n");
        for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
            printf("  -> %s\n", OGR_DR_GetName(OGRGetDriver($iDriver)));
        }
        return OGRERR_FAILURE;
    }
    if (!OGR_Dr_TestCapability($hSFDriver, ODrCCreateDataSource)) {
        printf("%s driver does not support data source creation.\n", $strFormat);
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Create the output data source.                                  */
    /* -------------------------------------------------------------------- */
    /*Uncomment and add options here. */
    /* $aoptions[0] = 'option1';
        $aoptions[1] = 'option2';
        $hODS = OGR_Dr_CreateDataSource( $hSFDriver, $strDestDataSource, $aoptions );
    */
    /* Or use no option.*/
    $hODS = OGR_Dr_CreateDataSource($hSFDriver, $strDestDataSource, NULL);
    if ($hODS == NULL) {
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Process only first layer in source dataset                      */
    /* -------------------------------------------------------------------- */
    if (OGR_DS_GetLayerCount($hDS) > 0) {
        $hLayer = OGR_DS_GetLayer($hDS, 0);
        if ($hLayer == NULL) {
            printf("FAILURE: Couldn't fetch advertised layer 0!\n");
            return OGRERR_FAILURE;
        }
        if (!TranslateLayer($hDS, $hLayer, $hODS, $amAttribute)) {
            return OGRERR_FAILURE;
        }
    }
    /* -------------------------------------------------------------------- */
    /*      Close down.                                                     */
    /* -------------------------------------------------------------------- */
    OGR_DS_Destroy($hDS);
    OGR_DS_Destroy($hODS);
    return OGRERR_NONE;
}
function OGR2Tab_SQL_main()
{
    /*Assigning initial value.*/
    $strFormat = "MapInfo File";
    $strDataSource = NULL;
    $strDestDataSource = NULL;
    $strWhere = NULL;
    $hSpatialFilter = NULL;
    $strSQLStatement = NULL;
    $strDialect = NULL;
    /* -------------------------------------------------------------------- */
    /*      Register format(s).                                             */
    /* -------------------------------------------------------------------- */
    OGRRegisterAll();
    /* -------------------------------------------------------------------- */
    /*      Processing command line arguments.                              */
    /* -------------------------------------------------------------------- */
    $numArgs = count($_SERVER["argv"]);
    for ($iArg = 1; $iArg < $numArgs; $iArg++) {
        /* Construct a spatial filter.*/
        if ($_SERVER["argv"][$iArg] == "-spat" && $_SERVER["argv"][$iArg + 1] != NULL && $_SERVER["argv"][$iArg + 2] != NULL && $_SERVER["argv"][$iArg + 3] != NULL && $_SERVER["argv"][$iArg + 4] != NULL) {
            $hSpatialFilter = OGR_G_CreateGeometry(wkbLinearRing);
            OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 1]), floatval($_SERVER["argv"][$iArg + 2]), 0.0);
            OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 1]), floatval($_SERVER["argv"][$iArg + 4]), 0.0);
            OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 3]), floatval($_SERVER["argv"][$iArg + 4]), 0.0);
            OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 3]), floatval($_SERVER["argv"][$iArg + 2]), 0.0);
            OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 1]), floatval($_SERVER["argv"][$iArg + 2]), 0.0);
            printf("Xmin = %s Ymin = %s Xmax = %s Ymax = %s\n", $_SERVER["argv"][$iArg + 1], $_SERVER["argv"][$iArg + 2], $_SERVER["argv"][$iArg + 3], $_SERVER["argv"][$iArg + 4]);
            $iArg += 4;
        } else {
            if ($_SERVER["argv"][$iArg] == "-sql" && $_SERVER["argv"][$iArg + 1] != NULL) {
                $strSQLStatement = $_SERVER["argv"][++$iArg];
                printf("SQLStatement = %s\n", $strSQLStatement);
            } else {
                if ($strDestDataSource == NULL) {
                    $strDestDataSource = $_SERVER["argv"][$iArg];
                    printf("DestDataSource = %s\n", $strDestDataSource);
                } else {
                    if ($strDataSource == NULL) {
                        $strDataSource = $_SERVER["argv"][$iArg];
                        printf("DataSource = %s\n", $strDataSource);
                    } else {
                        if ($_SERVER["argv"][$iArg][0] == '-') {
                            Usage();
                        }
                    }
                }
            }
        }
    }
    if ($strDataSource == NULL) {
        Usage();
    }
    /* -------------------------------------------------------------------- */
    /*      Open data source.                                               */
    /* -------------------------------------------------------------------- */
    $hSFDriver = NULL;
    $hDS = OGROpen($strDataSource, FALSE, $hSFDriver);
    /* -------------------------------------------------------------------- */
    /*      Report failure                                                  */
    /* -------------------------------------------------------------------- */
    if ($hDS == NULL) {
        printf("FAILURE:\nUnable to open datasource `%s' with the following drivers:\n", $strDataSource);
        for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
            printf("  -> %s\n", OGR_DR_GetName(OGRGetDriver($iDriver)));
        }
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Find the output driver.                                         */
    /* -------------------------------------------------------------------- */
    for ($iDriver = 0; $iDriver < OGRGetDriverCount() && $hSFDriver == NULL; $iDriver++) {
        if (!strcasecmp(OGR_DR_GetName(OGRGetDriver($iDriver)), $strFormat)) {
            $hSFDriver = OGRGetDriver($iDriver);
        }
    }
    if ($hSFDriver == NULL) {
        printf("Unable to find driver `%s'.\n", $strFormat);
        printf("The following drivers are available:\n");
        for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
            printf("  -> %s\n", OGR_DR_GetName(OGRGetDriver($iDriver)));
        }
        return OGRERR_FAILURE;
    }
    if (!OGR_Dr_TestCapability($hSFDriver, ODrCCreateDataSource)) {
        printf("%s driver does not support data source creation.\n", $strFormat);
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Create the output data source.                                  */
    /* -------------------------------------------------------------------- */
    /*Uncomment and add options here. */
    /* $aoptions[0] = 'option1';
        $aoptions[1] = 'option2';
        $hODS = OGR_Dr_CreateDataSource( $hSFDriver, $strDestDataSource, $aoptions );
    */
    /* Or use no option.*/
    $hODS = OGR_Dr_CreateDataSource($hSFDriver, $strDestDataSource, NULL);
    if ($hODS == NULL) {
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Fetch source layer using SQL statement.                         */
    /* -------------------------------------------------------------------- */
    $hLayer = OGR_DS_ExecuteSQL($hDS, $strSQLStatement, $hSpatialFilter, NULL);
    /*Copy datasource file to a new data source file*/
    if ($hLayer != NULL) {
        if (!TranslateLayer($hDS, $hLayer, $hODS)) {
            return OGRERR_FAILURE;
        }
        OGR_DS_ReleaseResultSet($hDS, $hLayer);
    }
    /* -------------------------------------------------------------------- */
    /*      Close down.                                                     */
    /* -------------------------------------------------------------------- */
    OGR_DS_Destroy($hDS);
    OGR_DS_Destroy($hODS);
    return OGRERR_NONE;
}
Пример #3
0
 /**
  * Return layer definition string
  * reads either *.msl file with same base name as data file
  *   or default polygon/line/point.lyr file from plugin dir
  * @return string $layerString
  */
 protected function getLayerDefinition($connParams, $layerSpatialType)
 {
     $ogrDBConn = false;
     if ($connParams['type'] == "generic") {
         $ogrConnectionStr = $this->data_path_prefix . $connParams['layer'];
         //error_log($ogrConnectionStr);
         // $layerDefFile = dirname(__FILE__) . "/" . $connParams['layer'] . ".msl";   ## swap comment if layer definition file in plugin dir
         $layerDefFile = $ogrConnectionStr . ".msl";
     } else {
         $ogrConnectionStr = "PG: " . $connParams['connection'];
         $layerDefFile = dirname(__FILE__) . "/layerdefinition/" . $connParams['schema'] . "." . $connParams['layer'] . ".msl";
         //$layerDefFile = dirname(__FILE__) . "/layerdefinition/" . $connParams['layer'] . ".msl";
     }
     if ($layerSpatialType == "vector") {
         if (!file_exists($ogrConnectionStr) && $connParams['type'] == "generic") {
             error_log($ogrConnectionStr . " does not exist.");
             return null;
         }
         // Register all drivers
         OGRRegisterAll();
         // Open data source
         $hSFDriver = NULL;
         $hDatasource = OGROpen($ogrConnectionStr, 0, $hSFDriver);
         if (!$hDatasource) {
             error_log("Unable to open %s\n" . $ogrConnectionStr);
             return 0;
         }
         if ($connParams['type'] == "generic") {
             $hLayer = OGR_DS_GetLayer($hDatasource, 0);
         } else {
             $hLayer = OGR_DS_GetLayerByName($hDatasource, $connParams['schema'] . "." . $connParams['layer']);
         }
         /* Dump info about this layer */
         $hLayerDefn = OGR_L_GetLayerDefn($hLayer);
         $hFeature = OGR_L_GetNextFeature($hLayer);
         if (OGR_F_GetGeometryRef($hFeature) != NULL) {
             $hGeom = OGR_F_GetGeometryRef($hFeature);
             //$geomType = OGR_G_GetGeometryType($hGeom);
             $geomNameOGR = OGR_G_GetGeometryName($hGeom);
         }
         /* Close data source */
         OGR_DS_Destroy($hDatasource);
         /*$geomList[1] = "point";
           $geomList[2] = "line";
           $geomList[3] = "polygon";
           $geomList[6] = "polygon";*/
         $geomList["POINT"] = "point";
         $geomList["MULTIPOINT"] = "point";
         $geomList["LINESTRING"] = "line";
         $geomList["MULTILINESTRING"] = "line";
         $geomList["POLYGON"] = "polygon";
         $geomList["MULTIPOLYGON"] = "polygon";
         //            $geomName = $geomList[$geomType];
         $geomName = $geomList[$geomNameOGR];
     } else {
         $geomName = "raster";
     }
     if (is_file($layerDefFile)) {
         //if (is_file($layerDefFile = $layerFilename . ".msl")) {
         $layerString = file_get_contents($layerDefFile);
     } else {
         $layerPath = dirname(__FILE__) . "/layerdefinition/{$geomName}.lyr";
         $layerString = file_get_contents($layerPath);
     }
     //error_log("layerDefFile: $layerDefFile");
     return $layerString;
 }
 function get_dst_layer($form_submission_id, $geom_type)
 {
     OGRRegisterAll();
     $dst_layer_name = "";
     $dst_layer = NULL;
     $sql_str = "SELECT " . "tng_spatial_data.table_name, " . "tng_spatial_data.pk_col_name " . "FROM " . "tng_form_submission " . "INNER JOIN tng_form_spatial_data " . "ON tng_form_submission.form_id = tng_form_spatial_data.form_id " . "INNER JOIN tng_spatial_attribute_table " . "ON tng_form_spatial_data.attr_table_id = tng_spatial_attribute_table.attr_table_id " . "INNER JOIN tng_spatial_data " . "ON tng_spatial_attribute_table.spatial_table_id = tng_spatial_data.spatial_table_id " . "WHERE " . "tng_form_submission.form_submission_id  = " . $form_submission_id . " " . "AND " . "POSITION(tng_spatial_data.geometry_type IN '" . $geom_type . "') > 0 ";
     // note: if geom_type is "multilinestring", then doing a straight
     // comparison (without the LIKE clause) will return no
     // results, because tng_spatial_data.geometry_type
     // is one of: polygon, linestring, point.
     $this->dbconn->connect();
     $result = pg_query($this->dbconn->conn, $sql_str);
     if (!$result) {
         echo "An error occurred while executing the query " . pg_last_error($this->dbconn->conn);
         $this->dbconn->disconnect();
     } else {
         $this->dst_layer_name = pg_fetch_result($result, 0, 0);
         $this->pk_col_name = pg_fetch_result($result, 0, 1);
         $this->dbconn->disconnect();
         // now attempt to create an OGR layer
         // from the spatial table name
         // use the same connection string as the
         // one in the dbconn object
         $dst_driver = NULL;
         $this->ogr_dst_ds = OGROpen("PG:" . $this->dbconn->conn_str, FALSE, $dst_driver);
         if ($this->ogr_dst_ds != NULL) {
             $dst_layer = OGR_DS_GetLayerByName($this->ogr_dst_ds, $this->dst_layer_name);
         }
     }
     return $dst_layer;
 }
 * 
 *
 **********************************************************************/
// Check for command-line arguments
if (count($_SERVER["argv"]) >= 2) {
    // Filename passed as argument in command-line mode
    $strfilename = $_SERVER["argv"][1];
} else {
    // Set your default test filename here (for use in web environment)
    $strfilename = "test.tab";
}
// Register all drivers
OGRRegisterAll();
// Open data source
$hSFDriver = NULL;
$hDatasource = OGROpen($strfilename, 0, $hSFDriver);
printf("\nDatasource = ");
print_r($hDatasource);
printf("\nSFDriver = ");
print_r($hSFDriver);
if (!$hDatasource) {
    printf("Unable to open %s\n", $strfilename);
    return -1;
}
/* Loop through layers and dump their contents */
$numLayers = OGR_DS_GetLayerCount($hDatasource);
printf("\nLayers count = %s\n", $numLayers);
for ($i = 0; $i < $numLayers; $i++) {
    $hLayer = OGR_DS_GetLayer($hDatasource, $i);
    /* Dump info about this layer */
    $hLayerDefn = OGR_L_GetLayerDefn($hLayer);
Пример #6
0
 /**
  *
  * Função que transforma o arquivo num formato qualquer para formato conhecido
  *
  * @param string $inputFile
  * @param string $outputFile
  * @param string $outputFormat
  * @param array  $options
  */
 public function ogrToOgr($inputFile, $outputFile, $outputFormat = 'KML', $options = NULL)
 {
     // pega todos os formatos disponíveis no GDAL / OGR
     OGRRegisterAll();
     $astrLayers = NULL;
     $hSFDriver = NULL;
     $hDS = OGROpen($inputFile, FALSE, $hSFDriver);
     // erro caso não consiga abrir o arquivo com os drivers disponíveis.
     if ($hDS == NULL) {
         for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
             $drivers[] = OGR_DR_GetName(OGRGetDriver($iDriver));
         }
         return sprintf(static::GEO_UNABLE_OPEN_FILE_DRIVERS, $inputFile, implode(',', $drivers));
     }
     // pega o driver de saída.
     for ($iDriver = 0; $iDriver < OGRGetDriverCount() && $hSFDriver == NULL; $iDriver++) {
         if (!strcasecmp(OGR_DR_GetName(OGRGetDriver($iDriver)), $outputFormat)) {
             $hSFDriver = OGRGetDriver($iDriver);
         }
     }
     // não encontrou driver para saída.
     if ($hSFDriver == NULL) {
         for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
             $drivers[] = OGR_DR_GetName(OGRGetDriver($iDriver));
         }
         return sprintf(static::GEO_FIND_DRIVERS, $outputFormat, implode(',', $drivers));
     }
     // driver de saída não suporta gerar arquivo físico.
     if (!OGR_Dr_TestCapability($hSFDriver, ODrCCreateDataSource)) {
         return sprintf(static::GEO_DRIVER_DONT_SUPORT_IMPORT, $outputFormat);
     }
     // cria o arquivo de saída
     $hODS = OGR_Dr_CreateDataSource($hSFDriver, $outputFile, $options);
     if ($hODS == NULL) {
         return OGRERR_FAILURE;
     }
     // processa cada layer do arquivo
     for ($iLayer = 0; $iLayer < OGR_DS_GetLayerCount($hDS); $iLayer++) {
         $hLayer = OGR_DS_GetLayer($hDS, $iLayer);
         if ($hLayer == NULL) {
             return sprintf(STATIC::GEO_UNABLE_PROCESS_LAYER, $ilayer);
         }
         if (count($astrLayers) == 0 || in_array(OGR_FD_GetName(OGR_L_GetLayerDefn($hLayer)), $astrLayers) != FALSE) {
             if (!$this->translateLayer($hDS, $hLayer, $hODS)) {
                 return OGRERR_FAILURE;
             }
         }
     }
     OGR_DS_Destroy($hDS);
     OGR_DS_Destroy($hODS);
     return NULL;
 }
function OGR2OGR_main()
{
    $strFormat = "ESRI Shapefile";
    $strDataSource = NULL;
    $strDestDataSource = NULL;
    $astrLayers = NULL;
    /* -------------------------------------------------------------------- */
    /*      Register format(s).                                             */
    /* -------------------------------------------------------------------- */
    OGRRegisterAll();
    /* -------------------------------------------------------------------- */
    /*      Processing command line arguments.                              */
    /* -------------------------------------------------------------------- */
    $numArgs = count($_SERVER["argv"]);
    for ($iArg = 1; $iArg < $numArgs; $iArg++) {
        if (!strcasecmp($_SERVER["argv"][$iArg], "-f") && $iArg < $numArgs - 1) {
            $strFormat = $_SERVER["argv"][++$iArg];
            printf("Format = %s\n", $strFormat);
        } else {
            if ($_SERVER["argv"][$iArg][0] == '-') {
                Usage();
            } else {
                if ($strDestDataSource == NULL) {
                    $strDestDataSource = $_SERVER["argv"][$iArg];
                    printf("DestDataSource = %s\n", $strDestDataSource);
                } else {
                    if ($strDataSource == NULL) {
                        $strDataSource = $_SERVER["argv"][$iArg];
                        printf("DataSource = %s\n", $strDataSource);
                    } else {
                        $astrLayers[] = $_SERVER["argv"][$iArg];
                    }
                }
            }
        }
    }
    $i = 0;
    while ($astrLayers[$i]) {
        printf("Layers [%d] = %s\n", $i, $astrLayers[$i]);
        $i++;
    }
    if ($strDataSource == NULL) {
        Usage();
    }
    /* -------------------------------------------------------------------- */
    /*      Open data source.                                               */
    /* -------------------------------------------------------------------- */
    $hSFDriver = NULL;
    $hDS = OGROpen($strDataSource, FALSE, $hSFDriver);
    /* -------------------------------------------------------------------- */
    /*      Report failure                                                  */
    /* -------------------------------------------------------------------- */
    if ($hDS == NULL) {
        printf("FAILURE:\nUnable to open datasource `%s' with the following drivers:\n", $strDataSource);
        for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
            printf("  -> %s\n", OGR_DR_GetName(OGRGetDriver($iDriver)));
        }
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Find the output driver.                                         */
    /* -------------------------------------------------------------------- */
    for ($iDriver = 0; $iDriver < OGRGetDriverCount() && $hSFDriver == NULL; $iDriver++) {
        if (!strcasecmp(OGR_DR_GetName(OGRGetDriver($iDriver)), $strFormat)) {
            $hSFDriver = OGRGetDriver($iDriver);
        }
    }
    if ($hSFDriver == NULL) {
        printf("Unable to find driver `%s'.\n", $strFormat);
        printf("The following drivers are available:\n");
        for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
            printf("  -> %s\n", OGR_DR_GetName(OGRGetDriver($iDriver)));
        }
        return OGRERR_FAILURE;
    }
    if (!OGR_Dr_TestCapability($hSFDriver, ODrCCreateDataSource)) {
        printf("%s driver does not support data source creation.\n", $strFormat);
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Create the output data source.                                  */
    /* -------------------------------------------------------------------- */
    /*Uncomment and add options here. */
    /* $aoptions[0] = 'option1';
        $aoptions[1] = 'option2';
        $hODS = OGR_Dr_CreateDataSource( $hSFDriver, $strDestDataSource, $aoptions );
    */
    /* Or use no option.*/
    $hODS = OGR_Dr_CreateDataSource($hSFDriver, $strDestDataSource, NULL);
    if ($hODS == NULL) {
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Process each data source layer.                                 */
    /* -------------------------------------------------------------------- */
    for ($iLayer = 0; $iLayer < OGR_DS_GetLayerCount($hDS); $iLayer++) {
        $hLayer = OGR_DS_GetLayer($hDS, $iLayer);
        if ($hLayer == NULL) {
            printf("FAILURE: Couldn't fetch advertised layer %d!\n", $iLayer);
            return OGRERR_FAILURE;
        }
        if (count($astrLayers) == 0 || in_array(OGR_FD_GetName(OGR_L_GetLayerDefn($hLayer)), $astrLayers) != FALSE) {
            if (!TranslateLayer($hDS, $hLayer, $hODS)) {
                return OGRERR_FAILURE;
            }
        }
    }
    /* -------------------------------------------------------------------- */
    /*      Close down.                                                     */
    /* -------------------------------------------------------------------- */
    OGR_DS_Destroy($hDS);
    OGR_DS_Destroy($hODS);
    return OGRERR_NONE;
}
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;
}
function OGR_SpatialFilter_main()
{
    /*Assigning initial value.*/
    $strFormat = "ESRI Shapefile";
    $strDataSource = NULL;
    $strDestDataSource = NULL;
    $strWhere = NULL;
    $hSpatialFilter = NULL;
    /* -------------------------------------------------------------------- */
    /*      Register format(s).                                             */
    /* -------------------------------------------------------------------- */
    OGRRegisterAll();
    /* -------------------------------------------------------------------- */
    /*      Processing command line arguments.                              */
    /* -------------------------------------------------------------------- */
    $numArgs = count($_SERVER["argv"]);
    for ($iArg = 1; $iArg < $numArgs; $iArg++) {
        if (!strcasecmp($_SERVER["argv"][$iArg], "-f") && $iArg < $numArgs - 1) {
            $strFormat = $_SERVER["argv"][++$iArg];
            printf("Format = %s\n", $strFormat);
        } else {
            if ($_SERVER["argv"][$iArg] == "-spat" && $_SERVER["argv"][$iArg + 1] != NULL && $_SERVER["argv"][$iArg + 2] != NULL && $_SERVER["argv"][$iArg + 3] != NULL && $_SERVER["argv"][$iArg + 4] != NULL) {
                $hSpatialFilter = OGR_G_CreateGeometry(wkbLinearRing);
                OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 1]), floatval($_SERVER["argv"][$iArg + 2]), 0.0);
                OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 1]), floatval($_SERVER["argv"][$iArg + 4]), 0.0);
                OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 3]), floatval($_SERVER["argv"][$iArg + 4]), 0.0);
                OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 3]), floatval($_SERVER["argv"][$iArg + 2]), 0.0);
                OGR_G_AddPoint($hSpatialFilter, floatval($_SERVER["argv"][$iArg + 1]), floatval($_SERVER["argv"][$iArg + 2]), 0.0);
                printf("Xmin = %s Ymin = %s Xmax = %s Ymax = %s\n", $_SERVER["argv"][$iArg + 1], $_SERVER["argv"][$iArg + 2], $_SERVER["argv"][$iArg + 3], $_SERVER["argv"][$iArg + 4]);
                $iArg += 4;
            } else {
                if ($_SERVER["argv"][$iArg] == "-where" && $_SERVER["argv"][$iArg + 1] != NULL) {
                    $strWhere = $_SERVER["argv"][++$iArg];
                    printf("where = %s\n", $strWhere);
                } else {
                    if ($_SERVER["argv"][$iArg][0] == '-') {
                        Usage();
                    } else {
                        if ($strDestDataSource == NULL) {
                            $strDestDataSource = $_SERVER["argv"][$iArg];
                            printf("DestDataSource = %s\n", $strDestDataSource);
                        } else {
                            if ($strDataSource == NULL) {
                                $strDataSource = $_SERVER["argv"][$iArg];
                                printf("DataSource = %s\n", $strDataSource);
                            }
                        }
                    }
                }
            }
        }
    }
    if ($strDataSource == NULL) {
        Usage();
    }
    /* -------------------------------------------------------------------- */
    /*      Open data source.                                               */
    /* -------------------------------------------------------------------- */
    $hSFDriver = NULL;
    $hDS = OGROpen($strDataSource, FALSE, $hSFDriver);
    /* -------------------------------------------------------------------- */
    /*      Report failure                                                  */
    /* -------------------------------------------------------------------- */
    if ($hDS == NULL) {
        printf("FAILURE:\nUnable to open datasource `%s' with the following drivers:\n", $strDataSource);
        for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
            printf("  -> %s\n", OGR_DR_GetName(OGRGetDriver($iDriver)));
        }
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Find the output driver.                                         */
    /* -------------------------------------------------------------------- */
    for ($iDriver = 0; $iDriver < OGRGetDriverCount() && $hSFDriver == NULL; $iDriver++) {
        if (!strcasecmp(OGR_DR_GetName(OGRGetDriver($iDriver)), $strFormat)) {
            $hSFDriver = OGRGetDriver($iDriver);
        }
    }
    if ($hSFDriver == NULL) {
        printf("Unable to find driver `%s'.\n", $strFormat);
        printf("The following drivers are available:\n");
        for ($iDriver = 0; $iDriver < OGRGetDriverCount(); $iDriver++) {
            printf("  -> %s\n", OGR_DR_GetName(OGRGetDriver($iDriver)));
        }
        return OGRERR_FAILURE;
    }
    if (!OGR_Dr_TestCapability($hSFDriver, ODrCCreateDataSource)) {
        printf("%s driver does not support data source creation.\n", $strFormat);
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Create the output data source.                                  */
    /* -------------------------------------------------------------------- */
    /*Uncomment and add options here. */
    /* $aoptions[0] = 'option1';
        $aoptions[1] = 'option2';
        $hODS = OGR_Dr_CreateDataSource( $hSFDriver, $strDestDataSource, $aoptions );
    */
    /* Or use no option.*/
    $hODS = OGR_Dr_CreateDataSource($hSFDriver, $strDestDataSource, NULL);
    if ($hODS == NULL) {
        return OGRERR_FAILURE;
    }
    /* -------------------------------------------------------------------- */
    /*      Process each data source layer.                                 */
    /* -------------------------------------------------------------------- */
    for ($iLayer = 0; $iLayer < OGR_DS_GetLayerCount($hDS); $iLayer++) {
        $hLayer = OGR_DS_GetLayer($hDS, $iLayer);
        if ($hLayer == NULL) {
            printf("FAILURE: Couldn't fetch advertised layer %d!\n", $iLayer);
            return OGRERR_FAILURE;
        }
        if (count($astrLayers) == 0 || in_array(OGR_FD_GetName(OGR_L_GetLayerDefn($hLayer)), $astrLayers) != FALSE) {
            /*Assign spatial filter*/
            if ($hSpatialFilter != NULL) {
                OGR_L_SetSpatialFilter($hLayer, $hSpatialFilter);
            }
            /*Assign attribute filter*/
            if ($strWhere != NULL) {
                $eErr = OGR_L_SetAttributeFilter($hLayer, $strWhere);
            }
            if ($eErr != OGRERR_NONE) {
                printf("Erreur when setting attribute filter", $eErr);
                return $eErr;
            }
            /*Copy datasource file to a new data source file*/
            if (!TranslateLayer($hDS, $hLayer, $hODS)) {
                return OGRERR_FAILURE;
            }
        }
    }
    /* -------------------------------------------------------------------- */
    /*      Close down.                                                     */
    /* -------------------------------------------------------------------- */
    OGR_DS_Destroy($hDS);
    OGR_DS_Destroy($hODS);
    return OGRERR_NONE;
}
Пример #10
0
 $dbf_name = "/tmp/test.dbf";
 $shx_name = "/tmp/test.shx";
 move_uploaded_file($shp_tmp_name, $shp_name);
 move_uploaded_file($dbf_tmp_name, $dbf_name);
 move_uploaded_file($shx_tmp_name, $shx_name);
 $src_driver = NULL;
 $src_ds = OGROpen($shp_name, FALSE, $src_driver);
 if ($src_ds == NULL) {
     echo "Could not open src shape file";
 } else {
     $dst_layer = "";
     $dst_layer_name = "";
     $dst_attr_table_name = "";
     $dst_driver = NULL;
     /* change this so that the connection info is read from class_dbconn.php */
     $dst_ds = OGROpen("PG:host=142.207.144.71 dbname=tng_dev user=tng_readwrite password=tng_readwrite", FALSE, $dst_driver);
     if ($dst_ds == NULL) {
         echo "could not open dest.";
         exit;
     }
     echo "shapefile opened, postgres source opened";
     // shapefiles only have one layer, so
     // the second arg to GetLayer is 0.
     $src_layer = OGR_DS_GetLayer($src_ds, 0);
     if ($src_layer == NULL) {
         echo "could not open source layer";
         return;
     }
     $feature_defn = OGR_L_GetLayerDefn($src_layer);
     /*---------------------------------------------------------------------------------------*/
     /* this is done to find out the geometry type only - should be moved to its own function */