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

Last change on this file since 917 was 917, checked in by djay, 5 years ago

Merge prototype-v0 branch in trunk

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 47.0 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    char  *pszDialect = NULL;
99    const char  *pszFormat = "ESRI Shapefile";
100    const char  *pszDataSource = NULL;
101    const char  *pszDestDataSource = NULL;
102    const char  *pszwebDestData = NULL;
103    char        **papszLayers = NULL;
104    char        **papszDSCO = NULL, **papszLCO = NULL;
105    int         bTransform = FALSE;
106    int         bAppend = FALSE, bUpdate = FALSE, bOverwrite = FALSE;
107    const char  *pszOutputSRSDef = NULL;
108    const char  *pszSourceSRSDef = NULL;
109    OGRSpatialReference *poOutputSRS = NULL;
110    OGRSpatialReference *poSourceSRS = NULL;
111    const char  *pszNewLayerName = NULL;
112    const char  *pszWHERE = NULL;
113    OGRGeometry *poSpatialFilter = NULL;
114    const char  *pszSelect;
115    char        **papszSelFields = NULL;
116    const char  *pszSQLStatement = NULL;
117    int         eGType = -2;
118    double      dfMaxSegmentLength = 0;
119
120#ifdef ZOO_SERVICE
121    dumpMaps(inputs);
122#endif
123    /* Check strict compilation and runtime library version as we use C++ API */
124    if (! GDAL_CHECK_VERSION("ogr2ogr"))
125#ifdef ZOO_SERVICE
126        {
127                fprintf(stderr,"Not correct version of the gdal library\n");
128                setMapInMaps(conf,"lenv","message","Unable to check gdal version for ogr2ogr_service.zo");
129                return SERVICE_FAILED;
130        }
131#else
132        exit(1);
133#endif
134/* -------------------------------------------------------------------- */
135/*      Register format(s).                                             */
136/* -------------------------------------------------------------------- */
137    OGRRegisterAll();
138
139#ifdef ZOO_SERVICE
140    map *tmpMap=NULL;
141    char dataPath[1024];
142    tmpMap=getMapFromMaps(conf,"main","dataPath");
143    if(tmpMap!=NULL)
144      sprintf(dataPath,"%s",tmpMap->value);
145    tmpMap=NULL;
146    char tempPath[1024];
147    tmpMap=getMapFromMaps(conf,"main","tmpPath");
148    if(tmpMap!=NULL){
149      sprintf(tempPath,"%s",tmpMap->value);
150    }
151   
152    tmpMap=NULL;
153    char serverAddress[1024];
154    tmpMap=getMapFromMaps(conf,"main","serverAddress");
155    if(tmpMap!=NULL){
156      sprintf(serverAddress,"%s",tmpMap->value);
157    }
158
159    tmpMap=NULL;
160    char tmpurl[1024];
161    tmpMap=getMapFromMaps(conf,"main","tmpurl");
162    if(tmpMap!=NULL){
163      sprintf(tmpurl,"%s",tmpMap->value);
164    }
165
166    tmpMap=NULL;
167    tmpMap=getMapFromMaps(inputs,"F","value");
168    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
169      pszFormat=tmpMap->value;
170    }
171
172    tmpMap=NULL;
173    tmpMap=getMapFromMaps(inputs,"DSCO","value");
174    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
175          papszDSCO = CSLAddString(papszDSCO, tmpMap->value );
176    }
177
178    tmpMap=NULL;
179    tmpMap=getMapFromMaps(inputs,"LCO","value");
180    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
181          papszLCO = CSLAddString(papszLCO, tmpMap->value );
182    }
183
184    tmpMap=NULL;
185    tmpMap=getMapFromMaps(inputs,"preserve_fid","value");
186    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
187          bPreserveFID = TRUE;
188    }
189
190    tmpMap=NULL;
191    tmpMap=getMapFromMaps(inputs,"skipfailure","value");
192    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
193      bSkipFailures = TRUE;
194      nGroupTransactions = 1; /* #2409 */
195    }
196
197    /* if exist, overwrite the data with the same name */
198    tmpMap=NULL;
199    tmpMap=getMapFromMaps(inputs,"overwrite","value");
200    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
201      bOverwrite = TRUE;
202    }
203
204    tmpMap=NULL;
205    tmpMap=getMapFromMaps(inputs,"append","value");
206    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"TRUE",4)==0){
207      bAppend = TRUE;
208    }
209
210    tmpMap=NULL;
211    tmpMap=getMapFromMaps(inputs,"update","value");
212    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"TRUE",4)==0){
213      bUpdate = TRUE;
214    }
215
216    tmpMap=NULL;
217    tmpMap=getMapFromMaps(inputs,"fid","value");
218    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
219      nFIDToFetch = atoi(tmpMap->value);
220    }
221
222    tmpMap=NULL;
223    tmpMap=getMapFromMaps(inputs,"sql","value");
224    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
225      pszSQLStatement = tmpMap->value;
226    }
227
228    tmpMap=getMapFromMaps(inputs,"dialect","value");
229    if(tmpMap!=NULL){
230      pszDialect=strdup(tmpMap->value);
231    }
232
233    tmpMap=NULL;
234    tmpMap=getMapFromMaps(inputs,"nln","value");
235    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
236          pszNewLayerName = tmpMap->value;
237    }
238
239    tmpMap=NULL;
240    tmpMap=getMapFromMaps(inputs,"nlt","value");
241    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
242          pszNewLayerName = tmpMap->value;
243          if( EQUAL(tmpMap->value,"NONE") )
244                  eGType = wkbNone;
245          else if( EQUAL(tmpMap->value,"GEOMETRY") )
246                  eGType = wkbUnknown;
247          else if( EQUAL(tmpMap->value,"POINT") )
248                  eGType = wkbPoint;
249          else if( EQUAL(tmpMap->value,"LINESTRING") )
250                  eGType = wkbLineString;
251          else if( EQUAL(tmpMap->value,"POLYGON") )
252                  eGType = wkbPolygon;
253          else if( EQUAL(tmpMap->value,"GEOMETRYCOLLECTION") )
254                  eGType = wkbGeometryCollection;
255          else if( EQUAL(tmpMap->value,"MULTIPOINT") )
256                  eGType = wkbMultiPoint;
257          else if( EQUAL(tmpMap->value,"MULTILINESTRING") )
258                  eGType = wkbMultiLineString;
259          else if( EQUAL(tmpMap->value,"MULTIPOLYGON") )
260                  eGType = wkbMultiPolygon;
261          else if( EQUAL(tmpMap->value,"GEOMETRY25D") )
262                  eGType = wkbUnknown | wkb25DBit;
263          else if( EQUAL(tmpMap->value,"POINT25D") )
264                  eGType = wkbPoint25D;
265          else if( EQUAL(tmpMap->value,"LINESTRING25D") )
266                  eGType = wkbLineString25D;
267          else if( EQUAL(tmpMap->value,"POLYGON25D") )
268                  eGType = wkbPolygon25D;
269          else if( EQUAL(tmpMap->value,"GEOMETRYCOLLECTION25D") )
270                  eGType = wkbGeometryCollection25D;
271          else if( EQUAL(tmpMap->value,"MULTIPOINT25D") )
272                  eGType = wkbMultiPoint25D;
273          else if( EQUAL(tmpMap->value,"MULTILINESTRING25D") )
274                  eGType = wkbMultiLineString25D;
275          else if( EQUAL(tmpMap->value,"MULTIPOLYGON25D") )
276                  eGType = wkbMultiPolygon25D;
277          else   
278          {
279                  fprintf( stderr, "-nlt %s: type not recognised.\n", 
280                          tmpMap->value );
281                  exit( 1 );
282          }
283    }
284
285    tmpMap=NULL;
286    tmpMap=getMapFromMaps(inputs,"tg","value");
287    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
288          nGroupTransactions = atoi(tmpMap->value);
289    }
290
291    tmpMap=NULL;
292    tmpMap=getMapFromMaps(inputs,"s_srs","value");
293    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
294      pszSourceSRSDef = strdup(tmpMap->value);
295    }
296
297    tmpMap=NULL;
298    tmpMap=getMapFromMaps(inputs,"a_srs","value");
299    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
300      pszOutputSRSDef = strdup(tmpMap->value);
301    }
302
303    tmpMap=NULL;
304    tmpMap=getMapFromMaps(inputs,"t_srs","value");
305    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
306      pszOutputSRSDef = strdup(tmpMap->value);
307      bTransform = TRUE;
308    }
309
310    tmpMap=NULL;
311    tmpMap=getMapFromMaps(inputs,"SPAT","value");
312    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
313      char *tmp=tmpMap->value;
314      char *t=strtok(tmp,",");
315      int cnt=0;
316      double dfULX, dfULY, dfLRX, dfLRY;
317      while(t!=NULL){
318        switch(cnt){
319        case 0:
320          dfULX = atof(t);
321          break;
322        case 1:
323          dfULY = atof(t);
324          break;
325        case 2:
326          dfLRX = atof(t);
327          break;
328        case 3:
329          dfLRY = atof(t);
330          break;
331        }
332        fprintf(stderr,"%s\n\n",t);
333        fprintf(stderr,"%f - %f - %f - %f\n\n",dfULX,dfULY,dfLRX,dfLRY);
334        t=strtok(NULL,",");
335        cnt++;
336      }
337
338      OGRLinearRing  oRing;
339     
340      oRing.addPoint( dfULX, dfULY );
341      oRing.addPoint( dfULX, dfLRY );
342      oRing.addPoint( dfLRX, dfLRY );
343      oRing.addPoint( dfLRX, dfULY );
344      oRing.addPoint( dfULX, dfULY );
345      poSpatialFilter = new OGRPolygon();
346      ((OGRPolygon *) poSpatialFilter)->addRing( &oRing );
347    }
348
349    tmpMap=NULL;
350    tmpMap=getMapFromMaps(inputs,"where","value");
351    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
352          pszWHERE = tmpMap->value;
353    }
354
355    tmpMap=NULL;
356    tmpMap=getMapFromMaps(inputs,"select","value");
357    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
358          pszSelect = tmpMap->value;
359          papszSelFields = CSLTokenizeStringComplex(pszSelect, " ,", 
360                  FALSE, FALSE );
361    }
362
363    tmpMap=NULL;
364    tmpMap=getMapFromMaps(inputs,"segmentize","value");
365    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
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                                        pszDialect );
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        }else{
844          setMapInMaps(conf,"lenv","message","There was an error when running yoru SQL query.");
845          if(pszDialect!=NULL)
846            free(pszDialect);
847          return SERVICE_FAILED;
848        }
849    }
850    if(pszDialect!=NULL)
851      free(pszDialect);
852
853/* -------------------------------------------------------------------- */
854/*      Process each data source layer.                                 */
855/* -------------------------------------------------------------------- */
856    for( int iLayer = 0; 
857         pszSQLStatement == NULL && iLayer < poDS->GetLayerCount(); 
858         iLayer++ )
859    {
860        OGRLayer        *poLayer = poDS->GetLayer(iLayer);
861
862        if( poLayer == NULL )
863        {
864            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
865                    iLayer );
866#ifdef ZOO_SERVICE
867            char tmp[1024];
868            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
869            setMapInMaps(conf,"lenv","message",tmp);
870            return SERVICE_FAILED;
871#else
872            exit( 1 );
873#endif
874        }
875
876        if( CSLCount(papszLayers) == 0
877            || CSLFindString( papszLayers,
878                              poLayer->GetLayerDefn()->GetName() ) != -1 )
879        {
880            if( pszWHERE != NULL )
881                poLayer->SetAttributeFilter( pszWHERE );
882           
883            if( poSpatialFilter != NULL )
884                poLayer->SetSpatialFilter( poSpatialFilter );
885           
886            if( !TranslateLayer( poDS, poLayer, poODS, papszLCO, 
887                                 pszNewLayerName, bTransform, poOutputSRS,
888                                 poSourceSRS, papszSelFields, bAppend, eGType,
889                                 bOverwrite, dfMaxSegmentLength ) 
890                && !bSkipFailures )
891            {
892#ifdef ZOO_SERVICE
893                char tmp[1024];
894                sprintf(tmp,"Terminating translation prematurely after failed of layer %s",poLayer->GetLayerDefn()->GetName() );
895                setMapInMaps(conf,"lenv","message",tmp);
896                return SERVICE_FAILED;
897#else
898                CPLError( CE_Failure, CPLE_AppDefined, 
899                          "Terminating translation prematurely after failed\n"
900                          "translation of layer %s (use -skipfailures to skip errors)\n", 
901                          poLayer->GetLayerDefn()->GetName() );
902
903                exit( 1 );
904#endif
905            }
906        }
907    }
908
909#ifdef ZOO_SERVICE
910    setMapInMaps(outputs,"OutputedDataSourceName","value",(char*)pszwebDestData);
911    //outputs->content=createMap("value",(char*)pszwebDestData);
912#endif
913
914/* -------------------------------------------------------------------- */
915/*      Close down.                                                     */
916/* -------------------------------------------------------------------- */
917    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
918    delete poOutputSRS;
919    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
920    delete poSourceSRS;
921    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
922    delete poODS;
923    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
924    delete poDS;
925    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
926
927#ifndef ZOO_SERVICE
928    CSLDestroy(papszSelFields);
929    CSLDestroy( papszArgv );
930#endif
931    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
932    CSLDestroy( papszLayers );
933    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
934#ifndef ZOO_SERVICE
935    CSLDestroy( papszDSCO );
936    CSLDestroy( papszLCO );
937#endif
938    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
939
940    //OGRCleanupAll();
941    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
942
943#ifdef DBMALLOC
944    malloc_dump(1);
945#endif
946    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
947   
948#ifdef ZOO_SERVICE
949    //sleep(10);
950    return SERVICE_SUCCEEDED;
951#else
952    return 0;
953#endif
954}
955
956/************************************************************************/
957/*                               Usage()                                */
958/************************************************************************/
959
960static void Usage()
961
962{
963#if GDAL_VERSION_MAJOR >= 2
964      GDALDataset *poDS;
965      GDALDataset *poODS;
966      GDALDriverManager* poR=GetGDALDriverManager();
967      GDALDriver          *poDriver = NULL;
968#else
969      OGRDataSource* poDS;
970      OGRDataSource *poODS;
971      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
972      OGRSFDriver          *poDriver = NULL;
973#endif
974
975#ifdef ZOO_SERVICE
976        fprintf(stderr,
977#else
978        printf(
979#endif
980                "Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]\n"
981                "               [-select field_list] [-where restricted_where] \n"
982                "               [-sql <sql statement>] \n" 
983                "               [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n"
984                "               [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n"
985                "               [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]\n"
986                "               [-segmentize max_dist]\n"
987                "               dst_datasource_name src_datasource_name\n"
988                "               [-lco NAME=VALUE] [-nln name] [-nlt type] [layer [layer ...]]\n"
989                "\n"
990                " -f format_name: output file format name, possible values are:\n");
991   
992    for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
993    {
994        poDriver = poR->GetDriver(iDriver);
995
996#if GDAL_VERSION_MAJOR >=2
997        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
998#else
999        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
1000#endif
1001#if GDAL_VERSION_MAJOR >= 2
1002            fprintf( stderr, "  -> %s\n", poDriver->GetDescription() );
1003#else
1004            fprintf( stderr, "  -> %s\n", poDriver->GetName() );
1005#endif
1006    }
1007
1008#ifdef ZOO_SERVICE
1009        fprintf(stderr,
1010#else
1011        printf(
1012#endif
1013                " -append: Append to existing layer instead of creating new if it exists\n"
1014                " -overwrite: delete the output layer and recreate it empty\n"
1015                " -update: Open existing output datasource in update mode\n"
1016                " -select field_list: Comma-delimited list of fields from input layer to\n"
1017                "                     copy to the new layer (defaults to all)\n" 
1018                " -where restricted_where: Attribute query (like SQL WHERE)\n" 
1019                " -sql statement: Execute given SQL statement and save result.\n"
1020                " -skipfailures: skip features or layers that fail to convert\n"
1021                " -gt n: group n features per transaction (default 200)\n"
1022                " -spat xmin ymin xmax ymax: spatial query extents\n"
1023                " -segmentize max_dist: maximum distance between 2 nodes.\n"
1024                "                       Used to create intermediate points\n"
1025                " -dsco NAME=VALUE: Dataset creation option (format specific)\n"
1026                " -lco  NAME=VALUE: Layer creation option (format specific)\n"
1027                " -nln name: Assign an alternate name to the new layer\n"
1028                " -nlt type: Force a geometry type for new layer.  One of NONE, GEOMETRY,\n"
1029                "      POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT,\n"
1030                "      MULTIPOLYGON, or MULTILINESTRING.  Add \"25D\" for 3D layers.\n"
1031                "      Default is type of source layer.\n" );
1032
1033#ifdef ZOO_SERVICE
1034        fprintf(stderr,
1035#else
1036        printf(
1037#endif
1038                " -a_srs srs_def: Assign an output SRS\n"
1039                " -t_srs srs_def: Reproject/transform to this SRS on output\n"
1040                " -s_srs srs_def: Override source SRS\n"
1041                "\n" 
1042                " Srs_def can be a full WKT definition (hard to escape properly),\n"
1043                " or a well known definition (ie. EPSG:4326) or a file with a WKT\n"
1044                " definition.\n" );
1045
1046
1047#ifndef ZOO_SERVICE
1048        exit( 1 );
1049#endif
1050}
1051
1052/************************************************************************/
1053/*                           TranslateLayer()                           */
1054/************************************************************************/
1055
1056static int TranslateLayer(
1057#if GDAL_VERSION_MAJOR >= 2
1058                          GDALDataset
1059#else
1060                          OGRDataSource
1061#endif
1062                          *poSrcDS, 
1063                          OGRLayer * poSrcLayer,
1064#if GDAL_VERSION_MAJOR >= 2
1065                          GDALDataset
1066#else
1067                          OGRDataSource
1068#endif
1069                          *poDstDS,
1070                          char **papszLCO,
1071                          const char *pszNewLayerName,
1072                          int bTransform, 
1073                          OGRSpatialReference *poOutputSRS,
1074                          OGRSpatialReference *poSourceSRS,
1075                          char **papszSelFields,
1076                          int bAppend, int eGType, int bOverwrite,
1077                          double dfMaxSegmentLength)
1078               
1079{
1080    OGRLayer    *poDstLayer;
1081    OGRFeatureDefn *poFDefn;
1082    OGRErr      eErr;
1083    int         bForceToPolygon = FALSE;
1084    int         bForceToMultiPolygon = FALSE;
1085
1086    if( pszNewLayerName == NULL )
1087        pszNewLayerName = poSrcLayer->GetLayerDefn()->GetName();
1088
1089    if( wkbFlatten(eGType) == wkbPolygon )
1090        bForceToPolygon = TRUE;
1091    else if( wkbFlatten(eGType) == wkbMultiPolygon )
1092        bForceToMultiPolygon = TRUE;
1093
1094/* -------------------------------------------------------------------- */
1095/*      Setup coordinate transformation if we need it.                  */
1096/* -------------------------------------------------------------------- */
1097    OGRCoordinateTransformation *poCT = NULL;
1098
1099    if( bTransform )
1100    {
1101        if( poSourceSRS == NULL )
1102            poSourceSRS = poSrcLayer->GetSpatialRef();
1103
1104        if( poSourceSRS == NULL )
1105        {
1106            fprintf( stderr, "Can't transform coordinates, source layer has no\n"
1107                    "coordinate system.  Use -s_srs to set one.\n" );
1108#ifdef ZOO_SERVICE
1109            return SERVICE_FAILED;
1110#else
1111            exit( 1 );
1112#endif
1113        }
1114
1115        CPLAssert( NULL != poSourceSRS );
1116        CPLAssert( NULL != poOutputSRS );
1117
1118        poCT = OGRCreateCoordinateTransformation( poSourceSRS, poOutputSRS );
1119        if( poCT == NULL )
1120        {
1121            char        *pszWKT = NULL;
1122
1123            fprintf( stderr, "Failed to create coordinate transformation between the\n"
1124                   "following coordinate systems.  This may be because they\n"
1125                   "are not transformable, or because projection services\n"
1126                   "(PROJ.4 DLL/.so) could not be loaded.\n" );
1127           
1128            poSourceSRS->exportToPrettyWkt( &pszWKT, FALSE );
1129            fprintf( stderr,  "Source:\n%s\n", pszWKT );
1130           
1131            poOutputSRS->exportToPrettyWkt( &pszWKT, FALSE );
1132            fprintf( stderr,  "Target:\n%s\n", pszWKT );
1133#ifdef ZOO_SERVICE
1134            return SERVICE_FAILED;
1135#else
1136            exit( 1 );
1137#endif
1138        }
1139    }
1140   
1141/* -------------------------------------------------------------------- */
1142/*      Get other info.                                                 */
1143/* -------------------------------------------------------------------- */
1144    poFDefn = poSrcLayer->GetLayerDefn();
1145   
1146    if( poOutputSRS == NULL )
1147        poOutputSRS = poSrcLayer->GetSpatialRef();
1148
1149/* -------------------------------------------------------------------- */
1150/*      Find the layer.                                                 */
1151/* -------------------------------------------------------------------- */
1152    int iLayer = -1;
1153    poDstLayer = NULL;
1154
1155    for( iLayer = 0; iLayer < poDstDS->GetLayerCount(); iLayer++ )
1156    {
1157        OGRLayer        *poLayer = poDstDS->GetLayer(iLayer);
1158
1159        if( poLayer != NULL 
1160            && EQUAL(poLayer->GetLayerDefn()->GetName(),pszNewLayerName) )
1161        {
1162            poDstLayer = poLayer;
1163            break;
1164        }
1165    }
1166   
1167/* -------------------------------------------------------------------- */
1168/*      If the user requested overwrite, and we have the layer in       */
1169/*      question we need to delete it now so it will get recreated      */
1170/*      (overwritten).                                                  */
1171/* -------------------------------------------------------------------- */
1172    if( poDstLayer != NULL && bOverwrite )
1173    {
1174        if( poDstDS->DeleteLayer( iLayer ) != OGRERR_NONE )
1175        {
1176            fprintf( stderr, 
1177                     "DeleteLayer() failed when overwrite requested.\n" );
1178            return FALSE;
1179        }
1180        poDstLayer = NULL;
1181    }
1182
1183/* -------------------------------------------------------------------- */
1184/*      If the layer does not exist, then create it.                    */
1185/* -------------------------------------------------------------------- */
1186    if( poDstLayer == NULL )
1187    {
1188        if( eGType == -2 )
1189            eGType = poFDefn->GetGeomType();
1190
1191        if( !poDstDS->TestCapability( ODsCCreateLayer ) )
1192        {
1193            fprintf( stderr, 
1194              "Layer %s not found, and CreateLayer not supported by driver.", 
1195                     pszNewLayerName );
1196            return FALSE;
1197        }
1198
1199        CPLErrorReset();
1200
1201        poDstLayer = poDstDS->CreateLayer( pszNewLayerName, poOutputSRS,
1202                                           (OGRwkbGeometryType) eGType, 
1203                                           papszLCO );
1204
1205        if( poDstLayer == NULL )
1206            return FALSE;
1207
1208        bAppend = FALSE;
1209    }
1210
1211/* -------------------------------------------------------------------- */
1212/*      Otherwise we will append to it, if append was requested.        */
1213/* -------------------------------------------------------------------- */
1214    else if( !bAppend )
1215    {
1216        fprintf( stderr, "FAILED: Layer %s already exists, and -append not specified.\n"
1217                "        Consider using -append, or -overwrite.\n",
1218                pszNewLayerName );
1219        return FALSE;
1220    }
1221    else
1222    {
1223        if( CSLCount(papszLCO) > 0 )
1224        {
1225            fprintf( stderr, "WARNING: Layer creation options ignored since an existing layer is\n"
1226                    "         being appended to.\n" );
1227        }
1228    }
1229
1230/* -------------------------------------------------------------------- */
1231/*      Add fields.  Default to copy all field.                         */
1232/*      If only a subset of all fields requested, then output only      */
1233/*      the selected fields, and in the order that they were            */
1234/*      selected.                                                       */
1235/* -------------------------------------------------------------------- */
1236    int         iField;
1237    int hasMmField=-1;
1238
1239    if (papszSelFields && !bAppend )
1240    {
1241        for( iField=0; papszSelFields[iField] != NULL; iField++)
1242        {
1243            int iSrcField = poFDefn->GetFieldIndex(papszSelFields[iField]);
1244            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iSrcField);
1245            fprintf(stderr,"NAME: %s\n",tmp->GetNameRef());
1246            fflush(stderr);
1247            if(tmp!=NULL && strncasecmp(tmp->GetNameRef(),"MMID",4)==0)
1248              hasMmField=1;
1249            if (iSrcField >= 0)
1250                poDstLayer->CreateField( poFDefn->GetFieldDefn(iSrcField) );
1251            else
1252            {
1253                fprintf( stderr, "Field '%s' not found in source layer.\n", 
1254                        papszSelFields[iField] );
1255                if( !bSkipFailures )
1256                    return FALSE;
1257            }
1258        }
1259
1260    }
1261    else if( !bAppend )
1262    {
1263      for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ){
1264            poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
1265            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
1266            if(tmp!=NULL && strncasecmp(tmp->GetNameRef(),"MMID",4)==0)
1267              hasMmField=1;
1268            fprintf(stderr,"NAME: %s\n",tmp->GetNameRef());
1269      }
1270    }
1271    /*if(hasMmField<0){
1272      OGRFieldDefn oField( "MMID", OFTInteger );
1273      poDstLayer->CreateField( &oField );
1274      }*/
1275
1276/* -------------------------------------------------------------------- */
1277/*      Transfer features.                                              */
1278/* -------------------------------------------------------------------- */
1279    OGRFeature  *poFeature;
1280    int         nFeaturesInTransaction = 0;
1281    int fCount=0;
1282    poSrcLayer->ResetReading();
1283
1284    if( nGroupTransactions )
1285        poDstLayer->StartTransaction();
1286
1287    while( TRUE )
1288    {
1289        OGRFeature      *poDstFeature = NULL;
1290
1291        if( nFIDToFetch != OGRNullFID )
1292        {
1293            // Only fetch feature on first pass.
1294            if( nFeaturesInTransaction == 0 )
1295                poFeature = poSrcLayer->GetFeature(nFIDToFetch);
1296            else
1297                poFeature = NULL;
1298        }
1299        else
1300            poFeature = poSrcLayer->GetNextFeature();
1301
1302        if( poFeature == NULL )
1303            break;
1304
1305        //poFeature->SetField((poFeature->GetFieldCount()-1),fCount);
1306
1307        if( ++nFeaturesInTransaction == nGroupTransactions )
1308        {
1309            poDstLayer->CommitTransaction();
1310            poDstLayer->StartTransaction();
1311            nFeaturesInTransaction = 0;
1312        }
1313
1314        CPLErrorReset();
1315        poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
1316
1317        if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
1318        {
1319            if( nGroupTransactions )
1320                poDstLayer->CommitTransaction();
1321           
1322            CPLError( CE_Failure, CPLE_AppDefined,
1323                      "Unable to translate feature %ld from layer %s.\n",
1324                      poFeature->GetFID(), poFDefn->GetName() );
1325           
1326            OGRFeature::DestroyFeature( poFeature );
1327            OGRFeature::DestroyFeature( poDstFeature );
1328            return FALSE;
1329        }
1330
1331        if( bPreserveFID )
1332            poDstFeature->SetFID( poFeature->GetFID() );
1333
1334        /*if(hasMmField<0){
1335          poDstFeature->SetField((poDstFeature->GetFieldCount()-1),fCount);
1336          fCount++;
1337        }*/
1338
1339#ifndef GDAL_1_5_0
1340        if (poDstFeature->GetGeometryRef() != NULL && dfMaxSegmentLength > 0)
1341            poDstFeature->GetGeometryRef()->segmentize(dfMaxSegmentLength);
1342#endif
1343
1344        if( poCT && poDstFeature->GetGeometryRef() != NULL )
1345        {
1346            eErr = poDstFeature->GetGeometryRef()->transform( poCT );
1347            if( eErr != OGRERR_NONE )
1348            {
1349                if( nGroupTransactions )
1350                    poDstLayer->CommitTransaction();
1351
1352                fprintf( stderr, "Failed to reproject feature %d (geometry probably out of source or destination SRS).\n", 
1353                        (int) poFeature->GetFID() );
1354                if( !bSkipFailures )
1355                {
1356                    OGRFeature::DestroyFeature( poFeature );
1357                    OGRFeature::DestroyFeature( poDstFeature );
1358                    return FALSE;
1359                }
1360            }
1361        }
1362
1363        if( poDstFeature->GetGeometryRef() != NULL && bForceToPolygon )
1364        {
1365            poDstFeature->SetGeometryDirectly( 
1366                OGRGeometryFactory::forceToPolygon(
1367                    poDstFeature->StealGeometry() ) );
1368        }
1369                   
1370        if( poDstFeature->GetGeometryRef() != NULL && bForceToMultiPolygon )
1371        {
1372            poDstFeature->SetGeometryDirectly( 
1373                OGRGeometryFactory::forceToMultiPolygon(
1374                    poDstFeature->StealGeometry() ) );
1375        }
1376                   
1377        OGRFeature::DestroyFeature( poFeature );
1378
1379        CPLErrorReset();
1380        if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE
1381            && !bSkipFailures )
1382        {
1383            if( nGroupTransactions )
1384                poDstLayer->RollbackTransaction();
1385
1386            OGRFeature::DestroyFeature( poDstFeature );
1387            return FALSE;
1388        }
1389
1390        OGRFeature::DestroyFeature( poDstFeature );
1391    }
1392
1393    if( nGroupTransactions )
1394        poDstLayer->CommitTransaction();
1395
1396/* -------------------------------------------------------------------- */
1397/*      Cleaning                                                        */
1398/* -------------------------------------------------------------------- */
1399    delete poCT;
1400
1401    return TRUE;
1402}
1403
1404#ifdef ZOO_SERVICE
1405}
1406#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