Vector papszDSCO = new Vector(), papszLCO = new Vector();
boolean bTransform = false;
boolean bAppend = false, bUpdate = false, bOverwrite = false;
String pszOutputSRSDef = null;
String pszSourceSRSDef = null;
SpatialReference poOutputSRS = null;
SpatialReference poSourceSRS = null;
String pszNewLayerName = null;
String pszWHERE = null;
Geometry poSpatialFilter = null;
String pszSelect;
Vector papszSelFields = null;
String pszSQLStatement = null;
int eGType = -2;
GeomOperation eGeomOp = GeomOperation.NONE;
double dfGeomOpParam = 0;
Vector papszFieldTypesToString = new Vector();
boolean bDisplayProgress = false;
ProgressCallback pfnProgress = null;
boolean bClipSrc = false;
Geometry poClipSrc = null;
String pszClipSrcDS = null;
String pszClipSrcSQL = null;
String pszClipSrcLayer = null;
String pszClipSrcWhere = null;
Geometry poClipDst = null;
String pszClipDstDS = null;
String pszClipDstSQL = null;
String pszClipDstLayer = null;
String pszClipDstWhere = null;
String pszSrcEncoding = null;
String pszDstEncoding = null;
boolean bExplodeCollections = false;
String pszZField = null;
ogr.DontUseExceptions();
/* -------------------------------------------------------------------- */
/* Register format(s). */
/* -------------------------------------------------------------------- */
if( ogr.GetDriverCount() == 0 )
ogr.RegisterAll();
/* -------------------------------------------------------------------- */
/* Processing command line arguments. */
/* -------------------------------------------------------------------- */
args = ogr.GeneralCmdLineProcessor( args );
if( args.length < 2 )
{
Usage();
System.exit( -1 );
}
for( int iArg = 0; iArg < args.length; iArg++ )
{
if( args[iArg].equalsIgnoreCase("-f") && iArg < args.length-1 )
{
pszFormat = args[++iArg];
}
else if( args[iArg].equalsIgnoreCase("-dsco") && iArg < args.length-1 )
{
papszDSCO.addElement(args[++iArg] );
}
else if( args[iArg].equalsIgnoreCase("-lco") && iArg < args.length-1 )
{
papszLCO.addElement(args[++iArg] );
}
else if( args[iArg].equalsIgnoreCase("-preserve_fid") )
{
bPreserveFID = true;
}
else if( args[iArg].length() >= 5 && args[iArg].substring(0, 5).equalsIgnoreCase("-skip") )
{
bSkipFailures = true;
nGroupTransactions = 1; /* #2409 */
}
else if( args[iArg].equalsIgnoreCase("-append") )
{
bAppend = true;
bUpdate = true;
}
else if( args[iArg].equalsIgnoreCase("-overwrite") )
{
bOverwrite = true;
bUpdate = true;
}
else if( args[iArg].equalsIgnoreCase("-update") )
{
bUpdate = true;
}
else if( args[iArg].equalsIgnoreCase("-fid") && args[iArg+1] != null )
{
nFIDToFetch = Integer.parseInt(args[++iArg]);
}
else if( args[iArg].equalsIgnoreCase("-sql") && args[iArg+1] != null )
{
pszSQLStatement = args[++iArg];
}
else if( args[iArg].equalsIgnoreCase("-nln") && iArg < args.length-1 )
{
pszNewLayerName = args[++iArg];
}
else if( args[iArg].equalsIgnoreCase("-nlt") && iArg < args.length-1 )
{
if( args[iArg+1].equalsIgnoreCase("NONE") )
eGType = ogr.wkbNone;
else if( args[iArg+1].equalsIgnoreCase("GEOMETRY") )
eGType = ogr.wkbUnknown;
else if( args[iArg+1].equalsIgnoreCase("POINT") )
eGType = ogr.wkbPoint;
else if( args[iArg+1].equalsIgnoreCase("LINESTRING") )
eGType = ogr.wkbLineString;
else if( args[iArg+1].equalsIgnoreCase("POLYGON") )
eGType = ogr.wkbPolygon;
else if( args[iArg+1].equalsIgnoreCase("GEOMETRYCOLLECTION") )
eGType = ogr.wkbGeometryCollection;
else if( args[iArg+1].equalsIgnoreCase("MULTIPOINT") )
eGType = ogr.wkbMultiPoint;
else if( args[iArg+1].equalsIgnoreCase("MULTILINESTRING") )
eGType = ogr.wkbMultiLineString;
else if( args[iArg+1].equalsIgnoreCase("MULTIPOLYGON") )
eGType = ogr.wkbMultiPolygon;
else if( args[iArg+1].equalsIgnoreCase("GEOMETRY25D") )
eGType = ogr.wkbUnknown | ogr.wkb25DBit;
else if( args[iArg+1].equalsIgnoreCase("POINT25D") )
eGType = ogr.wkbPoint25D;
else if( args[iArg+1].equalsIgnoreCase("LINESTRING25D") )
eGType = ogr.wkbLineString25D;
else if( args[iArg+1].equalsIgnoreCase("POLYGON25D") )
eGType = ogr.wkbPolygon25D;
else if( args[iArg+1].equalsIgnoreCase("GEOMETRYCOLLECTION25D") )
eGType = ogr.wkbGeometryCollection25D;
else if( args[iArg+1].equalsIgnoreCase("MULTIPOINT25D") )
eGType = ogr.wkbMultiPoint25D;
else if( args[iArg+1].equalsIgnoreCase("MULTILINESTRING25D") )
eGType = ogr.wkbMultiLineString25D;
else if( args[iArg+1].equalsIgnoreCase("MULTIPOLYGON25D") )
eGType = ogr.wkbMultiPolygon25D;
else
{
System.err.println("-nlt " + args[iArg+1] + ": type not recognised.");
System.exit( 1 );
}
iArg++;
}
else if( (args[iArg].equalsIgnoreCase("-tg") ||
args[iArg].equalsIgnoreCase("-gt")) && iArg < args.length-1 )
{
nGroupTransactions = Integer.parseInt(args[++iArg]);
}
else if( args[iArg].equalsIgnoreCase("-s_srs") && iArg < args.length-1 )
{
pszSourceSRSDef = args[++iArg];
}
else if( args[iArg].equalsIgnoreCase("-a_srs") && iArg < args.length-1 )
{
pszOutputSRSDef = args[++iArg];
}
else if( args[iArg].equalsIgnoreCase("-t_srs") && iArg < args.length-1 )
{
pszOutputSRSDef = args[++iArg];
bTransform = true;
}
else if (args[iArg].equalsIgnoreCase("-spat") && iArg + 4 < args.length)
{
Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
double xmin = new Double(args[++iArg]).doubleValue();
double ymin = new Double(args[++iArg]).doubleValue();
double xmax = new Double(args[++iArg]).doubleValue();
double ymax = new Double(args[++iArg]).doubleValue();
oRing.AddPoint(xmin, ymin);
oRing.AddPoint(xmin, ymax);
oRing.AddPoint(xmax, ymax);
oRing.AddPoint(xmax, ymin);
oRing.AddPoint(xmin, ymin);
poSpatialFilter = new Geometry(ogrConstants.wkbPolygon);
poSpatialFilter.AddGeometry(oRing);
}
else if( args[iArg].equalsIgnoreCase("-where") && args[iArg+1] != null )
{
pszWHERE = args[++iArg];
}
else if( args[iArg].equalsIgnoreCase("-select") && args[iArg+1] != null)
{
pszSelect = args[++iArg];
StringTokenizer tokenizer = new StringTokenizer(pszSelect, " ,");
papszSelFields = new Vector();
while(tokenizer.hasMoreElements())
papszSelFields.addElement(tokenizer.nextToken());
}
else if( args[iArg].equalsIgnoreCase("-simplify") && iArg < args.length-1 )
{
eGeomOp = GeomOperation.SIMPLIFY_PRESERVE_TOPOLOGY;
dfGeomOpParam = new Double(args[++iArg]).doubleValue();
}
else if( args[iArg].equalsIgnoreCase("-segmentize") && iArg < args.length-1 )
{
eGeomOp = GeomOperation.SEGMENTIZE;
dfGeomOpParam = new Double(args[++iArg]).doubleValue();
}
else if( args[iArg].equalsIgnoreCase("-fieldTypeToString") && iArg < args.length-1 )
{
StringTokenizer tokenizer = new StringTokenizer(args[++iArg], " ,");
while(tokenizer.hasMoreElements())
{
String token = (String)tokenizer.nextToken();
if (token.equalsIgnoreCase("Integer") ||
token.equalsIgnoreCase("Real") ||
token.equalsIgnoreCase("String") ||
token.equalsIgnoreCase("Date") ||
token.equalsIgnoreCase("Time") ||
token.equalsIgnoreCase("DateTime") ||
token.equalsIgnoreCase("Binary") ||
token.equalsIgnoreCase("IntegerList") ||
token.equalsIgnoreCase("RealList") ||
token.equalsIgnoreCase("StringList"))
{
papszFieldTypesToString.addElement(token);
}
else if (token.equalsIgnoreCase("All"))
{
papszFieldTypesToString = null;
papszFieldTypesToString.addElement("All");
break;
}
else
{
System.err.println("Unhandled type for fieldtypeasstring option : " + token);
Usage();
}
}
}
else if( args[iArg].equalsIgnoreCase("-progress") )
{
bDisplayProgress = true;
}
/*else if( args[iArg].equalsIgnoreCase("-wrapdateline") )
{
bWrapDateline = true;
}
*/
else if( args[iArg].equalsIgnoreCase("-clipsrc") && iArg < args.length-1 )
{
bClipSrc = true;
if ( IsNumber(args[iArg+1]) && iArg < args.length - 4 )
{
Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
double xmin = new Double(args[++iArg]).doubleValue();
double ymin = new Double(args[++iArg]).doubleValue();
double xmax = new Double(args[++iArg]).doubleValue();
double ymax = new Double(args[++iArg]).doubleValue();
oRing.AddPoint(xmin, ymin);
oRing.AddPoint(xmin, ymax);
oRing.AddPoint(xmax, ymax);
oRing.AddPoint(xmax, ymin);
oRing.AddPoint(xmin, ymin);
poClipSrc = new Geometry(ogrConstants.wkbPolygon);
poClipSrc.AddGeometry(oRing);
}
else if( (args[iArg+1].length() >= 7 && args[iArg+1].substring(0, 7).equalsIgnoreCase("POLYGON") ) ||
(args[iArg+1].length() >= 12 && args[iArg+1].substring(0, 12).equalsIgnoreCase("MULTIPOLYGON") ) )
{
poClipSrc = Geometry.CreateFromWkt(args[iArg+1]);
if (poClipSrc == null)
{
System.err.print("FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n\n");
Usage();
}
iArg ++;
}
else if (args[iArg+1].equalsIgnoreCase("spat_extent") )
{
iArg ++;
}
else
{
pszClipSrcDS = args[iArg+1];
iArg ++;
}
}
else if( args[iArg].equalsIgnoreCase("-clipsrcsql") && iArg < args.length-1 )
{
pszClipSrcSQL = args[iArg+1];
iArg ++;
}
else if( args[iArg].equalsIgnoreCase("-clipsrclayer") && iArg < args.length-1 )
{
pszClipSrcLayer = args[iArg+1];
iArg ++;
}
else if( args[iArg].equalsIgnoreCase("-clipsrcwhere") && iArg < args.length-1 )
{
pszClipSrcWhere = args[iArg+1];
iArg ++;
}
else if( args[iArg].equalsIgnoreCase("-clipdst") && iArg < args.length-1 )
{
if ( IsNumber(args[iArg+1]) && iArg < args.length - 4 )
{
Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
double xmin = new Double(args[++iArg]).doubleValue();
double ymin = new Double(args[++iArg]).doubleValue();
double xmax = new Double(args[++iArg]).doubleValue();
double ymax = new Double(args[++iArg]).doubleValue();
oRing.AddPoint(xmin, ymin);
oRing.AddPoint(xmin, ymax);
oRing.AddPoint(xmax, ymax);
oRing.AddPoint(xmax, ymin);
oRing.AddPoint(xmin, ymin);
poClipDst = new Geometry(ogrConstants.wkbPolygon);
poClipDst.AddGeometry(oRing);
}
else if( (args[iArg+1].length() >= 7 && args[iArg+1].substring(0, 7).equalsIgnoreCase("POLYGON") ) ||
(args[iArg+1].length() >= 12 && args[iArg+1].substring(0, 12).equalsIgnoreCase("MULTIPOLYGON") ) )
{
poClipDst = Geometry.CreateFromWkt(args[iArg+1]);
if (poClipDst == null)
{
System.err.print("FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n\n");
Usage();
}
iArg ++;
}
else if (args[iArg+1].equalsIgnoreCase("spat_extent") )
{
iArg ++;
}
else
{
pszClipDstDS = args[iArg+1];
iArg ++;
}
}
else if( args[iArg].equalsIgnoreCase("-clipdstsql") && iArg < args.length-1 )
{
pszClipDstSQL = args[iArg+1];
iArg ++;
}
else if( args[iArg].equalsIgnoreCase("-clipdstlayer") && iArg < args.length-1 )
{
pszClipDstLayer = args[iArg+1];
iArg ++;
}
else if( args[iArg].equalsIgnoreCase("-clipdstwhere") && iArg < args.length-1 )
{
pszClipDstWhere = args[iArg+1];
iArg ++;
}
else if( args[iArg].equalsIgnoreCase("-explodecollections") )
{
bExplodeCollections = true;
}
else if( args[iArg].equalsIgnoreCase("-zfield") && iArg < args.length-1 )
{
pszZField = args[iArg+1];
iArg ++;
}
else if( args[iArg].charAt(0) == '-' )
{
Usage();
}
else if( pszDestDataSource == null )
pszDestDataSource = args[iArg];
else if( pszDataSource == null )
pszDataSource = args[iArg];
else
papszLayers.addElement (args[iArg] );
}
if( pszDataSource == null )
Usage();
if( bPreserveFID && bExplodeCollections )
{
System.err.print("FAILURE: cannot use -preserve_fid and -explodecollections at the same time\n\n" );
Usage();
}
if( bClipSrc && pszClipSrcDS != null)
{
poClipSrc = LoadGeometry(pszClipSrcDS, pszClipSrcSQL, pszClipSrcLayer, pszClipSrcWhere);
if (poClipSrc == null)
{
System.err.print("FAILURE: cannot load source clip geometry\n\n" );
Usage();
}
}
else if( bClipSrc && poClipSrc == null )
{
if (poSpatialFilter != null)
poClipSrc = poSpatialFilter.Clone();
if (poClipSrc == null)
{
System.err.print("FAILURE: -clipsrc must be used with -spat option or a\n" +
"bounding box, WKT string or datasource must be specified\n\n");
Usage();
}
}
if( pszClipDstDS != null)
{
poClipDst = LoadGeometry(pszClipDstDS, pszClipDstSQL, pszClipDstLayer, pszClipDstWhere);
if (poClipDst == null)
{
System.err.print("FAILURE: cannot load dest clip geometry\n\n" );
Usage();
}
}
/* -------------------------------------------------------------------- */
/* Open data source. */
/* -------------------------------------------------------------------- */
DataSource poDS;
poDS = ogr.Open( pszDataSource, false );
/* -------------------------------------------------------------------- */
/* Report failure */
/* -------------------------------------------------------------------- */
if( poDS == null )
{
System.err.println("FAILURE:\n" +
"Unable to open datasource ` " + pszDataSource + "' with the following drivers.");
for( int iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ )
{
System.err.println(" . " + ogr.GetDriver(iDriver).GetName() );
}
System.exit( 1 );
}
/* -------------------------------------------------------------------- */
/* 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 );
}
}