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

Last change on this file since 395 was 395, checked in by djay, 11 years ago

Upgrade ogr2ogr to follow new ogr2ogr original source code.

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