|
1 |
/******************************************************************************
|
|
2 |
* $Id$
|
|
3 |
*
|
|
4 |
* Project: OpenGIS Simple Features Reference Implementation
|
|
5 |
* Purpose: Java port of a simple client for translating between formats.
|
|
6 |
* Author: Even Rouault, <even dot rouault at mines dash paris dot org>
|
|
7 |
*
|
|
8 |
* Port from ogr2ogr.cpp by Frank Warmerdam
|
|
9 |
*
|
|
10 |
******************************************************************************
|
|
11 |
* Copyright (c) 2009, Even Rouault
|
|
12 |
* Copyright (c) 1999, Frank Warmerdam
|
|
13 |
*
|
|
14 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
15 |
* copy of this software and associated documentation files (the "Software"),
|
|
16 |
* to deal in the Software without restriction, including without limitation
|
|
17 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
18 |
* and/or sell copies of the Software, and to permit persons to whom the
|
|
19 |
* Software is furnished to do so, subject to the following conditions:
|
|
20 |
*
|
|
21 |
* The above copyright notice and this permission notice shall be included
|
|
22 |
* in all copies or substantial portions of the Software.
|
|
23 |
*
|
|
24 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
25 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
26 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
27 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
28 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
29 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
30 |
* DEALINGS IN THE SOFTWARE.
|
|
31 |
****************************************************************************/
|
|
32 |
package org.gvsig.gdal.app.mainplugin.common;
|
|
33 |
|
|
34 |
import java.io.File;
|
|
35 |
import java.util.Vector;
|
|
36 |
import java.util.Enumeration;
|
|
37 |
import java.util.StringTokenizer;
|
|
38 |
|
|
39 |
import org.gdal.gdal.gdal;
|
|
40 |
import org.gdal.gdal.ProgressCallback;
|
|
41 |
import org.gdal.gdal.TermProgressCallback;
|
|
42 |
import org.gdal.ogr.ogr;
|
|
43 |
import org.gdal.ogr.ogrConstants;
|
|
44 |
import org.gdal.ogr.Driver;
|
|
45 |
import org.gdal.ogr.DataSource;
|
|
46 |
import org.gdal.ogr.Layer;
|
|
47 |
import org.gdal.ogr.Feature;
|
|
48 |
import org.gdal.ogr.FeatureDefn;
|
|
49 |
import org.gdal.ogr.FieldDefn;
|
|
50 |
import org.gdal.ogr.Geometry;
|
|
51 |
import org.gdal.osr.SpatialReference;
|
|
52 |
import org.gdal.osr.CoordinateTransformation;
|
|
53 |
|
|
54 |
/* Note : this is the most direct port of ogr2ogr.cpp possible */
|
|
55 |
/* It could be made much more java'ish ! */
|
|
56 |
|
|
57 |
class GDALScaledProgress extends ProgressCallback
|
|
58 |
{
|
|
59 |
private double pctMin;
|
|
60 |
private double pctMax;
|
|
61 |
private ProgressCallback mainCbk;
|
|
62 |
|
|
63 |
public GDALScaledProgress(double pctMin, double pctMax,
|
|
64 |
ProgressCallback mainCbk)
|
|
65 |
{
|
|
66 |
this.pctMin = pctMin;
|
|
67 |
this.pctMax = pctMax;
|
|
68 |
this.mainCbk = mainCbk;
|
|
69 |
}
|
|
70 |
|
|
71 |
public int run(double dfComplete, String message)
|
|
72 |
{
|
|
73 |
return mainCbk.run(pctMin + dfComplete * (pctMax - pctMin), message);
|
|
74 |
}
|
|
75 |
};
|
|
76 |
|
|
77 |
|
|
78 |
public class ogr2ogr
|
|
79 |
{
|
|
80 |
static boolean bSkipFailures = false;
|
|
81 |
static int nGroupTransactions = 200;
|
|
82 |
static boolean bPreserveFID = false;
|
|
83 |
static final int OGRNullFID = -1;
|
|
84 |
static int nFIDToFetch = OGRNullFID;
|
|
85 |
|
|
86 |
static class GeomOperation
|
|
87 |
{
|
|
88 |
private GeomOperation() {}
|
|
89 |
public static GeomOperation NONE = new GeomOperation();
|
|
90 |
public static GeomOperation SEGMENTIZE = new GeomOperation();
|
|
91 |
public static GeomOperation SIMPLIFY_PRESERVE_TOPOLOGY = new GeomOperation();
|
|
92 |
}
|
|
93 |
|
|
94 |
/************************************************************************/
|
|
95 |
/* main() */
|
|
96 |
/************************************************************************/
|
|
97 |
|
|
98 |
public static class ExitException extends RuntimeException {
|
|
99 |
public int status = 0;
|
|
100 |
|
|
101 |
public ExitException(int status) {
|
|
102 |
super();
|
|
103 |
this.status = status;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
public static void System_exit(int status) {
|
|
108 |
throw new ExitException(status);
|
|
109 |
}
|
|
110 |
|
|
111 |
|
|
112 |
public static void main(String[] args)
|
|
113 |
{
|
|
114 |
String pszFormat = "ESRI Shapefile";
|
|
115 |
String pszDataSource = null;
|
|
116 |
String pszDestDataSource = null;
|
|
117 |
Vector papszLayers = new Vector();
|
|
118 |
Vector papszDSCO = new Vector(), papszLCO = new Vector();
|
|
119 |
boolean bTransform = false;
|
|
120 |
boolean bAppend = false, bUpdate = false, bOverwrite = false;
|
|
121 |
String pszOutputSRSDef = null;
|
|
122 |
String pszSourceSRSDef = null;
|
|
123 |
SpatialReference poOutputSRS = null;
|
|
124 |
SpatialReference poSourceSRS = null;
|
|
125 |
String pszNewLayerName = null;
|
|
126 |
String pszWHERE = null;
|
|
127 |
Geometry poSpatialFilter = null;
|
|
128 |
String pszSelect;
|
|
129 |
Vector papszSelFields = null;
|
|
130 |
String pszSQLStatement = null;
|
|
131 |
int eGType = -2;
|
|
132 |
GeomOperation eGeomOp = GeomOperation.NONE;
|
|
133 |
double dfGeomOpParam = 0;
|
|
134 |
Vector papszFieldTypesToString = new Vector();
|
|
135 |
boolean bDisplayProgress = false;
|
|
136 |
ProgressCallback pfnProgress = null;
|
|
137 |
boolean bClipSrc = false;
|
|
138 |
Geometry poClipSrc = null;
|
|
139 |
String pszClipSrcDS = null;
|
|
140 |
String pszClipSrcSQL = null;
|
|
141 |
String pszClipSrcLayer = null;
|
|
142 |
String pszClipSrcWhere = null;
|
|
143 |
Geometry poClipDst = null;
|
|
144 |
String pszClipDstDS = null;
|
|
145 |
String pszClipDstSQL = null;
|
|
146 |
String pszClipDstLayer = null;
|
|
147 |
String pszClipDstWhere = null;
|
|
148 |
String pszSrcEncoding = null;
|
|
149 |
String pszDstEncoding = null;
|
|
150 |
boolean bExplodeCollections = false;
|
|
151 |
String pszZField = null;
|
|
152 |
DataSource poODS = null;
|
|
153 |
Driver poDriver = null;
|
|
154 |
DataSource poDS = null;
|
|
155 |
|
|
156 |
try {
|
|
157 |
ogr.DontUseExceptions();
|
|
158 |
|
|
159 |
/* -------------------------------------------------------------------- */
|
|
160 |
/* Register format(s). */
|
|
161 |
/* -------------------------------------------------------------------- */
|
|
162 |
if( ogr.GetDriverCount() == 0 )
|
|
163 |
ogr.RegisterAll();
|
|
164 |
|
|
165 |
/* -------------------------------------------------------------------- */
|
|
166 |
/* Processing command line arguments. */
|
|
167 |
/* -------------------------------------------------------------------- */
|
|
168 |
args = ogr.GeneralCmdLineProcessor( args );
|
|
169 |
|
|
170 |
if( args.length < 2 )
|
|
171 |
{
|
|
172 |
Usage();
|
|
173 |
System_exit( -1 );
|
|
174 |
}
|
|
175 |
|
|
176 |
for( int iArg = 0; iArg < args.length; iArg++ )
|
|
177 |
{
|
|
178 |
if( args[iArg].equalsIgnoreCase("-f") && iArg < args.length-1 )
|
|
179 |
{
|
|
180 |
pszFormat = args[++iArg];
|
|
181 |
}
|
|
182 |
else if( args[iArg].equalsIgnoreCase("-dsco") && iArg < args.length-1 )
|
|
183 |
{
|
|
184 |
papszDSCO.addElement(args[++iArg] );
|
|
185 |
}
|
|
186 |
else if( args[iArg].equalsIgnoreCase("-lco") && iArg < args.length-1 )
|
|
187 |
{
|
|
188 |
papszLCO.addElement(args[++iArg] );
|
|
189 |
}
|
|
190 |
else if( args[iArg].equalsIgnoreCase("-preserve_fid") )
|
|
191 |
{
|
|
192 |
bPreserveFID = true;
|
|
193 |
}
|
|
194 |
else if( args[iArg].length() >= 5 && args[iArg].substring(0, 5).equalsIgnoreCase("-skip") )
|
|
195 |
{
|
|
196 |
bSkipFailures = true;
|
|
197 |
nGroupTransactions = 1; /* #2409 */
|
|
198 |
}
|
|
199 |
else if( args[iArg].equalsIgnoreCase("-append") )
|
|
200 |
{
|
|
201 |
bAppend = true;
|
|
202 |
bUpdate = true;
|
|
203 |
}
|
|
204 |
else if( args[iArg].equalsIgnoreCase("-overwrite") )
|
|
205 |
{
|
|
206 |
bOverwrite = true;
|
|
207 |
bUpdate = true;
|
|
208 |
}
|
|
209 |
else if( args[iArg].equalsIgnoreCase("-update") )
|
|
210 |
{
|
|
211 |
bUpdate = true;
|
|
212 |
}
|
|
213 |
else if( args[iArg].equalsIgnoreCase("-fid") && args[iArg+1] != null )
|
|
214 |
{
|
|
215 |
nFIDToFetch = Integer.parseInt(args[++iArg]);
|
|
216 |
}
|
|
217 |
else if( args[iArg].equalsIgnoreCase("-sql") && args[iArg+1] != null )
|
|
218 |
{
|
|
219 |
pszSQLStatement = args[++iArg];
|
|
220 |
}
|
|
221 |
else if( args[iArg].equalsIgnoreCase("-nln") && iArg < args.length-1 )
|
|
222 |
{
|
|
223 |
pszNewLayerName = args[++iArg];
|
|
224 |
}
|
|
225 |
else if( args[iArg].equalsIgnoreCase("-nlt") && iArg < args.length-1 )
|
|
226 |
{
|
|
227 |
if( args[iArg+1].equalsIgnoreCase("NONE") )
|
|
228 |
eGType = ogr.wkbNone;
|
|
229 |
else if( args[iArg+1].equalsIgnoreCase("GEOMETRY") )
|
|
230 |
eGType = ogr.wkbUnknown;
|
|
231 |
else if( args[iArg+1].equalsIgnoreCase("POINT") )
|
|
232 |
eGType = ogr.wkbPoint;
|
|
233 |
else if( args[iArg+1].equalsIgnoreCase("LINESTRING") )
|
|
234 |
eGType = ogr.wkbLineString;
|
|
235 |
else if( args[iArg+1].equalsIgnoreCase("POLYGON") )
|
|
236 |
eGType = ogr.wkbPolygon;
|
|
237 |
else if( args[iArg+1].equalsIgnoreCase("GEOMETRYCOLLECTION") )
|
|
238 |
eGType = ogr.wkbGeometryCollection;
|
|
239 |
else if( args[iArg+1].equalsIgnoreCase("MULTIPOINT") )
|
|
240 |
eGType = ogr.wkbMultiPoint;
|
|
241 |
else if( args[iArg+1].equalsIgnoreCase("MULTILINESTRING") )
|
|
242 |
eGType = ogr.wkbMultiLineString;
|
|
243 |
else if( args[iArg+1].equalsIgnoreCase("MULTIPOLYGON") )
|
|
244 |
eGType = ogr.wkbMultiPolygon;
|
|
245 |
else if( args[iArg+1].equalsIgnoreCase("GEOMETRY25D") )
|
|
246 |
eGType = ogr.wkbUnknown | ogr.wkb25DBit;
|
|
247 |
else if( args[iArg+1].equalsIgnoreCase("POINT25D") )
|
|
248 |
eGType = ogr.wkbPoint25D;
|
|
249 |
else if( args[iArg+1].equalsIgnoreCase("LINESTRING25D") )
|
|
250 |
eGType = ogr.wkbLineString25D;
|
|
251 |
else if( args[iArg+1].equalsIgnoreCase("POLYGON25D") )
|
|
252 |
eGType = ogr.wkbPolygon25D;
|
|
253 |
else if( args[iArg+1].equalsIgnoreCase("GEOMETRYCOLLECTION25D") )
|
|
254 |
eGType = ogr.wkbGeometryCollection25D;
|
|
255 |
else if( args[iArg+1].equalsIgnoreCase("MULTIPOINT25D") )
|
|
256 |
eGType = ogr.wkbMultiPoint25D;
|
|
257 |
else if( args[iArg+1].equalsIgnoreCase("MULTILINESTRING25D") )
|
|
258 |
eGType = ogr.wkbMultiLineString25D;
|
|
259 |
else if( args[iArg+1].equalsIgnoreCase("MULTIPOLYGON25D") )
|
|
260 |
eGType = ogr.wkbMultiPolygon25D;
|
|
261 |
else
|
|
262 |
{
|
|
263 |
System.err.println("-nlt " + args[iArg+1] + ": type not recognised.");
|
|
264 |
System_exit( 1 );
|
|
265 |
}
|
|
266 |
iArg++;
|
|
267 |
}
|
|
268 |
else if( (args[iArg].equalsIgnoreCase("-tg") ||
|
|
269 |
args[iArg].equalsIgnoreCase("-gt")) && iArg < args.length-1 )
|
|
270 |
{
|
|
271 |
nGroupTransactions = Integer.parseInt(args[++iArg]);
|
|
272 |
}
|
|
273 |
else if( args[iArg].equalsIgnoreCase("-s_srs") && iArg < args.length-1 )
|
|
274 |
{
|
|
275 |
pszSourceSRSDef = args[++iArg];
|
|
276 |
}
|
|
277 |
else if( args[iArg].equalsIgnoreCase("-a_srs") && iArg < args.length-1 )
|
|
278 |
{
|
|
279 |
pszOutputSRSDef = args[++iArg];
|
|
280 |
}
|
|
281 |
else if( args[iArg].equalsIgnoreCase("-t_srs") && iArg < args.length-1 )
|
|
282 |
{
|
|
283 |
pszOutputSRSDef = args[++iArg];
|
|
284 |
bTransform = true;
|
|
285 |
}
|
|
286 |
else if (args[iArg].equalsIgnoreCase("-spat") && iArg + 4 < args.length)
|
|
287 |
{
|
|
288 |
Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
|
|
289 |
double xmin = new Double(args[++iArg]).doubleValue();
|
|
290 |
double ymin = new Double(args[++iArg]).doubleValue();
|
|
291 |
double xmax = new Double(args[++iArg]).doubleValue();
|
|
292 |
double ymax = new Double(args[++iArg]).doubleValue();
|
|
293 |
oRing.AddPoint(xmin, ymin);
|
|
294 |
oRing.AddPoint(xmin, ymax);
|
|
295 |
oRing.AddPoint(xmax, ymax);
|
|
296 |
oRing.AddPoint(xmax, ymin);
|
|
297 |
oRing.AddPoint(xmin, ymin);
|
|
298 |
|
|
299 |
poSpatialFilter = new Geometry(ogrConstants.wkbPolygon);
|
|
300 |
poSpatialFilter.AddGeometry(oRing);
|
|
301 |
}
|
|
302 |
else if( args[iArg].equalsIgnoreCase("-where") && args[iArg+1] != null )
|
|
303 |
{
|
|
304 |
pszWHERE = args[++iArg];
|
|
305 |
}
|
|
306 |
else if( args[iArg].equalsIgnoreCase("-select") && args[iArg+1] != null)
|
|
307 |
{
|
|
308 |
pszSelect = args[++iArg];
|
|
309 |
StringTokenizer tokenizer = new StringTokenizer(pszSelect, " ,");
|
|
310 |
papszSelFields = new Vector();
|
|
311 |
while(tokenizer.hasMoreElements())
|
|
312 |
papszSelFields.addElement(tokenizer.nextToken());
|
|
313 |
}
|
|
314 |
else if( args[iArg].equalsIgnoreCase("-simplify") && iArg < args.length-1 )
|
|
315 |
{
|
|
316 |
eGeomOp = GeomOperation.SIMPLIFY_PRESERVE_TOPOLOGY;
|
|
317 |
dfGeomOpParam = new Double(args[++iArg]).doubleValue();
|
|
318 |
}
|
|
319 |
else if( args[iArg].equalsIgnoreCase("-segmentize") && iArg < args.length-1 )
|
|
320 |
{
|
|
321 |
eGeomOp = GeomOperation.SEGMENTIZE;
|
|
322 |
dfGeomOpParam = new Double(args[++iArg]).doubleValue();
|
|
323 |
}
|
|
324 |
else if( args[iArg].equalsIgnoreCase("-fieldTypeToString") && iArg < args.length-1 )
|
|
325 |
{
|
|
326 |
StringTokenizer tokenizer = new StringTokenizer(args[++iArg], " ,");
|
|
327 |
while(tokenizer.hasMoreElements())
|
|
328 |
{
|
|
329 |
String token = (String)tokenizer.nextToken();
|
|
330 |
if (token.equalsIgnoreCase("Integer") ||
|
|
331 |
token.equalsIgnoreCase("Real") ||
|
|
332 |
token.equalsIgnoreCase("String") ||
|
|
333 |
token.equalsIgnoreCase("Date") ||
|
|
334 |
token.equalsIgnoreCase("Time") ||
|
|
335 |
token.equalsIgnoreCase("DateTime") ||
|
|
336 |
token.equalsIgnoreCase("Binary") ||
|
|
337 |
token.equalsIgnoreCase("IntegerList") ||
|
|
338 |
token.equalsIgnoreCase("RealList") ||
|
|
339 |
token.equalsIgnoreCase("StringList"))
|
|
340 |
{
|
|
341 |
papszFieldTypesToString.addElement(token);
|
|
342 |
}
|
|
343 |
else if (token.equalsIgnoreCase("All"))
|
|
344 |
{
|
|
345 |
papszFieldTypesToString = null;
|
|
346 |
papszFieldTypesToString.addElement("All");
|
|
347 |
break;
|
|
348 |
}
|
|
349 |
else
|
|
350 |
{
|
|
351 |
System.err.println("Unhandled type for fieldtypeasstring option : " + token);
|
|
352 |
Usage();
|
|
353 |
}
|
|
354 |
}
|
|
355 |
}
|
|
356 |
else if( args[iArg].equalsIgnoreCase("-progress") )
|
|
357 |
{
|
|
358 |
bDisplayProgress = true;
|
|
359 |
}
|
|
360 |
/*else if( args[iArg].equalsIgnoreCase("-wrapdateline") )
|
|
361 |
{
|
|
362 |
bWrapDateline = true;
|
|
363 |
}
|
|
364 |
*/
|
|
365 |
else if( args[iArg].equalsIgnoreCase("-clipsrc") && iArg < args.length-1 )
|
|
366 |
{
|
|
367 |
bClipSrc = true;
|
|
368 |
if ( IsNumber(args[iArg+1]) && iArg < args.length - 4 )
|
|
369 |
{
|
|
370 |
Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
|
|
371 |
double xmin = new Double(args[++iArg]).doubleValue();
|
|
372 |
double ymin = new Double(args[++iArg]).doubleValue();
|
|
373 |
double xmax = new Double(args[++iArg]).doubleValue();
|
|
374 |
double ymax = new Double(args[++iArg]).doubleValue();
|
|
375 |
oRing.AddPoint(xmin, ymin);
|
|
376 |
oRing.AddPoint(xmin, ymax);
|
|
377 |
oRing.AddPoint(xmax, ymax);
|
|
378 |
oRing.AddPoint(xmax, ymin);
|
|
379 |
oRing.AddPoint(xmin, ymin);
|
|
380 |
|
|
381 |
poClipSrc = new Geometry(ogrConstants.wkbPolygon);
|
|
382 |
poClipSrc.AddGeometry(oRing);
|
|
383 |
}
|
|
384 |
else if( (args[iArg+1].length() >= 7 && args[iArg+1].substring(0, 7).equalsIgnoreCase("POLYGON") ) ||
|
|
385 |
(args[iArg+1].length() >= 12 && args[iArg+1].substring(0, 12).equalsIgnoreCase("MULTIPOLYGON") ) )
|
|
386 |
{
|
|
387 |
poClipSrc = Geometry.CreateFromWkt(args[iArg+1]);
|
|
388 |
if (poClipSrc == null)
|
|
389 |
{
|
|
390 |
System.err.print("FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n\n");
|
|
391 |
Usage();
|
|
392 |
}
|
|
393 |
iArg ++;
|
|
394 |
}
|
|
395 |
else if (args[iArg+1].equalsIgnoreCase("spat_extent") )
|
|
396 |
{
|
|
397 |
iArg ++;
|
|
398 |
}
|
|
399 |
else
|
|
400 |
{
|
|
401 |
pszClipSrcDS = args[iArg+1];
|
|
402 |
iArg ++;
|
|
403 |
}
|
|
404 |
}
|
|
405 |
else if( args[iArg].equalsIgnoreCase("-clipsrcsql") && iArg < args.length-1 )
|
|
406 |
{
|
|
407 |
pszClipSrcSQL = args[iArg+1];
|
|
408 |
iArg ++;
|
|
409 |
}
|
|
410 |
else if( args[iArg].equalsIgnoreCase("-clipsrclayer") && iArg < args.length-1 )
|
|
411 |
{
|
|
412 |
pszClipSrcLayer = args[iArg+1];
|
|
413 |
iArg ++;
|
|
414 |
}
|
|
415 |
else if( args[iArg].equalsIgnoreCase("-clipsrcwhere") && iArg < args.length-1 )
|
|
416 |
{
|
|
417 |
pszClipSrcWhere = args[iArg+1];
|
|
418 |
iArg ++;
|
|
419 |
}
|
|
420 |
else if( args[iArg].equalsIgnoreCase("-clipdst") && iArg < args.length-1 )
|
|
421 |
{
|
|
422 |
if ( IsNumber(args[iArg+1]) && iArg < args.length - 4 )
|
|
423 |
{
|
|
424 |
Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
|
|
425 |
double xmin = new Double(args[++iArg]).doubleValue();
|
|
426 |
double ymin = new Double(args[++iArg]).doubleValue();
|
|
427 |
double xmax = new Double(args[++iArg]).doubleValue();
|
|
428 |
double ymax = new Double(args[++iArg]).doubleValue();
|
|
429 |
oRing.AddPoint(xmin, ymin);
|
|
430 |
oRing.AddPoint(xmin, ymax);
|
|
431 |
oRing.AddPoint(xmax, ymax);
|
|
432 |
oRing.AddPoint(xmax, ymin);
|
|
433 |
oRing.AddPoint(xmin, ymin);
|
|
434 |
|
|
435 |
poClipDst = new Geometry(ogrConstants.wkbPolygon);
|
|
436 |
poClipDst.AddGeometry(oRing);
|
|
437 |
}
|
|
438 |
else if( (args[iArg+1].length() >= 7 && args[iArg+1].substring(0, 7).equalsIgnoreCase("POLYGON") ) ||
|
|
439 |
(args[iArg+1].length() >= 12 && args[iArg+1].substring(0, 12).equalsIgnoreCase("MULTIPOLYGON") ) )
|
|
440 |
{
|
|
441 |
poClipDst = Geometry.CreateFromWkt(args[iArg+1]);
|
|
442 |
if (poClipDst == null)
|
|
443 |
{
|
|
444 |
System.err.print("FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n\n");
|
|
445 |
Usage();
|
|
446 |
}
|
|
447 |
iArg ++;
|
|
448 |
}
|
|
449 |
else if (args[iArg+1].equalsIgnoreCase("spat_extent") )
|
|
450 |
{
|
|
451 |
iArg ++;
|
|
452 |
}
|
|
453 |
else
|
|
454 |
{
|
|
455 |
pszClipDstDS = args[iArg+1];
|
|
456 |
iArg ++;
|
|
457 |
}
|
|
458 |
}
|
|
459 |
else if( args[iArg].equalsIgnoreCase("-clipdstsql") && iArg < args.length-1 )
|
|
460 |
{
|
|
461 |
pszClipDstSQL = args[iArg+1];
|
|
462 |
iArg ++;
|
|
463 |
}
|
|
464 |
else if( args[iArg].equalsIgnoreCase("-clipdstlayer") && iArg < args.length-1 )
|
|
465 |
{
|
|
466 |
pszClipDstLayer = args[iArg+1];
|
|
467 |
iArg ++;
|
|
468 |
}
|
|
469 |
else if( args[iArg].equalsIgnoreCase("-clipdstwhere") && iArg < args.length-1 )
|
|
470 |
{
|
|
471 |
pszClipDstWhere = args[iArg+1];
|
|
472 |
iArg ++;
|
|
473 |
}
|
|
474 |
else if( args[iArg].equalsIgnoreCase("-explodecollections") )
|
|
475 |
{
|
|
476 |
bExplodeCollections = true;
|
|
477 |
}
|
|
478 |
else if( args[iArg].equalsIgnoreCase("-zfield") && iArg < args.length-1 )
|
|
479 |
{
|
|
480 |
pszZField = args[iArg+1];
|
|
481 |
iArg ++;
|
|
482 |
}
|
|
483 |
else if( args[iArg].charAt(0) == '-' )
|
|
484 |
{
|
|
485 |
Usage();
|
|
486 |
}
|
|
487 |
else if( pszDestDataSource == null )
|
|
488 |
pszDestDataSource = args[iArg];
|
|
489 |
else if( pszDataSource == null )
|
|
490 |
pszDataSource = args[iArg];
|
|
491 |
else
|
|
492 |
papszLayers.addElement (args[iArg] );
|
|
493 |
}
|
|
494 |
|
|
495 |
if( pszDataSource == null )
|
|
496 |
Usage();
|
|
497 |
|
|
498 |
if( bPreserveFID && bExplodeCollections )
|
|
499 |
{
|
|
500 |
System.err.print("FAILURE: cannot use -preserve_fid and -explodecollections at the same time\n\n" );
|
|
501 |
Usage();
|
|
502 |
}
|
|
503 |
|
|
504 |
if( bClipSrc && pszClipSrcDS != null)
|
|
505 |
{
|
|
506 |
poClipSrc = LoadGeometry(pszClipSrcDS, pszClipSrcSQL, pszClipSrcLayer, pszClipSrcWhere);
|
|
507 |
if (poClipSrc == null)
|
|
508 |
{
|
|
509 |
System.err.print("FAILURE: cannot load source clip geometry\n\n" );
|
|
510 |
Usage();
|
|
511 |
}
|
|
512 |
}
|
|
513 |
else if( bClipSrc && poClipSrc == null )
|
|
514 |
{
|
|
515 |
if (poSpatialFilter != null)
|
|
516 |
poClipSrc = poSpatialFilter.Clone();
|
|
517 |
if (poClipSrc == null)
|
|
518 |
{
|
|
519 |
System.err.print("FAILURE: -clipsrc must be used with -spat option or a\n" +
|
|
520 |
"bounding box, WKT string or datasource must be specified\n\n");
|
|
521 |
Usage();
|
|
522 |
}
|
|
523 |
}
|
|
524 |
|
|
525 |
if( pszClipDstDS != null)
|
|
526 |
{
|
|
527 |
poClipDst = LoadGeometry(pszClipDstDS, pszClipDstSQL, pszClipDstLayer, pszClipDstWhere);
|
|
528 |
if (poClipDst == null)
|
|
529 |
{
|
|
530 |
System.err.print("FAILURE: cannot load dest clip geometry\n\n" );
|
|
531 |
Usage();
|
|
532 |
}
|
|
533 |
}
|
|
534 |
/* -------------------------------------------------------------------- */
|
|
535 |
/* Open data source. */
|
|
536 |
/* -------------------------------------------------------------------- */
|
|
537 |
|
|
538 |
poDS = ogr.Open( pszDataSource, false );
|
|
539 |
|
|
540 |
/* -------------------------------------------------------------------- */
|
|
541 |
/* Report failure */
|
|
542 |
/* -------------------------------------------------------------------- */
|
|
543 |
if( poDS == null )
|
|
544 |
{
|
|
545 |
System.err.println("FAILURE:\n" +
|
|
546 |
"Unable to open datasource ` " + pszDataSource + "' with the following drivers.");
|
|
547 |
|
|
548 |
for( int iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ )
|
|
549 |
{
|
|
550 |
System.err.println(" . " + ogr.GetDriver(iDriver).GetName() );
|
|
551 |
}
|
|
552 |
|
|
553 |
System_exit( 1 );
|
|
554 |
}
|
|
555 |
|
|
556 |
/* -------------------------------------------------------------------- */
|
|
557 |
/* Try opening the output datasource as an existing, writable */
|
|
558 |
/* -------------------------------------------------------------------- */
|
|
559 |
|
|
560 |
|
|
561 |
if( bUpdate )
|
|
562 |
{
|
|
563 |
poODS = ogr.Open( pszDestDataSource, true );
|
|
564 |
if( poODS == null )
|
|
565 |
{
|
|
566 |
if (bOverwrite || bAppend)
|
|
567 |
{
|
|
568 |
poODS = ogr.Open( pszDestDataSource, false );
|
|
569 |
if ( poODS == null )
|
|
570 |
{
|
|
571 |
/* ok the datasource doesn't exist at all */
|
|
572 |
bUpdate = false;
|
|
573 |
}
|
|
574 |
else
|
|
575 |
{
|
|
576 |
poODS.delete();
|
|
577 |
poODS = null;
|
|
578 |
}
|
|
579 |
}
|
|
580 |
|
|
581 |
if (bUpdate)
|
|
582 |
{
|
|
583 |
System.err.println("FAILURE:\n" +
|
|
584 |
"Unable to open existing output datasource `" + pszDestDataSource + "'.");
|
|
585 |
System_exit( 1 );
|
|
586 |
}
|
|
587 |
}
|
|
588 |
|
|
589 |
else if( papszDSCO.size() > 0 )
|
|
590 |
{
|
|
591 |
System.err.println("WARNING: Datasource creation options ignored since an existing datasource\n" +
|
|
592 |
" being updated." );
|
|
593 |
}
|
|
594 |
|
|
595 |
if (poODS != null)
|
|
596 |
poDriver = poODS.GetDriver();
|
|
597 |
}
|
|
598 |
|
|
599 |
/* -------------------------------------------------------------------- */
|
|
600 |
/* Find the output driver. */
|
|
601 |
/* -------------------------------------------------------------------- */
|
|
602 |
if( !bUpdate )
|
|
603 |
{
|
|
604 |
int iDriver;
|
|
605 |
|
|
606 |
poDriver = ogr.GetDriverByName(pszFormat);
|
|
607 |
if( poDriver == null )
|
|
608 |
{
|
|
609 |
System.err.println("Unable to find driver `" + pszFormat +"'." );
|
|
610 |
System.err.println( "The following drivers are available:" );
|
|
611 |
|
|
612 |
for( iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ )
|
|
613 |
{
|
|
614 |
System.err.println(" . " + ogr.GetDriver(iDriver).GetName() );
|
|
615 |
}
|
|
616 |
System_exit( 1 );
|
|
617 |
}
|
|
618 |
|
|
619 |
if( poDriver.TestCapability( ogr.ODrCCreateDataSource ) == false )
|
|
620 |
{
|
|
621 |
System.err.println( pszFormat + " driver does not support data source creation.");
|
|
622 |
System_exit( 1 );
|
|
623 |
}
|
|
624 |
|
|
625 |
/* -------------------------------------------------------------------- */
|
|
626 |
/* Special case to improve user experience when translating */
|
|
627 |
/* a datasource with multiple layers into a shapefile. If the */
|
|
628 |
/* user gives a target datasource with .shp and it does not exist, */
|
|
629 |
/* the shapefile driver will try to create a file, but this is not */
|
|
630 |
/* appropriate because here we have several layers, so create */
|
|
631 |
/* a directory instead. */
|
|
632 |
/* -------------------------------------------------------------------- */
|
|
633 |
if (poDriver.GetName().equalsIgnoreCase("ESRI Shapefile") &&
|
|
634 |
pszSQLStatement == null &&
|
|
635 |
(papszLayers.size() > 1 ||
|
|
636 |
(papszLayers.size() == 0 && poDS.GetLayerCount() > 1)) &&
|
|
637 |
pszNewLayerName == null &&
|
|
638 |
(pszDestDataSource.endsWith(".shp") || pszDestDataSource.endsWith(".SHP")))
|
|
639 |
{
|
|
640 |
File f = new File(pszDestDataSource);
|
|
641 |
if (!f.exists())
|
|
642 |
{
|
|
643 |
if (!f.mkdir())
|
|
644 |
{
|
|
645 |
System.err.println(
|
|
646 |
"Failed to create directory " + pszDestDataSource + "\n" +
|
|
647 |
"for shapefile datastore.");
|
|
648 |
System_exit(1);
|
|
649 |
}
|
|
650 |
}
|
|
651 |
}
|
|
652 |
|
|
653 |
/* -------------------------------------------------------------------- */
|
|
654 |
/* Create the output data source. */
|
|
655 |
/* -------------------------------------------------------------------- */
|
|
656 |
poODS = poDriver.CreateDataSource( pszDestDataSource, papszDSCO );
|
|
657 |
if( poODS == null )
|
|
658 |
{
|
|
659 |
System.err.println( pszFormat + " driver failed to create "+ pszDestDataSource );
|
|
660 |
System_exit( 1 );
|
|
661 |
}
|
|
662 |
}
|
|
663 |
|
|
664 |
/* -------------------------------------------------------------------- */
|
|
665 |
/* Parse the output SRS definition if possible. */
|
|
666 |
/* -------------------------------------------------------------------- */
|
|
667 |
if( pszOutputSRSDef != null )
|
|
668 |
{
|
|
669 |
poOutputSRS = new SpatialReference();
|
|
670 |
if( poOutputSRS.SetFromUserInput( pszOutputSRSDef ) != 0 )
|
|
671 |
{
|
|
672 |
System.err.println( "Failed to process SRS definition: " + pszOutputSRSDef );
|
|
673 |
System_exit( 1 );
|
|
674 |
}
|
|
675 |
}
|
|
676 |
|
|
677 |
/* -------------------------------------------------------------------- */
|
|
678 |
/* Parse the source SRS definition if possible. */
|
|
679 |
/* -------------------------------------------------------------------- */
|
|
680 |
if( pszSourceSRSDef != null )
|
|
681 |
{
|
|
682 |
poSourceSRS = new SpatialReference();
|
|
683 |
if( poSourceSRS.SetFromUserInput( pszSourceSRSDef ) != 0 )
|
|
684 |
{
|
|
685 |
System.err.println( "Failed to process SRS definition: " + pszSourceSRSDef );
|
|
686 |
System_exit( 1 );
|
|
687 |
}
|
|
688 |
}
|
|
689 |
|
|
690 |
/* -------------------------------------------------------------------- */
|
|
691 |
/* Special case for -sql clause. No source layers required. */
|
|
692 |
/* -------------------------------------------------------------------- */
|
|
693 |
if( pszSQLStatement != null )
|
|
694 |
{
|
|
695 |
Layer poResultSet;
|
|
696 |
|
|
697 |
if( pszWHERE != null )
|
|
698 |
System.err.println( "-where clause ignored in combination with -sql." );
|
|
699 |
if( papszLayers.size() > 0 )
|
|
700 |
System.err.println( "layer names ignored in combination with -sql." );
|
|
701 |
|
|
702 |
poResultSet = poDS.ExecuteSQL( pszSQLStatement, poSpatialFilter,
|
|
703 |
null );
|
|
704 |
|
|
705 |
if( poResultSet != null )
|
|
706 |
{
|
|
707 |
long nCountLayerFeatures = 0;
|
|
708 |
if (bDisplayProgress)
|
|
709 |
{
|
|
710 |
if (!poResultSet.TestCapability(ogr.OLCFastFeatureCount))
|
|
711 |
{
|
|
712 |
System.err.println( "Progress turned off as fast feature count is not available.");
|
|
713 |
bDisplayProgress = false;
|
|
714 |
}
|
|
715 |
else
|
|
716 |
{
|
|
717 |
nCountLayerFeatures = poResultSet.GetFeatureCount();
|
|
718 |
pfnProgress = new TermProgressCallback();
|
|
719 |
}
|
|
720 |
}
|
|
721 |
|
|
722 |
/* -------------------------------------------------------------------- */
|
|
723 |
/* Special case to improve user experience when translating into */
|
|
724 |
/* single file shapefile and source has only one layer, and that */
|
|
725 |
/* the layer name isn't specified */
|
|
726 |
/* -------------------------------------------------------------------- */
|
|
727 |
if (poDriver.GetName().equalsIgnoreCase("ESRI Shapefile") &&
|
|
728 |
pszNewLayerName == null)
|
|
729 |
{
|
|
730 |
File f = new File(pszDestDataSource);
|
|
731 |
if (f.exists() && f.listFiles() == null)
|
|
732 |
{
|
|
733 |
pszNewLayerName = f.getName();
|
|
734 |
int posPoint = pszNewLayerName.lastIndexOf('.');
|
|
735 |
if (posPoint != -1)
|
|
736 |
pszNewLayerName = pszNewLayerName.substring(0, posPoint);
|
|
737 |
}
|
|
738 |
}
|
|
739 |
|
|
740 |
if( !TranslateLayer( poDS, poResultSet, poODS, papszLCO,
|
|
741 |
pszNewLayerName, bTransform, poOutputSRS,
|
|
742 |
poSourceSRS, papszSelFields, bAppend, eGType,
|
|
743 |
bOverwrite, eGeomOp, dfGeomOpParam, papszFieldTypesToString,
|
|
744 |
nCountLayerFeatures, poClipSrc, poClipDst, bExplodeCollections,
|
|
745 |
pszZField, pszWHERE, pfnProgress ))
|
|
746 |
{
|
|
747 |
System.err.println(
|
|
748 |
"Terminating translation prematurely after failed\n" +
|
|
749 |
"translation from sql statement." );
|
|
750 |
|
|
751 |
System_exit( 1 );
|
|
752 |
}
|
|
753 |
poDS.ReleaseResultSet( poResultSet );
|
|
754 |
}
|
|
755 |
}
|
|
756 |
else
|
|
757 |
{
|
|
758 |
int nLayerCount = 0;
|
|
759 |
Layer[] papoLayers = null;
|
|
760 |
|
|
761 |
/* -------------------------------------------------------------------- */
|
|
762 |
/* Process each data source layer. */
|
|
763 |
/* -------------------------------------------------------------------- */
|
|
764 |
if ( papszLayers.size() == 0)
|
|
765 |
{
|
|
766 |
nLayerCount = poDS.GetLayerCount();
|
|
767 |
papoLayers = new Layer[nLayerCount];
|
|
768 |
|
|
769 |
for( int iLayer = 0;
|
|
770 |
iLayer < nLayerCount;
|
|
771 |
iLayer++ )
|
|
772 |
{
|
|
773 |
Layer poLayer = poDS.GetLayer(iLayer);
|
|
774 |
|
|
775 |
if( poLayer == null )
|
|
776 |
{
|
|
777 |
System.err.println("FAILURE: Couldn't fetch advertised layer " + iLayer + "!");
|
|
778 |
System_exit( 1 );
|
|
779 |
}
|
|
780 |
|
|
781 |
papoLayers[iLayer] = poLayer;
|
|
782 |
}
|
|
783 |
}
|
|
784 |
/* -------------------------------------------------------------------- */
|
|
785 |
/* Process specified data source layers. */
|
|
786 |
/* -------------------------------------------------------------------- */
|
|
787 |
else
|
|
788 |
{
|
|
789 |
nLayerCount = papszLayers.size();
|
|
790 |
papoLayers = new Layer[nLayerCount];
|
|
791 |
|
|
792 |
for( int iLayer = 0;
|
|
793 |
iLayer < papszLayers.size();
|
|
794 |
iLayer++ )
|
|
795 |
{
|
|
796 |
Layer poLayer = poDS.GetLayerByName((String)papszLayers.get(iLayer));
|
|
797 |
|
|
798 |
if( poLayer == null )
|
|
799 |
{
|
|
800 |
System.err.println("FAILURE: Couldn't fetch advertised layer " + (String)papszLayers.get(iLayer) + "!");
|
|
801 |
System_exit( 1 );
|
|
802 |
}
|
|
803 |
|
|
804 |
papoLayers[iLayer] = poLayer;
|
|
805 |
}
|
|
806 |
}
|
|
807 |
|
|
808 |
/* -------------------------------------------------------------------- */
|
|
809 |
/* Special case to improve user experience when translating into */
|
|
810 |
/* single file shapefile and source has only one layer, and that */
|
|
811 |
/* the layer name isn't specified */
|
|
812 |
/* -------------------------------------------------------------------- */
|
|
813 |
if (poDriver.GetName().equalsIgnoreCase("ESRI Shapefile") &&
|
|
814 |
nLayerCount == 1 && pszNewLayerName == null)
|
|
815 |
{
|
|
816 |
File f = new File(pszDestDataSource);
|
|
817 |
if (f.exists() && f.listFiles() == null)
|
|
818 |
{
|
|
819 |
pszNewLayerName = f.getName();
|
|
820 |
int posPoint = pszNewLayerName.lastIndexOf('.');
|
|
821 |
if (posPoint != -1)
|
|
822 |
pszNewLayerName = pszNewLayerName.substring(0, posPoint);
|
|
823 |
}
|
|
824 |
}
|
|
825 |
|
|
826 |
long[] panLayerCountFeatures = new long[nLayerCount];
|
|
827 |
long nCountLayersFeatures = 0;
|
|
828 |
long nAccCountFeatures = 0;
|
|
829 |
|
|
830 |
/* First pass to apply filters and count all features if necessary */
|
|
831 |
for( int iLayer = 0;
|
|
832 |
iLayer < nLayerCount;
|
|
833 |
iLayer++ )
|
|
834 |
{
|
|
835 |
Layer poLayer = papoLayers[iLayer];
|
|
836 |
|
|
837 |
if( pszWHERE != null )
|
|
838 |
{
|
|
839 |
if( poLayer.SetAttributeFilter( pszWHERE ) != ogr.OGRERR_NONE )
|
|
840 |
{
|
|
841 |
System.err.println("FAILURE: SetAttributeFilter(" + pszWHERE + ") failed.");
|
|
842 |
if (!bSkipFailures)
|
|
843 |
System_exit( 1 );
|
|
844 |
}
|
|
845 |
}
|
|
846 |
|
|
847 |
if( poSpatialFilter != null )
|
|
848 |
poLayer.SetSpatialFilter( poSpatialFilter );
|
|
849 |
|
|
850 |
if (bDisplayProgress)
|
|
851 |
{
|
|
852 |
if (!poLayer.TestCapability(ogr.OLCFastFeatureCount))
|
|
853 |
{
|
|
854 |
System.err.println("Progress turned off as fast feature count is not available.");
|
|
855 |
bDisplayProgress = false;
|
|
856 |
}
|
|
857 |
else
|
|
858 |
{
|
|
859 |
panLayerCountFeatures[iLayer] = poLayer.GetFeatureCount();
|
|
860 |
nCountLayersFeatures += panLayerCountFeatures[iLayer];
|
|
861 |
}
|
|
862 |
}
|
|
863 |
}
|
|
864 |
|
|
865 |
/* Second pass to do the real job */
|
|
866 |
for( int iLayer = 0;
|
|
867 |
iLayer < nLayerCount;
|
|
868 |
iLayer++ )
|
|
869 |
{
|
|
870 |
Layer poLayer = papoLayers[iLayer];
|
|
871 |
|
|
872 |
if (bDisplayProgress)
|
|
873 |
{
|
|
874 |
pfnProgress = new GDALScaledProgress(
|
|
875 |
nAccCountFeatures * 1.0 / nCountLayersFeatures,
|
|
876 |
(nAccCountFeatures + panLayerCountFeatures[iLayer]) * 1.0 / nCountLayersFeatures,
|
|
877 |
new TermProgressCallback());
|
|
878 |
}
|
|
879 |
|
|
880 |
nAccCountFeatures += panLayerCountFeatures[iLayer];
|
|
881 |
|
|
882 |
if( !TranslateLayer( poDS, poLayer, poODS, papszLCO,
|
|
883 |
pszNewLayerName, bTransform, poOutputSRS,
|
|
884 |
poSourceSRS, papszSelFields, bAppend, eGType,
|
|
885 |
bOverwrite, eGeomOp, dfGeomOpParam, papszFieldTypesToString,
|
|
886 |
panLayerCountFeatures[iLayer], poClipSrc, poClipDst, bExplodeCollections,
|
|
887 |
pszZField, pszWHERE, pfnProgress)
|
|
888 |
&& !bSkipFailures )
|
|
889 |
{
|
|
890 |
System.err.println(
|
|
891 |
"Terminating translation prematurely after failed\n" +
|
|
892 |
"translation of layer " + poLayer.GetLayerDefn().GetName() + " (use -skipfailures to skip errors)");
|
|
893 |
|
|
894 |
System_exit( 1 );
|
|
895 |
}
|
|
896 |
}
|
|
897 |
}
|
|
898 |
|
|
899 |
} catch(ExitException ex) {
|
|
900 |
// Do nothing
|
|
901 |
}
|
|
902 |
/* -------------------------------------------------------------------- */
|
|
903 |
/* Close down. */
|
|
904 |
/* -------------------------------------------------------------------- */
|
|
905 |
/* We must explicitly destroy the output dataset in order the file */
|
|
906 |
/* to be properly closed ! */
|
|
907 |
if( poODS!=null ) {
|
|
908 |
poODS.delete();
|
|
909 |
}
|
|
910 |
if( poDS!=null ) {
|
|
911 |
poDS.delete();
|
|
912 |
}
|
|
913 |
}
|
|
914 |
|
|
915 |
/************************************************************************/
|
|
916 |
/* Usage() */
|
|
917 |
/************************************************************************/
|
|
918 |
|
|
919 |
static void Usage()
|
|
920 |
|
|
921 |
{
|
|
922 |
System.out.print( "Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]\n" +
|
|
923 |
" [-select field_list] [-where restricted_where] \n" +
|
|
924 |
" [-progress] [-sql <sql statement>] \n" +
|
|
925 |
" [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n" +
|
|
926 |
" [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n" +
|
|
927 |
" [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]\n" +
|
|
928 |
" [-simplify tolerance]\n" +
|
|
929 |
// " [-segmentize max_dist] [-fieldTypeToString All|(type1[,type2]*)]\n" +
|
|
930 |
" [-fieldTypeToString All|(type1[,type2]*)] [-explodecollections]\n" +
|
|
931 |
" dst_datasource_name src_datasource_name\n" +
|
|
932 |
" [-lco NAME=VALUE] [-nln name] [-nlt type] [layer [layer ...]]\n" +
|
|
933 |
"\n" +
|
|
934 |
" -f format_name: output file format name, possible values are:\n");
|
|
935 |
|
|
936 |
for( int iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ )
|
|
937 |
{
|
|
938 |
Driver poDriver = ogr.GetDriver(iDriver);
|
|
939 |
|
|
940 |
if( poDriver.TestCapability( ogr.ODrCCreateDataSource ) )
|
|
941 |
System.out.print( " -f \"" + poDriver.GetName() + "\"\n" );
|
|
942 |
}
|
|
943 |
|
|
944 |
System.out.print( " -append: Append to existing layer instead of creating new if it exists\n" +
|
|
945 |
" -overwrite: delete the output layer and recreate it empty\n" +
|
|
946 |
" -update: Open existing output datasource in update mode\n" +
|
|
947 |
" -progress: Display progress on terminal. Only works if input layers have the \"fast feature count\" capability\n" +
|
|
948 |
" -select field_list: Comma-delimited list of fields from input layer to\n" +
|
|
949 |
" copy to the new layer (defaults to all)\n" +
|
|
950 |
" -where restricted_where: Attribute query (like SQL WHERE)\n" +
|
|
951 |
" -sql statement: Execute given SQL statement and save result.\n" +
|
|
952 |
" -skipfailures: skip features or layers that fail to convert\n" +
|
|
953 |
" -gt n: group n features per transaction (default 200)\n" +
|
|
954 |
" -spat xmin ymin xmax ymax: spatial query extents\n" +
|
|
955 |
" -simplify tolerance: distance tolerance for simplification.\n" +
|
|
956 |
//" -segmentize max_dist: maximum distance between 2 nodes.\n" +
|
|
957 |
//" Used to create intermediate points\n" +
|
|
958 |
" -dsco NAME=VALUE: Dataset creation option (format specific)\n" +
|
|
959 |
" -lco NAME=VALUE: Layer creation option (format specific)\n" +
|
|
960 |
" -nln name: Assign an alternate name to the new layer\n" +
|
|
961 |
" -nlt type: Force a geometry type for new layer. One of NONE, GEOMETRY,\n" +
|
|
962 |
" POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT,\n" +
|
|
963 |
" MULTIPOLYGON, or MULTILINESTRING. Add \"25D\" for 3D layers.\n" +
|
|
964 |
" Default is type of source layer.\n" +
|
|
965 |
" -fieldTypeToString type1,...: Converts fields of specified types to\n" +
|
|
966 |
" fields of type string in the new layer. Valid types are : \n" +
|
|
967 |
" Integer, Real, String, Date, Time, DateTime, Binary, IntegerList, RealList,\n" +
|
|
968 |
" StringList. Special value All can be used to convert all fields to strings.\n");
|
|
969 |
|
|
970 |
System.out.print(" -a_srs srs_def: Assign an output SRS\n" +
|
|
971 |
" -t_srs srs_def: Reproject/transform to this SRS on output\n" +
|
|
972 |
" -s_srs srs_def: Override source SRS\n" +
|
|
973 |
"\n" +
|
|
974 |
" Srs_def can be a full WKT definition (hard to escape properly),\n" +
|
|
975 |
" or a well known definition (i.e. EPSG:4326) or a file with a WKT\n" +
|
|
976 |
" definition.\n" );
|
|
977 |
|
|
978 |
System_exit( 1 );
|
|
979 |
}
|
|
980 |
|
|
981 |
static int CSLFindString(Vector v, String str)
|
|
982 |
{
|
|
983 |
int i = 0;
|
|
984 |
Enumeration e = v.elements();
|
|
985 |
while(e.hasMoreElements())
|
|
986 |
{
|
|
987 |
String strIter = (String)e.nextElement();
|
|
988 |
if (strIter.equalsIgnoreCase(str))
|
|
989 |
return i;
|
|
990 |
i ++;
|
|
991 |
}
|
|
992 |
return -1;
|
|
993 |
}
|
|
994 |
|
|
995 |
static boolean IsNumber(String pszStr)
|
|
996 |
{
|
|
997 |
try
|
|
998 |
{
|
|
999 |
Double.parseDouble(pszStr);
|
|
1000 |
return true;
|
|
1001 |
}
|
|
1002 |
catch(Exception ex)
|
|
1003 |
{
|
|
1004 |
return false;
|
|
1005 |
}
|
|
1006 |
}
|
|
1007 |
|
|
1008 |
static Geometry LoadGeometry( String pszDS,
|
|
1009 |
String pszSQL,
|
|
1010 |
String pszLyr,
|
|
1011 |
String pszWhere)
|
|
1012 |
{
|
|
1013 |
DataSource poDS;
|
|
1014 |
Layer poLyr;
|
|
1015 |
Feature poFeat;
|
|
1016 |
Geometry poGeom = null;
|
|
1017 |
|
|
1018 |
poDS = ogr.Open( pszDS, false );
|
|
1019 |
if (poDS == null)
|
|
1020 |
return null;
|
|
1021 |
|
|
1022 |
if (pszSQL != null)
|
|
1023 |
poLyr = poDS.ExecuteSQL( pszSQL, null, null );
|
|
1024 |
else if (pszLyr != null)
|
|
1025 |
poLyr = poDS.GetLayerByName(pszLyr);
|
|
1026 |
else
|
|
1027 |
poLyr = poDS.GetLayer(0);
|
|
1028 |
|
|
1029 |
if (poLyr == null)
|
|
1030 |
{
|
|
1031 |
System.err.print("Failed to identify source layer from datasource.\n");
|
|
1032 |
poDS.delete();
|
|
1033 |
return null;
|
|
1034 |
}
|
|
1035 |
|
|
1036 |
if (pszWhere != null)
|
|
1037 |
poLyr.SetAttributeFilter(pszWhere);
|
|
1038 |
|
|
1039 |
while ((poFeat = poLyr.GetNextFeature()) != null)
|
|
1040 |
{
|
|
1041 |
Geometry poSrcGeom = poFeat.GetGeometryRef();
|
|
1042 |
if (poSrcGeom != null)
|
|
1043 |
{
|
|
1044 |
int eType = wkbFlatten(poSrcGeom.GetGeometryType());
|
|
1045 |
|
|
1046 |
if (poGeom == null)
|
|
1047 |
poGeom = new Geometry( ogr.wkbMultiPolygon );
|
|
1048 |
|
|
1049 |
if( eType == ogr.wkbPolygon )
|
|
1050 |
poGeom.AddGeometry( poSrcGeom );
|
|
1051 |
else if( eType == ogr.wkbMultiPolygon )
|
|
1052 |
{
|
|
1053 |
int iGeom;
|
|
1054 |
int nGeomCount = poSrcGeom.GetGeometryCount();
|
|
1055 |
|
|
1056 |
for( iGeom = 0; iGeom < nGeomCount; iGeom++ )
|
|
1057 |
{
|
|
1058 |
poGeom.AddGeometry(poSrcGeom.GetGeometryRef(iGeom) );
|
|
1059 |
}
|
|
1060 |
}
|
|
1061 |
else
|
|
1062 |
{
|
|
1063 |
System.err.print("ERROR: Geometry not of polygon type.\n" );
|
|
1064 |
if( pszSQL != null )
|
|
1065 |
poDS.ReleaseResultSet( poLyr );
|
|
1066 |
poDS.delete();
|
|
1067 |
return null;
|
|
1068 |
}
|
|
1069 |
}
|
|
1070 |
}
|
|
1071 |
|
|
1072 |
if( pszSQL != null )
|
|
1073 |
poDS.ReleaseResultSet( poLyr );
|
|
1074 |
poDS.delete();
|
|
1075 |
|
|
1076 |
return poGeom;
|
|
1077 |
}
|
|
1078 |
|
|
1079 |
|
|
1080 |
static int wkbFlatten(int eType)
|
|
1081 |
{
|
|
1082 |
return eType & (~ogrConstants.wkb25DBit);
|
|
1083 |
}
|
|
1084 |
|
|
1085 |
/************************************************************************/
|
|
1086 |
/* SetZ() */
|
|
1087 |
/************************************************************************/
|
|
1088 |
static void SetZ (Geometry poGeom, double dfZ )
|
|
1089 |
{
|
|
1090 |
if (poGeom == null)
|
|
1091 |
return;
|
|
1092 |
switch (wkbFlatten(poGeom.GetGeometryType()))
|
|
1093 |
{
|
|
1094 |
case ogr.wkbPoint:
|
|
1095 |
poGeom.SetPoint(0, poGeom.GetX(), poGeom.GetY(), dfZ);
|
|
1096 |
break;
|
|
1097 |
|
|
1098 |
case ogr.wkbLineString:
|
|
1099 |
case ogr.wkbLinearRing:
|
|
1100 |
{
|
|
1101 |
int i;
|
|
1102 |
for(i=0;i<poGeom.GetPointCount();i++)
|
|
1103 |
poGeom.SetPoint(i, poGeom.GetX(i), poGeom.GetY(i), dfZ);
|
|
1104 |
break;
|
|
1105 |
}
|
|
1106 |
|
|
1107 |
case ogr.wkbPolygon:
|
|
1108 |
case ogr.wkbMultiPoint:
|
|
1109 |
case ogr.wkbMultiLineString:
|
|
1110 |
case ogr.wkbMultiPolygon:
|
|
1111 |
case ogr.wkbGeometryCollection:
|
|
1112 |
{
|
|
1113 |
int i;
|
|
1114 |
for(i=0;i<poGeom.GetGeometryCount();i++)
|
|
1115 |
SetZ(poGeom.GetGeometryRef(i), dfZ);
|
|
1116 |
break;
|
|
1117 |
}
|
|
1118 |
|
|
1119 |
default:
|
|
1120 |
break;
|
|
1121 |
}
|
|
1122 |
}
|
|
1123 |
|
|
1124 |
|
|
1125 |
/************************************************************************/
|
|
1126 |
/* TranslateLayer() */
|
|
1127 |
/************************************************************************/
|
|
1128 |
|
|
1129 |
static boolean TranslateLayer( DataSource poSrcDS,
|
|
1130 |
Layer poSrcLayer,
|
|
1131 |
DataSource poDstDS,
|
|
1132 |
Vector papszLCO,
|
|
1133 |
String pszNewLayerName,
|
|
1134 |
boolean bTransform,
|
|
1135 |
SpatialReference poOutputSRS,
|
|
1136 |
SpatialReference poSourceSRS,
|
|
1137 |
Vector papszSelFields,
|
|
1138 |
boolean bAppend, int eGType, boolean bOverwrite,
|
|
1139 |
GeomOperation eGeomOp,
|
|
1140 |
double dfGeomOpParam,
|
|
1141 |
Vector papszFieldTypesToString,
|
|
1142 |
long nCountLayerFeatures,
|
|
1143 |
Geometry poClipSrc,
|
|
1144 |
Geometry poClipDst,
|
|
1145 |
boolean bExplodeCollections,
|
|
1146 |
String pszZField,
|
|
1147 |
String pszWHERE,
|
|
1148 |
ProgressCallback pfnProgress)
|
|
1149 |
|
|
1150 |
{
|
|
1151 |
Layer poDstLayer;
|
|
1152 |
FeatureDefn poSrcFDefn;
|
|
1153 |
int eErr;
|
|
1154 |
boolean bForceToPolygon = false;
|
|
1155 |
boolean bForceToMultiPolygon = false;
|
|
1156 |
boolean bForceToMultiLineString = false;
|
|
1157 |
|
|
1158 |
if( pszNewLayerName == null )
|
|
1159 |
pszNewLayerName = poSrcLayer.GetLayerDefn().GetName();
|
|
1160 |
|
|
1161 |
if( wkbFlatten(eGType) == ogr.wkbPolygon )
|
|
1162 |
bForceToPolygon = true;
|
|
1163 |
else if( wkbFlatten(eGType) == ogr.wkbMultiPolygon )
|
|
1164 |
bForceToMultiPolygon = true;
|
|
1165 |
else if( wkbFlatten(eGType) == ogr.wkbMultiLineString )
|
|
1166 |
bForceToMultiLineString = true;
|
|
1167 |
|
|
1168 |
/* -------------------------------------------------------------------- */
|
|
1169 |
/* Setup coordinate transformation if we need it. */
|
|
1170 |
/* -------------------------------------------------------------------- */
|
|
1171 |
CoordinateTransformation poCT = null;
|
|
1172 |
|
|
1173 |
if( bTransform )
|
|
1174 |
{
|
|
1175 |
if( poSourceSRS == null )
|
|
1176 |
poSourceSRS = poSrcLayer.GetSpatialRef();
|
|
1177 |
|
|
1178 |
if( poSourceSRS == null )
|
|
1179 |
{
|
|
1180 |
System.err.println("Can't transform coordinates, source layer has no\n" +
|
|
1181 |
"coordinate system. Use -s_srs to set one." );
|
|
1182 |
System_exit( 1 );
|
|
1183 |
}
|
|
1184 |
|
|
1185 |
/*CPLAssert( null != poSourceSRS );
|
|
1186 |
CPLAssert( null != poOutputSRS );*/
|
|
1187 |
|
|
1188 |
/* New in GDAL 1.10. Before was "new CoordinateTransformation(srs,dst)". */
|
|
1189 |
poCT = CoordinateTransformation.CreateCoordinateTransformation( poSourceSRS, poOutputSRS );
|
|
1190 |
if( poCT == null )
|
|
1191 |
{
|
|
1192 |
String pszWKT = null;
|
|
1193 |
|
|
1194 |
System.err.println("Failed to create coordinate transformation between the\n" +
|
|
1195 |
"following coordinate systems. This may be because they\n" +
|
|
1196 |
"are not transformable, or because projection services\n" +
|
|
1197 |
"(PROJ.4 DLL/.so) could not be loaded." );
|
|
1198 |
|
|
1199 |
pszWKT = poSourceSRS.ExportToPrettyWkt( 0 );
|
|
1200 |
System.err.println( "Source:\n" + pszWKT );
|
|
1201 |
|
|
1202 |
pszWKT = poOutputSRS.ExportToPrettyWkt( 0 );
|
|
1203 |
System.err.println( "Target:\n" + pszWKT );
|
|
1204 |
System_exit( 1 );
|
|
1205 |
}
|
|
1206 |
}
|
|
1207 |
|
|
1208 |
/* -------------------------------------------------------------------- */
|
|
1209 |
/* Get other info. */
|
|
1210 |
/* -------------------------------------------------------------------- */
|
|
1211 |
poSrcFDefn = poSrcLayer.GetLayerDefn();
|
|
1212 |
|
|
1213 |
if( poOutputSRS == null )
|
|
1214 |
poOutputSRS = poSrcLayer.GetSpatialRef();
|
|
1215 |
|
|
1216 |
/* -------------------------------------------------------------------- */
|
|
1217 |
/* Find the layer. */
|
|
1218 |
/* -------------------------------------------------------------------- */
|
|
1219 |
|
|
1220 |
/* GetLayerByName() can instantiate layers that would have been */
|
|
1221 |
/* 'hidden' otherwise, for example, non-spatial tables in a */
|
|
1222 |
/* PostGIS-enabled database, so this apparently useless command is */
|
|
1223 |
/* not useless. (#4012) */
|
|
1224 |
gdal.PushErrorHandler("CPLQuietErrorHandler");
|
|
1225 |
poDstLayer = poDstDS.GetLayerByName(pszNewLayerName);
|
|
1226 |
gdal.PopErrorHandler();
|
|
1227 |
gdal.ErrorReset();
|
|
1228 |
|
|
1229 |
int iLayer = -1;
|
|
1230 |
if( poDstLayer != null )
|
|
1231 |
{
|
|
1232 |
int nLayerCount = poDstDS.GetLayerCount();
|
|
1233 |
for( iLayer = 0; iLayer < nLayerCount; iLayer++ )
|
|
1234 |
{
|
|
1235 |
Layer poLayer = poDstDS.GetLayer(iLayer);
|
|
1236 |
|
|
1237 |
if( poLayer != null
|
|
1238 |
&& poLayer.GetName().equals(poDstLayer.GetName()) )
|
|
1239 |
{
|
|
1240 |
break;
|
|
1241 |
}
|
|
1242 |
}
|
|
1243 |
|
|
1244 |
if (iLayer == nLayerCount)
|
|
1245 |
/* shouldn't happen with an ideal driver */
|
|
1246 |
poDstLayer = null;
|
|
1247 |
}
|
|
1248 |
|
|
1249 |
/* -------------------------------------------------------------------- */
|
|
1250 |
/* If the user requested overwrite, and we have the layer in */
|
|
1251 |
/* question we need to delete it now so it will get recreated */
|
|
1252 |
/* (overwritten). */
|
|
1253 |
/* -------------------------------------------------------------------- */
|
|
1254 |
if( poDstLayer != null && bOverwrite )
|
|
1255 |
{
|
|
1256 |
if( poDstDS.DeleteLayer( iLayer ) != 0 )
|
|
1257 |
{
|
|
1258 |
System.err.println(
|
|
1259 |
"DeleteLayer() failed when overwrite requested." );
|
|
1260 |
return false;
|
|
1261 |
}
|
|
1262 |
poDstLayer = null;
|
|
1263 |
}
|
|
1264 |
|
|
1265 |
/* -------------------------------------------------------------------- */
|
|
1266 |
/* If the layer does not exist, then create it. */
|
|
1267 |
/* -------------------------------------------------------------------- */
|
|
1268 |
if( poDstLayer == null )
|
|
1269 |
{
|
|
1270 |
if( eGType == -2 )
|
|
1271 |
{
|
|
1272 |
eGType = poSrcFDefn.GetGeomType();
|
|
1273 |
|
|
1274 |
if ( bExplodeCollections )
|
|
1275 |
{
|
|
1276 |
int n25DBit = eGType & ogr.wkb25DBit;
|
|
1277 |
if (wkbFlatten(eGType) == ogr.wkbMultiPoint)
|
|
1278 |
{
|
|
1279 |
eGType = ogr.wkbPoint | n25DBit;
|
|
1280 |
}
|
|
1281 |
else if (wkbFlatten(eGType) == ogr.wkbMultiLineString)
|
|
1282 |
{
|
|
1283 |
eGType = ogr.wkbLineString | n25DBit;
|
|
1284 |
}
|
|
1285 |
else if (wkbFlatten(eGType) == ogr.wkbMultiPolygon)
|
|
1286 |
{
|
|
1287 |
eGType = ogr.wkbPolygon | n25DBit;
|
|
1288 |
}
|
|
1289 |
else if (wkbFlatten(eGType) == ogr.wkbGeometryCollection)
|
|
1290 |
{
|
|
1291 |
eGType = ogr.wkbUnknown | n25DBit;
|
|
1292 |
}
|
|
1293 |
}
|
|
1294 |
|
|
1295 |
if ( pszZField != null )
|
|
1296 |
eGType |= ogr.wkb25DBit;
|
|
1297 |
}
|
|
1298 |
|
|
1299 |
if( poDstDS.TestCapability( ogr.ODsCCreateLayer ) == false)
|
|
1300 |
{
|
|
1301 |
System.err.println(
|
|
1302 |
"Layer " + pszNewLayerName + "not found, and CreateLayer not supported by driver.");
|
|
1303 |
return false;
|
|
1304 |
}
|
|
1305 |
|
|
1306 |
gdal.ErrorReset();
|
|
1307 |
|
|
1308 |
poDstLayer = poDstDS.CreateLayer( pszNewLayerName, poOutputSRS,
|
|
1309 |
eGType, papszLCO );
|
|
1310 |
|
|
1311 |
if( poDstLayer == null )
|
|
1312 |
return false;
|
|
1313 |
|
|
1314 |
bAppend = false;
|
|
1315 |
}
|
|
1316 |
|
|
1317 |
/* -------------------------------------------------------------------- */
|
|
1318 |
/* Otherwise we will append to it, if append was requested. */
|
|
1319 |
/* -------------------------------------------------------------------- */
|
|
1320 |
else if( !bAppend )
|
|
1321 |
{
|
|
1322 |
System.err.println("FAILED: Layer " + pszNewLayerName + "already exists, and -append not specified.\n" +
|
|
1323 |
" Consider using -append, or -overwrite.");
|
|
1324 |
return false;
|
|
1325 |
}
|
|
1326 |
else
|
|
1327 |
{
|
|
1328 |
if( papszLCO.size() > 0 )
|
|
1329 |
{
|
|
1330 |
System.err.println("WARNING: Layer creation options ignored since an existing layer is\n" +
|
|
1331 |
" being appended to." );
|
|
1332 |
}
|
|
1333 |
}
|
|
1334 |
|