source: branches/prototype-v0/zoo-project/zoo-services/ogr/ogr2ogr/service.c @ 862

Last change on this file since 862 was 862, checked in by djay, 6 years ago

Add the capability to publish heatmap or any templated mapfile using the epecific msInclude and msLayer keys for an output. For MapServer? published output, define 4096 as the default maxsize and use pixel width or height for raster files. use the correct MapServer? imagemode depending on GDALGetRasterDataType (MS_IMAGEMODE_BYTE for GDT_Byte, MS_IMAGEMODE_INT16 for GDT_Int16 and MS_IMAGEMODE_FLOAT32 for GDT_Float32). Create a text file (.maps) listing every mapfiles created for a MapServer? published output (or inputs) using saveMapNames function. Fixes in ulinet, use uuid for naming temporary files. Add dialect input to the ogr2ogr service. Use the .maps file for removing a file from the DeleteData? service

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