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

Last change on this file was 960, checked in by djay, 4 years ago

Add LCO and DSCO multiple occurence in Ogr2Ogr service. Update ZOO-Kernel messages.po file.

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

Search

Context Navigation

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