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

Last change on this file since 766 was 766, checked in by djay, 8 years ago

Add support for GDAL 2 (base-vect-ops).

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 46.1 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 *poODS;
595      GDALDriverManager* poR=GetGDALDriverManager();
596      GDALDriver          *poDriver = NULL;
597#else
598      OGRDataSource* poDS;
599      OGRDataSource *poODS;
600      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
601      OGRSFDriver          *poDriver = NULL;
602#endif
603
604/* -------------------------------------------------------------------- */
605/*      Report failure                                                  */
606/* -------------------------------------------------------------------- */
607    if( poDS == NULL )
608    {
609        fprintf( stderr, "FAILURE:\n"
610                "Unable to open datasource `%s' with the following drivers.\n",
611                pszDataSource );
612
613        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
614        {
615#if GDAL_VERSION_MAJOR >= 2
616            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
617#else
618            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
619#endif
620        }
621#ifdef ZOO_SERVICE
622        char tmp[1024];
623        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",pszDataSource);
624        setMapInMaps(conf,"lenv","message",tmp);
625        return SERVICE_FAILED;
626#else
627        exit( 1 );
628#endif
629    }
630
631/* -------------------------------------------------------------------- */
632/*      Try opening the output datasource as an existing, writable      */
633/* -------------------------------------------------------------------- */
634    if( bUpdate )
635    {
636        poODS = 
637#if GDAL_VERSION_MAJOR >= 2
638          (GDALDataset*) GDALOpenEx( pszDestDataSource,
639                                     GDAL_OF_UPDATE | GDAL_OF_VECTOR,
640                                     NULL, NULL, NULL )
641#else
642          OGRSFDriverRegistrar::Open( pszDestDataSource, TRUE )
643#endif
644          ;
645        if( poODS == NULL )
646        {
647            fprintf( stderr, "FAILURE:\n"
648                    "Unable to open existing output datasource `%s'.\n",
649                    pszDestDataSource );
650#ifdef ZOO_SERVICE
651            char tmp[1024];
652            sprintf(tmp,"Unable to open existing output datasource `%s'.",pszDestDataSource);
653            setMapInMaps(conf,"lenv","message",tmp);
654            return SERVICE_FAILED;
655#else
656        exit( 1 );
657#endif
658        }
659
660        if( CSLCount(papszDSCO) > 0 )
661        {
662            fprintf( stderr, "WARNING: Datasource creation options ignored since an existing datasource\n"
663                    "         being updated.\n" );
664        }
665    }
666
667/* -------------------------------------------------------------------- */
668/*      Find the output driver.                                         */
669/* -------------------------------------------------------------------- */
670    else
671    {
672        int                  iDriver;
673
674        for( iDriver = 0;
675             iDriver < poR->GetDriverCount() && poDriver == NULL;
676             iDriver++ )
677        {
678#if GDAL_VERSION_MAJOR >=2
679            if( EQUAL(poR->GetDriver(iDriver)->GetDescription(),pszFormat) )
680#else
681            if( EQUAL(poR->GetDriver(iDriver)->GetName(),pszFormat) )
682#endif
683            {
684                poDriver = poR->GetDriver(iDriver);
685            }
686        }
687
688        if( poDriver == NULL )
689        {
690            fprintf( stderr, "Unable to find driver `%s'.\n", pszFormat );
691            fprintf( stderr,  "The following drivers are available:\n" );
692       
693            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
694            {
695#if GDAL_VERSION_MAJOR >= 2
696              fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
697#else
698              fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
699#endif
700            }
701#ifdef ZOO_SERVICE
702            char tmp[1024];
703            sprintf(tmp,"Unable to find driver `%s'.",pszFormat);
704            setMapInMaps(conf,"lenv","message",tmp);
705            return SERVICE_FAILED;
706#else
707            exit( 1 );
708#endif
709        }
710
711#if GDAL_VERSION_MAJOR >=2
712        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
713#else
714        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
715#endif
716        {
717            fprintf( stderr,  "%s driver does not support data source creation.\n",
718                    pszFormat );
719#ifdef ZOO_SERVICE
720            char tmp[1024];
721            sprintf(tmp,"%s driver does not support data source creation.",pszFormat);
722            setMapInMaps(conf,"lenv","message",tmp);
723            return SERVICE_FAILED;
724#else
725            exit( 1 );
726#endif
727        }
728
729/* -------------------------------------------------------------------- */
730/*      Create the output data source.                                  */
731/* -------------------------------------------------------------------- */
732#if GDAL_VERSION_MAJOR >=2
733        poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO );
734#else
735        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
736#endif
737        if( poODS == NULL )
738        {
739            fprintf( stderr,  "%s driver failed to create %s\n", 
740                    pszFormat, pszDestDataSource );
741#ifdef ZOO_SERVICE
742            char tmp[1024];
743            sprintf(tmp,"%s driver failed to create %s",pszFormat, pszDestDataSource);
744            setMapInMaps(conf,"lenv","message",tmp);
745            return SERVICE_FAILED;
746#else
747            exit( 1 );
748#endif
749        }
750    }
751
752/* -------------------------------------------------------------------- */
753/*      Parse the output SRS definition if possible.                    */
754/* -------------------------------------------------------------------- */
755    if( pszOutputSRSDef != NULL )
756    {
757        poOutputSRS = new OGRSpatialReference();
758        if( poOutputSRS->SetFromUserInput( pszOutputSRSDef ) != OGRERR_NONE )
759        {
760            fprintf( stderr,  "Failed to process SRS definition: %s\n", 
761                    pszOutputSRSDef );
762#ifdef ZOO_SERVICE
763            char tmp[1024];
764            sprintf(tmp,"Failed to process SRS definition: %s",pszOutputSRSDef);
765            setMapInMaps(conf,"lenv","message",tmp);
766            return SERVICE_FAILED;
767#else
768            exit( 1 );
769#endif
770        }
771    }
772
773/* -------------------------------------------------------------------- */
774/*      Parse the source SRS definition if possible.                    */
775/* -------------------------------------------------------------------- */
776    if( pszSourceSRSDef != NULL )
777    {
778        poSourceSRS = new OGRSpatialReference();
779        if( poSourceSRS->SetFromUserInput( pszSourceSRSDef ) != OGRERR_NONE )
780        {
781            fprintf( stderr,  "Failed to process SRS definition: %s\n", 
782                    pszSourceSRSDef );
783#ifdef ZOO_SERVICE
784            char tmp[1024];
785            sprintf(tmp,"Failed to process SRS definition: %s",pszOutputSRSDef);
786            setMapInMaps(conf,"lenv","message",tmp);
787            return SERVICE_FAILED;
788#else
789            exit( 1 );
790#endif
791        }
792    }
793
794/* -------------------------------------------------------------------- */
795/*      Special case for -sql clause.  No source layers required.       */
796/* -------------------------------------------------------------------- */
797    if( pszSQLStatement != NULL )
798    {
799        OGRLayer *poResultSet;
800
801        if( pszWHERE != NULL )
802            fprintf( stderr,  "-where clause ignored in combination with -sql.\n" );
803        if( CSLCount(papszLayers) > 0 )
804          fprintf( stderr,  "layer names ignored in combination with -sql. (%s)\n", pszSQLStatement);
805       
806        poResultSet = poDS->ExecuteSQL( pszSQLStatement, poSpatialFilter, 
807                                        NULL );
808
809        if( poResultSet != NULL )
810        {
811            if( !TranslateLayer( poDS, poResultSet, poODS, papszLCO, 
812                                 pszNewLayerName, bTransform, poOutputSRS,
813                                 poSourceSRS, papszSelFields, bAppend, eGType,
814                                 bOverwrite, dfMaxSegmentLength ))
815            {
816                CPLError( CE_Failure, CPLE_AppDefined, 
817                          "Terminating translation prematurely after failed\n"
818                          "translation from sql statement." );
819
820                exit( 1 );
821            }
822            poDS->ReleaseResultSet( poResultSet );
823        }
824    }
825
826/* -------------------------------------------------------------------- */
827/*      Process each data source layer.                                 */
828/* -------------------------------------------------------------------- */
829    for( int iLayer = 0; 
830         pszSQLStatement == NULL && iLayer < poDS->GetLayerCount(); 
831         iLayer++ )
832    {
833        OGRLayer        *poLayer = poDS->GetLayer(iLayer);
834
835        if( poLayer == NULL )
836        {
837            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
838                    iLayer );
839#ifdef ZOO_SERVICE
840            char tmp[1024];
841            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
842            setMapInMaps(conf,"lenv","message",tmp);
843            return SERVICE_FAILED;
844#else
845            exit( 1 );
846#endif
847        }
848
849        if( CSLCount(papszLayers) == 0
850            || CSLFindString( papszLayers,
851                              poLayer->GetLayerDefn()->GetName() ) != -1 )
852        {
853            if( pszWHERE != NULL )
854                poLayer->SetAttributeFilter( pszWHERE );
855           
856            if( poSpatialFilter != NULL )
857                poLayer->SetSpatialFilter( poSpatialFilter );
858           
859            if( !TranslateLayer( poDS, poLayer, poODS, papszLCO, 
860                                 pszNewLayerName, bTransform, poOutputSRS,
861                                 poSourceSRS, papszSelFields, bAppend, eGType,
862                                 bOverwrite, dfMaxSegmentLength ) 
863                && !bSkipFailures )
864            {
865#ifdef ZOO_SERVICE
866                char tmp[1024];
867                sprintf(tmp,"Terminating translation prematurely after failed of layer %s",poLayer->GetLayerDefn()->GetName() );
868                setMapInMaps(conf,"lenv","message",tmp);
869                return SERVICE_FAILED;
870#else
871                CPLError( CE_Failure, CPLE_AppDefined, 
872                          "Terminating translation prematurely after failed\n"
873                          "translation of layer %s (use -skipfailures to skip errors)\n", 
874                          poLayer->GetLayerDefn()->GetName() );
875
876                exit( 1 );
877#endif
878            }
879        }
880    }
881
882#ifdef ZOO_SERVICE
883    outputs->content=createMap("value",(char*)pszwebDestData);
884#endif
885
886/* -------------------------------------------------------------------- */
887/*      Close down.                                                     */
888/* -------------------------------------------------------------------- */
889    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
890    delete poOutputSRS;
891    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
892    delete poSourceSRS;
893    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
894    delete poODS;
895    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
896    delete poDS;
897    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
898
899#ifndef ZOO_SERVICE
900    CSLDestroy(papszSelFields);
901    CSLDestroy( papszArgv );
902#endif
903    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
904    CSLDestroy( papszLayers );
905    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
906#ifndef ZOO_SERVICE
907    CSLDestroy( papszDSCO );
908    CSLDestroy( papszLCO );
909#endif
910    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
911
912    OGRCleanupAll();
913    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
914
915#ifdef DBMALLOC
916    malloc_dump(1);
917#endif
918    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
919   
920#ifdef ZOO_SERVICE
921    return SERVICE_SUCCEEDED;
922#else
923    return 0;
924#endif
925}
926
927/************************************************************************/
928/*                               Usage()                                */
929/************************************************************************/
930
931static void Usage()
932
933{
934#if GDAL_VERSION_MAJOR >= 2
935      GDALDataset *poDS;
936      GDALDataset *poODS;
937      GDALDriverManager* poR=GetGDALDriverManager();
938      GDALDriver          *poDriver = NULL;
939#else
940      OGRDataSource* poDS;
941      OGRDataSource *poODS;
942      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
943      OGRSFDriver          *poDriver = NULL;
944#endif
945
946#ifdef ZOO_SERVICE
947        fprintf(stderr,
948#else
949        printf(
950#endif
951                "Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]\n"
952                "               [-select field_list] [-where restricted_where] \n"
953                "               [-sql <sql statement>] \n" 
954                "               [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n"
955                "               [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n"
956                "               [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]\n"
957                "               [-segmentize max_dist]\n"
958                "               dst_datasource_name src_datasource_name\n"
959                "               [-lco NAME=VALUE] [-nln name] [-nlt type] [layer [layer ...]]\n"
960                "\n"
961                " -f format_name: output file format name, possible values are:\n");
962   
963    for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
964    {
965        poDriver = poR->GetDriver(iDriver);
966
967#if GDAL_VERSION_MAJOR >=2
968        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
969#else
970        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
971#endif
972#if GDAL_VERSION_MAJOR >= 2
973            fprintf( stderr, "  -> %s\n", poDriver->GetDescription() );
974#else
975            fprintf( stderr, "  -> %s\n", poDriver->GetName() );
976#endif
977    }
978
979#ifdef ZOO_SERVICE
980        fprintf(stderr,
981#else
982        printf(
983#endif
984                " -append: Append to existing layer instead of creating new if it exists\n"
985                " -overwrite: delete the output layer and recreate it empty\n"
986                " -update: Open existing output datasource in update mode\n"
987                " -select field_list: Comma-delimited list of fields from input layer to\n"
988                "                     copy to the new layer (defaults to all)\n" 
989                " -where restricted_where: Attribute query (like SQL WHERE)\n" 
990                " -sql statement: Execute given SQL statement and save result.\n"
991                " -skipfailures: skip features or layers that fail to convert\n"
992                " -gt n: group n features per transaction (default 200)\n"
993                " -spat xmin ymin xmax ymax: spatial query extents\n"
994                " -segmentize max_dist: maximum distance between 2 nodes.\n"
995                "                       Used to create intermediate points\n"
996                " -dsco NAME=VALUE: Dataset creation option (format specific)\n"
997                " -lco  NAME=VALUE: Layer creation option (format specific)\n"
998                " -nln name: Assign an alternate name to the new layer\n"
999                " -nlt type: Force a geometry type for new layer.  One of NONE, GEOMETRY,\n"
1000                "      POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT,\n"
1001                "      MULTIPOLYGON, or MULTILINESTRING.  Add \"25D\" for 3D layers.\n"
1002                "      Default is type of source layer.\n" );
1003
1004#ifdef ZOO_SERVICE
1005        fprintf(stderr,
1006#else
1007        printf(
1008#endif
1009                " -a_srs srs_def: Assign an output SRS\n"
1010                " -t_srs srs_def: Reproject/transform to this SRS on output\n"
1011                " -s_srs srs_def: Override source SRS\n"
1012                "\n" 
1013                " Srs_def can be a full WKT definition (hard to escape properly),\n"
1014                " or a well known definition (ie. EPSG:4326) or a file with a WKT\n"
1015                " definition.\n" );
1016
1017
1018#ifndef ZOO_SERVICE
1019        exit( 1 );
1020#endif
1021}
1022
1023/************************************************************************/
1024/*                           TranslateLayer()                           */
1025/************************************************************************/
1026
1027static int TranslateLayer(
1028#if GDAL_VERSION_MAJOR >= 2
1029                          GDALDataset
1030#else
1031                          OGRDataSource
1032#endif
1033                          *poSrcDS, 
1034                          OGRLayer * poSrcLayer,
1035#if GDAL_VERSION_MAJOR >= 2
1036                          GDALDataset
1037#else
1038                          OGRDataSource
1039#endif
1040                          *poDstDS,
1041                          char **papszLCO,
1042                          const char *pszNewLayerName,
1043                          int bTransform, 
1044                          OGRSpatialReference *poOutputSRS,
1045                          OGRSpatialReference *poSourceSRS,
1046                          char **papszSelFields,
1047                          int bAppend, int eGType, int bOverwrite,
1048                          double dfMaxSegmentLength)
1049               
1050{
1051    OGRLayer    *poDstLayer;
1052    OGRFeatureDefn *poFDefn;
1053    OGRErr      eErr;
1054    int         bForceToPolygon = FALSE;
1055    int         bForceToMultiPolygon = FALSE;
1056
1057    if( pszNewLayerName == NULL )
1058        pszNewLayerName = poSrcLayer->GetLayerDefn()->GetName();
1059
1060    if( wkbFlatten(eGType) == wkbPolygon )
1061        bForceToPolygon = TRUE;
1062    else if( wkbFlatten(eGType) == wkbMultiPolygon )
1063        bForceToMultiPolygon = TRUE;
1064
1065/* -------------------------------------------------------------------- */
1066/*      Setup coordinate transformation if we need it.                  */
1067/* -------------------------------------------------------------------- */
1068    OGRCoordinateTransformation *poCT = NULL;
1069
1070    if( bTransform )
1071    {
1072        if( poSourceSRS == NULL )
1073            poSourceSRS = poSrcLayer->GetSpatialRef();
1074
1075        if( poSourceSRS == NULL )
1076        {
1077            fprintf( stderr, "Can't transform coordinates, source layer has no\n"
1078                    "coordinate system.  Use -s_srs to set one.\n" );
1079#ifdef ZOO_SERVICE
1080            return SERVICE_FAILED;
1081#else
1082            exit( 1 );
1083#endif
1084        }
1085
1086        CPLAssert( NULL != poSourceSRS );
1087        CPLAssert( NULL != poOutputSRS );
1088
1089        poCT = OGRCreateCoordinateTransformation( poSourceSRS, poOutputSRS );
1090        if( poCT == NULL )
1091        {
1092            char        *pszWKT = NULL;
1093
1094            fprintf( stderr, "Failed to create coordinate transformation between the\n"
1095                   "following coordinate systems.  This may be because they\n"
1096                   "are not transformable, or because projection services\n"
1097                   "(PROJ.4 DLL/.so) could not be loaded.\n" );
1098           
1099            poSourceSRS->exportToPrettyWkt( &pszWKT, FALSE );
1100            fprintf( stderr,  "Source:\n%s\n", pszWKT );
1101           
1102            poOutputSRS->exportToPrettyWkt( &pszWKT, FALSE );
1103            fprintf( stderr,  "Target:\n%s\n", pszWKT );
1104#ifdef ZOO_SERVICE
1105            return SERVICE_FAILED;
1106#else
1107            exit( 1 );
1108#endif
1109        }
1110    }
1111   
1112/* -------------------------------------------------------------------- */
1113/*      Get other info.                                                 */
1114/* -------------------------------------------------------------------- */
1115    poFDefn = poSrcLayer->GetLayerDefn();
1116   
1117    if( poOutputSRS == NULL )
1118        poOutputSRS = poSrcLayer->GetSpatialRef();
1119
1120/* -------------------------------------------------------------------- */
1121/*      Find the layer.                                                 */
1122/* -------------------------------------------------------------------- */
1123    int iLayer = -1;
1124    poDstLayer = NULL;
1125
1126    for( iLayer = 0; iLayer < poDstDS->GetLayerCount(); iLayer++ )
1127    {
1128        OGRLayer        *poLayer = poDstDS->GetLayer(iLayer);
1129
1130        if( poLayer != NULL 
1131            && EQUAL(poLayer->GetLayerDefn()->GetName(),pszNewLayerName) )
1132        {
1133            poDstLayer = poLayer;
1134            break;
1135        }
1136    }
1137   
1138/* -------------------------------------------------------------------- */
1139/*      If the user requested overwrite, and we have the layer in       */
1140/*      question we need to delete it now so it will get recreated      */
1141/*      (overwritten).                                                  */
1142/* -------------------------------------------------------------------- */
1143    if( poDstLayer != NULL && bOverwrite )
1144    {
1145        if( poDstDS->DeleteLayer( iLayer ) != OGRERR_NONE )
1146        {
1147            fprintf( stderr, 
1148                     "DeleteLayer() failed when overwrite requested.\n" );
1149            return FALSE;
1150        }
1151        poDstLayer = NULL;
1152    }
1153
1154/* -------------------------------------------------------------------- */
1155/*      If the layer does not exist, then create it.                    */
1156/* -------------------------------------------------------------------- */
1157    if( poDstLayer == NULL )
1158    {
1159        if( eGType == -2 )
1160            eGType = poFDefn->GetGeomType();
1161
1162        if( !poDstDS->TestCapability( ODsCCreateLayer ) )
1163        {
1164            fprintf( stderr, 
1165              "Layer %s not found, and CreateLayer not supported by driver.", 
1166                     pszNewLayerName );
1167            return FALSE;
1168        }
1169
1170        CPLErrorReset();
1171
1172        poDstLayer = poDstDS->CreateLayer( pszNewLayerName, poOutputSRS,
1173                                           (OGRwkbGeometryType) eGType, 
1174                                           papszLCO );
1175
1176        if( poDstLayer == NULL )
1177            return FALSE;
1178
1179        bAppend = FALSE;
1180    }
1181
1182/* -------------------------------------------------------------------- */
1183/*      Otherwise we will append to it, if append was requested.        */
1184/* -------------------------------------------------------------------- */
1185    else if( !bAppend )
1186    {
1187        fprintf( stderr, "FAILED: Layer %s already exists, and -append not specified.\n"
1188                "        Consider using -append, or -overwrite.\n",
1189                pszNewLayerName );
1190        return FALSE;
1191    }
1192    else
1193    {
1194        if( CSLCount(papszLCO) > 0 )
1195        {
1196            fprintf( stderr, "WARNING: Layer creation options ignored since an existing layer is\n"
1197                    "         being appended to.\n" );
1198        }
1199    }
1200
1201/* -------------------------------------------------------------------- */
1202/*      Add fields.  Default to copy all field.                         */
1203/*      If only a subset of all fields requested, then output only      */
1204/*      the selected fields, and in the order that they were            */
1205/*      selected.                                                       */
1206/* -------------------------------------------------------------------- */
1207    int         iField;
1208    int hasMmField=-1;
1209
1210    if (papszSelFields && !bAppend )
1211    {
1212        for( iField=0; papszSelFields[iField] != NULL; iField++)
1213        {
1214            int iSrcField = poFDefn->GetFieldIndex(papszSelFields[iField]);
1215            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iSrcField);
1216            fprintf(stderr,"NAME: %s\n",tmp->GetNameRef());
1217            fflush(stderr);
1218            if(tmp!=NULL && strncasecmp(tmp->GetNameRef(),"MMID",4)==0)
1219              hasMmField=1;
1220            if (iSrcField >= 0)
1221                poDstLayer->CreateField( poFDefn->GetFieldDefn(iSrcField) );
1222            else
1223            {
1224                fprintf( stderr, "Field '%s' not found in source layer.\n", 
1225                        papszSelFields[iField] );
1226                if( !bSkipFailures )
1227                    return FALSE;
1228            }
1229        }
1230
1231    }
1232    else if( !bAppend )
1233    {
1234      for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ){
1235            poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
1236            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
1237            if(tmp!=NULL && strncasecmp(tmp->GetNameRef(),"MMID",4)==0)
1238              hasMmField=1;
1239            fprintf(stderr,"NAME: %s\n",tmp->GetNameRef());
1240      }
1241    }
1242    /*if(hasMmField<0){
1243      OGRFieldDefn oField( "MMID", OFTInteger );
1244      poDstLayer->CreateField( &oField );
1245      }*/
1246
1247/* -------------------------------------------------------------------- */
1248/*      Transfer features.                                              */
1249/* -------------------------------------------------------------------- */
1250    OGRFeature  *poFeature;
1251    int         nFeaturesInTransaction = 0;
1252    int fCount=0;
1253    poSrcLayer->ResetReading();
1254
1255    if( nGroupTransactions )
1256        poDstLayer->StartTransaction();
1257
1258    while( TRUE )
1259    {
1260        OGRFeature      *poDstFeature = NULL;
1261
1262        if( nFIDToFetch != OGRNullFID )
1263        {
1264            // Only fetch feature on first pass.
1265            if( nFeaturesInTransaction == 0 )
1266                poFeature = poSrcLayer->GetFeature(nFIDToFetch);
1267            else
1268                poFeature = NULL;
1269        }
1270        else
1271            poFeature = poSrcLayer->GetNextFeature();
1272
1273        if( poFeature == NULL )
1274            break;
1275
1276        //poFeature->SetField((poFeature->GetFieldCount()-1),fCount);
1277
1278        if( ++nFeaturesInTransaction == nGroupTransactions )
1279        {
1280            poDstLayer->CommitTransaction();
1281            poDstLayer->StartTransaction();
1282            nFeaturesInTransaction = 0;
1283        }
1284
1285        CPLErrorReset();
1286        poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
1287
1288        if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
1289        {
1290            if( nGroupTransactions )
1291                poDstLayer->CommitTransaction();
1292           
1293            CPLError( CE_Failure, CPLE_AppDefined,
1294                      "Unable to translate feature %ld from layer %s.\n",
1295                      poFeature->GetFID(), poFDefn->GetName() );
1296           
1297            OGRFeature::DestroyFeature( poFeature );
1298            OGRFeature::DestroyFeature( poDstFeature );
1299            return FALSE;
1300        }
1301
1302        if( bPreserveFID )
1303            poDstFeature->SetFID( poFeature->GetFID() );
1304
1305        /*if(hasMmField<0){
1306          poDstFeature->SetField((poDstFeature->GetFieldCount()-1),fCount);
1307          fCount++;
1308        }*/
1309
1310#ifndef GDAL_1_5_0
1311        if (poDstFeature->GetGeometryRef() != NULL && dfMaxSegmentLength > 0)
1312            poDstFeature->GetGeometryRef()->segmentize(dfMaxSegmentLength);
1313#endif
1314
1315        if( poCT && poDstFeature->GetGeometryRef() != NULL )
1316        {
1317            eErr = poDstFeature->GetGeometryRef()->transform( poCT );
1318            if( eErr != OGRERR_NONE )
1319            {
1320                if( nGroupTransactions )
1321                    poDstLayer->CommitTransaction();
1322
1323                fprintf( stderr, "Failed to reproject feature %d (geometry probably out of source or destination SRS).\n", 
1324                        (int) poFeature->GetFID() );
1325                if( !bSkipFailures )
1326                {
1327                    OGRFeature::DestroyFeature( poFeature );
1328                    OGRFeature::DestroyFeature( poDstFeature );
1329                    return FALSE;
1330                }
1331            }
1332        }
1333
1334        if( poDstFeature->GetGeometryRef() != NULL && bForceToPolygon )
1335        {
1336            poDstFeature->SetGeometryDirectly( 
1337                OGRGeometryFactory::forceToPolygon(
1338                    poDstFeature->StealGeometry() ) );
1339        }
1340                   
1341        if( poDstFeature->GetGeometryRef() != NULL && bForceToMultiPolygon )
1342        {
1343            poDstFeature->SetGeometryDirectly( 
1344                OGRGeometryFactory::forceToMultiPolygon(
1345                    poDstFeature->StealGeometry() ) );
1346        }
1347                   
1348        OGRFeature::DestroyFeature( poFeature );
1349
1350        CPLErrorReset();
1351        if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE
1352            && !bSkipFailures )
1353        {
1354            if( nGroupTransactions )
1355                poDstLayer->RollbackTransaction();
1356
1357            OGRFeature::DestroyFeature( poDstFeature );
1358            return FALSE;
1359        }
1360
1361        OGRFeature::DestroyFeature( poDstFeature );
1362    }
1363
1364    if( nGroupTransactions )
1365        poDstLayer->CommitTransaction();
1366
1367/* -------------------------------------------------------------------- */
1368/*      Cleaning                                                        */
1369/* -------------------------------------------------------------------- */
1370    delete poCT;
1371
1372    return TRUE;
1373}
1374
1375#ifdef ZOO_SERVICE
1376}
1377#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