source: trunk/zoo-project/zoo-services/ogr/ogr2ogr/service.c @ 790

Last change on this file since 790 was 790, checked in by djay, 7 years ago

Add support for nested inputs and outputs.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 46.7 KB
Line 
1/******************************************************************************
2 * $Id: ogr2ogr.cpp 15473 2008-10-07 20:59:24Z warmerdam $
3 *
4 * Project:  OpenGIS Simple Features Reference Implementation
5 * Purpose:  Simple client for translating between formats.
6 * Author:   Frank Warmerdam, warmerdam@pobox.com
7 *
8 ******************************************************************************
9 * Copyright (c) 1999, Frank Warmerdam
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ****************************************************************************/
29
30#include "ogrsf_frmts.h"
31#include "ogr_p.h"
32#include "cpl_conv.h"
33#include "cpl_string.h"
34#include "ogr_api.h"
35#if GDAL_VERSION_MAJOR >= 2
36#include <gdal_priv.h>
37#endif
38#ifdef ZOO_SERVICE
39#include "service.h"
40#endif
41
42CPL_CVSID("$Id: ogr2ogr.cpp 15473 2008-10-07 20:59:24Z warmerdam $");
43
44#ifdef WIN32
45#define strcasecmp _stricmp
46#define strncasecmp _strnicmp
47#endif
48
49#ifdef ZOO_SERVICE
50extern "C" {
51#endif
52
53static void Usage();
54
55static int TranslateLayer(
56#if GDAL_VERSION_MAJOR >= 2
57                          GDALDataset
58#else
59                          OGRDataSource
60#endif
61                          *poSrcDS, 
62                          OGRLayer * poSrcLayer,
63#if GDAL_VERSION_MAJOR >= 2
64                          GDALDataset
65#else
66                          OGRDataSource
67#endif
68                          *poDstDS,
69                          char ** papszLSCO,
70                          const char *pszNewLayerName,
71                          int bTransform, 
72                          OGRSpatialReference *poOutputSRS,
73                          OGRSpatialReference *poSourceSRS,
74                          char **papszSelFields,
75                          int bAppend, int eGType,
76                          int bOverwrite,
77                          double dfMaxSegmentLength
78                           );
79 
80static int bSkipFailures = FALSE;
81static int nGroupTransactions = 200;
82static int bPreserveFID = TRUE;
83static int nFIDToFetch = OGRNullFID;
84
85/************************************************************************/
86/*                                main()                                */
87/************************************************************************/
88
89#ifdef ZOO_SERVICE
90#ifdef WIN32
91__declspec(dllexport)
92#endif
93int Ogr2Ogr(maps*& conf,maps*& inputs,maps*& outputs)
94#else
95int main( int nArgc, char ** papszArgv )
96#endif
97{
98    const char  *pszFormat = "ESRI Shapefile";
99    const char  *pszDataSource = NULL;
100    const char  *pszDestDataSource = NULL;
101    const char  *pszwebDestData = NULL;
102    char        **papszLayers = NULL;
103    char        **papszDSCO = NULL, **papszLCO = NULL;
104    int         bTransform = FALSE;
105    int         bAppend = FALSE, bUpdate = FALSE, bOverwrite = FALSE;
106    const char  *pszOutputSRSDef = NULL;
107    const char  *pszSourceSRSDef = NULL;
108    OGRSpatialReference *poOutputSRS = NULL;
109    OGRSpatialReference *poSourceSRS = NULL;
110    const char  *pszNewLayerName = NULL;
111    const char  *pszWHERE = NULL;
112    OGRGeometry *poSpatialFilter = NULL;
113    const char  *pszSelect;
114    char        **papszSelFields = NULL;
115    const char  *pszSQLStatement = NULL;
116    int         eGType = -2;
117    double      dfMaxSegmentLength = 0;
118
119#ifdef ZOO_SERVICE
120    dumpMaps(inputs);
121#endif
122    /* Check strict compilation and runtime library version as we use C++ API */
123    if (! GDAL_CHECK_VERSION("ogr2ogr"))
124#ifdef ZOO_SERVICE
125        {
126                fprintf(stderr,"Not correct version of the gdal library\n");
127                setMapInMaps(conf,"lenv","message","Unable to check gdal version for ogr2ogr_service.zo");
128                return SERVICE_FAILED;
129        }
130#else
131        exit(1);
132#endif
133/* -------------------------------------------------------------------- */
134/*      Register format(s).                                             */
135/* -------------------------------------------------------------------- */
136    OGRRegisterAll();
137
138#ifdef ZOO_SERVICE
139    map *tmpMap=NULL;
140    char dataPath[1024];
141    tmpMap=getMapFromMaps(conf,"main","dataPath");
142    if(tmpMap!=NULL)
143      sprintf(dataPath,"%s",tmpMap->value);
144    tmpMap=NULL;
145    char tempPath[1024];
146    tmpMap=getMapFromMaps(conf,"main","tmpPath");
147    if(tmpMap!=NULL){
148      sprintf(tempPath,"%s",tmpMap->value);
149    }
150   
151    tmpMap=NULL;
152    char serverAddress[1024];
153    tmpMap=getMapFromMaps(conf,"main","serverAddress");
154    if(tmpMap!=NULL){
155      sprintf(serverAddress,"%s",tmpMap->value);
156    }
157   
158    tmpMap=NULL;
159    char tmpurl[1024];
160    tmpMap=getMapFromMaps(conf,"main","tmpurl");
161    if(tmpMap!=NULL){
162      sprintf(tmpurl,"%s",tmpMap->value);
163    }
164
165    tmpMap=NULL;
166    tmpMap=getMapFromMaps(inputs,"F","value");
167    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
168      pszFormat=tmpMap->value;
169    }
170
171    tmpMap=NULL;
172    tmpMap=getMapFromMaps(inputs,"DSCO","value");
173    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
174          papszDSCO = CSLAddString(papszDSCO, tmpMap->value );
175    }
176
177    tmpMap=NULL;
178    tmpMap=getMapFromMaps(inputs,"LCO","value");
179    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
180          papszLCO = CSLAddString(papszLCO, tmpMap->value );
181    }
182
183    tmpMap=NULL;
184    tmpMap=getMapFromMaps(inputs,"preserve_fid","value");
185    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
186          bPreserveFID = TRUE;
187    }
188
189    tmpMap=NULL;
190    tmpMap=getMapFromMaps(inputs,"skipfailure","value");
191    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
192      bSkipFailures = TRUE;
193      nGroupTransactions = 1; /* #2409 */
194    }
195
196    /* if exist, overwrite the data with the same name */
197    tmpMap=NULL;
198    tmpMap=getMapFromMaps(inputs,"overwrite","value");
199    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
200      bOverwrite = TRUE;
201    }
202
203    tmpMap=NULL;
204    tmpMap=getMapFromMaps(inputs,"append","value");
205    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"TRUE",4)==0){
206      bAppend = TRUE;
207    }
208
209    tmpMap=NULL;
210    tmpMap=getMapFromMaps(inputs,"update","value");
211    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"TRUE",4)==0){
212      bUpdate = TRUE;
213    }
214
215    tmpMap=NULL;
216    tmpMap=getMapFromMaps(inputs,"fid","value");
217    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
218      nFIDToFetch = atoi(tmpMap->value);
219    }
220
221    tmpMap=NULL;
222    tmpMap=getMapFromMaps(inputs,"sql","value");
223    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
224      pszSQLStatement = tmpMap->value;
225    }
226
227    tmpMap=NULL;
228    tmpMap=getMapFromMaps(inputs,"nln","value");
229    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
230          pszNewLayerName = tmpMap->value;
231    }
232
233    tmpMap=NULL;
234    tmpMap=getMapFromMaps(inputs,"nlt","value");
235    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
236          pszNewLayerName = tmpMap->value;
237          if( EQUAL(tmpMap->value,"NONE") )
238                  eGType = wkbNone;
239          else if( EQUAL(tmpMap->value,"GEOMETRY") )
240                  eGType = wkbUnknown;
241          else if( EQUAL(tmpMap->value,"POINT") )
242                  eGType = wkbPoint;
243          else if( EQUAL(tmpMap->value,"LINESTRING") )
244                  eGType = wkbLineString;
245          else if( EQUAL(tmpMap->value,"POLYGON") )
246                  eGType = wkbPolygon;
247          else if( EQUAL(tmpMap->value,"GEOMETRYCOLLECTION") )
248                  eGType = wkbGeometryCollection;
249          else if( EQUAL(tmpMap->value,"MULTIPOINT") )
250                  eGType = wkbMultiPoint;
251          else if( EQUAL(tmpMap->value,"MULTILINESTRING") )
252                  eGType = wkbMultiLineString;
253          else if( EQUAL(tmpMap->value,"MULTIPOLYGON") )
254                  eGType = wkbMultiPolygon;
255          else if( EQUAL(tmpMap->value,"GEOMETRY25D") )
256                  eGType = wkbUnknown | wkb25DBit;
257          else if( EQUAL(tmpMap->value,"POINT25D") )
258                  eGType = wkbPoint25D;
259          else if( EQUAL(tmpMap->value,"LINESTRING25D") )
260                  eGType = wkbLineString25D;
261          else if( EQUAL(tmpMap->value,"POLYGON25D") )
262                  eGType = wkbPolygon25D;
263          else if( EQUAL(tmpMap->value,"GEOMETRYCOLLECTION25D") )
264                  eGType = wkbGeometryCollection25D;
265          else if( EQUAL(tmpMap->value,"MULTIPOINT25D") )
266                  eGType = wkbMultiPoint25D;
267          else if( EQUAL(tmpMap->value,"MULTILINESTRING25D") )
268                  eGType = wkbMultiLineString25D;
269          else if( EQUAL(tmpMap->value,"MULTIPOLYGON25D") )
270                  eGType = wkbMultiPolygon25D;
271          else   
272          {
273                  fprintf( stderr, "-nlt %s: type not recognised.\n", 
274                          tmpMap->value );
275                  exit( 1 );
276          }
277    }
278
279    tmpMap=NULL;
280    tmpMap=getMapFromMaps(inputs,"tg","value");
281    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
282          nGroupTransactions = atoi(tmpMap->value);
283    }
284
285    tmpMap=NULL;
286    tmpMap=getMapFromMaps(inputs,"s_srs","value");
287    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
288      pszSourceSRSDef = strdup(tmpMap->value);
289    }
290
291    tmpMap=NULL;
292    tmpMap=getMapFromMaps(inputs,"a_srs","value");
293    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
294      pszOutputSRSDef = strdup(tmpMap->value);
295    }
296
297    tmpMap=NULL;
298    tmpMap=getMapFromMaps(inputs,"t_srs","value");
299    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
300      pszOutputSRSDef = strdup(tmpMap->value);
301      bTransform = TRUE;
302    }
303
304    tmpMap=NULL;
305    tmpMap=getMapFromMaps(inputs,"SPAT","value");
306    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
307      char *tmp=tmpMap->value;
308      char *t=strtok(tmp,",");
309      int cnt=0;
310      double dfULX, dfULY, dfLRX, dfLRY;
311      while(t!=NULL){
312        switch(cnt){
313        case 0:
314          dfULX = atof(t);
315          break;
316        case 1:
317          dfULY = atof(t);
318          break;
319        case 2:
320          dfLRX = atof(t);
321          break;
322        case 3:
323          dfLRY = atof(t);
324          break;
325        }
326        fprintf(stderr,"%s\n\n",t);
327        fprintf(stderr,"%f - %f - %f - %f\n\n",dfULX,dfULY,dfLRX,dfLRY);
328        t=strtok(NULL,",");
329        cnt++;
330      }
331
332      OGRLinearRing  oRing;
333     
334      oRing.addPoint( dfULX, dfULY );
335      oRing.addPoint( dfULX, dfLRY );
336      oRing.addPoint( dfLRX, dfLRY );
337      oRing.addPoint( dfLRX, dfULY );
338      oRing.addPoint( dfULX, dfULY );
339      poSpatialFilter = new OGRPolygon();
340      ((OGRPolygon *) poSpatialFilter)->addRing( &oRing );
341    }
342
343    tmpMap=NULL;
344    tmpMap=getMapFromMaps(inputs,"where","value");
345    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
346          pszWHERE = tmpMap->value;
347    }
348
349    tmpMap=NULL;
350    tmpMap=getMapFromMaps(inputs,"select","value");
351    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
352          pszSelect = tmpMap->value;
353          papszSelFields = CSLTokenizeStringComplex(pszSelect, " ,", 
354                  FALSE, FALSE );
355    }
356
357    tmpMap=NULL;
358    tmpMap=getMapFromMaps(inputs,"segmentize","value");
359    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
360          dfMaxSegmentLength = atof(tmpMap->value);
361    }
362
363    /*tmpMap=NULL;
364    tmpMap=getMapFromMaps(inputs,"segmentize","value");
365    if(tmpMap!=NULL){
366          dfMaxSegmentLength = atof(tmpMap->value);
367    }*/
368
369    tmpMap=NULL;
370    tmpMap=getMapFromMaps(inputs,"InputDSN","value");
371    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
372      if(strncasecmp(tmpMap->value,"PG",2)!=0 &&
373         strncasecmp(tmpMap->value,"My",2)!=0 &&
374         strncasecmp(tmpMap->value,"OCI",3)!=0 ){
375        if(strncasecmp(dataPath,tmpMap->value,strlen(dataPath))==0 ||
376           strncasecmp(tempPath,tmpMap->value,strlen(tempPath))==0){
377          pszDataSource=strdup(tmpMap->value);
378        }else{
379          pszDataSource=(char*)malloc(sizeof(char)*(strlen(dataPath)+strlen(tmpMap->value)+2));
380          sprintf((char*)pszDataSource,"%s/%s",dataPath,tmpMap->value);
381        }
382      }else{
383        pszDataSource=(char*)malloc(sizeof(char)*(strlen(tmpMap->value)+1));
384        sprintf((char*)pszDataSource,"%s",tmpMap->value);       
385      }
386    }
387
388    tmpMap=NULL;
389    tmpMap=getMapFromMaps(inputs,"OutputDSN","value");
390    if(tmpMap!=NULL){
391      if(strncasecmp(tmpMap->value,"PG",2)!=0 &&
392         strncasecmp(tmpMap->value,"MY",2)!=0 &&
393         strncasecmp(tmpMap->value,"OCI",3)!=0){
394        pszDestDataSource=(char*)malloc(sizeof(char)*(strlen(tempPath)+strlen(tmpMap->value)+4));
395        sprintf((char*)pszDestDataSource,"%s/%s",tempPath,tmpMap->value/*,ext*/);
396        pszwebDestData=(char*)malloc(sizeof(char)*(strlen(serverAddress)+strlen(tmpurl)+strlen(tmpMap->value)+4));
397        sprintf((char*)pszwebDestData,"%s%s/%s",serverAddress,tmpurl,tmpMap->value/*,ext*/);
398      }
399      else{
400        pszDestDataSource=(char*)malloc(sizeof(char)*(strlen(tmpMap->value)+1));
401        sprintf((char*)pszDestDataSource,"%s",tmpMap->value/*,ext*/);
402        pszwebDestData=(char*)malloc(sizeof(char)*(strlen(serverAddress)+strlen(tmpurl)+strlen(tmpMap->value)+4));
403        sprintf((char*)pszwebDestData,"%s%s/%s",serverAddress,tmpurl,tmpMap->value/*,ext*/);
404      }
405    }
406
407    fprintf(stderr,"Message %s\n",pszDestDataSource);
408#else
409/* -------------------------------------------------------------------- */
410/*      Processing command line arguments.                              */
411/* -------------------------------------------------------------------- */
412    nArgc = OGRGeneralCmdLineProcessor( nArgc, &papszArgv, 0 );
413   
414    if( nArgc < 1 )
415        exit( -nArgc );
416
417    for( int iArg = 1; iArg < nArgc; iArg++ )
418    {
419        if( EQUAL(papszArgv[iArg], "--utility_version") )
420        {
421                                printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
422                   papszArgv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
423            return 0;
424        }
425        else if( EQUAL(papszArgv[iArg],"-f") && iArg < nArgc-1 )
426        {
427            pszFormat = papszArgv[++iArg];
428        }
429        else if( EQUAL(papszArgv[iArg],"-dsco") && iArg < nArgc-1 )
430        {
431            papszDSCO = CSLAddString(papszDSCO, papszArgv[++iArg] );
432        }
433        else if( EQUAL(papszArgv[iArg],"-lco") && iArg < nArgc-1 )
434        {
435            papszLCO = CSLAddString(papszLCO, papszArgv[++iArg] );
436        }
437        else if( EQUAL(papszArgv[iArg],"-preserve_fid") )
438        {
439            bPreserveFID = TRUE;
440        }
441        else if( EQUALN(papszArgv[iArg],"-skip",5) )
442        {
443            bSkipFailures = TRUE;
444            nGroupTransactions = 1; /* #2409 */
445        }
446        else if( EQUAL(papszArgv[iArg],"-append") )
447        {
448            bAppend = TRUE;
449        }
450        else if( EQUAL(papszArgv[iArg],"-overwrite") )
451        {
452            bOverwrite = TRUE;
453        }
454        else if( EQUAL(papszArgv[iArg],"-update") )
455        {
456            bUpdate = TRUE;
457        }
458        else if( EQUAL(papszArgv[iArg],"-fid") && papszArgv[iArg+1] != NULL )
459        {
460            nFIDToFetch = atoi(papszArgv[++iArg]);
461        }
462        else if( EQUAL(papszArgv[iArg],"-sql") && papszArgv[iArg+1] != NULL )
463        {
464            pszSQLStatement = papszArgv[++iArg];
465        }
466        else if( EQUAL(papszArgv[iArg],"-nln") && iArg < nArgc-1 )
467        {
468            pszNewLayerName = papszArgv[++iArg];
469        }
470        else if( EQUAL(papszArgv[iArg],"-nlt") && iArg < nArgc-1 )
471        {
472            if( EQUAL(papszArgv[iArg+1],"NONE") )
473                eGType = wkbNone;
474            else if( EQUAL(papszArgv[iArg+1],"GEOMETRY") )
475                eGType = wkbUnknown;
476            else if( EQUAL(papszArgv[iArg+1],"POINT") )
477                eGType = wkbPoint;
478            else if( EQUAL(papszArgv[iArg+1],"LINESTRING") )
479                eGType = wkbLineString;
480            else if( EQUAL(papszArgv[iArg+1],"POLYGON") )
481                eGType = wkbPolygon;
482            else if( EQUAL(papszArgv[iArg+1],"GEOMETRYCOLLECTION") )
483                eGType = wkbGeometryCollection;
484            else if( EQUAL(papszArgv[iArg+1],"MULTIPOINT") )
485                eGType = wkbMultiPoint;
486            else if( EQUAL(papszArgv[iArg+1],"MULTILINESTRING") )
487                eGType = wkbMultiLineString;
488            else if( EQUAL(papszArgv[iArg+1],"MULTIPOLYGON") )
489                eGType = wkbMultiPolygon;
490            else if( EQUAL(papszArgv[iArg+1],"GEOMETRY25D") )
491                eGType = wkbUnknown | wkb25DBit;
492            else if( EQUAL(papszArgv[iArg+1],"POINT25D") )
493                eGType = wkbPoint25D;
494            else if( EQUAL(papszArgv[iArg+1],"LINESTRING25D") )
495                eGType = wkbLineString25D;
496            else if( EQUAL(papszArgv[iArg+1],"POLYGON25D") )
497                eGType = wkbPolygon25D;
498            else if( EQUAL(papszArgv[iArg+1],"GEOMETRYCOLLECTION25D") )
499                eGType = wkbGeometryCollection25D;
500            else if( EQUAL(papszArgv[iArg+1],"MULTIPOINT25D") )
501                eGType = wkbMultiPoint25D;
502            else if( EQUAL(papszArgv[iArg+1],"MULTILINESTRING25D") )
503                eGType = wkbMultiLineString25D;
504            else if( EQUAL(papszArgv[iArg+1],"MULTIPOLYGON25D") )
505                eGType = wkbMultiPolygon25D;
506            else
507            {
508                fprintf( stderr, "-nlt %s: type not recognised.\n", 
509                         papszArgv[iArg+1] );
510                exit( 1 );
511            }
512            iArg++;
513        }
514        else if( (EQUAL(papszArgv[iArg],"-tg") ||
515                  EQUAL(papszArgv[iArg],"-gt")) && iArg < nArgc-1 )
516        {
517            nGroupTransactions = atoi(papszArgv[++iArg]);
518        }
519        else if( EQUAL(papszArgv[iArg],"-s_srs") && iArg < nArgc-1 )
520        {
521            pszSourceSRSDef = papszArgv[++iArg];
522        }
523        else if( EQUAL(papszArgv[iArg],"-a_srs") && iArg < nArgc-1 )
524        {
525            pszOutputSRSDef = papszArgv[++iArg];
526        }
527        else if( EQUAL(papszArgv[iArg],"-t_srs") && iArg < nArgc-1 )
528        {
529            pszOutputSRSDef = papszArgv[++iArg];
530            bTransform = TRUE;
531        }
532        else if( EQUAL(papszArgv[iArg],"-spat") 
533                 && papszArgv[iArg+1] != NULL 
534                 && papszArgv[iArg+2] != NULL 
535                 && papszArgv[iArg+3] != NULL 
536                 && papszArgv[iArg+4] != NULL )
537        {
538            OGRLinearRing  oRing;
539
540            oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+2]) );
541            oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+4]) );
542            oRing.addPoint( atof(papszArgv[iArg+3]), atof(papszArgv[iArg+4]) );
543            oRing.addPoint( atof(papszArgv[iArg+3]), atof(papszArgv[iArg+2]) );
544            oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+2]) );
545
546            poSpatialFilter = new OGRPolygon();
547            ((OGRPolygon *) poSpatialFilter)->addRing( &oRing );
548            iArg += 4;
549        }
550        else if( EQUAL(papszArgv[iArg],"-where") && papszArgv[iArg+1] != NULL )
551        {
552            pszWHERE = papszArgv[++iArg];
553        }
554        else if( EQUAL(papszArgv[iArg],"-select") && papszArgv[iArg+1] != NULL)
555        {
556            pszSelect = papszArgv[++iArg];
557            papszSelFields = CSLTokenizeStringComplex(pszSelect, " ,", 
558                                                      FALSE, FALSE );
559        }
560        else if( EQUAL(papszArgv[iArg],"-segmentize") && iArg < nArgc-1 )
561        {
562            dfMaxSegmentLength = atof(papszArgv[++iArg]);
563        }
564        else if( papszArgv[iArg][0] == '-' )
565        {
566            Usage();
567        }
568        else if( pszDestDataSource == NULL )
569            pszDestDataSource = papszArgv[iArg];
570        else if( pszDataSource == NULL )
571            pszDataSource = papszArgv[iArg];
572        else
573            papszLayers = CSLAddString( papszLayers, papszArgv[iArg] );
574    }
575#endif
576
577    if( pszDataSource == NULL )
578#ifdef ZOO_SERVICE
579        {
580#else
581          Usage();
582#endif
583#ifdef ZOO_SERVICE
584          setMapInMaps(conf,"lenv","message","Wrong parameter");
585          return SERVICE_FAILED;
586        }
587#endif
588
589/* -------------------------------------------------------------------- */
590/*      Open data source.                                               */
591/* -------------------------------------------------------------------- */
592#if GDAL_VERSION_MAJOR >= 2
593      GDALDataset *poDS
594        = (GDALDataset*) GDALOpenEx( pszDataSource,
595                                     GDAL_OF_READONLY | GDAL_OF_VECTOR,
596                                     NULL, NULL, NULL );
597      GDALDataset *poODS;
598      GDALDriverManager* poR=GetGDALDriverManager();
599      GDALDriver          *poDriver = NULL;
600#else
601      OGRDataSource* poDS
602        = OGRSFDriverRegistrar::Open( pszDataSource, FALSE );
603      OGRDataSource *poODS;
604      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
605      OGRSFDriver          *poDriver = NULL;
606#endif
607
608/* -------------------------------------------------------------------- */
609/*      Report failure                                                  */
610/* -------------------------------------------------------------------- */
611    if( poDS == NULL )
612    {
613        fprintf( stderr, "FAILURE:\n"
614                "Unable to open datasource `%s' with the following drivers.\n",
615                pszDataSource );
616
617        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
618        {
619#if GDAL_VERSION_MAJOR >= 2
620            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
621#else
622            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
623#endif
624        }
625#ifdef ZOO_SERVICE
626        char tmp[1024];
627        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",pszDataSource);
628        setMapInMaps(conf,"lenv","message",tmp);
629        return SERVICE_FAILED;
630#else
631        exit( 1 );
632#endif
633    }
634
635/* -------------------------------------------------------------------- */
636/*      Try opening the output datasource as an existing, writable      */
637/* -------------------------------------------------------------------- */
638    if( bUpdate )
639    {
640        poODS = 
641#if GDAL_VERSION_MAJOR >= 2
642          (GDALDataset*) GDALOpenEx( pszDestDataSource,
643                                     GDAL_OF_UPDATE | GDAL_OF_VECTOR,
644                                     NULL, NULL, NULL )
645#else
646          OGRSFDriverRegistrar::Open( pszDestDataSource, TRUE )
647#endif
648          ;
649        if( poODS == NULL )
650        {
651            fprintf( stderr, "FAILURE:\n"
652                    "Unable to open existing output datasource `%s'.\n",
653                    pszDestDataSource );
654#ifdef ZOO_SERVICE
655            char tmp[1024];
656            sprintf(tmp,"Unable to open existing output datasource `%s'.",pszDestDataSource);
657            setMapInMaps(conf,"lenv","message",tmp);
658            return SERVICE_FAILED;
659#else
660        exit( 1 );
661#endif
662        }
663
664        if( CSLCount(papszDSCO) > 0 )
665        {
666            fprintf( stderr, "WARNING: Datasource creation options ignored since an existing datasource\n"
667                    "         being updated.\n" );
668        }
669    }
670
671/* -------------------------------------------------------------------- */
672/*      Find the output driver.                                         */
673/* -------------------------------------------------------------------- */
674    else
675    {
676        int                  iDriver;
677
678        for( iDriver = 0;
679             iDriver < poR->GetDriverCount() && poDriver == NULL;
680             iDriver++ )
681        {
682#if GDAL_VERSION_MAJOR >=2
683            if( EQUAL(poR->GetDriver(iDriver)->GetDescription(),pszFormat) )
684#else
685            if( EQUAL(poR->GetDriver(iDriver)->GetName(),pszFormat) )
686#endif
687            {
688                poDriver = poR->GetDriver(iDriver);
689            }
690        }
691
692        if( poDriver == NULL )
693        {
694            fprintf( stderr, "Unable to find driver `%s'.\n", pszFormat );
695            fprintf( stderr,  "The following drivers are available:\n" );
696       
697            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
698            {
699#if GDAL_VERSION_MAJOR >= 2
700              fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
701#else
702              fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
703#endif
704            }
705#ifdef ZOO_SERVICE
706            char tmp[1024];
707            sprintf(tmp,"Unable to find driver `%s'.",pszFormat);
708            setMapInMaps(conf,"lenv","message",tmp);
709            return SERVICE_FAILED;
710#else
711            exit( 1 );
712#endif
713        }
714
715#if GDAL_VERSION_MAJOR >=2
716        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
717#else
718        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
719#endif
720        {
721            fprintf( stderr,  "%s driver does not support data source creation.\n",
722                    pszFormat );
723#ifdef ZOO_SERVICE
724            char tmp[1024];
725            sprintf(tmp,"%s driver does not support data source creation.",pszFormat);
726            setMapInMaps(conf,"lenv","message",tmp);
727            return SERVICE_FAILED;
728#else
729            exit( 1 );
730#endif
731        }
732
733/* -------------------------------------------------------------------- */
734/*      Create the output data source.                                  */
735/* -------------------------------------------------------------------- */
736#if GDAL_VERSION_MAJOR >=2
737        poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO );
738#else
739        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
740#endif
741        if( poODS == NULL )
742        {
743          poODS = 
744#if GDAL_VERSION_MAJOR >= 2
745            (GDALDataset*) GDALOpenEx( pszDestDataSource,
746                                       GDAL_OF_UPDATE | GDAL_OF_VECTOR,
747                                       NULL, NULL, NULL )
748#else
749            OGRSFDriverRegistrar::Open( pszDestDataSource, TRUE )
750#endif
751            ;
752          if( poODS == NULL )
753            {
754              fprintf( stderr, "FAILURE:\n"
755                       "Unable to open existing output datasource `%s'.\n",
756                       pszDestDataSource );
757#ifdef ZOO_SERVICE
758              char tmp[1024];
759              sprintf(tmp,"Unable to open existing output datasource `%s'.",pszDestDataSource);
760              setMapInMaps(conf,"lenv","message",tmp);
761              return SERVICE_FAILED;
762#else
763              exit( 1 );
764#endif
765
766            fprintf( stderr,  "%s driver failed to create %s\n", 
767                    pszFormat, pszDestDataSource );
768            }
769        }
770    }
771
772/* -------------------------------------------------------------------- */
773/*      Parse the output SRS definition if possible.                    */
774/* -------------------------------------------------------------------- */
775    if( pszOutputSRSDef != NULL )
776    {
777        poOutputSRS = new OGRSpatialReference();
778        if( poOutputSRS->SetFromUserInput( pszOutputSRSDef ) != OGRERR_NONE )
779        {
780            fprintf( stderr,  "Failed to process SRS definition: %s\n", 
781                    pszOutputSRSDef );
782#ifdef ZOO_SERVICE
783            char tmp[1024];
784            sprintf(tmp,"Failed to process SRS definition: %s",pszOutputSRSDef);
785            setMapInMaps(conf,"lenv","message",tmp);
786            return SERVICE_FAILED;
787#else
788            exit( 1 );
789#endif
790        }
791    }
792
793/* -------------------------------------------------------------------- */
794/*      Parse the source SRS definition if possible.                    */
795/* -------------------------------------------------------------------- */
796    if( pszSourceSRSDef != NULL )
797    {
798        poSourceSRS = new OGRSpatialReference();
799        if( poSourceSRS->SetFromUserInput( pszSourceSRSDef ) != OGRERR_NONE )
800        {
801            fprintf( stderr,  "Failed to process SRS definition: %s\n", 
802                    pszSourceSRSDef );
803#ifdef ZOO_SERVICE
804            char tmp[1024];
805            sprintf(tmp,"Failed to process SRS definition: %s",pszOutputSRSDef);
806            setMapInMaps(conf,"lenv","message",tmp);
807            return SERVICE_FAILED;
808#else
809            exit( 1 );
810#endif
811        }
812    }
813
814/* -------------------------------------------------------------------- */
815/*      Special case for -sql clause.  No source layers required.       */
816/* -------------------------------------------------------------------- */
817    if( pszSQLStatement != NULL )
818    {
819        OGRLayer *poResultSet;
820
821        if( pszWHERE != NULL )
822            fprintf( stderr,  "-where clause ignored in combination with -sql.\n" );
823        if( CSLCount(papszLayers) > 0 )
824          fprintf( stderr,  "layer names ignored in combination with -sql. (%s)\n", pszSQLStatement);
825       
826        poResultSet = poDS->ExecuteSQL( pszSQLStatement, poSpatialFilter, 
827                                        NULL );
828
829        if( poResultSet != NULL )
830        {
831            if( !TranslateLayer( poDS, poResultSet, poODS, papszLCO, 
832                                 pszNewLayerName, bTransform, poOutputSRS,
833                                 poSourceSRS, papszSelFields, bAppend, eGType,
834                                 bOverwrite, dfMaxSegmentLength ))
835            {
836                CPLError( CE_Failure, CPLE_AppDefined, 
837                          "Terminating translation prematurely after failed\n"
838                          "translation from sql statement." );
839
840                exit( 1 );
841            }
842            poDS->ReleaseResultSet( poResultSet );
843        }
844    }
845
846/* -------------------------------------------------------------------- */
847/*      Process each data source layer.                                 */
848/* -------------------------------------------------------------------- */
849    for( int iLayer = 0; 
850         pszSQLStatement == NULL && iLayer < poDS->GetLayerCount(); 
851         iLayer++ )
852    {
853        OGRLayer        *poLayer = poDS->GetLayer(iLayer);
854
855        if( poLayer == NULL )
856        {
857            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
858                    iLayer );
859#ifdef ZOO_SERVICE
860            char tmp[1024];
861            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
862            setMapInMaps(conf,"lenv","message",tmp);
863            return SERVICE_FAILED;
864#else
865            exit( 1 );
866#endif
867        }
868
869        if( CSLCount(papszLayers) == 0
870            || CSLFindString( papszLayers,
871                              poLayer->GetLayerDefn()->GetName() ) != -1 )
872        {
873            if( pszWHERE != NULL )
874                poLayer->SetAttributeFilter( pszWHERE );
875           
876            if( poSpatialFilter != NULL )
877                poLayer->SetSpatialFilter( poSpatialFilter );
878           
879            if( !TranslateLayer( poDS, poLayer, poODS, papszLCO, 
880                                 pszNewLayerName, bTransform, poOutputSRS,
881                                 poSourceSRS, papszSelFields, bAppend, eGType,
882                                 bOverwrite, dfMaxSegmentLength ) 
883                && !bSkipFailures )
884            {
885#ifdef ZOO_SERVICE
886                char tmp[1024];
887                sprintf(tmp,"Terminating translation prematurely after failed of layer %s",poLayer->GetLayerDefn()->GetName() );
888                setMapInMaps(conf,"lenv","message",tmp);
889                return SERVICE_FAILED;
890#else
891                CPLError( CE_Failure, CPLE_AppDefined, 
892                          "Terminating translation prematurely after failed\n"
893                          "translation of layer %s (use -skipfailures to skip errors)\n", 
894                          poLayer->GetLayerDefn()->GetName() );
895
896                exit( 1 );
897#endif
898            }
899        }
900    }
901
902#ifdef ZOO_SERVICE
903    outputs->content=createMap("value",(char*)pszwebDestData);
904#endif
905
906/* -------------------------------------------------------------------- */
907/*      Close down.                                                     */
908/* -------------------------------------------------------------------- */
909    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
910    delete poOutputSRS;
911    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
912    delete poSourceSRS;
913    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
914    delete poODS;
915    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
916    delete poDS;
917    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
918
919#ifndef ZOO_SERVICE
920    CSLDestroy(papszSelFields);
921    CSLDestroy( papszArgv );
922#endif
923    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
924    CSLDestroy( papszLayers );
925    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
926#ifndef ZOO_SERVICE
927    CSLDestroy( papszDSCO );
928    CSLDestroy( papszLCO );
929#endif
930    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
931
932    OGRCleanupAll();
933    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
934
935#ifdef DBMALLOC
936    malloc_dump(1);
937#endif
938    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
939   
940#ifdef ZOO_SERVICE
941    return SERVICE_SUCCEEDED;
942#else
943    return 0;
944#endif
945}
946
947/************************************************************************/
948/*                               Usage()                                */
949/************************************************************************/
950
951static void Usage()
952
953{
954#if GDAL_VERSION_MAJOR >= 2
955      GDALDataset *poDS;
956      GDALDataset *poODS;
957      GDALDriverManager* poR=GetGDALDriverManager();
958      GDALDriver          *poDriver = NULL;
959#else
960      OGRDataSource* poDS;
961      OGRDataSource *poODS;
962      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
963      OGRSFDriver          *poDriver = NULL;
964#endif
965
966#ifdef ZOO_SERVICE
967        fprintf(stderr,
968#else
969        printf(
970#endif
971                "Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]\n"
972                "               [-select field_list] [-where restricted_where] \n"
973                "               [-sql <sql statement>] \n" 
974                "               [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n"
975                "               [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n"
976                "               [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]\n"
977                "               [-segmentize max_dist]\n"
978                "               dst_datasource_name src_datasource_name\n"
979                "               [-lco NAME=VALUE] [-nln name] [-nlt type] [layer [layer ...]]\n"
980                "\n"
981                " -f format_name: output file format name, possible values are:\n");
982   
983    for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
984    {
985        poDriver = poR->GetDriver(iDriver);
986
987#if GDAL_VERSION_MAJOR >=2
988        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
989#else
990        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
991#endif
992#if GDAL_VERSION_MAJOR >= 2
993            fprintf( stderr, "  -> %s\n", poDriver->GetDescription() );
994#else
995            fprintf( stderr, "  -> %s\n", poDriver->GetName() );
996#endif
997    }
998
999#ifdef ZOO_SERVICE
1000        fprintf(stderr,
1001#else
1002        printf(
1003#endif
1004                " -append: Append to existing layer instead of creating new if it exists\n"
1005                " -overwrite: delete the output layer and recreate it empty\n"
1006                " -update: Open existing output datasource in update mode\n"
1007                " -select field_list: Comma-delimited list of fields from input layer to\n"
1008                "                     copy to the new layer (defaults to all)\n" 
1009                " -where restricted_where: Attribute query (like SQL WHERE)\n" 
1010                " -sql statement: Execute given SQL statement and save result.\n"
1011                " -skipfailures: skip features or layers that fail to convert\n"
1012                " -gt n: group n features per transaction (default 200)\n"
1013                " -spat xmin ymin xmax ymax: spatial query extents\n"
1014                " -segmentize max_dist: maximum distance between 2 nodes.\n"
1015                "                       Used to create intermediate points\n"
1016                " -dsco NAME=VALUE: Dataset creation option (format specific)\n"
1017                " -lco  NAME=VALUE: Layer creation option (format specific)\n"
1018                " -nln name: Assign an alternate name to the new layer\n"
1019                " -nlt type: Force a geometry type for new layer.  One of NONE, GEOMETRY,\n"
1020                "      POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT,\n"
1021                "      MULTIPOLYGON, or MULTILINESTRING.  Add \"25D\" for 3D layers.\n"
1022                "      Default is type of source layer.\n" );
1023
1024#ifdef ZOO_SERVICE
1025        fprintf(stderr,
1026#else
1027        printf(
1028#endif
1029                " -a_srs srs_def: Assign an output SRS\n"
1030                " -t_srs srs_def: Reproject/transform to this SRS on output\n"
1031                " -s_srs srs_def: Override source SRS\n"
1032                "\n" 
1033                " Srs_def can be a full WKT definition (hard to escape properly),\n"
1034                " or a well known definition (ie. EPSG:4326) or a file with a WKT\n"
1035                " definition.\n" );
1036
1037
1038#ifndef ZOO_SERVICE
1039        exit( 1 );
1040#endif
1041}
1042
1043/************************************************************************/
1044/*                           TranslateLayer()                           */
1045/************************************************************************/
1046
1047static int TranslateLayer(
1048#if GDAL_VERSION_MAJOR >= 2
1049                          GDALDataset
1050#else
1051                          OGRDataSource
1052#endif
1053                          *poSrcDS, 
1054                          OGRLayer * poSrcLayer,
1055#if GDAL_VERSION_MAJOR >= 2
1056                          GDALDataset
1057#else
1058                          OGRDataSource
1059#endif
1060                          *poDstDS,
1061                          char **papszLCO,
1062                          const char *pszNewLayerName,
1063                          int bTransform, 
1064                          OGRSpatialReference *poOutputSRS,
1065                          OGRSpatialReference *poSourceSRS,
1066                          char **papszSelFields,
1067                          int bAppend, int eGType, int bOverwrite,
1068                          double dfMaxSegmentLength)
1069               
1070{
1071    OGRLayer    *poDstLayer;
1072    OGRFeatureDefn *poFDefn;
1073    OGRErr      eErr;
1074    int         bForceToPolygon = FALSE;
1075    int         bForceToMultiPolygon = FALSE;
1076
1077    if( pszNewLayerName == NULL )
1078        pszNewLayerName = poSrcLayer->GetLayerDefn()->GetName();
1079
1080    if( wkbFlatten(eGType) == wkbPolygon )
1081        bForceToPolygon = TRUE;
1082    else if( wkbFlatten(eGType) == wkbMultiPolygon )
1083        bForceToMultiPolygon = TRUE;
1084
1085/* -------------------------------------------------------------------- */
1086/*      Setup coordinate transformation if we need it.                  */
1087/* -------------------------------------------------------------------- */
1088    OGRCoordinateTransformation *poCT = NULL;
1089
1090    if( bTransform )
1091    {
1092        if( poSourceSRS == NULL )
1093            poSourceSRS = poSrcLayer->GetSpatialRef();
1094
1095        if( poSourceSRS == NULL )
1096        {
1097            fprintf( stderr, "Can't transform coordinates, source layer has no\n"
1098                    "coordinate system.  Use -s_srs to set one.\n" );
1099#ifdef ZOO_SERVICE
1100            return SERVICE_FAILED;
1101#else
1102            exit( 1 );
1103#endif
1104        }
1105
1106        CPLAssert( NULL != poSourceSRS );
1107        CPLAssert( NULL != poOutputSRS );
1108
1109        poCT = OGRCreateCoordinateTransformation( poSourceSRS, poOutputSRS );
1110        if( poCT == NULL )
1111        {
1112            char        *pszWKT = NULL;
1113
1114            fprintf( stderr, "Failed to create coordinate transformation between the\n"
1115                   "following coordinate systems.  This may be because they\n"
1116                   "are not transformable, or because projection services\n"
1117                   "(PROJ.4 DLL/.so) could not be loaded.\n" );
1118           
1119            poSourceSRS->exportToPrettyWkt( &pszWKT, FALSE );
1120            fprintf( stderr,  "Source:\n%s\n", pszWKT );
1121           
1122            poOutputSRS->exportToPrettyWkt( &pszWKT, FALSE );
1123            fprintf( stderr,  "Target:\n%s\n", pszWKT );
1124#ifdef ZOO_SERVICE
1125            return SERVICE_FAILED;
1126#else
1127            exit( 1 );
1128#endif
1129        }
1130    }
1131   
1132/* -------------------------------------------------------------------- */
1133/*      Get other info.                                                 */
1134/* -------------------------------------------------------------------- */
1135    poFDefn = poSrcLayer->GetLayerDefn();
1136   
1137    if( poOutputSRS == NULL )
1138        poOutputSRS = poSrcLayer->GetSpatialRef();
1139
1140/* -------------------------------------------------------------------- */
1141/*      Find the layer.                                                 */
1142/* -------------------------------------------------------------------- */
1143    int iLayer = -1;
1144    poDstLayer = NULL;
1145
1146    for( iLayer = 0; iLayer < poDstDS->GetLayerCount(); iLayer++ )
1147    {
1148        OGRLayer        *poLayer = poDstDS->GetLayer(iLayer);
1149
1150        if( poLayer != NULL 
1151            && EQUAL(poLayer->GetLayerDefn()->GetName(),pszNewLayerName) )
1152        {
1153            poDstLayer = poLayer;
1154            break;
1155        }
1156    }
1157   
1158/* -------------------------------------------------------------------- */
1159/*      If the user requested overwrite, and we have the layer in       */
1160/*      question we need to delete it now so it will get recreated      */
1161/*      (overwritten).                                                  */
1162/* -------------------------------------------------------------------- */
1163    if( poDstLayer != NULL && bOverwrite )
1164    {
1165        if( poDstDS->DeleteLayer( iLayer ) != OGRERR_NONE )
1166        {
1167            fprintf( stderr, 
1168                     "DeleteLayer() failed when overwrite requested.\n" );
1169            return FALSE;
1170        }
1171        poDstLayer = NULL;
1172    }
1173
1174/* -------------------------------------------------------------------- */
1175/*      If the layer does not exist, then create it.                    */
1176/* -------------------------------------------------------------------- */
1177    if( poDstLayer == NULL )
1178    {
1179        if( eGType == -2 )
1180            eGType = poFDefn->GetGeomType();
1181
1182        if( !poDstDS->TestCapability( ODsCCreateLayer ) )
1183        {
1184            fprintf( stderr, 
1185              "Layer %s not found, and CreateLayer not supported by driver.", 
1186                     pszNewLayerName );
1187            return FALSE;
1188        }
1189
1190        CPLErrorReset();
1191
1192        poDstLayer = poDstDS->CreateLayer( pszNewLayerName, poOutputSRS,
1193                                           (OGRwkbGeometryType) eGType, 
1194                                           papszLCO );
1195
1196        if( poDstLayer == NULL )
1197            return FALSE;
1198
1199        bAppend = FALSE;
1200    }
1201
1202/* -------------------------------------------------------------------- */
1203/*      Otherwise we will append to it, if append was requested.        */
1204/* -------------------------------------------------------------------- */
1205    else if( !bAppend )
1206    {
1207        fprintf( stderr, "FAILED: Layer %s already exists, and -append not specified.\n"
1208                "        Consider using -append, or -overwrite.\n",
1209                pszNewLayerName );
1210        return FALSE;
1211    }
1212    else
1213    {
1214        if( CSLCount(papszLCO) > 0 )
1215        {
1216            fprintf( stderr, "WARNING: Layer creation options ignored since an existing layer is\n"
1217                    "         being appended to.\n" );
1218        }
1219    }
1220
1221/* -------------------------------------------------------------------- */
1222/*      Add fields.  Default to copy all field.                         */
1223/*      If only a subset of all fields requested, then output only      */
1224/*      the selected fields, and in the order that they were            */
1225/*      selected.                                                       */
1226/* -------------------------------------------------------------------- */
1227    int         iField;
1228    int hasMmField=-1;
1229
1230    if (papszSelFields && !bAppend )
1231    {
1232        for( iField=0; papszSelFields[iField] != NULL; iField++)
1233        {
1234            int iSrcField = poFDefn->GetFieldIndex(papszSelFields[iField]);
1235            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iSrcField);
1236            fprintf(stderr,"NAME: %s\n",tmp->GetNameRef());
1237            fflush(stderr);
1238            if(tmp!=NULL && strncasecmp(tmp->GetNameRef(),"MMID",4)==0)
1239              hasMmField=1;
1240            if (iSrcField >= 0)
1241                poDstLayer->CreateField( poFDefn->GetFieldDefn(iSrcField) );
1242            else
1243            {
1244                fprintf( stderr, "Field '%s' not found in source layer.\n", 
1245                        papszSelFields[iField] );
1246                if( !bSkipFailures )
1247                    return FALSE;
1248            }
1249        }
1250
1251    }
1252    else if( !bAppend )
1253    {
1254      for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ){
1255            poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
1256            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
1257            if(tmp!=NULL && strncasecmp(tmp->GetNameRef(),"MMID",4)==0)
1258              hasMmField=1;
1259            fprintf(stderr,"NAME: %s\n",tmp->GetNameRef());
1260      }
1261    }
1262    /*if(hasMmField<0){
1263      OGRFieldDefn oField( "MMID", OFTInteger );
1264      poDstLayer->CreateField( &oField );
1265      }*/
1266
1267/* -------------------------------------------------------------------- */
1268/*      Transfer features.                                              */
1269/* -------------------------------------------------------------------- */
1270    OGRFeature  *poFeature;
1271    int         nFeaturesInTransaction = 0;
1272    int fCount=0;
1273    poSrcLayer->ResetReading();
1274
1275    if( nGroupTransactions )
1276        poDstLayer->StartTransaction();
1277
1278    while( TRUE )
1279    {
1280        OGRFeature      *poDstFeature = NULL;
1281
1282        if( nFIDToFetch != OGRNullFID )
1283        {
1284            // Only fetch feature on first pass.
1285            if( nFeaturesInTransaction == 0 )
1286                poFeature = poSrcLayer->GetFeature(nFIDToFetch);
1287            else
1288                poFeature = NULL;
1289        }
1290        else
1291            poFeature = poSrcLayer->GetNextFeature();
1292
1293        if( poFeature == NULL )
1294            break;
1295
1296        //poFeature->SetField((poFeature->GetFieldCount()-1),fCount);
1297
1298        if( ++nFeaturesInTransaction == nGroupTransactions )
1299        {
1300            poDstLayer->CommitTransaction();
1301            poDstLayer->StartTransaction();
1302            nFeaturesInTransaction = 0;
1303        }
1304
1305        CPLErrorReset();
1306        poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
1307
1308        if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
1309        {
1310            if( nGroupTransactions )
1311                poDstLayer->CommitTransaction();
1312           
1313            CPLError( CE_Failure, CPLE_AppDefined,
1314                      "Unable to translate feature %ld from layer %s.\n",
1315                      poFeature->GetFID(), poFDefn->GetName() );
1316           
1317            OGRFeature::DestroyFeature( poFeature );
1318            OGRFeature::DestroyFeature( poDstFeature );
1319            return FALSE;
1320        }
1321
1322        if( bPreserveFID )
1323            poDstFeature->SetFID( poFeature->GetFID() );
1324
1325        /*if(hasMmField<0){
1326          poDstFeature->SetField((poDstFeature->GetFieldCount()-1),fCount);
1327          fCount++;
1328        }*/
1329
1330#ifndef GDAL_1_5_0
1331        if (poDstFeature->GetGeometryRef() != NULL && dfMaxSegmentLength > 0)
1332            poDstFeature->GetGeometryRef()->segmentize(dfMaxSegmentLength);
1333#endif
1334
1335        if( poCT && poDstFeature->GetGeometryRef() != NULL )
1336        {
1337            eErr = poDstFeature->GetGeometryRef()->transform( poCT );
1338            if( eErr != OGRERR_NONE )
1339            {
1340                if( nGroupTransactions )
1341                    poDstLayer->CommitTransaction();
1342
1343                fprintf( stderr, "Failed to reproject feature %d (geometry probably out of source or destination SRS).\n", 
1344                        (int) poFeature->GetFID() );
1345                if( !bSkipFailures )
1346                {
1347                    OGRFeature::DestroyFeature( poFeature );
1348                    OGRFeature::DestroyFeature( poDstFeature );
1349                    return FALSE;
1350                }
1351            }
1352        }
1353
1354        if( poDstFeature->GetGeometryRef() != NULL && bForceToPolygon )
1355        {
1356            poDstFeature->SetGeometryDirectly( 
1357                OGRGeometryFactory::forceToPolygon(
1358                    poDstFeature->StealGeometry() ) );
1359        }
1360                   
1361        if( poDstFeature->GetGeometryRef() != NULL && bForceToMultiPolygon )
1362        {
1363            poDstFeature->SetGeometryDirectly( 
1364                OGRGeometryFactory::forceToMultiPolygon(
1365                    poDstFeature->StealGeometry() ) );
1366        }
1367                   
1368        OGRFeature::DestroyFeature( poFeature );
1369
1370        CPLErrorReset();
1371        if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE
1372            && !bSkipFailures )
1373        {
1374            if( nGroupTransactions )
1375                poDstLayer->RollbackTransaction();
1376
1377            OGRFeature::DestroyFeature( poDstFeature );
1378            return FALSE;
1379        }
1380
1381        OGRFeature::DestroyFeature( poDstFeature );
1382    }
1383
1384    if( nGroupTransactions )
1385        poDstLayer->CommitTransaction();
1386
1387/* -------------------------------------------------------------------- */
1388/*      Cleaning                                                        */
1389/* -------------------------------------------------------------------- */
1390    delete poCT;
1391
1392    return TRUE;
1393}
1394
1395#ifdef ZOO_SERVICE
1396}
1397#endif
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png