Index: trunk/zoo-project/zoo-services/ogr/ogr2ogr/service.c
===================================================================
--- trunk/zoo-project/zoo-services/ogr/ogr2ogr/service.c	(revision 389)
+++ trunk/zoo-project/zoo-services/ogr/ogr2ogr/service.c	(revision 395)
@@ -39,4 +39,9 @@
 CPL_CVSID("$Id: ogr2ogr.cpp 15473 2008-10-07 20:59:24Z warmerdam $");
 
+#ifdef WIN32
+#define strcasecmp _stricmp
+#define strncasecmp _strnicmp 
+#endif
+
 #ifdef ZOO_SERVICE
 extern "C" {
@@ -60,5 +65,5 @@
 static int bSkipFailures = FALSE;
 static int nGroupTransactions = 200;
-static int bPreserveFID = FALSE;
+static int bPreserveFID = TRUE;
 static int nFIDToFetch = OGRNullFID;
 
@@ -97,4 +102,7 @@
     double      dfMaxSegmentLength = 0;
 
+#ifdef ZOO_SERVICE
+    dumpMaps(inputs);
+#endif
     /* Check strict compilation and runtime library version as we use C++ API */
     if (! GDAL_CHECK_VERSION("ogr2ogr"))
@@ -142,5 +150,5 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"F","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
       pszFormat=tmpMap->value;
     }
@@ -148,5 +156,5 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"DSCO","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  papszDSCO = CSLAddString(papszDSCO, tmpMap->value );
     }
@@ -154,5 +162,5 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"LCO","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  papszLCO = CSLAddString(papszLCO, tmpMap->value );
     }
@@ -160,5 +168,5 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"preserve_fid","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  bPreserveFID = TRUE;
     }
@@ -166,40 +174,43 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"skipfailure","value");
-    if(tmpMap!=NULL){
-	  bPreserveFID = TRUE;
-	  bSkipFailures = TRUE;
-	  nGroupTransactions = 1; /* #2409 */
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
+      bSkipFailures = TRUE;
+      nGroupTransactions = 1; /* #2409 */
+    }
+
+    /* if exist, overwrite the data with the same name */
+    tmpMap=NULL;
+    tmpMap=getMapFromMaps(inputs,"overwrite","value");
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
+      bOverwrite = TRUE;
     }
 
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"append","value");
-    if(tmpMap!=NULL){
-	  bAppend = TRUE;
-    }
-
-    /* if exist, overwrite the data with the same name */
-    bOverwrite = TRUE;
-    
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"TRUE",4)==0){
+      bAppend = TRUE;
+    }
+
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"update","value");
-    if(tmpMap!=NULL){
-	  bUpdate = TRUE;
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"TRUE",4)==0){
+      bUpdate = TRUE;
     }
 
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"fid","value");
-    if(tmpMap!=NULL){
-	  nFIDToFetch = atoi(tmpMap->value);
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
+      nFIDToFetch = atoi(tmpMap->value);
     }
 
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"sql","value");
-    if(tmpMap!=NULL){
-	  pszSQLStatement = tmpMap->value;
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
+      pszSQLStatement = tmpMap->value;
     }
 
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"nln","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  pszNewLayerName = tmpMap->value;
     }
@@ -207,5 +218,5 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"nlt","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  pszNewLayerName = tmpMap->value;
 	  if( EQUAL(tmpMap->value,"NONE") )
@@ -253,5 +264,5 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"tg","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  nGroupTransactions = atoi(tmpMap->value);
     }
@@ -259,24 +270,24 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"s_srs","value");
-    if(tmpMap!=NULL){
-	  pszSourceSRSDef = tmpMap->value;
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
+      pszSourceSRSDef = strdup(tmpMap->value);
     }
 
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"a_srs","value");
-    if(tmpMap!=NULL){
-	  pszOutputSRSDef = tmpMap->value;
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
+      pszOutputSRSDef = strdup(tmpMap->value);
     }
 
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"t_srs","value");
-    if(tmpMap!=NULL){
-	  pszOutputSRSDef = tmpMap->value;
-	  bTransform = TRUE;
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
+      pszOutputSRSDef = strdup(tmpMap->value);
+      bTransform = TRUE;
     }
 
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"SPAT","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
       char *tmp=tmpMap->value;
       char *t=strtok(tmp,",");
@@ -317,5 +328,5 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"where","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  pszWHERE = tmpMap->value;
     }
@@ -323,5 +334,5 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"select","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  pszSelect = tmpMap->value;
 	  papszSelFields = CSLTokenizeStringComplex(pszSelect, " ,", 
@@ -331,19 +342,31 @@
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"segmentize","value");
-    if(tmpMap!=NULL){
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
 	  dfMaxSegmentLength = atof(tmpMap->value);
     }
 
