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