/* -------------------------------------------------------------------- */
/* Try opening the output datasource as an existing, writable */
/* -------------------------------------------------------------------- */
DataSource poODS = null;
Driver poDriver = null;
if( bUpdate )
{
poODS = ogr.Open( pszDestDataSource, true );
if( poODS == null )
{
if (bOverwrite || bAppend)
{
poODS = ogr.Open( pszDestDataSource, false );
if ( poODS == null )
{
/* ok the datasource doesn't exist at all */
bUpdate = false;
}
else
{
poODS.delete();
poODS = null;
}
}
if (bUpdate)
{
System.err.println("FAILURE:\n" +
"Unable to open existing output datasource `" + pszDestDataSource + "'.");
System.exit( 1 );
}
}
else if( papszDSCO.size() > 0 )
{
System.err.println("WARNING: Datasource creation options ignored since an existing datasource\n" +
" being updated." );
}
if (poODS != null)
poDriver = poODS.GetDriver();
}
/* -------------------------------------------------------------------- */
/* Find the output driver. */
/* -------------------------------------------------------------------- */
if( !bUpdate )
{
int iDriver;
poDriver = ogr.GetDriverByName(pszFormat);
if( poDriver == null )
{
System.err.println("Unable to find driver `" + pszFormat +"'." );
System.err.println( "The following drivers are available:" );
for( iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ )
{
System.err.println(" . " + ogr.GetDriver(iDriver).GetName() );
}
System.exit( 1 );
}
if( poDriver.TestCapability( ogr.ODrCCreateDataSource ) == false )
{
System.err.println( pszFormat + " driver does not support data source creation.");
System.exit( 1 );
}
/* -------------------------------------------------------------------- */
/* Special case to improve user experience when translating */
/* a datasource with multiple layers into a shapefile. If the */
/* user gives a target datasource with .shp and it does not exist, */
/* the shapefile driver will try to create a file, but this is not */
/* appropriate because here we have several layers, so create */
/* a directory instead. */
/* -------------------------------------------------------------------- */
if (poDriver.GetName().equalsIgnoreCase("ESRI Shapefile") &&
pszSQLStatement == null &&
(papszLayers.size() > 1 ||
(papszLayers.size() == 0 && poDS.GetLayerCount() > 1)) &&
pszNewLayerName == null &&
(pszDestDataSource.endsWith(".shp") || pszDestDataSource.endsWith(".SHP")))
{
File f = new File(pszDestDataSource);
if (!f.exists())
{
if (!f.mkdir())
{
System.err.println(
"Failed to create directory " + pszDestDataSource + "\n" +
"for shapefile datastore.");
System.exit(1);
}
}
}
/* -------------------------------------------------------------------- */
/* Create the output data source. */
/* -------------------------------------------------------------------- */
poODS = poDriver.CreateDataSource( pszDestDataSource, papszDSCO );
if( poODS == null )
{
System.err.println( pszFormat + " driver failed to create "+ pszDestDataSource );
System.exit( 1 );
}
}
/* -------------------------------------------------------------------- */
/* Parse the output SRS definition if possible. */
/* -------------------------------------------------------------------- */
if( pszOutputSRSDef != null )
{
poOutputSRS = new SpatialReference();
if( poOutputSRS.SetFromUserInput( pszOutputSRSDef ) != 0 )
{
System.err.println( "Failed to process SRS definition: " + pszOutputSRSDef );
System.exit( 1 );
}
}
/* -------------------------------------------------------------------- */
/* Parse the source SRS definition if possible. */
/* -------------------------------------------------------------------- */
if( pszSourceSRSDef != null )
{
poSourceSRS = new SpatialReference();
if( poSourceSRS.SetFromUserInput( pszSourceSRSDef ) != 0 )
{
System.err.println( "Failed to process SRS definition: " + pszSourceSRSDef );
System.exit( 1 );
}
}
/* -------------------------------------------------------------------- */
/* Special case for -sql clause. No source layers required. */
/* -------------------------------------------------------------------- */
if( pszSQLStatement != null )
{
Layer poResultSet;
if( pszWHERE != null )
System.err.println( "-where clause ignored in combination with -sql." );
if( papszLayers.size() > 0 )
System.err.println( "layer names ignored in combination with -sql." );
poResultSet = poDS.ExecuteSQL( pszSQLStatement, poSpatialFilter,
null );
if( poResultSet != null )
{
long nCountLayerFeatures = 0;
if (bDisplayProgress)
{
if (!poResultSet.TestCapability(ogr.OLCFastFeatureCount))
{
System.err.println( "Progress turned off as fast feature count is not available.");
bDisplayProgress = false;
}
else
{
nCountLayerFeatures = poResultSet.GetFeatureCount();
pfnProgress = new TermProgressCallback();
}
}
/* -------------------------------------------------------------------- */
/* Special case to improve user experience when translating into */
/* single file shapefile and source has only one layer, and that */
/* the layer name isn't specified */
/* -------------------------------------------------------------------- */
if (poDriver.GetName().equalsIgnoreCase("ESRI Shapefile") &&
pszNewLayerName == null)
{
File f = new File(pszDestDataSource);
if (f.exists() && f.listFiles() == null)
{
pszNewLayerName = f.getName();
int posPoint = pszNewLayerName.lastIndexOf('.');
if (posPoint != -1)
pszNewLayerName = pszNewLayerName.substring(0, posPoint);
}
}
if( !TranslateLayer( poDS, poResultSet, poODS, papszLCO,
pszNewLayerName, bTransform, poOutputSRS,
poSourceSRS, papszSelFields, bAppend, eGType,
bOverwrite, eGeomOp, dfGeomOpParam, papszFieldTypesToString,
nCountLayerFeatures, poClipSrc, poClipDst, bExplodeCollections,
pszZField, pszWHERE, pfnProgress ))
{
System.err.println(
"Terminating translation prematurely after failed\n" +
"translation from sql statement." );
System.exit( 1 );
}
poDS.ReleaseResultSet( poResultSet );
}
}
else
{
int nLayerCount = 0;
Layer[] papoLayers = null;
/* -------------------------------------------------------------------- */
/* Process each data source layer. */
/* -------------------------------------------------------------------- */
if ( papszLayers.size() == 0)
{
nLayerCount = poDS.GetLayerCount();
papoLayers = new Layer[nLayerCount];
for( int iLayer = 0;
iLayer < nLayerCount;
iLayer++ )
{
Layer poLayer = poDS.GetLayer(iLayer);
if( poLayer == null )
{
System.err.println("FAILURE: Couldn't fetch advertised layer " + iLayer + "!");
System.exit( 1 );
}
papoLayers[iLayer] = poLayer;
}
}
/* -------------------------------------------------------------------- */
/* Process specified data source layers. */
/* -------------------------------------------------------------------- */
else
{
nLayerCount = papszLayers.size();
papoLayers = new Layer[nLayerCount];
for( int iLayer = 0;
iLayer < papszLayers.size();
iLayer++ )
{
Layer poLayer = poDS.GetLayerByName((String)papszLayers.get(iLayer));
if( poLayer == null )
{
System.err.println("FAILURE: Couldn't fetch advertised layer " + (String)papszLayers.get(iLayer) + "!");
System.exit( 1 );
}
papoLayers[iLayer] = poLayer;
}
}
/* -------------------------------------------------------------------- */
/* Special case to improve user experience when translating into */
/* single file shapefile and source has only one layer, and that */
/* the layer name isn't specified */
/* -------------------------------------------------------------------- */
if (poDriver.GetName().equalsIgnoreCase("ESRI Shapefile") &&
nLayerCount == 1 && pszNewLayerName == null)
{
File f = new File(pszDestDataSource);
if (f.exists() && f.listFiles() == null)
{