-    tmpMap=NULL;
+    /*tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"segmentize","value");
     if(tmpMap!=NULL){
 	  dfMaxSegmentLength = atof(tmpMap->value);
-    }
+    }*/
 
     tmpMap=NULL;
     tmpMap=getMapFromMaps(inputs,"InputDSN","value");
-    if(tmpMap!=NULL){
-      pszDataSource=(char*)malloc(sizeof(char)*(strlen(dataPath)+strlen(tmpMap->value)+1));
-      sprintf((char*)pszDataSource,"%s/%s",dataPath,tmpMap->value);
+    if(tmpMap!=NULL && strncasecmp(tmpMap->value,"NULL",4)!=0){
+      if(strncasecmp(tmpMap->value,"PG",2)!=0 &&
+	 strncasecmp(tmpMap->value,"My",2)!=0 &&
+	 strncasecmp(tmpMap->value,"OCI",3)!=0 ){
+	if(strncasecmp(dataPath,tmpMap->value,strlen(dataPath))==0 ||
+	   strncasecmp(tempPath,tmpMap->value,strlen(tempPath))==0){
+	  pszDataSource=strdup(tmpMap->value);
+	}else{
+	  pszDataSource=(char*)malloc(sizeof(char)*(strlen(dataPath)+strlen(tmpMap->value)+2));
+	  sprintf((char*)pszDataSource,"%s/%s",dataPath,tmpMap->value);
+	}
+      }else{
+	pszDataSource=(char*)malloc(sizeof(char)*(strlen(tmpMap->value)+1));
+	sprintf((char*)pszDataSource,"%s",tmpMap->value);	
+      }
     }
 
@@ -351,10 +374,21 @@
     tmpMap=getMapFromMaps(inputs,"OutputDSN","value");
     if(tmpMap!=NULL){
-      pszDestDataSource=(char*)malloc(sizeof(char)*(strlen(tempPath)+strlen(tmpMap->value)+4));
-      sprintf((char*)pszDestDataSource,"%s/%s",tempPath,tmpMap->value/*,ext*/);
-      pszwebDestData=(char*)malloc(sizeof(char)*(strlen(serverAddress)+strlen(tmpurl)+strlen(tmpMap->value)+4));
-      sprintf((char*)pszwebDestData,"%s%s/%s",serverAddress,tmpurl,tmpMap->value/*,ext*/);
-    }
-
+      if(strncasecmp(tmpMap->value,"PG",2)!=0 &&
+	 strncasecmp(tmpMap->value,"MY",2)!=0 &&
+	 strncasecmp(tmpMap->value,"OCI",3)!=0){
+	pszDestDataSource=(char*)malloc(sizeof(char)*(strlen(tempPath)+strlen(tmpMap->value)+4));
+	sprintf((char*)pszDestDataSource,"%s/%s",tempPath,tmpMap->value/*,ext*/);
+	pszwebDestData=(char*)malloc(sizeof(char)*(strlen(serverAddress)+strlen(tmpurl)+strlen(tmpMap->value)+4));
+	sprintf((char*)pszwebDestData,"%s%s/%s",serverAddress,tmpurl,tmpMap->value/*,ext*/);
+      }
+      else{
+	pszDestDataSource=(char*)malloc(sizeof(char)*(strlen(tmpMap->value)+1));
+	sprintf((char*)pszDestDataSource,"%s",tmpMap->value/*,ext*/);
+	pszwebDestData=(char*)malloc(sizeof(char)*(strlen(serverAddress)+strlen(tmpurl)+strlen(tmpMap->value)+4));
+	sprintf((char*)pszwebDestData,"%s%s/%s",serverAddress,tmpurl,tmpMap->value/*,ext*/);
+      }
+    }
+
+    fprintf(stderr,"Message %s\n",pszDestDataSource);
 #else
 /* -------------------------------------------------------------------- */
