📄 ogr2ogr.cpp
字号:
else if( EQUAL(papszArgv[iArg],"-spat")
&& papszArgv[iArg+1] != NULL
&& papszArgv[iArg+2] != NULL
&& papszArgv[iArg+3] != NULL
&& papszArgv[iArg+4] != NULL )
{
OGRLinearRing oRing;
oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+2]) );
oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+4]) );
oRing.addPoint( atof(papszArgv[iArg+3]), atof(papszArgv[iArg+4]) );
oRing.addPoint( atof(papszArgv[iArg+3]), atof(papszArgv[iArg+2]) );
oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+2]) );
poSpatialFilter = new OGRPolygon();
((OGRPolygon *) poSpatialFilter)->addRing( &oRing );
iArg += 4;
}
else if( EQUAL(papszArgv[iArg],"-where") && papszArgv[iArg+1] != NULL )
{
pszWHERE = papszArgv[++iArg];
}
else if( EQUAL(papszArgv[iArg],"-select") && papszArgv[iArg+1] != NULL)
{
pszSelect = papszArgv[++iArg];
papszSelFields = CSLTokenizeStringComplex(pszSelect, " ,",
FALSE, FALSE );
}
else if( papszArgv[iArg][0] == '-' )
{
Usage();
}
else if( pszDestDataSource == NULL )
pszDestDataSource = papszArgv[iArg];
else if( pszDataSource == NULL )
pszDataSource = papszArgv[iArg];
else
papszLayers = CSLAddString( papszLayers, papszArgv[iArg] );
}
if( pszDataSource == NULL )
Usage();
/* -------------------------------------------------------------------- */
/* Open data source. */
/* -------------------------------------------------------------------- */
OGRDataSource *poDS;
poDS = OGRSFDriverRegistrar::Open( pszDataSource, FALSE );
/* -------------------------------------------------------------------- */
/* Report failure */
/* -------------------------------------------------------------------- */
if( poDS == NULL )
{
OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
printf( "FAILURE:\n"
"Unable to open datasource `%s' with the following drivers.\n",
pszDataSource );
for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
{
printf( " -> %s\n", poR->GetDriver(iDriver)->GetName() );
}
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* Try opening the output datasource as an existing, writable */
/* -------------------------------------------------------------------- */
OGRDataSource *poODS;
if( bUpdate )
{
poODS = OGRSFDriverRegistrar::Open( pszDestDataSource, TRUE );
if( poODS == NULL )
{
printf( "FAILURE:\n"
"Unable to open existing output datasource `%s'.\n",
pszDestDataSource );
exit( 1 );
}
}
/* -------------------------------------------------------------------- */
/* Find the output driver. */
/* -------------------------------------------------------------------- */
else
{
OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
OGRSFDriver *poDriver = NULL;
int iDriver;
for( iDriver = 0;
iDriver < poR->GetDriverCount() && poDriver == NULL;
iDriver++ )
{
if( EQUAL(poR->GetDriver(iDriver)->GetName(),pszFormat) )
{
poDriver = poR->GetDriver(iDriver);
}
}
if( poDriver == NULL )
{
printf( "Unable to find driver `%s'.\n", pszFormat );
printf( "The following drivers are available:\n" );
for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
{
printf( " -> `%s'\n", poR->GetDriver(iDriver)->GetName() );
}
exit( 1 );
}
if( !poDriver->TestCapability( ODrCCreateDataSource ) )
{
printf( "%s driver does not support data source creation.\n",
pszFormat );
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* Create the output data source. */
/* -------------------------------------------------------------------- */
poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
if( poODS == NULL )
{
printf( "%s driver failed to create %s\n",
pszFormat, pszDestDataSource );
exit( 1 );
}
}
/* -------------------------------------------------------------------- */
/* Parse the output SRS definition if possible. */
/* -------------------------------------------------------------------- */
if( pszOutputSRSDef != NULL )
{
poOutputSRS = new OGRSpatialReference();
if( poOutputSRS->SetFromUserInput( pszOutputSRSDef ) != OGRERR_NONE )
{
printf( "Failed to process SRS definition: %s\n",
pszOutputSRSDef );
exit( 1 );
}
}
/* -------------------------------------------------------------------- */
/* Parse the source SRS definition if possible. */
/* -------------------------------------------------------------------- */
if( pszSourceSRSDef != NULL )
{
poSourceSRS = new OGRSpatialReference();
if( poSourceSRS->SetFromUserInput( pszSourceSRSDef ) != OGRERR_NONE )
{
printf( "Failed to process SRS definition: %s\n",
pszSourceSRSDef );
exit( 1 );
}
}
/* -------------------------------------------------------------------- */
/* Special case for -sql clause. No source layers required. */
/* -------------------------------------------------------------------- */
if( pszSQLStatement != NULL )
{
OGRLayer *poResultSet;
if( pszWHERE != NULL )
printf( "-where clause ignored in combination with -sql.\n" );
if( CSLCount(papszLayers) > 0 )
printf( "layer names ignored in combination with -sql.\n" );
poResultSet = poDS->ExecuteSQL( pszSQLStatement, poSpatialFilter,
NULL );
if( poResultSet != NULL )
{
if( !TranslateLayer( poDS, poResultSet, poODS, papszLCO,
pszNewLayerName, bTransform, poOutputSRS,
poSourceSRS, papszSelFields, bAppend, eGType))
{
CPLError( CE_Failure, CPLE_AppDefined,
"Terminating translation prematurely after failed\n"
"translation from sql statement." );
exit( 1 );
}
poDS->ReleaseResultSet( poResultSet );
}
}
/* -------------------------------------------------------------------- */
/* Process each data source layer. */
/* -------------------------------------------------------------------- */
for( int iLayer = 0;
pszSQLStatement == NULL && iLayer < poDS->GetLayerCount();
iLayer++ )
{
OGRLayer *poLayer = poDS->GetLayer(iLayer);
if( poLayer == NULL )
{
printf( "FAILURE: Couldn't fetch advertised layer %d!\n",
iLayer );
exit( 1 );
}
if( CSLCount(papszLayers) == 0
|| CSLFindString( papszLayers,
poLayer->GetLayerDefn()->GetName() ) != -1 )
{
if( pszWHERE != NULL )
poLayer->SetAttributeFilter( pszWHERE );
if( poSpatialFilter != NULL )
poLayer->SetSpatialFilter( poSpatialFilter );
if( !TranslateLayer( poDS, poLayer, poODS, papszLCO,
pszNewLayerName, bTransform, poOutputSRS,
poSourceSRS, papszSelFields, bAppend, eGType)
&& !bSkipFailures )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Terminating translation prematurely after failed\n"
"translation of layer %s\n",
poLayer->GetLayerDefn()->GetName() );
exit( 1 );
}
}
}
/* -------------------------------------------------------------------- */
/* Close down. */
/* -------------------------------------------------------------------- */
delete poODS;
delete poDS;
CSLDestroy(papszSelFields);
#ifdef DBMALLOC
malloc_dump(1);
#endif
return 0;
}
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage()
{
OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
printf( "Usage: ogr2ogr [-skipfailures] [-append] [-update] [-f format_name]\n"
" [-select field_list] [-where restricted_where] \n"
" [-sql <sql statement>] \n"
" [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n"
" [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n"
" [[-dsco NAME=VALUE] ...] dst_datasource_name\n"
" src_datasource_name\n"
" [-lco NAME=VALUE] [-nln name] [-nlt type] layer [layer ...]]\n"
"\n"
" -f format_name: output file format name, possible values are:\n");
for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
{
OGRSFDriver *poDriver = poR->GetDriver(iDriver);
if( poDriver->TestCapability( ODrCCreateDataSource ) )
printf( " -f \"%s\"\n", poDriver->GetName() );
}
printf( " -append: Append to existing layer instead of creating new\n"
" -update: Open existing output datasource in update mode\n"
" -select field_list: Comma-delimited list of fields from input layer to\n"
" copy to the new layer (defaults to all)\n"
" -where restricted_where: Attribute query (like SQL WHERE)\n"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -