Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c	(revision 851)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c	(revision 854)
@@ -138,4 +138,5 @@
     exit(-1);
   }
+  int finalProto=-1;
   map *msOgcVersion=getMapFromMaps(m,"main","msOgcVersion");
   map *dataPath=getMapFromMaps(m,"main","dataPath");
@@ -147,10 +148,19 @@
   map* protoMap=getMap(tmpI->content,"msOgc");
   map* versionMap=getMap(tmpI->content,"msOgcVersion");
-  char options[3][5][25]={
+  char options[4][5][25]={
     {"WMS","1.3.0","GetMap","layers=%s","wms_extent"},
     {"WFS","1.1.0","GetFeature","typename=%s","wcs_extent"},
-    //{"WCS","1.1.0","GetCoverage","coverage=%s","wcs_extent"}
-    {"WCS","2.0.0","GetCoverage","coverageid=%s","wcs_extent"}
+    {"WCS","2.0.0","GetCoverage","coverageid=%s","wcs_extent"},
+    {"WCS","1.1.0","GetCoverage","coverage=%s","wcs_extent"}
   };
+  map* datatype=getMap(tmpI->content,"geodatatype");
+  if(datatype==NULL || strncmp(datatype->value,"other",5)==0){
+    setMapInMaps(m,"lenv","mapError","true");
+    setMapInMaps(m,"lenv","locator",tmpI->name);
+    setMapInMaps(m,"lenv","message",_("The ZOO-Kernel was able to retrieve the data but could not read it as geographic data."));
+    if(getMapFromMaps(m,"lenv","state")==NULL)
+      errorException (m, _("Unable to find any geographic data"), "WrongInputData", tmpI->name);
+    return;
+  }
   int proto=0;
   if(rformat==NULL){
@@ -194,7 +204,8 @@
 
   char* webService_url=(char*)malloc((strlen(msUrl->value)+strlen(format->value)+strlen(tmpI->name)+strlen(width->value)+strlen(height->value)+strlen(extent->value)+256)*sizeof(char));
-  map* datatype=getMap(tmpI->content,"geodatatype");
   
   if(proto>0){
+    if(proto==2)
+      finalProto=1;
     sprintf(webService_url,
 	    "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
@@ -248,9 +259,12 @@
   }
   addToMap(tmpI->content,"Reference",webService_url);
-
+  memset(layers,0,128);
+  sprintf(layers,options[proto][3],tmpI->name);
   protoVersion=options[proto][1];
   extent=getMap(tmpI->content,options[proto][4]);
   memset(webService_url,0,strlen(webService_url));
   if(proto>0){
+    if(proto==2)
+      finalProto=1;
     sprintf(webService_url,
 	    "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
@@ -292,4 +306,27 @@
     addToMap(tmpI->content,"ref_wms_link",webService_url);
   }
+  if(finalProto>0){
+    proto=3;
+    memset(layers,0,128);
+    sprintf(layers,options[proto][3],tmpI->name);
+    protoVersion=options[proto][1];
+    extent=getMap(tmpI->content,options[proto][4]);
+    memset(webService_url,0,strlen(webService_url));
+    sprintf(webService_url,
+	    "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
+	    msUrl->value,
+	    dataPath->value,
+	    tmpI->name,
+	    sid->value,
+	    options[proto][2],
+	    options[proto][0],
+	    protoVersion,
+	    layers,
+	    rformat->value,
+	    extent->value,
+	    crs->value
+	    );
+    addToMap(tmpI->content,"ref_wcs_preview_link",webService_url);
+  }
   if(hasCRS==0){
     freeMap(&crs);
@@ -299,5 +336,4 @@
   free(rformat);
   free(webService_url);
-  dumpMaps(tmpI);
 }
 
@@ -492,6 +528,5 @@
   /**
    * Try to load the file as ZIP
-   */
-
+   *
   OGRDataSourceH poDS1 = NULL;
   OGRSFDriverH *poDriver1 = NULL;
@@ -534,4 +569,6 @@
     fprintf(stderr,"Unable to access the DataSource as ZIP File\n");
     setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
+    fprintf(stderr,"Remove ZIP File!\n");
+    unlink(odsName);
     //OGR_DS_Destroy(poDS1);
   }else{
@@ -594,7 +631,7 @@
     }
     OGR_DS_Destroy(poDS1);
-  }
+    }
   free(sdsName);
-  free(dsName);
+  free(dsName);*/
   
   OGRDataSourceH poDS = NULL;
@@ -649,4 +686,6 @@
     myLayer->status = MS_ON;
     msConnectLayer(myLayer,MS_OGR,pszDataSource);
+
+    addIntToMap(output->content,"nb_features",OGR_L_GetFeatureCount(poLayer,1));
 
     /**
@@ -811,5 +850,5 @@
    */
   GDALAllRegister();
-  hDataset = GDALOpen( pszFilename, GA_ReadOnly );
+  hDataset = GDALOpen( pszFilename, GA_Update );
   if( hDataset == NULL ){
 #ifdef DEBUGMS
@@ -866,4 +905,5 @@
   m->width=GDALGetRasterXSize( hDataset );
   m->height=GDALGetRasterYSize( hDataset );
+  addIntToMap(output->content,"nb_pixels",GDALGetRasterXSize( hDataset )*GDALGetRasterYSize( hDataset ));
   
   /**
@@ -880,5 +920,9 @@
     setSrsInformations(output,m,myLayer,pszProjection);
   }else{
-    fprintf(stderr,"NO SRS FOUND ! %s\n",GDALGetProjectionRef( hDataset ));    
+    fprintf(stderr,"NO SRS FOUND ! %s\n",GDALGetProjectionRef( hDataset ));
+    CPLErr sp=GDALSetProjection( hDataset , "+init=epsg:4326" );
+    if(sp!=CE_None){
+      fprintf(stderr,"NO SRS FOUND ! %s\n",CPLGetLastErrorMsg());
+    }
   }
 
@@ -905,4 +949,12 @@
       addToMap(output->content,"boundingbox",extent);
     }
+  }else{
+    int scale=1;
+    if(m->width>2048){
+      addIntToMap(output->content,"width",2048);
+      scale=2048/m->width;
+    }else
+      addIntToMap(output->content,"width",m->width);
+    addIntToMap(output->content,"height",m->height*scale);
   }
 
@@ -919,9 +971,13 @@
   }
   if(nBandsI>=3)
-    msLayerAddProcessing(myLayer,"BANDS=1,2,3");
+    if(nBandsI==4)
+      msLayerAddProcessing(myLayer,"BANDS=1,2,3,4");
+    else
+      msLayerAddProcessing(myLayer,"BANDS=1,2,3");
   else if(nBandsI>=2)
     msLayerAddProcessing(myLayer,"BANDS=1,2");
   else
     msLayerAddProcessing(myLayer,"BANDS=1");
+  
 
   /**
@@ -929,7 +985,7 @@
    */
   char lBands[7];
-  memset(&nBands,0,7);
   char *nameBands=NULL;
   for( iBand = 0; iBand < nBandsI; iBand++ ){
+    memset(&lBands,0,7);
     sprintf(lBands,"Band%d",iBand+1);    
     if(nameBands==NULL){
@@ -937,5 +993,5 @@
       sprintf(nameBands,"%s",lBands);
     }else{
-      if(iBand<4){
+      /*if(iBand<4)*/{
 	char *tmpS=zStrdup(nameBands);
 	nameBands=(char*)realloc(nameBands,(strlen(tmpS)+strlen(lBands)+2)*sizeof(char));
@@ -954,21 +1010,20 @@
    */
   for( iBand = 0; iBand < nBandsI; iBand++ ){
-    //int         bGotNodata;//, bSuccess;
-    double      adfCMinMax[2]/*, dfNoData*/;
-    //int         nBlockXSize, nBlockYSize, nMaskFlags;
-    //double      /*dfMean, dfStdDev*/;
+    double      pdfMin, pdfMax, pdfMean, pdfStdDev;
     hBand = GDALGetRasterBand( hDataset, iBand+1 );
 
     CPLErrorReset();
-    GDALComputeRasterMinMax( hBand, FALSE, adfCMinMax );
+    GDALComputeRasterStatistics( hBand, TRUE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev, NULL,NULL);
     char tmpN[21];
     sprintf(tmpN,"Band%d",iBand+1);
     if (CPLGetLastErrorType() == CE_None){
-      char tmpMm[100];
-      sprintf(tmpMm,"%.3f %.3f",adfCMinMax[0],adfCMinMax[1]);
+      char tmpMm[100],tmpMp[100];
+      sprintf(tmpMm,"%.3f %.3f",pdfMin,pdfMax);
+      sprintf(tmpMp,"SCALE_%d=%.3f,%.3f",iBand+1,pdfMean-(2*pdfStdDev),pdfMean+(2*pdfStdDev));
       char tmpI[31];      
       sprintf(tmpI,"%s_interval",tmpN);
       msInsertHashTable(&(myLayer->metadata), tmpI, tmpMm);
-
+      if(pdfMax>255)
+	msLayerAddProcessing(myLayer,tmpMp);
       map* test=getMap(output->content,"msClassify");
       if(test!=NULL && strncasecmp(test->value,"true",4)==0){
@@ -990,7 +1045,7 @@
 	  
 	if(nBandsI==1){
-	  double delta=adfCMinMax[1]-adfCMinMax[0];
+	  double delta=pdfMax-pdfMin;
 	  double interval=delta/10;
-	  double cstep=adfCMinMax[0];
+	  double cstep=pdfMin;
 	  for(i=0;i<10;i++){
 	    /**
@@ -1036,5 +1091,5 @@
 	  
 	  char tmpMm[100];
-	  sprintf(tmpMm,"%.3f %.3f",adfCMinMax[0],adfCMinMax[1]);
+	  sprintf(tmpMm,"%.3f %.3f",pdfMin,pdfMax);
 	  
 	}
@@ -1046,5 +1101,4 @@
 	  myLayer->offsite.blue=0;
 	}
-	msLayerAddProcessing(myLayer,"RESAMPLE=BILINEAR");
       }
     }
@@ -1056,4 +1110,5 @@
 
   }
+  msLayerAddProcessing(myLayer,"RESAMPLE=BILINEAR");
 
   m->layerorder[m->numlayers] = m->numlayers;
@@ -1081,24 +1136,27 @@
       ext="json";
 
-  map* tmpMap=getMapFromMaps(conf,"main","dataPath");
-  map* sidMap=getMapFromMaps(conf,"lenv","usid");
-  char *pszDataSource=(char*)malloc((strlen(tmpMap->value)+strlen(sidMap->value)+strlen(outputs->name)+17)*sizeof(char));
-  sprintf(pszDataSource,"%s/ZOO_DATA_%s_%s.%s",tmpMap->value,outputs->name,sidMap->value,ext);
-  int f=zOpen(pszDataSource,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
-  map *gfile=getMap(outputs->content,"generated_file");
-  if(gfile!=NULL){
-    readGeneratedFile(conf,outputs->content,gfile->value);	    
-  }
-  map* sizeMap=getMap(outputs->content,"size");
-  map* vData=getMap(outputs->content,"value");
-  if(sizeMap!=NULL){
-    zWrite(f,vData->value,atoi(sizeMap->value)*sizeof(char));
-  }
-  else{
-    zWrite(f,vData->value,(strlen(vData->value)+1)*sizeof(char));
-  }
-  close(f);
-  addToMap(outputs->content,"storage",pszDataSource);
-  free(pszDataSource);
+  map* storage=getMap(outputs->content,"storage");
+  if(storage==NULL){
+    map* tmpMap=getMapFromMaps(conf,"main","dataPath");
+    map* sidMap=getMapFromMaps(conf,"lenv","usid");
+    char *pszDataSource=(char*)malloc((strlen(tmpMap->value)+strlen(sidMap->value)+strlen(outputs->name)+17)*sizeof(char));
+    sprintf(pszDataSource,"%s/ZOO_DATA_%s_%s.%s",tmpMap->value,outputs->name,sidMap->value,ext);
+    int f=zOpen(pszDataSource,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
+    map *gfile=getMap(outputs->content,"generated_file");
+    if(gfile!=NULL){
+      readGeneratedFile(conf,outputs->content,gfile->value);	    
+    }
+    map* sizeMap=getMap(outputs->content,"size");
+    map* vData=getMap(outputs->content,"value");
+    if(sizeMap!=NULL){
+      zWrite(f,vData->value,atoi(sizeMap->value)*sizeof(char));
+    }
+    else{
+      zWrite(f,vData->value,(strlen(vData->value)+1)*sizeof(char));
+    }
+    close(f);
+    addToMap(outputs->content,"storage",pszDataSource);
+    free(pszDataSource);
+  }
 
   /*
@@ -1174,4 +1232,15 @@
 #endif
 
+  outputFormatObj *o6=msCreateDefaultOutputFormat(NULL,"GDAL/GTiff","geotiff");
+  if(!o6)
+    fprintf(stderr,"Unable to initialize GDAL driver !\n");
+  else{
+    o6->imagemode=MS_IMAGEMODE_BYTE;
+    o6->mimetype=strdup("image/geotiff");
+    o6->inmapfile=MS_TRUE;
+    msAppendOutputFormat(myMap,msCloneOutputFormat(o6));
+    msFreeOutputFormat(o6);
+  }
+
   /*
    * Set default projection to EPSG:4326
@@ -1262,6 +1331,8 @@
   msSaveMap(myMap,mapPath);
   free(mapPath);
+  //msFreeSymbolSet(&myMap->symbolset);
+  msFreeMap(myMap);
+  //msFree(myMap);
   msGDALCleanup();
-  msFreeMap(myMap);
 }
 