@@ -529,6 +563,7 @@
 #ifdef ZOO_SERVICE
 	{
-#endif
+#else
 	  Usage();
+#endif
 #ifdef ZOO_SERVICE
 	  setMapInMaps(conf,"lenv","message","Wrong parameter");
@@ -722,5 +757,5 @@
             fprintf( stderr,  "-where clause ignored in combination with -sql.\n" );
         if( CSLCount(papszLayers) > 0 )
-            fprintf( stderr,  "layer names ignored in combination with -sql.\n" );
+	  fprintf( stderr,  "layer names ignored in combination with -sql. (%s)\n", pszSQLStatement);
         
         poResultSet = poDS->ExecuteSQL( pszSQLStatement, poSpatialFilter, 
@@ -783,4 +818,10 @@
                 && !bSkipFailures )
             {
+#ifdef ZOO_SERVICE
+		char tmp[1024];
+		sprintf(tmp,"Terminating translation prematurely after failed of layer %s",poLayer->GetLayerDefn()->GetName() );
+		setMapInMaps(conf,"lenv","message",tmp);
+		return SERVICE_FAILED;
+#else
                 CPLError( CE_Failure, CPLE_AppDefined, 
                           "Terminating translation prematurely after failed\n"
@@ -788,10 +829,4 @@
                           poLayer->GetLayerDefn()->GetName() );
 
-#ifdef ZOO_SERVICE
-		char tmp[1024];
-		sprintf(tmp,"Terminating translation prematurely after failed of layer %s",poLayer->GetLayerDefn()->GetName() );
-		setMapInMaps(conf,"lenv","message",tmp);
-		return SERVICE_FAILED;
-#else
                 exit( 1 );
 #endif
@@ -800,31 +835,46 @@
     }
 
+#ifdef ZOO_SERVICE
+    outputs->content=createMap("value",(char*)pszwebDestData);
+#endif
+
 /* -------------------------------------------------------------------- */
 /*      Close down.                                                     */
 /* -------------------------------------------------------------------- */
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
     delete poOutputSRS;
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
     delete poSourceSRS;
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
     delete poODS;
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
     delete poDS;
-
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+
+#ifndef ZOO_SERVICE
     CSLDestroy(papszSelFields);
+    CSLDestroy( papszArgv );
+#endif
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+    CSLDestroy( papszLayers );
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
 #ifndef ZOO_SERVICE
-	CSLDestroy( papszArgv );
-#endif
-    CSLDestroy( papszLayers );
     CSLDestroy( papszDSCO );
     CSLDestroy( papszLCO );
+#endif
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
 
     OGRCleanupAll();
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
 
 #ifdef DBMALLOC
     malloc_dump(1);
 #endif
+    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
     
 #ifdef ZOO_SERVICE
-    outputs->content=createMap("value",(char*)pszwebDestData);
     return SERVICE_SUCCEEDED;
 #else
-	return 0;
+    return 0;
 #endif
 }
@@ -1082,4 +1132,5 @@
 /* -------------------------------------------------------------------- */
     int         iField;
+    int hasMmField=-1;
 
     if (papszSelFields && !bAppend )
@@ -1088,4 +1139,9 @@
         {
             int iSrcField = poFDefn->GetFieldIndex(papszSelFields[iField]);
+	    OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iSrcField);
+	    fprintf(stderr,"NAME: %s\n",tmp->GetNameRef());
+	    fflush(stderr);
+	    if(tmp!=NULL && strncasecmp(tmp->GetNameRef(),"MMID",4)==0)
+	      hasMmField=1;
             if (iSrcField >= 0)
                 poDstLayer->CreateField( poFDefn->GetFieldDefn(iSrcField) );
@@ -1098,10 +1154,20 @@
             }
         }
+
     }
     else if( !bAppend )
     {
-        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
+      for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ){
             poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
-    }
+	    OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
+	    if(tmp!=NULL && strncasecmp(tmp->GetNameRef(),"MMID",4)==0)
+	      hasMmField=1;
+	    fprintf(stderr,"NAME: %s\n",tmp->GetNameRef());
+      }
+    }
+    /*if(hasMmField<0){
+      OGRFieldDefn oField( "MMID", OFTInteger );
+      poDstLayer->CreateField( &oField );
+      }*/
 
 /* -------------------------------------------------------------------- */
@@ -1110,5 +1176,5 @@
     OGRFeature  *poFeature;
     int         nFeaturesInTransaction = 0;
-    
+    int fCount=0;
     poSrcLayer->ResetReading();
 
@@ -1130,7 +1196,9 @@
         else
             poFeature = poSrcLayer->GetNextFeature();
-        
+
         if( poFeature == NULL )
             break;
+
+	//poFeature->SetField((poFeature->GetFieldCount()-1),fCount);
 
         if( ++nFeaturesInTransaction == nGroupTransactions )
@@ -1161,4 +1229,9 @@
             poDstFeature->SetFID( poFeature->GetFID() );
 
+	/*if(hasMmField<0){
+	  poDstFeature->SetField((poDstFeature->GetFieldCount()-1),fCount);
+	  fCount++;
+	}*/
+
 #ifndef GDAL_1_5_0
         if (poDstFeature->GetGeometryRef() != NULL && dfMaxSegmentLength > 0)
