1 | /****************************************************************************** |
---|
2 | * |
---|
3 | * Project: OpenGIS Simple Features Reference Implementation |
---|
4 | * Purpose: Program to generate a UMN MapServer compatible tile index for a |
---|
5 | * set of OGR data sources. |
---|
6 | * Author: Frank Warmerdam, warmerdam@pobox.com |
---|
7 | * |
---|
8 | ****************************************************************************** |
---|
9 | * Copyright (c) 2002, Frank Warmerdam |
---|
10 | * Copyright (c) 2007-2010, Even Rouault <even dot rouault at mines-paris dot org> |
---|
11 | * |
---|
12 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
13 | * copy of this software and associated documentation files (the "Software"), |
---|
14 | * to deal in the Software without restriction, including without limitation |
---|
15 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
16 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
17 | * Software is furnished to do so, subject to the following conditions: |
---|
18 | * |
---|
19 | * The above copyright notice and this permission notice shall be included |
---|
20 | * in all copies or substantial portions of the Software. |
---|
21 | * |
---|
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
23 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
25 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
28 | * DEALINGS IN THE SOFTWARE. |
---|
29 | ****************************************************************************/ |
---|
30 | |
---|
31 | #include "cpl_port.h" |
---|
32 | |
---|
33 | #include <cassert> |
---|
34 | #include <vector> |
---|
35 | |
---|
36 | #include "cpl_conv.h" |
---|
37 | #include "cpl_string.h" |
---|
38 | #include "ogr_api.h" |
---|
39 | #include "ogrsf_frmts.h" |
---|
40 | #include "ogr_p.h" |
---|
41 | #include "gdal_version.h" |
---|
42 | //#include "commonutils.h" |
---|
43 | #if GDAL_VERSION_MAJOR >= 2 |
---|
44 | #include "gdal.h" |
---|
45 | #include "gdal_priv.h" |
---|
46 | #endif |
---|
47 | #ifdef ZOO_SERVICE |
---|
48 | #include "service.h" |
---|
49 | #include "service_internal.h" |
---|
50 | #endif |
---|
51 | |
---|
52 | CPL_CVSID("$Id: service.c 884 2018-09-13 16:03:52Z djay $") |
---|
53 | |
---|
54 | #ifdef ZOO_SERVICE |
---|
55 | extern "C" { |
---|
56 | #endif |
---|
57 | |
---|
58 | typedef enum |
---|
59 | { |
---|
60 | FORMAT_AUTO, |
---|
61 | FORMAT_WKT, |
---|
62 | FORMAT_EPSG, |
---|
63 | FORMAT_PROJ |
---|
64 | } SrcSRSFormat; |
---|
65 | |
---|
66 | /************************************************************************/ |
---|
67 | /* Usage() */ |
---|
68 | /************************************************************************/ |
---|
69 | |
---|
70 | static void Usage() |
---|
71 | |
---|
72 | { |
---|
73 | printf("Usage: ogrtindex [-lnum n]... [-lname name]... [-f output_format]\n" |
---|
74 | " [-write_absolute_path] [-skip_different_projection]\n" |
---|
75 | " [-t_srs target_srs]\n" |
---|
76 | " [-src_srs_name field_name] [-src_srs_format [AUTO|WKT|EPSG|PROJ]\n" |
---|
77 | " [-accept_different_schemas]\n" |
---|
78 | " output_dataset src_dataset...\n"); |
---|
79 | printf("\n"); |
---|
80 | printf(" -lnum n: Add layer number 'n' from each source file\n" |
---|
81 | " in the tile index.\n"); |
---|
82 | printf(" -lname name: Add the layer named 'name' from each source file\n" |
---|
83 | " in the tile index.\n"); |
---|
84 | printf(" -f output_format: Select an output format name.\n"); |
---|
85 | printf(" -tileindex field_name: The name to use for the dataset name.\n" |
---|
86 | " Defaults to LOCATION.\n"); |
---|
87 | printf(" -write_absolute_path: Filenames are written with absolute paths.\n"); |
---|
88 | printf(" -skip_different_projection: Only layers with same projection ref \n" |
---|
89 | " as layers already inserted in the tileindex will be inserted.\n"); |
---|
90 | printf(" -accept_different_schemas: by default ogrtindex checks that all layers inserted\n" |
---|
91 | " into the index have the same attribute schemas. If you\n" |
---|
92 | " specify this option, this test will be disabled. Be aware that\n" |
---|
93 | " resulting index may be incompatible with MapServer!\n"); |
---|
94 | printf( |
---|
95 | " - If -t_srs is specified, geometries of input files will be transformed to the desired\n" |
---|
96 | " target coordinate reference system.\n" |
---|
97 | " Note that using this option generates files that are NOT compatible with MapServer < 7.2.\n" |
---|
98 | " - Simple rectangular polygons are generated in the same coordinate reference system\n" |
---|
99 | " as the vectors, or in target reference system if the -t_srs option is used.\n"); |
---|
100 | printf("\n"); |
---|
101 | printf("If no -lnum or -lname arguments are given it is assumed that\n" |
---|
102 | "all layers in source datasets should be added to the tile index\n" |
---|
103 | "as independent records.\n"); |
---|
104 | exit(1); |
---|
105 | } |
---|
106 | |
---|
107 | /************************************************************************/ |
---|
108 | /* main() */ |
---|
109 | /************************************************************************/ |
---|
110 | |
---|
111 | #ifdef ZOO_SERVICE |
---|
112 | #ifdef WIN32 |
---|
113 | __declspec(dllexport) |
---|
114 | #endif |
---|
115 | int Ogrtindex(maps*& conf,maps*& inputs,maps*& outputs) |
---|
116 | #else |
---|
117 | MAIN_START(nArgc, papszArgv) |
---|
118 | #endif |
---|
119 | { |
---|
120 | FILE* log=fopen("/tmp/error.log","w+"); |
---|
121 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
122 | fflush(log); |
---|
123 | //return SERVICE_FAILED; |
---|
124 | // Check strict compilation and runtime library version as we use C++ API. |
---|
125 | #ifdef ZOO_SERVICE |
---|
126 | if( !GDAL_CHECK_VERSION("ogrtindex") ) |
---|
127 | { |
---|
128 | setMapInMaps(conf,"lenv","message",_("Compilation and runtime library versions differ")); |
---|
129 | return SERVICE_FAILED; |
---|
130 | } |
---|
131 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
132 | fflush(log); |
---|
133 | #else |
---|
134 | if( !GDAL_CHECK_VERSION(papszArgv[0]) ) |
---|
135 | exit(1); |
---|
136 | #endif |
---|
137 | /* -------------------------------------------------------------------- */ |
---|
138 | /* Register format(s). */ |
---|
139 | /* -------------------------------------------------------------------- */ |
---|
140 | OGRRegisterAll(); |
---|
141 | |
---|
142 | /* -------------------------------------------------------------------- */ |
---|
143 | /* Processing command line arguments. */ |
---|
144 | /* -------------------------------------------------------------------- */ |
---|
145 | int nFirstSourceDataset = -1; |
---|
146 | bool bLayersWildcarded = true; |
---|
147 | const char *pszFormat = nullptr; |
---|
148 | const char *pszTileIndexField = "LOCATION"; |
---|
149 | const char *pszOutputName = nullptr; |
---|
150 | bool write_absolute_path = false; |
---|
151 | bool skip_different_projection = false; |
---|
152 | char* current_path = nullptr; |
---|
153 | bool accept_different_schemas = false; |
---|
154 | bool bFirstWarningForNonMatchingAttributes = true; |
---|
155 | const char *pszTargetSRS = ""; |
---|
156 | bool bSetTargetSRS = false; |
---|
157 | const char* pszSrcSRSName = nullptr; |
---|
158 | int i_SrcSRSName = -1; |
---|
159 | bool bSrcSRSFormatSpecified = false; |
---|
160 | SrcSRSFormat eSrcSRSFormat = FORMAT_AUTO; |
---|
161 | size_t nMaxFieldSize = 254; |
---|
162 | |
---|
163 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
164 | fflush(log); |
---|
165 | #ifdef ZOO_SERVICE |
---|
166 | // f / of |
---|
167 | map* tmpMap=getMapFromMaps(inputs,"f","value"); |
---|
168 | if(tmpMap==NULL) |
---|
169 | tmpMap=getMapFromMaps(inputs,"of","value"); |
---|
170 | if(tmpMap!=NULL) |
---|
171 | pszFormat = zStrdup(tmpMap->value); |
---|
172 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
173 | fflush(log); |
---|
174 | |
---|
175 | // write_absolute_path |
---|
176 | tmpMap=getMapFromMaps(inputs,"write_absolute_path","value"); |
---|
177 | if(tmpMap!=NULL && strncasecmp(tmpMap->value,"true",4)==0) |
---|
178 | write_absolute_path = true; |
---|
179 | // skip_different_projection |
---|
180 | tmpMap=getMapFromMaps(inputs,"skip_different_projection","value"); |
---|
181 | if(tmpMap!=NULL && strncasecmp(tmpMap->value,"true",4)==0) |
---|
182 | skip_different_projection = true; |
---|
183 | // accept_different_schemas |
---|
184 | tmpMap=getMapFromMaps(inputs,"accept_different_schemas","value"); |
---|
185 | if(tmpMap!=NULL && strncasecmp(tmpMap->value,"true",4)==0) |
---|
186 | accept_different_schemas = true; |
---|
187 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
188 | fflush(log); |
---|
189 | // tileindex |
---|
190 | tmpMap=getMapFromMaps(inputs,"tileindex","value"); |
---|
191 | if(tmpMap!=NULL) |
---|
192 | pszTileIndexField = zStrdup(tmpMap->value); |
---|
193 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
194 | fflush(log); |
---|
195 | // lname / lnum |
---|
196 | tmpMap=getMapFromMaps(inputs,"lname","value"); |
---|
197 | if(tmpMap==NULL) |
---|
198 | tmpMap=getMapFromMaps(inputs,"lnum","value"); |
---|
199 | if(tmpMap!=NULL) |
---|
200 | bLayersWildcarded = false; |
---|
201 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
202 | fflush(log); |
---|
203 | //dumpMaps(inputs); |
---|
204 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
205 | fflush(log); |
---|
206 | // t_srs |
---|
207 | tmpMap=getMapFromMaps(inputs,"t_srs","value"); |
---|
208 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
209 | fflush(log); |
---|
210 | if(tmpMap!=NULL){ |
---|
211 | pszTargetSRS = zStrdup(tmpMap->value); |
---|
212 | bSetTargetSRS = true; |
---|
213 | } |
---|
214 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
215 | fflush(log); |
---|
216 | // src_srs_name |
---|
217 | //dumpMaps(inputs); |
---|
218 | tmpMap=getMapFromMaps(inputs,"src_srs_name","value"); |
---|
219 | if(tmpMap!=NULL){ |
---|
220 | pszSrcSRSName = zStrdup(tmpMap->value); |
---|
221 | } |
---|
222 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
223 | fflush(log); |
---|
224 | // src_srs_format |
---|
225 | tmpMap=getMapFromMaps(inputs,"src_srs_format","value"); |
---|
226 | if(tmpMap!=NULL){ |
---|
227 | bSrcSRSFormatSpecified = true; |
---|
228 | const char* pszSRSFormat = zStrdup(tmpMap->value); |
---|
229 | if( EQUAL(pszSRSFormat, "AUTO") ) |
---|
230 | eSrcSRSFormat = FORMAT_AUTO; |
---|
231 | else if( EQUAL(pszSRSFormat, "WKT") ) |
---|
232 | eSrcSRSFormat = FORMAT_WKT; |
---|
233 | else if( EQUAL(pszSRSFormat, "EPSG") ) |
---|
234 | eSrcSRSFormat = FORMAT_EPSG; |
---|
235 | else if( EQUAL(pszSRSFormat, "PROJ") ) |
---|
236 | eSrcSRSFormat = FORMAT_PROJ; |
---|
237 | } |
---|
238 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
239 | fflush(log); |
---|
240 | // OutputName |
---|
241 | tmpMap=getMapFromMaps(inputs,"OutputName","value"); |
---|
242 | if(tmpMap!=NULL){ |
---|
243 | pszOutputName = zStrdup(tmpMap->value); |
---|
244 | } |
---|
245 | nFirstSourceDataset = 0; |
---|
246 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
247 | fflush(log); |
---|
248 | #else |
---|
249 | for( int iArg = 1; iArg < nArgc; iArg++ ) |
---|
250 | { |
---|
251 | if( EQUAL(papszArgv[iArg], "--utility_version") ) |
---|
252 | { |
---|
253 | printf("%s was compiled against GDAL %s and " |
---|
254 | "is running against GDAL %s\n", |
---|
255 | papszArgv[0], GDAL_RELEASE_NAME, |
---|
256 | GDALVersionInfo("RELEASE_NAME")); |
---|
257 | return 0; |
---|
258 | } |
---|
259 | else if( iArg < nArgc-1 && |
---|
260 | (EQUAL(papszArgv[iArg],"-f") || EQUAL(papszArgv[iArg],"-of")) ) |
---|
261 | { |
---|
262 | pszFormat = papszArgv[++iArg]; |
---|
263 | } |
---|
264 | else if( EQUAL(papszArgv[iArg],"-write_absolute_path")) |
---|
265 | { |
---|
266 | write_absolute_path = true; |
---|
267 | } |
---|
268 | else if( EQUAL(papszArgv[iArg],"-skip_different_projection")) |
---|
269 | { |
---|
270 | skip_different_projection = true; |
---|
271 | } |
---|
272 | else if( EQUAL(papszArgv[iArg],"-accept_different_schemas")) |
---|
273 | { |
---|
274 | accept_different_schemas = true; |
---|
275 | } |
---|
276 | else if( iArg < nArgc-1 && EQUAL(papszArgv[iArg],"-tileindex") ) |
---|
277 | { |
---|
278 | pszTileIndexField = papszArgv[++iArg]; |
---|
279 | } |
---|
280 | else if( EQUAL(papszArgv[iArg],"-lnum") |
---|
281 | || EQUAL(papszArgv[iArg],"-lname") ) |
---|
282 | { |
---|
283 | iArg++; |
---|
284 | bLayersWildcarded = false; |
---|
285 | } |
---|
286 | else if( iArg < nArgc-1 && strcmp(papszArgv[iArg],"-t_srs") == 0 ) |
---|
287 | { |
---|
288 | pszTargetSRS = papszArgv[++iArg]; |
---|
289 | bSetTargetSRS = true; |
---|
290 | } |
---|
291 | else if( iArg < nArgc-1 && |
---|
292 | strcmp(papszArgv[iArg], "-src_srs_name") == 0 ) |
---|
293 | { |
---|
294 | pszSrcSRSName = papszArgv[++iArg]; |
---|
295 | } |
---|
296 | else if( iArg < nArgc-1 && |
---|
297 | strcmp(papszArgv[iArg], "-src_srs_format") == 0 ) |
---|
298 | { |
---|
299 | bSrcSRSFormatSpecified = true; |
---|
300 | const char* pszSRSFormat = papszArgv[++iArg]; |
---|
301 | if( EQUAL(pszSRSFormat, "AUTO") ) |
---|
302 | eSrcSRSFormat = FORMAT_AUTO; |
---|
303 | else if( EQUAL(pszSRSFormat, "WKT") ) |
---|
304 | eSrcSRSFormat = FORMAT_WKT; |
---|
305 | else if( EQUAL(pszSRSFormat, "EPSG") ) |
---|
306 | eSrcSRSFormat = FORMAT_EPSG; |
---|
307 | else if( EQUAL(pszSRSFormat, "PROJ") ) |
---|
308 | eSrcSRSFormat = FORMAT_PROJ; |
---|
309 | } |
---|
310 | else if( papszArgv[iArg][0] == '-' ) |
---|
311 | { |
---|
312 | Usage(); |
---|
313 | } |
---|
314 | else if( pszOutputName == nullptr ) |
---|
315 | { |
---|
316 | pszOutputName = papszArgv[iArg]; |
---|
317 | } |
---|
318 | else if( nFirstSourceDataset == -1 ) |
---|
319 | { |
---|
320 | nFirstSourceDataset = iArg; |
---|
321 | } |
---|
322 | } |
---|
323 | #endif |
---|
324 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
325 | fflush(log); |
---|
326 | if( pszOutputName == nullptr || nFirstSourceDataset == -1 ) |
---|
327 | Usage(); |
---|
328 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
329 | fflush(log); |
---|
330 | |
---|
331 | if( bSrcSRSFormatSpecified && pszSrcSRSName == nullptr ) |
---|
332 | { |
---|
333 | fprintf(log, |
---|
334 | "-src_srs_name must be specified when -src_srs_format is " |
---|
335 | "specified.\n"); |
---|
336 | Usage(); |
---|
337 | } |
---|
338 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
339 | fflush(log); |
---|
340 | |
---|
341 | /* -------------------------------------------------------------------- */ |
---|
342 | /* Create and validate target SRS if given. */ |
---|
343 | /* -------------------------------------------------------------------- */ |
---|
344 | OGRSpatialReference* poTargetSRS = nullptr; |
---|
345 | if( bSetTargetSRS ) |
---|
346 | { |
---|
347 | if( skip_different_projection ) |
---|
348 | { |
---|
349 | fprintf(log, |
---|
350 | "Warning : -skip_different_projection does not apply " |
---|
351 | "when -t_srs is requested.\n"); |
---|
352 | } |
---|
353 | poTargetSRS = new OGRSpatialReference(); |
---|
354 | // coverity[tainted_data] |
---|
355 | if( poTargetSRS->SetFromUserInput( pszTargetSRS ) != CE_None ) |
---|
356 | { |
---|
357 | delete poTargetSRS; |
---|
358 | fprintf(log, "Invalid target SRS `%s'.\n", |
---|
359 | pszTargetSRS); |
---|
360 | exit(1); |
---|
361 | } |
---|
362 | } |
---|
363 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
364 | fflush(log); |
---|
365 | |
---|
366 | /* -------------------------------------------------------------------- */ |
---|
367 | /* Try to open as an existing dataset for update access. */ |
---|
368 | /* -------------------------------------------------------------------- */ |
---|
369 | GDALDataset *poDstDS = reinterpret_cast<GDALDataset*>( |
---|
370 | OGROpen(pszOutputName, TRUE, nullptr)); |
---|
371 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
372 | fflush(log); |
---|
373 | |
---|
374 | /* -------------------------------------------------------------------- */ |
---|
375 | /* If that failed, find the driver so we can create the tile index.*/ |
---|
376 | /* -------------------------------------------------------------------- */ |
---|
377 | OGRLayer *poDstLayer = nullptr; |
---|
378 | int nArgc; |
---|
379 | |
---|
380 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
381 | fflush(log); |
---|
382 | if( poDstDS == nullptr ) |
---|
383 | { |
---|
384 | CPLString osFormat; |
---|
385 | |
---|
386 | OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar(); |
---|
387 | GDALDriver *poDriver = NULL; |
---|
388 | int iDriver; |
---|
389 | |
---|
390 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
391 | fflush(log); |
---|
392 | for( iDriver = 0; |
---|
393 | iDriver < poR->GetDriverCount() && poDriver == NULL; |
---|
394 | iDriver++ ) |
---|
395 | { |
---|
396 | if( EQUAL(poR->GetDriver(iDriver)->GetDescription(),pszFormat) ) |
---|
397 | { |
---|
398 | osFormat=pszFormat; |
---|
399 | poDriver = poR->GetDriver(iDriver); |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
404 | fflush(log); |
---|
405 | if( poDriver == NULL ) |
---|
406 | { |
---|
407 | fprintf( log, "Unable to find driver `%s'.\n", pszFormat ); |
---|
408 | fprintf( log, "The following drivers are available:\n" ); |
---|
409 | |
---|
410 | for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ ) |
---|
411 | { |
---|
412 | fprintf( log, " -> `%s'\n", poR->GetDriver(iDriver)->GetDescription() ); |
---|
413 | } |
---|
414 | #ifdef ZOO_SERVICE |
---|
415 | setMapInMaps(conf,"lenv","message",_("Unable to find driver!")); |
---|
416 | return SERVICE_FAILED; |
---|
417 | #else |
---|
418 | exit( 1 ); |
---|
419 | #endif |
---|
420 | } |
---|
421 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
422 | fflush(log); |
---|
423 | |
---|
424 | /* if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) ) |
---|
425 | { |
---|
426 | fprintf( log, "%s driver does not support data source creation.\n", |
---|
427 | pszFormat ); |
---|
428 | #ifdef ZOO_SERVICE |
---|
429 | setMapInMaps(conf,"lenv","message",_("This driver does not support data source creation!")); |
---|
430 | return SERVICE_FAILED; |
---|
431 | #else |
---|
432 | exit( 1 ); |
---|
433 | #endif |
---|
434 | }*/ |
---|
435 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
436 | fflush(log); |
---|
437 | |
---|
438 | /* -------------------------------------------------------------------- */ |
---|
439 | /* Now create it. */ |
---|
440 | /* -------------------------------------------------------------------- */ |
---|
441 | |
---|
442 | poDstDS = reinterpret_cast<GDALDataset*>( |
---|
443 | GDALCreate(poDriver, pszOutputName, 0, 0, 0, GDT_Unknown, nullptr)); |
---|
444 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
445 | fflush(log); |
---|
446 | if( poDstDS == nullptr ) |
---|
447 | { |
---|
448 | fprintf(log, "%s driver failed to create %s\n", |
---|
449 | osFormat.c_str(), pszOutputName); |
---|
450 | #ifdef ZOO_SERVICE |
---|
451 | setMapInMaps(conf,"lenv","message",_("This driver failed to create!")); |
---|
452 | return SERVICE_FAILED; |
---|
453 | #else |
---|
454 | exit( 1 ); |
---|
455 | #endif |
---|
456 | } |
---|
457 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
458 | fflush(log); |
---|
459 | |
---|
460 | if( poDstDS->GetLayerCount() == 0 ) |
---|
461 | { |
---|
462 | #ifndef ZOO_SERVICE |
---|
463 | if( nFirstSourceDataset < nArgc && |
---|
464 | papszArgv[nFirstSourceDataset][0] == '-' ) |
---|
465 | { |
---|
466 | nFirstSourceDataset++; |
---|
467 | } |
---|
468 | #else |
---|
469 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
470 | fflush(log); |
---|
471 | map* lengthMap=getMapFromMaps(inputs,"files","length"); |
---|
472 | maps* tmpMaps=getMaps(inputs,"files"); |
---|
473 | if(lengthMap!=NULL) |
---|
474 | nArgc=atoi(lengthMap->value); |
---|
475 | else |
---|
476 | nArgc=1; |
---|
477 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
478 | fflush(log); |
---|
479 | #endif |
---|
480 | OGRSpatialReference* poSrcSpatialRef = nullptr; |
---|
481 | if( bSetTargetSRS ) |
---|
482 | { |
---|
483 | // Fetches the SRS from target SRS (if set), or from the SRS of |
---|
484 | // the first layer and use it when creating the |
---|
485 | // tileindex layer. |
---|
486 | poSrcSpatialRef = poTargetSRS->Clone(); |
---|
487 | } |
---|
488 | else if( nFirstSourceDataset < nArgc ) |
---|
489 | { |
---|
490 | #ifdef ZOO_SERVICE |
---|
491 | fprintf(log,"**** %s %d %s \n",__FILE__,__LINE__,nFirstSourceDataset); |
---|
492 | fflush(log); |
---|
493 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
494 | fflush(log); |
---|
495 | tmpMap=getMapArray(tmpMaps->content,"value",0); |
---|
496 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
497 | fflush(log); |
---|
498 | //dumpMap(tmpMap); |
---|
499 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
500 | fflush(log); |
---|
501 | #endif |
---|
502 | GDALDataset* poDS = |
---|
503 | reinterpret_cast<GDALDataset*>( |
---|
504 | #ifndef ZOO_SERVICE |
---|
505 | OGROpen(papszArgv[nFirstSourceDataset], FALSE, nullptr) |
---|
506 | #else |
---|
507 | OGROpen(tmpMap->value, FALSE, nullptr) |
---|
508 | #endif |
---|
509 | ); |
---|
510 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
511 | fflush(log); |
---|
512 | if( poDS != nullptr ) |
---|
513 | { |
---|
514 | for( int iLayer = 0; |
---|
515 | iLayer < poDS->GetLayerCount(); |
---|
516 | iLayer++ ) |
---|
517 | { |
---|
518 | bool bRequested = bLayersWildcarded; |
---|
519 | OGRLayer *poLayer = poDS->GetLayer(iLayer); |
---|
520 | |
---|
521 | if( !bRequested ) |
---|
522 | continue; |
---|
523 | |
---|
524 | if( poLayer->GetSpatialRef() ) |
---|
525 | poSrcSpatialRef = poLayer->GetSpatialRef()->Clone(); |
---|
526 | break; |
---|
527 | } |
---|
528 | } |
---|
529 | |
---|
530 | GDALClose(poDS); |
---|
531 | } |
---|
532 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
533 | fflush(log); |
---|
534 | |
---|
535 | poDstLayer = poDstDS->CreateLayer("tileindex", poSrcSpatialRef); |
---|
536 | |
---|
537 | OGRFieldDefn oLocation(pszTileIndexField, OFTString); |
---|
538 | oLocation.SetWidth(200); |
---|
539 | poDstLayer->CreateField(&oLocation); |
---|
540 | |
---|
541 | if( pszSrcSRSName != nullptr ) |
---|
542 | { |
---|
543 | OGRFieldDefn oSrcSRSNameField(pszSrcSRSName, OFTString); |
---|
544 | poDstLayer->CreateField(&oSrcSRSNameField); |
---|
545 | } |
---|
546 | |
---|
547 | if( poSrcSpatialRef ) |
---|
548 | poSrcSpatialRef->Release(); |
---|
549 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
550 | fflush(log); |
---|
551 | } |
---|
552 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
553 | fflush(log); |
---|
554 | } |
---|
555 | |
---|
556 | /* -------------------------------------------------------------------- */ |
---|
557 | /* Identify target layer and field. */ |
---|
558 | /* -------------------------------------------------------------------- */ |
---|
559 | |
---|
560 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
561 | fflush(log); |
---|
562 | poDstLayer = poDstDS->GetLayer(0); |
---|
563 | if( poDstLayer == nullptr ) |
---|
564 | { |
---|
565 | fprintf(log, "Can't find any layer in output tileindex!\n"); |
---|
566 | exit(1); |
---|
567 | } |
---|
568 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
569 | fflush(log); |
---|
570 | |
---|
571 | const int iTileIndexField = |
---|
572 | poDstLayer->GetLayerDefn()->GetFieldIndex(pszTileIndexField); |
---|
573 | if( iTileIndexField == -1 ) |
---|
574 | { |
---|
575 | fprintf(log, "Can't find %s field in tile index dataset.\n", |
---|
576 | pszTileIndexField); |
---|
577 | exit(1); |
---|
578 | } |
---|
579 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
580 | fflush(log); |
---|
581 | |
---|
582 | if( pszSrcSRSName != nullptr ) |
---|
583 | i_SrcSRSName = poDstLayer->GetLayerDefn()->GetFieldIndex(pszSrcSRSName); |
---|
584 | |
---|
585 | OGRFeatureDefn* poFeatureDefn = nullptr; |
---|
586 | |
---|
587 | // Load in memory existing file names in SHP. |
---|
588 | char **existingLayersTab = nullptr; |
---|
589 | OGRSpatialReference* alreadyExistingSpatialRef = nullptr; |
---|
590 | bool alreadyExistingSpatialRefValid = false; |
---|
591 | const int nExistingLayers = static_cast<int>(poDstLayer->GetFeatureCount()); |
---|
592 | if( nExistingLayers ) |
---|
593 | { |
---|
594 | existingLayersTab = static_cast<char **>( |
---|
595 | CPLMalloc(nExistingLayers * sizeof(char*))); |
---|
596 | for( int i = 0; i < nExistingLayers; i++ ) |
---|
597 | { |
---|
598 | OGRFeature* feature = poDstLayer->GetNextFeature(); |
---|
599 | existingLayersTab[i] = |
---|
600 | CPLStrdup(feature->GetFieldAsString( iTileIndexField)); |
---|
601 | if( i == 0 ) |
---|
602 | { |
---|
603 | char* filename = CPLStrdup(existingLayersTab[i]); |
---|
604 | // j used after for. |
---|
605 | int j = static_cast<int>(strlen(filename)) - 1; |
---|
606 | for( ; j >= 0; j-- ) |
---|
607 | { |
---|
608 | if( filename[j] == ',' ) |
---|
609 | break; |
---|
610 | } |
---|
611 | GDALDataset *poDS = nullptr; |
---|
612 | if( j >= 0 ) |
---|
613 | { |
---|
614 | const int iLayer = atoi(filename + j + 1); |
---|
615 | filename[j] = 0; |
---|
616 | poDS = reinterpret_cast<GDALDataset *>( |
---|
617 | OGROpen(filename, FALSE, nullptr)); |
---|
618 | if( poDS != nullptr ) |
---|
619 | { |
---|
620 | OGRLayer *poLayer = poDS->GetLayer(iLayer); |
---|
621 | if( poLayer ) |
---|
622 | { |
---|
623 | alreadyExistingSpatialRefValid = true; |
---|
624 | alreadyExistingSpatialRef = |
---|
625 | poLayer->GetSpatialRef() ? |
---|
626 | poLayer->GetSpatialRef()->Clone() : nullptr; |
---|
627 | |
---|
628 | if( poFeatureDefn == nullptr ) |
---|
629 | poFeatureDefn = |
---|
630 | poLayer->GetLayerDefn()->Clone(); |
---|
631 | } |
---|
632 | GDALClose(poDS); |
---|
633 | } |
---|
634 | } |
---|
635 | } |
---|
636 | } |
---|
637 | } |
---|
638 | |
---|
639 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
640 | fflush(log); |
---|
641 | if( write_absolute_path ) |
---|
642 | { |
---|
643 | current_path = CPLGetCurrentDir(); |
---|
644 | if( current_path == nullptr ) |
---|
645 | { |
---|
646 | fprintf(log, |
---|
647 | "This system does not support the CPLGetCurrentDir call. " |
---|
648 | "The option -write_absolute_path will have no effect\n"); |
---|
649 | write_absolute_path = false; |
---|
650 | } |
---|
651 | } |
---|
652 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
653 | fflush(log); |
---|
654 | /* ==================================================================== */ |
---|
655 | /* Process each input datasource in turn. */ |
---|
656 | /* ==================================================================== */ |
---|
657 | #ifdef ZOO_SERVICE |
---|
658 | maps* tmpMaps=getMaps(inputs,"files"); |
---|
659 | nFirstSourceDataset=0; |
---|
660 | #endif |
---|
661 | for( ; nFirstSourceDataset < nArgc; nFirstSourceDataset++ ) |
---|
662 | { |
---|
663 | #ifndef ZOO_SERVICE |
---|
664 | if( papszArgv[nFirstSourceDataset][0] == '-' ) |
---|
665 | { |
---|
666 | nFirstSourceDataset++; |
---|
667 | continue; |
---|
668 | } |
---|
669 | #else |
---|
670 | tmpMap=getMapArray(tmpMaps->content,"value",nFirstSourceDataset); |
---|
671 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
672 | fflush(log); |
---|
673 | //dumpMap(tmpMap); |
---|
674 | fprintf(log,"**** %s %d \n",__FILE__,__LINE__); |
---|
675 | fflush(log); |
---|
676 | |
---|
677 | #endif |
---|
678 | char* fileNameToWrite = nullptr; |
---|
679 | VSIStatBuf sStatBuf; |
---|
680 | if( write_absolute_path && |
---|
681 | #ifdef ZOO_SERVICE |
---|
682 | CPLIsFilenameRelative( tmpMap->value ) && |
---|
683 | VSIStat( tmpMap->value, &sStatBuf ) == 0 |
---|
684 | #else |
---|
685 | CPLIsFilenameRelative( papszArgv[nFirstSourceDataset] ) && |
---|
686 | VSIStat( papszArgv[nFirstSourceDataset], &sStatBuf ) == 0 |
---|
687 | #endif |
---|
688 | ) |
---|
689 | { |
---|
690 | fileNameToWrite = |
---|
691 | CPLStrdup(CPLProjectRelativeFilename( |
---|
692 | current_path, |
---|
693 | #ifdef ZOO_SERVICE |
---|
694 | tmpMap->value |
---|
695 | #else |
---|
696 | papszArgv[nFirstSourceDataset] |
---|
697 | #endif |
---|
698 | )); |
---|
699 | } |
---|
700 | else |
---|
701 | { |
---|
702 | #ifdef ZOO_SERVICE |
---|
703 | fileNameToWrite = CPLStrdup(tmpMap->value); |
---|
704 | #else |
---|
705 | fileNameToWrite = CPLStrdup(papszArgv[nFirstSourceDataset]); |
---|
706 | #endif |
---|
707 | } |
---|
708 | |
---|
709 | GDALDataset *poDS = reinterpret_cast<GDALDataset*>( |
---|
710 | OGROpen( |
---|
711 | #ifdef ZOO_SERVICE |
---|
712 | tmpMap->value |
---|
713 | #else |
---|
714 | papszArgv[nFirstSourceDataset] |
---|
715 | #endif |
---|
716 | , |
---|
717 | FALSE, |
---|
718 | nullptr ) |
---|
719 | ); |
---|
720 | |
---|
721 | if( poDS == nullptr ) |
---|
722 | { |
---|
723 | fprintf(log, "Failed to open dataset %s, skipping.\n", |
---|
724 | #ifndef ZOO_SERVICE |
---|
725 | papszArgv[nFirstSourceDataset] |
---|
726 | #else |
---|
727 | tmpMap->value |
---|
728 | #endif |
---|
729 | ); |
---|
730 | CPLFree(fileNameToWrite); |
---|
731 | continue; |
---|
732 | } |
---|
733 | |
---|
734 | /* -------------------------------------------------------------------- */ |
---|
735 | /* Check all layers, and see if they match requests. */ |
---|
736 | /* -------------------------------------------------------------------- */ |
---|
737 | for( int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++ ) |
---|
738 | { |
---|
739 | bool bRequested = bLayersWildcarded; |
---|
740 | OGRLayer *poLayer = poDS->GetLayer(iLayer); |
---|
741 | |
---|
742 | #ifndef ZOO_SERVICE |
---|
743 | for( int iArg = 1; iArg < nArgc && !bRequested; iArg++ ) |
---|
744 | { |
---|
745 | if( EQUAL(papszArgv[iArg], "-lnum") |
---|
746 | && atoi(papszArgv[iArg+1]) == iLayer ) |
---|
747 | bRequested = true; |
---|
748 | else if( EQUAL(papszArgv[iArg], "-lname" ) |
---|
749 | && EQUAL(papszArgv[iArg+1], |
---|
750 | poLayer->GetLayerDefn()->GetName()) ) |
---|
751 | bRequested = true; |
---|
752 | } |
---|
753 | #endif |
---|
754 | |
---|
755 | if( !bRequested ) |
---|
756 | continue; |
---|
757 | |
---|
758 | // Checks that the layer is not already in tileindex. |
---|
759 | int i = 0; // Used after for. |
---|
760 | for( ; i < nExistingLayers; i++ ) |
---|
761 | { |
---|
762 | // TODO(schwehr): Move this off of the stack. |
---|
763 | char szLocation[5000] = {}; |
---|
764 | snprintf(szLocation, sizeof(szLocation), "%s,%d", |
---|
765 | fileNameToWrite, iLayer); |
---|
766 | if( EQUAL(szLocation, existingLayersTab[i]) ) |
---|
767 | { |
---|
768 | fprintf(log, "Layer %d of %s is already in tileindex. " |
---|
769 | "Skipping it.\n", |
---|
770 | iLayer, szLocation); |
---|
771 | break; |
---|
772 | } |
---|
773 | } |
---|
774 | if( i != nExistingLayers ) |
---|
775 | { |
---|
776 | continue; |
---|
777 | } |
---|
778 | |
---|
779 | OGRSpatialReference* spatialRef = poLayer->GetSpatialRef(); |
---|
780 | // If not set target srs, test that the current file uses same |
---|
781 | // projection as others. |
---|
782 | if( !bSetTargetSRS ) |
---|
783 | { |
---|
784 | if( alreadyExistingSpatialRefValid ) |
---|
785 | { |
---|
786 | if( (spatialRef != nullptr && |
---|
787 | alreadyExistingSpatialRef != nullptr && |
---|
788 | spatialRef->IsSame(alreadyExistingSpatialRef) == |
---|
789 | FALSE) || |
---|
790 | ((spatialRef != nullptr) != |
---|
791 | (alreadyExistingSpatialRef != nullptr)) ) |
---|
792 | { |
---|
793 | fprintf( |
---|
794 | log, |
---|
795 | "Warning : layer %d of %s is not using the same " |
---|
796 | "projection system as other files in the " |
---|
797 | "tileindex. This may cause problems when using it " |
---|
798 | "in MapServer for example.%s\n", |
---|
799 | iLayer, |
---|
800 | #ifdef ZOO_SERVICE |
---|
801 | tmpMap->value |
---|
802 | #else |
---|
803 | papszArgv[nFirstSourceDataset] |
---|
804 | #endif |
---|
805 | , |
---|
806 | skip_different_projection ? " Skipping it" : ""); |
---|
807 | if( skip_different_projection ) |
---|
808 | { |
---|
809 | continue; |
---|
810 | } |
---|
811 | } |
---|
812 | } |
---|
813 | else |
---|
814 | { |
---|
815 | alreadyExistingSpatialRefValid = true; |
---|
816 | alreadyExistingSpatialRef = |
---|
817 | spatialRef ? spatialRef->Clone() : nullptr; |
---|
818 | } |
---|
819 | } |
---|
820 | |
---|
821 | /* -------------------------------------------------------------------- */ |
---|
822 | /* Check if all layers in dataset have the same attributes schema. */ |
---|
823 | /* -------------------------------------------------------------------- */ |
---|
824 | if( poFeatureDefn == nullptr ) |
---|
825 | { |
---|
826 | poFeatureDefn = poLayer->GetLayerDefn()->Clone(); |
---|
827 | } |
---|
828 | else if( !accept_different_schemas ) |
---|
829 | { |
---|
830 | OGRFeatureDefn* poFeatureDefnCur = poLayer->GetLayerDefn(); |
---|
831 | assert(nullptr != poFeatureDefnCur); |
---|
832 | |
---|
833 | const int fieldCount = poFeatureDefnCur->GetFieldCount(); |
---|
834 | |
---|
835 | if( fieldCount != poFeatureDefn->GetFieldCount()) |
---|
836 | { |
---|
837 | fprintf( log, "Number of attributes of layer %s of %s " |
---|
838 | "does not match ... skipping it.\n", |
---|
839 | poLayer->GetLayerDefn()->GetName(), |
---|
840 | #ifdef ZOO_SERVICE |
---|
841 | tmpMap->value |
---|
842 | #else |
---|
843 | papszArgv[nFirstSourceDataset] |
---|
844 | #endif |
---|
845 | ); |
---|
846 | if( bFirstWarningForNonMatchingAttributes ) |
---|
847 | { |
---|
848 | fprintf( |
---|
849 | log, "Note : you can override this " |
---|
850 | "behaviour with -accept_different_schemas option\n" |
---|
851 | "but this may result in a tileindex incompatible " |
---|
852 | "with MapServer\n"); |
---|
853 | bFirstWarningForNonMatchingAttributes = false; |
---|
854 | } |
---|
855 | continue; |
---|
856 | } |
---|
857 | |
---|
858 | bool bSkip = false; |
---|
859 | for( int fn = 0; fn < poFeatureDefnCur->GetFieldCount(); fn++ ) |
---|
860 | { |
---|
861 | OGRFieldDefn* poField = poFeatureDefn->GetFieldDefn(fn); |
---|
862 | OGRFieldDefn* poFieldCur = |
---|
863 | poFeatureDefnCur->GetFieldDefn(fn); |
---|
864 | |
---|
865 | // XXX - Should those pointers be checked against NULL? |
---|
866 | assert(nullptr != poField); |
---|
867 | assert(nullptr != poFieldCur); |
---|
868 | |
---|
869 | if( poField->GetType() != poFieldCur->GetType() |
---|
870 | || poField->GetWidth() != poFieldCur->GetWidth() |
---|
871 | || poField->GetPrecision() != poFieldCur->GetPrecision() |
---|
872 | || !EQUAL( poField->GetNameRef(), |
---|
873 | poFieldCur->GetNameRef() ) ) |
---|
874 | { |
---|
875 | fprintf( |
---|
876 | log, "Schema of attributes of layer %s of %s " |
---|
877 | "does not match. Skipping it.\n", |
---|
878 | poLayer->GetLayerDefn()->GetName(), |
---|
879 | #ifdef ZOO_SERVICE |
---|
880 | tmpMap->value |
---|
881 | #else |
---|
882 | papszArgv[nFirstSourceDataset] |
---|
883 | #endif |
---|
884 | ); |
---|
885 | if( bFirstWarningForNonMatchingAttributes ) |
---|
886 | { |
---|
887 | fprintf( |
---|
888 | log, "Note : you can override this " |
---|
889 | "behaviour with -accept_different_schemas " |
---|
890 | "option,\nbut this may result in a tileindex " |
---|
891 | "incompatible with MapServer\n"); |
---|
892 | bFirstWarningForNonMatchingAttributes = false; |
---|
893 | } |
---|
894 | bSkip = true; |
---|
895 | break; |
---|
896 | } |
---|
897 | } |
---|
898 | |
---|
899 | if( bSkip ) |
---|
900 | continue; |
---|
901 | } |
---|
902 | |
---|
903 | /* -------------------------------------------------------------------- */ |
---|
904 | /* Get layer extents, and create a corresponding polygon */ |
---|
905 | /* geometry. */ |
---|
906 | /* -------------------------------------------------------------------- */ |
---|
907 | OGREnvelope sExtents; |
---|
908 | |
---|
909 | if( poLayer->GetExtent( &sExtents, TRUE ) != OGRERR_NONE ) |
---|
910 | { |
---|
911 | fprintf(log, |
---|
912 | "GetExtent() failed on layer %s of %s, skipping.\n", |
---|
913 | poLayer->GetLayerDefn()->GetName(), |
---|
914 | #ifdef ZOO_SERVICE |
---|
915 | tmpMap->value |
---|
916 | #else |
---|
917 | papszArgv[nFirstSourceDataset] |
---|
918 | #endif |
---|
919 | ); |
---|
920 | continue; |
---|
921 | } |
---|
922 | |
---|
923 | OGRLinearRing oRing; |
---|
924 | oRing.addPoint(sExtents.MinX, sExtents.MinY); |
---|
925 | oRing.addPoint(sExtents.MinX, sExtents.MaxY); |
---|
926 | oRing.addPoint(sExtents.MaxX, sExtents.MaxY); |
---|
927 | oRing.addPoint(sExtents.MaxX, sExtents.MinY); |
---|
928 | oRing.addPoint(sExtents.MinX, sExtents.MinY); |
---|
929 | |
---|
930 | OGRPolygon oRegion; |
---|
931 | oRegion.addRing(&oRing); |
---|
932 | |
---|
933 | // If set target srs, do the forward transformation of all points. |
---|
934 | if( bSetTargetSRS && spatialRef != nullptr ) |
---|
935 | { |
---|
936 | OGRCoordinateTransformation* poCT = nullptr; |
---|
937 | if( !spatialRef->IsSame(poTargetSRS) ) |
---|
938 | { |
---|
939 | poCT = OGRCreateCoordinateTransformation(spatialRef, |
---|
940 | poTargetSRS); |
---|
941 | if( poCT == nullptr || |
---|
942 | oRegion.transform(poCT) == OGRERR_FAILURE ) |
---|
943 | { |
---|
944 | char* pszSourceWKT = nullptr; |
---|
945 | spatialRef->exportToWkt(&pszSourceWKT); |
---|
946 | fprintf( |
---|
947 | log, |
---|
948 | "Warning : unable to transform points from source " |
---|
949 | "SRS `%s' to target SRS `%s'\n" |
---|
950 | "for file `%s' - file skipped\n", |
---|
951 | pszSourceWKT, pszTargetSRS, |
---|
952 | #ifdef ZOO_SERVICE |
---|
953 | tmpMap->value |
---|
954 | #else |
---|
955 | papszArgv[nFirstSourceDataset] |
---|
956 | #endif |
---|
957 | ); |
---|
958 | CPLFree(pszSourceWKT); |
---|
959 | delete poCT; |
---|
960 | continue; |
---|
961 | } |
---|
962 | delete poCT; |
---|
963 | } |
---|
964 | } |
---|
965 | |
---|
966 | /* -------------------------------------------------------------------- */ |
---|
967 | /* Add layer to tileindex. */ |
---|
968 | /* -------------------------------------------------------------------- */ |
---|
969 | OGRFeature oTileFeat(poDstLayer->GetLayerDefn()); |
---|
970 | |
---|
971 | // TODO(schwehr): Move this off of the stack. |
---|
972 | char szLocation[5000] = {}; |
---|
973 | snprintf(szLocation, sizeof(szLocation), "%s,%d", |
---|
974 | fileNameToWrite, iLayer); |
---|
975 | oTileFeat.SetGeometry(&oRegion); |
---|
976 | oTileFeat.SetField(iTileIndexField, szLocation); |
---|
977 | |
---|
978 | if( i_SrcSRSName >= 0 && spatialRef != nullptr ) |
---|
979 | { |
---|
980 | const char* pszAuthorityCode = |
---|
981 | spatialRef->GetAuthorityCode(nullptr); |
---|
982 | const char* pszAuthorityName = |
---|
983 | spatialRef->GetAuthorityName(nullptr); |
---|
984 | char* pszWKT = nullptr; |
---|
985 | spatialRef->exportToWkt(&pszWKT); |
---|
986 | if( eSrcSRSFormat == FORMAT_AUTO ) |
---|
987 | { |
---|
988 | if( pszAuthorityName != nullptr && |
---|
989 | pszAuthorityCode != nullptr ) |
---|
990 | { |
---|
991 | oTileFeat.SetField(i_SrcSRSName, |
---|
992 | CPLSPrintf("%s:%s", |
---|
993 | pszAuthorityName, pszAuthorityCode)); |
---|
994 | } |
---|
995 | else if( nMaxFieldSize == 0 || |
---|
996 | strlen(pszWKT) <= nMaxFieldSize ) |
---|
997 | { |
---|
998 | oTileFeat.SetField(i_SrcSRSName, pszWKT); |
---|
999 | } |
---|
1000 | else |
---|
1001 | { |
---|
1002 | char* pszProj4 = nullptr; |
---|
1003 | if( spatialRef->exportToProj4(&pszProj4) == |
---|
1004 | OGRERR_NONE ) |
---|
1005 | { |
---|
1006 | oTileFeat.SetField(i_SrcSRSName, pszProj4); |
---|
1007 | CPLFree(pszProj4); |
---|
1008 | } |
---|
1009 | else |
---|
1010 | { |
---|
1011 | oTileFeat.SetField(i_SrcSRSName, pszWKT); |
---|
1012 | } |
---|
1013 | } |
---|
1014 | } |
---|
1015 | else if( eSrcSRSFormat == FORMAT_WKT ) |
---|
1016 | { |
---|
1017 | if( nMaxFieldSize == 0 || |
---|
1018 | strlen(pszWKT) <= nMaxFieldSize ) |
---|
1019 | { |
---|
1020 | oTileFeat.SetField(i_SrcSRSName, pszWKT); |
---|
1021 | } |
---|
1022 | else |
---|
1023 | { |
---|
1024 | fprintf( |
---|
1025 | log, |
---|
1026 | "Cannot write WKT for file %s as it is too long!\n", |
---|
1027 | fileNameToWrite); |
---|
1028 | } |
---|
1029 | } |
---|
1030 | else if( eSrcSRSFormat == FORMAT_PROJ ) |
---|
1031 | { |
---|
1032 | char* pszProj4 = nullptr; |
---|
1033 | if( spatialRef->exportToProj4(&pszProj4) == OGRERR_NONE ) |
---|
1034 | { |
---|
1035 | oTileFeat.SetField(i_SrcSRSName, pszProj4); |
---|
1036 | CPLFree(pszProj4); |
---|
1037 | } |
---|
1038 | } |
---|
1039 | else if( eSrcSRSFormat == FORMAT_EPSG ) |
---|
1040 | { |
---|
1041 | if( pszAuthorityName != nullptr && |
---|
1042 | pszAuthorityCode != nullptr ) |
---|
1043 | oTileFeat.SetField(i_SrcSRSName, |
---|
1044 | CPLSPrintf("%s:%s", |
---|
1045 | pszAuthorityName, pszAuthorityCode)); |
---|
1046 | } |
---|
1047 | CPLFree(pszWKT); |
---|
1048 | } |
---|
1049 | if( poDstLayer->CreateFeature(&oTileFeat) != OGRERR_NONE ) |
---|
1050 | { |
---|
1051 | fprintf(log, |
---|
1052 | "Failed to create feature on tile index. " |
---|
1053 | "Terminating."); |
---|
1054 | GDALClose(poDstDS); |
---|
1055 | exit(1); |
---|
1056 | } |
---|
1057 | } |
---|
1058 | |
---|
1059 | /* -------------------------------------------------------------------- */ |
---|
1060 | /* Cleanup this data source. */ |
---|
1061 | /* -------------------------------------------------------------------- */ |
---|
1062 | CPLFree(fileNameToWrite); |
---|
1063 | GDALClose(poDS); |
---|
1064 | } |
---|
1065 | |
---|
1066 | /* -------------------------------------------------------------------- */ |
---|
1067 | /* Close tile index and clear buffers. */ |
---|
1068 | /* -------------------------------------------------------------------- */ |
---|
1069 | GDALClose(poDstDS); |
---|
1070 | OGRFeatureDefn::DestroyFeatureDefn(poFeatureDefn); |
---|
1071 | |
---|
1072 | if( alreadyExistingSpatialRef != nullptr ) |
---|
1073 | alreadyExistingSpatialRef->Release(); |
---|
1074 | delete poTargetSRS; |
---|
1075 | |
---|
1076 | CPLFree(current_path); |
---|
1077 | |
---|
1078 | if( nExistingLayers ) |
---|
1079 | { |
---|
1080 | for( int i = 0; i < nExistingLayers; i++ ) |
---|
1081 | { |
---|
1082 | CPLFree(existingLayersTab[i]); |
---|
1083 | } |
---|
1084 | CPLFree(existingLayersTab); |
---|
1085 | } |
---|
1086 | |
---|
1087 | OGRCleanupAll(); |
---|
1088 | #ifdef ZOO_SERVICE |
---|
1089 | //setMapInMaps(outputs,"Result","generated_file",pszOutputName); |
---|
1090 | setMapInMaps(outputs,"Result","value",pszOutputName); |
---|
1091 | return SERVICE_SUCCEEDED; |
---|
1092 | #else |
---|
1093 | return 0; |
---|
1094 | #endif |
---|
1095 | } |
---|
1096 | #ifndef ZOO_SERVICE |
---|
1097 | MAIN_END |
---|
1098 | #endif |
---|
1099 | |
---|
1100 | #ifdef ZOO_SERVICE |
---|
1101 | } |
---|
1102 | #endif |
---|