source: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c @ 851

Last change on this file since 851 was 851, checked in by djay, 7 years ago

Invoke callback asynchronously. Still the ZOO-Kernel has still to wait for every requests to finish before stoping its execution.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 39.5 KB
Line 
1/*
2 * Author : Gérald FENOY
3 *
4 *  Copyright 2010-2011 Fondazione Edmund Mach. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/**
26 * Cross platform definition of layerObj->class
27 */
28#ifndef WIN32
29#define CLASS class
30#else
31#define CLASS _class
32#endif
33#include "service_internal_ms.h"
34#include "server_internal.h"
35#include "response_print.h"
36
37/**
38 * Get a list of configuration keys having a corresponding mandatory ows_*.
39 * Map composed by a main.cfg maps name as key and the corresponding
40 * MapServer Mafile Metadata name to use
41 * see doc from here :
42 *  - http://mapserver.org/ogc/wms_server.html
43 *  - http://mapserver.org/ogc/wfs_server.html
44 *  - http://mapserver.org/ogc/wcs_server.html
45 *
46 * @return a new map containing a table linking a name of a configuration key
47 * to a corresponding mandatory ows_* keyword (ie. "fees" => "ows_fees").
48 */
49map* getCorrespondance(){
50  map* res=createMap("encoding","ows_encoding");
51  addToMap(res,"abstract","ows_abstract");
52  addToMap(res,"title","ows_title");
53  addToMap(res,"keywords","ows_keywordlist");
54  addToMap(res,"fees","ows_fees");
55  addToMap(res,"accessConstraints","ows_accessconstraints");
56  addToMap(res,"providerName","ows_attribution_title");
57  addToMap(res,"providerSite","ows_service_onlineresource");
58  addToMap(res,"individualName","ows_contactperson");
59  addToMap(res,"positionName","ows_contactposition");
60  addToMap(res,"providerName","ows_contactorganization");
61  addToMap(res,"role","ows_role");
62  addToMap(res,"addressType","ows_addresstype");
63  addToMap(res,"addressCity","ows_city");
64  addToMap(res,"addressDeliveryPoint","ows_address");
65  addToMap(res,"addressPostalCode","ows_postcode");
66  addToMap(res,"addressAdministrativeArea","ows_stateorprovince");
67  addToMap(res,"addressCountry","ows_country");
68  addToMap(res,"phoneVoice","ows_contactvoicetelephone");
69  addToMap(res,"phoneFacsimile","ows_contactfacsimiletelephone");
70  addToMap(res,"addressElectronicMailAddress","ows_contactelectronicmailaddress");
71  // Missing Madatory Informations
72  addToMap(res,"hoursOfService","ows_hoursofservice");
73  addToMap(res,"contactInstructions","ows_contactinstructions");
74  return res;
75}
76
77/**
78 * Add width and height keys to an output maps containing the maximum width
79 * and height for displaying the full data extent.
80 * Restriction to an image having a size of 640x480 (width * height)
81 *
82 * @param output
83 * @param minx the lower left x coordinate
84 * @param miny the lower left y coordinate
85 * @param maxx the upper right x coordinate
86 * @param maxy the upper right y coordinate
87 */
88void setMapSize(maps* output,double minx,double miny,double maxx,double maxy){
89  double maxWidth=640;
90  double maxHeight=480;
91  double deltaX=maxx-minx;
92  double deltaY=maxy-miny;
93  double qWidth;
94  qWidth=maxWidth/deltaX;
95  double qHeight;
96  qHeight=maxHeight/deltaY;
97#ifdef DEBUGMS
98  fprintf(stderr,"deltaX : %.15f \ndeltaY : %.15f\n",deltaX,deltaY);
99  fprintf(stderr,"qWidth : %.15f \nqHeight : %.15f\n",qWidth,qHeight);
100#endif
101
102  double width=deltaX*qWidth;
103  double height=height=deltaY*qWidth;
104  if(deltaX<deltaY){
105    width=deltaX*qHeight;
106    height=deltaY*qHeight;
107  }
108  if(height<0)
109    height=-height;
110  if(width<0)
111    width=-width;
112  char sWidth[1024];
113  char sHeight[1024];
114  sprintf(sWidth,"%.3f",width);
115  sprintf(sHeight,"%.3f",height);
116#ifdef DEBUGMS
117  fprintf(stderr,"sWidth : %.15f \nsHeight : %.15f\n",sWidth,sHeight);
118#endif
119  if(output!=NULL){
120    addToMap(output->content,"width",sWidth);
121    addToMap(output->content,"height",sHeight);
122  }
123}
124
125/**
126 * Add a Reference key to an output containing the WMFS/WFS/WCS request for
127 * accessing service result
128 *
129 * @param m the conf maps containing the main.cfg settings
130 * @param tmpI the specific output maps to add the Reference key
131 */
132void setReferenceUrl(maps* m,maps* tmpI){
133  outputMapfile(m,tmpI);
134  map *msUrl=getMapFromMaps(m,"main","mapserverAddress");
135  if(msUrl==NULL){
136    errorException (m, _("Unable to find any mapserverAddress defined in the main.cfg file"),
137                    "InternalError", NULL);
138    exit(-1);
139  }
140  map *msOgcVersion=getMapFromMaps(m,"main","msOgcVersion");
141  map *dataPath=getMapFromMaps(m,"main","dataPath");
142  map *sid=getMapFromMaps(m,"lenv","usid");
143  map* format=getMap(tmpI->content,"mimeType");
144  map* rformat=getMap(tmpI->content,"requestedMimeType");
145  map* width=getMap(tmpI->content,"width");
146  map* height=getMap(tmpI->content,"height");
147  map* protoMap=getMap(tmpI->content,"msOgc");
148  map* versionMap=getMap(tmpI->content,"msOgcVersion");
149  char options[3][5][25]={
150    {"WMS","1.3.0","GetMap","layers=%s","wms_extent"},
151    {"WFS","1.1.0","GetFeature","typename=%s","wcs_extent"},
152    //{"WCS","1.1.0","GetCoverage","coverage=%s","wcs_extent"}
153    {"WCS","2.0.0","GetCoverage","coverageid=%s","wcs_extent"}
154  };
155  int proto=0;
156  if(rformat==NULL){
157    rformat=getMap(tmpI->content,"mimeType");
158  }
159  if(strncasecmp(rformat->value,"text/xml",8)==0)
160    proto=1;
161  if(strncasecmp(rformat->value,"image/tiff",10)==0 ||
162     strncasecmp(rformat->value,"image/geotiff",10)==0)
163    proto=2;
164  if(protoMap!=NULL){
165    if(strncasecmp(protoMap->value,"WMS",3)==0)
166      proto=0;
167    else{
168      if(strncasecmp(protoMap->value,"WFS",3)==0)
169        proto=1;
170      else 
171        proto=2;
172    }
173  }
174  char *protoVersion=options[proto][1];
175  if(proto==1){
176    if(msOgcVersion!=NULL)
177      protoVersion=msOgcVersion->value;
178    if(versionMap!=NULL)
179      protoVersion=versionMap->value;
180  }
181
182  map* extent=getMap(tmpI->content,options[proto][4]);
183  map* crs=getMap(tmpI->content,"crs");
184  int hasCRS=1;
185  if(crs==NULL){
186    crs=getMapFromMaps(m,"main","crs");
187    if(crs==NULL){
188      crs=createMap("crs","epsg:4326");
189      hasCRS=0;
190    }
191  }
192  char layers[128];
193  sprintf(layers,options[proto][3],tmpI->name);
194
195  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));
196  map* datatype=getMap(tmpI->content,"geodatatype");
197 
198  if(proto>0){
199    sprintf(webService_url,
200            "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
201            msUrl->value,
202            dataPath->value,
203            tmpI->name,
204            sid->value,
205            options[proto][2],
206            options[proto][0],
207            protoVersion,
208            layers,
209            rformat->value,
210            extent->value,
211            crs->value
212            );
213    if(datatype!=NULL && strncasecmp(datatype->value,"raster",6)==0){
214      addToMap(tmpI->content,"ref_wcs_link",webService_url);
215    }
216    else{
217      addToMap(tmpI->content,"ref_wfs_link",webService_url);
218    }
219    proto=0;
220    rformat=createMap("mimeType","image/png");
221  }
222  else{
223    sprintf(webService_url,
224            "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&width=%s&height=%s&format=%s&bbox=%s&crs=%s",
225            msUrl->value,
226            dataPath->value,
227            tmpI->name,
228            sid->value,
229            options[proto][2],
230            options[proto][0],
231            protoVersion,
232            layers,
233            width->value,
234            height->value,
235            rformat->value,
236            extent->value,
237            crs->value
238            );
239    addToMap(tmpI->content,"ref_wms_link",webService_url);
240    if(datatype!=NULL && strncasecmp(datatype->value,"raster",6)==0){
241      proto=2;
242      rformat=createMap("mimeType","image/tiff");
243    }
244    else{
245      proto=1;
246      rformat=createMap("mimeType","text/xml");
247    }
248  }
249  addToMap(tmpI->content,"Reference",webService_url);
250
251  protoVersion=options[proto][1];
252  extent=getMap(tmpI->content,options[proto][4]);
253  memset(webService_url,0,strlen(webService_url));
254  if(proto>0){
255    sprintf(webService_url,
256            "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
257            msUrl->value,
258            dataPath->value,
259            tmpI->name,
260            sid->value,
261            options[proto][2],
262            options[proto][0],
263            protoVersion,
264            layers,
265            rformat->value,
266            extent->value,
267            crs->value
268            );
269    if(datatype!=NULL && strncasecmp(datatype->value,"raster",6)==0){
270      addToMap(tmpI->content,"ref_wcs_link",webService_url);
271    }
272    else{
273      addToMap(tmpI->content,"ref_wfs_link",webService_url);
274    }
275  }else{
276    sprintf(webService_url,
277            "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&width=%s&height=%s&format=%s&bbox=%s&crs=%s",
278            msUrl->value,
279            dataPath->value,
280            tmpI->name,
281            sid->value,
282            options[proto][2],
283            options[proto][0],
284            protoVersion,
285            layers,
286            width->value,
287            height->value,
288            rformat->value,
289            extent->value,
290            crs->value
291            );
292    addToMap(tmpI->content,"ref_wms_link",webService_url);
293  }
294  if(hasCRS==0){
295    freeMap(&crs);
296    free(crs);
297  }
298  freeMap(&rformat);
299  free(rformat);
300  free(webService_url);
301  dumpMaps(tmpI);
302}
303
304/**
305 * Set projection for a layer in a MAPFILE using Authority Code and Name if
306 * available or fallback to proj4 definition if available or fallback to
307 * default EPSG:4326
308 *
309 * @param output the output maps
310 * @param m the opened mapObj
311 * @param myLayer the layerObj
312 * @param pszProjection a char* containing the SRS definition in WKT format
313 */
314void setSrsInformations(maps* output,mapObj* m,layerObj* myLayer,
315                        char* pszProjection){
316  OGRSpatialReferenceH  hSRS;
317  map* msSrs=NULL;
318  hSRS = OSRNewSpatialReference(NULL);
319  if( pszProjection!=NULL && strlen(pszProjection)>1){
320    if(OSRImportFromWkt( hSRS, &pszProjection ) == CE_None ){
321      char *proj4Str=NULL;
322      if(OSRGetAuthorityName(hSRS,NULL)!=NULL && 
323         OSRGetAuthorityCode(hSRS,NULL)!=NULL){
324        char tmpSrs[20];
325        sprintf(tmpSrs,"%s:%s",
326                OSRGetAuthorityName(hSRS,NULL),OSRGetAuthorityCode(hSRS,NULL));
327        msLoadProjectionStringEPSG(&m->projection,tmpSrs);
328        msLoadProjectionStringEPSG(&myLayer->projection,tmpSrs);
329       
330        char tmpSrss[256];
331        sprintf(tmpSrss,"EPSG:4326 EPSG:900913 EPSG:3857 %s",tmpSrs);
332       
333        msInsertHashTable(&(m->web.metadata), "ows_srs", tmpSrss);
334        msInsertHashTable(&(myLayer->metadata), "ows_srs", tmpSrss);
335       
336#ifdef DEBUGMS
337        fprintf(stderr,"isGeo %b\n\n",OSRIsGeographic(hSRS)==TRUE);
338#endif
339        if(output!=NULL){
340          if(OSRIsGeographic(hSRS)==TRUE)
341            addToMap(output->content,"crs_isGeographic","true");
342          else
343            addToMap(output->content,"crs_isGeographic","false");
344          addToMap(output->content,"crs",tmpSrs);
345        }
346      }
347      else{
348        OSRExportToProj4(hSRS,&proj4Str);
349        if(proj4Str!=NULL && strlen(proj4Str)>0){
350#ifdef DEBUGMS
351          fprintf(stderr,"PROJ (%s)\n",proj4Str);
352#endif
353          msLoadProjectionString(&(m->projection),proj4Str);
354          msLoadProjectionString(&(myLayer->projection),proj4Str);
355          if(output!=NULL){ 
356            if(OSRIsGeographic(hSRS)==TRUE)
357              addToMap(output->content,"crs_isGeographic","true");
358            else
359              addToMap(output->content,"crs_isGeographic","false");
360          }
361          free(proj4Str);
362        }
363        else{
364          msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
365          msLoadProjectionStringEPSG(&myLayer->projection,"EPSG:4326");
366          if(output!=NULL){
367            addToMap(output->content,"crs_isGeographic","true");
368          }
369        }
370        if(output!=NULL){
371          addToMap(output->content,"crs","EPSG:4326");
372          addToMap(output->content,"real_extent","true");
373        }
374        msInsertHashTable(&(m->web.metadata),"ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
375        msInsertHashTable(&(myLayer->metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
376      }
377    }
378  }
379  else{
380    if(output!=NULL){
381      msSrs=getMap(output->content,"msSrs");
382    }
383    if(msSrs!=NULL){
384      msLoadProjectionStringEPSG(&m->projection,msSrs->value);
385      msLoadProjectionStringEPSG(&myLayer->projection,msSrs->value);
386      char tmpSrs[128];
387      sprintf(tmpSrs,"%s EPSG:4326 EPSG:900913 EPSG:3857",msSrs->value);
388      msInsertHashTable(&(m->web.metadata),"ows_srs",tmpSrs);
389      msInsertHashTable(&(myLayer->metadata),"ows_srs",tmpSrs);
390    }else{
391      msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
392      msLoadProjectionStringEPSG(&myLayer->projection,"EPSG:4326");
393      msInsertHashTable(&(m->web.metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
394      msInsertHashTable(&(myLayer->metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
395    }
396    if(output!=NULL){
397      addToMap(output->content,"crs",msSrs->value);
398      addToMap(output->content,"crs_isGeographic","true");
399    }
400  }
401
402  OSRDestroySpatialReference( hSRS );
403}
404
405/**
406 * Set the MAPFILE extent, the the ows_extent for the layer, add wms_extent and
407 * wfs_extent to the output maps and call setMapSize.
408 *
409 * @param output the specific output
410 * @param m the mapObj
411 * @param myLayer the layerObj
412 * @param minX the lower left x coordinate
413 * @param minY the lower left y coordinate
414 * @param maxX the upper right x coordinate
415 * @param maxY the upper right y coordinate
416 * @see setMapSize
417 */
418void setMsExtent(maps* output,mapObj* m,layerObj* myLayer,
419                 double minX,double minY,double maxX,double maxY){
420  msMapSetExtent(m,minX,minY,maxX,maxY);
421#ifdef DEBUGMS
422  fprintf(stderr,"Extent %.15f %.15f %.15f %.15f\n",minX,minY,maxX,maxY);
423#endif
424  char tmpExtent[1024];
425  sprintf(tmpExtent,"%.15f %.15f %.15f %.15f",minX,minY,maxX,maxY);
426#ifdef DEBUGMS
427  fprintf(stderr,"Extent %s\n",tmpExtent);
428#endif
429  msInsertHashTable(&(myLayer->metadata), "ows_extent", tmpExtent);
430 
431  if(output!=NULL){
432    map* test=getMap(output->content,"real_extent");
433    if(test!=NULL){
434      pointObj min, max;
435      projectionObj tempSrs;
436      min.x = m->extent.minx;
437      min.y = m->extent.miny;
438      max.x = m->extent.maxx;
439      max.y = m->extent.maxy;
440      char tmpSrsStr[1024];
441      msInitProjection(&tempSrs);
442      msLoadProjectionStringEPSG(&tempSrs,"EPSG:4326");
443
444      msProjectPoint(&(m->projection),&tempSrs,&min);
445      msProjectPoint(&m->projection,&tempSrs,&max);
446     
447      sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",min.y,min.x,max.y,max.x);
448      map* isGeo=getMap(output->content,"crs_isGeographic");
449#ifdef DEBUGMS
450      fprintf(stderr,"isGeo = %s\n",isGeo->value);
451#endif
452      if(isGeo!=NULL && strcasecmp("true",isGeo->value)==0)
453        sprintf(tmpExtent,"%f,%f,%f,%f", minY,minX, maxY, maxX);
454      addToMap(output->content,"wms_extent",tmpExtent);
455      sprintf(tmpSrsStr,"%.3f,%.3f,%.3f,%.3f",min.x,min.y,max.x,max.y);
456      addToMap(output->content,"wcs_extent",tmpExtent);
457    }else{
458      sprintf(tmpExtent,"%f,%f,%f,%f",minX, minY, maxX, maxY);
459      map* isGeo=getMap(output->content,"crs_isGeographic");
460      if(isGeo!=NULL){
461#ifdef DEBUGMS
462        fprintf(stderr,"isGeo = %s\n",isGeo->value);
463#endif
464        if(isGeo!=NULL && strcasecmp("true",isGeo->value)==0)
465          sprintf(tmpExtent,"%f,%f,%f,%f", minY,minX, maxY, maxX);
466      }
467      addToMap(output->content,"wms_extent",tmpExtent); 
468      sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",minX,minY,maxX,maxY);
469      addToMap(output->content,"wcs_extent",tmpExtent);
470    }
471  }
472
473  setMapSize(output,minX,minY,maxX,maxY);
474}
475
476/**
477 * Try to open a vector output and define the corresponding layer in the MAPFILE
478 *
479 * @param conf the conf maps containing the main.cfg settings
480 * @param output the specific output maps
481 * @param m the mapObj
482 */
483int tryOgr(maps* conf,maps* output,mapObj* m){
484
485  map* tmpMap=getMap(output->content,"storage");
486  char *pszDataSource=tmpMap->value;
487
488  /**
489   * Try to open the DataSource using OGR
490   */
491  OGRRegisterAll();
492  /**
493   * Try to load the file as ZIP
494   */
495
496  OGRDataSourceH poDS1 = NULL;
497  OGRSFDriverH *poDriver1 = NULL;
498  char *dsName=(char*)malloc((8+strlen(pszDataSource)+1)*sizeof(char));
499  char *odsName=zStrdup(pszDataSource);
500  char *sdsName=zStrdup(pszDataSource);
501  char *demo=".data";
502  sdsName[strlen(sdsName)-(strlen(demo)-1)]='d';
503  sdsName[strlen(sdsName)-(strlen(demo)-2)]='i';
504  sdsName[strlen(sdsName)-(strlen(demo)-3)]='r';
505  sdsName[strlen(sdsName)-(strlen(demo)-4)]=0;
506
507  odsName[strlen(odsName)-(strlen(demo)-1)]='z';
508  odsName[strlen(odsName)-(strlen(demo)-2)]='i';
509  odsName[strlen(odsName)-(strlen(demo)-3)]='p';
510  odsName[strlen(odsName)-(strlen(demo)-4)]=0;
511  sprintf(dsName,"/vsizip/%s",odsName);
512
513#ifdef DEBUGMS
514  fprintf(stderr,"Try loading %s, %s, %s\n",dsName,odsName,dsName);
515#endif
516
517  FILE* file = fopen(pszDataSource, "rb");
518  FILE* fileZ = fopen(odsName, "wb");
519  free(odsName);
520  fseek(file, 0, SEEK_END);
521  unsigned long fileLen=ftell(file);
522  fseek(file, 0, SEEK_SET);
523  char *buffer=(char *)malloc(fileLen+1);
524  fread(buffer, fileLen, 1, file);
525  fwrite(buffer,fileLen, 1, fileZ);
526  fclose(file);
527  fclose(fileZ);
528  free(buffer);
529#ifdef DEBUGMS
530  fprintf(stderr,"Try loading %s",dsName);
531#endif
532  poDS1 = OGROpen( dsName, FALSE, poDriver1 );
533  if( poDS1 == NULL ){
534    fprintf(stderr,"Unable to access the DataSource as ZIP File\n");
535    setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
536    //OGR_DS_Destroy(poDS1);
537  }else{
538#ifdef DEBUGMS
539    fprintf(stderr,"The DataSource is a  ZIP File\n");
540#endif
541    char** demo=VSIReadDir(dsName);
542    int i=0;
543    zMkdir(sdsName
544#ifndef WIN32
545                ,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
546#endif
547                );
548    while(demo[i]!=NULL){
549#ifdef DEBUGMS
550      fprintf(stderr,"ZIP File content : %s\n",demo[i]);
551#endif
552      char *tmpDs=(char*)malloc((strlen(dsName)+strlen(demo[i])+2)*sizeof(char));
553      sprintf(tmpDs,"%s/%s",dsName,demo[i]);
554      fprintf(stderr,"read : %s\n",tmpDs);
555     
556      VSILFILE* vsif=VSIFOpenL(tmpDs,"rb");
557#ifdef DEBUGMS
558      fprintf(stderr,"open : %s\n",tmpDs);
559#endif
560      VSIFSeekL(vsif,0,SEEK_END);
561      vsi_l_offset size=VSIFTellL(vsif);
562#ifdef DEBUGMS
563      fprintf(stderr,"size : %d\n",size);
564#endif
565      VSIFSeekL(vsif,0,SEEK_SET);
566      char *vsifcontent=(char*) malloc(((int)size+1)*sizeof(char));
567      VSIFReadL(vsifcontent,1,(size_t)size,vsif);
568      char *fpath=(char*) malloc((strlen(sdsName)+strlen(demo[1])+2)*sizeof(char));
569      sprintf(fpath,"%s/%s",sdsName,demo[i]);
570      int f=zOpen(fpath,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
571      zWrite(f,vsifcontent,(int)size);
572      close(f);
573      chmod(fpath,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH);
574      char* tmpP=strstr(fpath,".shp");
575      if(tmpP==NULL)
576        tmpP=strstr(fpath,".SHP");
577      if(tmpP!=NULL){
578#ifdef DEBUGMS
579        fprintf(stderr,"*** DEBUG %s\n",strstr(tmpP,"."));
580#endif
581        if( strcmp(tmpP,".shp")==0 || strcmp(tmpP,".SHP")==0 ){
582          tmpMap=getMap(output->content,"storage");
583          free(tmpMap->value);
584          tmpMap->value=(char*) malloc((strlen(fpath)+1)*sizeof(char));
585          sprintf(tmpMap->value,"%s",fpath);
586          pszDataSource=tmpMap->value;
587#ifdef DEBUGMS
588          fprintf(stderr,"*** DEBUG %s\n",pszDataSource);
589#endif
590        }
591      }
592      VSIFCloseL(vsif);
593      i++;
594    }
595    OGR_DS_Destroy(poDS1);
596  }
597  free(sdsName);
598  free(dsName);
599 
600  OGRDataSourceH poDS = NULL;
601  OGRSFDriverH *poDriver = NULL;
602  poDS = OGROpen( pszDataSource, FALSE, poDriver );
603  if( poDS == NULL ){
604#ifdef DEBUGMS
605    fprintf(stderr,"Unable to access the DataSource %s\n",pszDataSource);
606#endif
607    setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
608    //OGR_DS_Destroy(poDS);
609    //OGRCleanupAll();
610#ifdef DEBUGMS
611    fprintf(stderr,"Unable to access the DataSource, exit! \n"); 
612#endif
613    return -1;
614  }
615
616  addToMap(output->content,"geodatatype","vector");
617  int iLayer = 0;
618  for( iLayer=0; iLayer < OGR_DS_GetLayerCount(poDS); iLayer++ ){
619    OGRLayerH poLayer = OGR_DS_GetLayer(poDS,iLayer);
620
621    if( poLayer == NULL ){
622#ifdef DEBUGMS
623      fprintf(stderr,"Unable to access the DataSource Layer \n");
624#endif
625      setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
626      return -1;
627    }
628
629    /**
630     * Add a new layer set name, data
631     */
632    if(msGrowMapLayers(m)==NULL){
633      return -1;
634    }
635    if(initLayer((m->layers[m->numlayers]), m) == -1){
636      return -1;
637    }
638
639    layerObj* myLayer=m->layers[m->numlayers];
640#ifdef DEBUGMS
641    dumpMaps(output);
642#endif
643    myLayer->name = zStrdup(output->name);
644    myLayer->tileitem=NULL;
645    myLayer->data = zStrdup(OGR_L_GetName(poLayer));
646    myLayer->connection = zStrdup(pszDataSource);
647    myLayer->index = m->numlayers;
648    myLayer->dump = MS_TRUE;
649    myLayer->status = MS_ON;
650    msConnectLayer(myLayer,MS_OGR,pszDataSource);
651
652    /**
653     * Detect the Geometry Type or use Polygon
654     */
655    if(OGR_L_GetGeomType(poLayer) != wkbUnknown){
656      switch(OGR_L_GetGeomType(poLayer)){
657      case wkbPoint:
658      case wkbMultiPoint:
659      case wkbPoint25D:
660      case wkbMultiPoint25D:
661#ifdef DEBUGMS
662        fprintf(stderr,"POINT DataSource Layer \n");
663#endif
664        myLayer->type = MS_LAYER_POINT;
665        break;
666      case wkbLineString :
667      case wkbMultiLineString :
668      case wkbLineString25D:
669      case wkbMultiLineString25D:
670#ifdef DEBUGMS
671        fprintf(stderr,"LINE DataSource Layer \n");
672#endif
673        myLayer->type = MS_LAYER_LINE;
674        break;
675      case wkbPolygon:
676      case wkbMultiPolygon:
677      case wkbPolygon25D:
678      case wkbMultiPolygon25D:
679#ifdef DEBUGMS
680        fprintf(stderr,"POLYGON DataSource Layer \n");
681#endif
682        myLayer->type = MS_LAYER_POLYGON;
683        break;
684      default:
685        myLayer->type = MS_LAYER_POLYGON;
686        break;
687      }
688    }else
689      myLayer->type = MS_LAYER_POLYGON;
690
691    /**
692     * Detect spatial reference or use WGS84
693     */
694    OGRSpatialReferenceH srs=OGR_L_GetSpatialRef(poLayer);
695    if(srs!=NULL){
696      char *wkt=NULL;
697      OSRExportToWkt(srs,&wkt);
698      setSrsInformations(output,m,myLayer,wkt);
699      free(wkt);
700    }
701    else{
702      addToMap(output->content,"crs","EPSG:4326");
703      addToMap(output->content,"crs_isGeographic","true");
704      msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
705      msInsertHashTable(&(m->web.metadata), "ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
706      msInsertHashTable(&(myLayer->metadata), "ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
707    }
708
709    OGREnvelope oExt;
710    if (OGR_L_GetExtent(poLayer,&oExt, TRUE) == OGRERR_NONE){
711      setMsExtent(output,m,myLayer,oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
712      char extent[1024];
713      memset(&extent,0,1024);
714      sprintf(extent,"%d,%d,%d,%d",oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
715      addToMap(output->content,"boundingbox",extent);
716    }
717 
718    /**
719     * Detect the FID column or use the first attribute field as FID
720     */
721    char *fid=(char*)OGR_L_GetFIDColumn(poLayer);
722    if(strlen(fid)==0){
723      OGRFeatureDefnH def=OGR_L_GetLayerDefn(poLayer);
724      int fIndex=0;
725      for(fIndex=0;fIndex<OGR_FD_GetFieldCount(def);fIndex++){
726        OGRFieldDefnH fdef=OGR_FD_GetFieldDefn(def,fIndex);
727        fid=(char*)OGR_Fld_GetNameRef(fdef);
728        break;
729      }
730    }
731    msInsertHashTable(&(myLayer->metadata), "gml_featureid", fid);
732    msInsertHashTable(&(myLayer->metadata), "gml_include_items", "all");
733    msInsertHashTable(&(myLayer->metadata), "ows_name", output->name);
734    map* tmpMap=getMap(output->content,"title");
735    if(tmpMap!=NULL)
736      msInsertHashTable(&(myLayer->metadata), "ows_title", tmpMap->value);
737    else
738      msInsertHashTable(&(myLayer->metadata), "ows_title", "Default Title");
739   
740    if(msGrowLayerClasses(myLayer) == NULL)
741      return -1;
742    if(initClass((myLayer->CLASS[myLayer->numclasses])) == -1)
743      return -1;
744    if(msGrowClassStyles(myLayer->CLASS[myLayer->numclasses]) == NULL)
745      return -1;
746    if(initStyle(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]) == -1)
747      return -1;
748    /**
749     * Apply msStyle else fallback to the default style
750     */
751    tmpMap=getMap(output->content,"msStyle");
752    if(tmpMap!=NULL)
753      msUpdateStyleFromString(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles],tmpMap->value,0);
754    else{
755      /**
756       * Set style
757       */
758      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.red=125;
759      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.green=125;
760      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.blue=255;
761      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.red=80;
762      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.green=80;
763      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.blue=80;
764     
765      /**
766       * Set specific style depending on type
767       */
768      if(myLayer->type == MS_LAYER_POLYGON)
769        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
770      if(myLayer->type == MS_LAYER_LINE){
771        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
772        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinewidth=1.5;
773      }
774      if(myLayer->type == MS_LAYER_POINT){
775        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->symbol=1;
776        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->size=15;
777      }
778     
779    }
780    myLayer->CLASS[myLayer->numclasses]->numstyles++;
781    myLayer->numclasses++;
782   
783    m->layerorder[m->numlayers] = m->numlayers;
784    m->numlayers++;
785
786  }
787
788  OGR_DS_Destroy(poDS);
789  //OGRCleanupAll();
790
791  return 1;
792}
793
794/**
795 * Try to open a raster output and define the corresponding layer in the MAPFILE
796 *
797 * @param conf the conf maps containing the main.cfg settings
798 * @param output the specific output maps
799 * @param m the mapObj
800 */
801int tryGdal(maps* conf,maps* output,mapObj* m){
802  map* tmpMap=getMap(output->content,"storage");
803  char *pszFilename=tmpMap->value;
804  GDALDatasetH hDataset;
805  GDALRasterBandH hBand;
806  double adfGeoTransform[6];
807  int i, iBand;
808 
809  /**
810   * Try to open the DataSource using GDAL
811   */
812  GDALAllRegister();
813  hDataset = GDALOpen( pszFilename, GA_ReadOnly );
814  if( hDataset == NULL ){
815#ifdef DEBUGMS
816    fprintf(stderr,"Unable to access the DataSource %s \n",pszFilename);
817#endif
818    addToMap(output->content,"geodatatype","other");
819    setMapInMaps(conf,"lenv","message","gdalinfo failed - unable to open");
820    GDALDestroyDriverManager();
821    return -1;
822  }
823#ifdef DEBUGMS
824  fprintf(stderr,"Accessing the DataSource %s %d\n",pszFilename,__LINE__);
825#endif
826
827  addToMap(output->content,"geodatatype","raster");
828  /**
829   * Add a new layer set name, data
830   */
831  if(msGrowMapLayers(m)==NULL){
832    return -1;
833  }
834  if(initLayer((m->layers[m->numlayers]), m) == -1){
835    return -1;
836  }
837  m->layers[m->numlayers]->index=m->numlayers;
838
839  layerObj* myLayer=m->layers[m->numlayers];
840  myLayer->name = zStrdup(output->name);
841  myLayer->tileitem=NULL;
842  myLayer->data = zStrdup(pszFilename);
843  myLayer->index = m->numlayers;
844  myLayer->dump = MS_TRUE;
845  myLayer->status = MS_ON;
846  myLayer->type = MS_LAYER_RASTER;
847
848  char *title=output->name;
849  tmpMap=getMap(output->content,"title");
850  if(tmpMap!=NULL)
851    title=tmpMap->value;
852  char *abstract=output->name;
853  tmpMap=getMap(output->content,"abstract");
854  if(tmpMap!=NULL)
855    abstract=tmpMap->value;
856
857  msInsertHashTable(&(myLayer->metadata), "ows_label", title);
858  msInsertHashTable(&(myLayer->metadata), "ows_title", title);
859  msInsertHashTable(&(myLayer->metadata), "ows_abstract", abstract);
860  msInsertHashTable(&(myLayer->metadata), "ows_rangeset_name", output->name);
861  msInsertHashTable(&(myLayer->metadata), "ows_rangeset_label", title);
862
863  /**
864   * Set Map Size to the raster size
865   */
866  m->width=GDALGetRasterXSize( hDataset );
867  m->height=GDALGetRasterYSize( hDataset );
868 
869  /**
870   * Set projection using Authority Code and Name if available or fallback to
871   * proj4 definition if available or fallback to default EPSG:4326
872   */
873  const char *tRef=GDALGetProjectionRef( hDataset );
874  if( tRef != NULL && strlen(tRef)>0 ){
875    char *pszProjection;
876    pszProjection = (char *) GDALGetProjectionRef( hDataset );
877#ifdef DEBUGMS
878    fprintf(stderr,"Accessing the DataSource %s\n",GDALGetProjectionRef( hDataset ));
879#endif
880    setSrsInformations(output,m,myLayer,pszProjection);
881  }else{
882    fprintf(stderr,"NO SRS FOUND ! %s\n",GDALGetProjectionRef( hDataset ));   
883  }
884
885  /**
886   * Set extent
887   */
888  if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ){
889    if( adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0 ){
890
891      double minX = adfGeoTransform[0]
892        + adfGeoTransform[2] * GDALGetRasterYSize(hDataset);
893      double minY = adfGeoTransform[3]
894        + adfGeoTransform[5] * GDALGetRasterYSize(hDataset);
895
896      double maxX = adfGeoTransform[0]
897        + adfGeoTransform[1] * GDALGetRasterXSize(hDataset);
898      double maxY = adfGeoTransform[3]
899        + adfGeoTransform[4] * GDALGetRasterXSize(hDataset);
900
901      setMsExtent(output,m,myLayer,minX,minY,maxX,maxY);
902      char extent[1024];
903      memset(&extent,0,1024);
904      sprintf(extent,"%d,%d,%d,%d",minX,minY,maxX,maxY);
905      addToMap(output->content,"boundingbox",extent);
906    }
907  }
908
909  /**
910   * Extract information about available bands to set the bandcount and the
911   * processing directive
912   */
913  char nBands[3];
914  memset(&nBands,0,3);
915  int nBandsI=GDALGetRasterCount( hDataset );
916  if(nBandsI<100){
917    sprintf(nBands,"%d",GDALGetRasterCount( hDataset ));
918    msInsertHashTable(&(myLayer->metadata), "ows_bandcount", nBands);
919  }
920  if(nBandsI>=3)
921    msLayerAddProcessing(myLayer,"BANDS=1,2,3");
922  else if(nBandsI>=2)
923    msLayerAddProcessing(myLayer,"BANDS=1,2");
924  else
925    msLayerAddProcessing(myLayer,"BANDS=1");
926
927  /**
928   * Name available Bands
929   */
930  char lBands[7];
931  memset(&nBands,0,7);
932  char *nameBands=NULL;
933  for( iBand = 0; iBand < nBandsI; iBand++ ){
934    sprintf(lBands,"Band%d",iBand+1);   
935    if(nameBands==NULL){
936      nameBands=(char*)malloc((strlen(lBands)+1)*sizeof(char));
937      sprintf(nameBands,"%s",lBands);
938    }else{
939      if(iBand<4){
940        char *tmpS=zStrdup(nameBands);
941        nameBands=(char*)realloc(nameBands,(strlen(tmpS)+strlen(lBands)+2)*sizeof(char));
942        sprintf(nameBands,"%s %s",tmpS,lBands);
943        free(tmpS);
944      }
945    }
946  }
947  if(nameBands!=NULL){
948    msInsertHashTable(&(myLayer->metadata), "ows_bandnames", nameBands);
949    free(nameBands);
950  }
951
952  /**
953   * Loops over metadata information to setup specific information
954   */
955  for( iBand = 0; iBand < nBandsI; iBand++ ){
956    //int         bGotNodata;//, bSuccess;
957    double      adfCMinMax[2]/*, dfNoData*/;
958    //int         nBlockXSize, nBlockYSize, nMaskFlags;
959    //double      /*dfMean, dfStdDev*/;
960    hBand = GDALGetRasterBand( hDataset, iBand+1 );
961
962    CPLErrorReset();
963    GDALComputeRasterMinMax( hBand, FALSE, adfCMinMax );
964    char tmpN[21];
965    sprintf(tmpN,"Band%d",iBand+1);
966    if (CPLGetLastErrorType() == CE_None){
967      char tmpMm[100];
968      sprintf(tmpMm,"%.3f %.3f",adfCMinMax[0],adfCMinMax[1]);
969      char tmpI[31];     
970      sprintf(tmpI,"%s_interval",tmpN);
971      msInsertHashTable(&(myLayer->metadata), tmpI, tmpMm);
972
973      map* test=getMap(output->content,"msClassify");
974      if(test!=NULL && strncasecmp(test->value,"true",4)==0){
975        /**
976         * Classify one band raster pixel value using regular interval
977         */
978        int _tmpColors[10][3]={
979          {102,153,204},
980          {51,102,153},
981          {102,102,204},
982          {51,204,0},
983          {153,255,102},
984          {204,255,102},
985          {102,204,153},
986          {255,69,64},
987          {255,192,115},
988          {255,201,115}
989        };
990         
991        if(nBandsI==1){
992          double delta=adfCMinMax[1]-adfCMinMax[0];
993          double interval=delta/10;
994          double cstep=adfCMinMax[0];
995          for(i=0;i<10;i++){
996            /**
997             * Create a new class
998             */
999            if(msGrowLayerClasses(myLayer) == NULL)
1000              return -1;
1001            if(initClass((myLayer->CLASS[myLayer->numclasses])) == -1)
1002              return -1;
1003            if(msGrowClassStyles(myLayer->CLASS[myLayer->numclasses]) == NULL)
1004              return -1;
1005            if(initStyle(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]) == -1)
1006              return -1;
1007           
1008            /**
1009             * Set class name
1010             */
1011            char className[7];
1012            sprintf(className,"class%d",i);
1013            myLayer->CLASS[myLayer->numclasses]->name=zStrdup(className);
1014           
1015            /**
1016             * Set expression
1017             */
1018            char expression[1024];
1019            if(i+1<10)
1020              sprintf(expression,"([pixel]>=%.3f AND [pixel]<%.3f)",cstep,cstep+interval);
1021            else
1022              sprintf(expression,"([pixel]>=%.3f AND [pixel]<=%.3f)",cstep,cstep+interval);
1023            msLoadExpressionString(&myLayer->CLASS[myLayer->numclasses]->expression,expression);
1024           
1025            /**
1026             * Set color
1027             */
1028            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.red=_tmpColors[i][0];
1029            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.green=_tmpColors[i][1];
1030            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.blue=_tmpColors[i][2];
1031            cstep+=interval;
1032            myLayer->CLASS[myLayer->numclasses]->numstyles++;
1033            myLayer->numclasses++;
1034           
1035          }
1036         
1037          char tmpMm[100];
1038          sprintf(tmpMm,"%.3f %.3f",adfCMinMax[0],adfCMinMax[1]);
1039         
1040        }
1041      }
1042      else{
1043        if(nBandsI==1){
1044          myLayer->offsite.red=0;
1045          myLayer->offsite.green=0;
1046          myLayer->offsite.blue=0;
1047        }
1048        msLayerAddProcessing(myLayer,"RESAMPLE=BILINEAR");
1049      }
1050    }
1051    if( strlen(GDALGetRasterUnitType(hBand)) > 0 ){
1052      char tmpU[31];
1053      sprintf(tmpU,"%s_band_uom",tmpN);
1054      msInsertHashTable(&(myLayer->metadata), tmpU, GDALGetRasterUnitType(hBand));
1055    }
1056
1057  }
1058
1059  m->layerorder[m->numlayers] = m->numlayers;
1060  m->numlayers++;
1061  GDALClose( hDataset );
1062  GDALDestroyDriverManager();
1063  CPLCleanupTLS();
1064  return 1;
1065}
1066
1067/**
1068 * Create a MapFile for WMS, WFS or WCS Service output
1069 *
1070 * @param conf the conf maps containing the main.cfg settings
1071 * @param outputs a specific output maps
1072 */
1073void outputMapfile(maps* conf,maps* outputs){
1074  /**
1075   * First store the value on disk
1076   */
1077  map* mime=getMap(outputs->content,"mimeType");
1078  char *ext="data";
1079  if(mime!=NULL)
1080    if(strncasecmp(mime->value,"application/json",16)==0)
1081      ext="json";
1082
1083  map* tmpMap=getMapFromMaps(conf,"main","dataPath");
1084  map* sidMap=getMapFromMaps(conf,"lenv","usid");
1085  char *pszDataSource=(char*)malloc((strlen(tmpMap->value)+strlen(sidMap->value)+strlen(outputs->name)+17)*sizeof(char));
1086  sprintf(pszDataSource,"%s/ZOO_DATA_%s_%s.%s",tmpMap->value,outputs->name,sidMap->value,ext);
1087  int f=zOpen(pszDataSource,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
1088  map *gfile=getMap(outputs->content,"generated_file");
1089  if(gfile!=NULL){
1090    readGeneratedFile(conf,outputs->content,gfile->value);         
1091  }
1092  map* sizeMap=getMap(outputs->content,"size");
1093  map* vData=getMap(outputs->content,"value");
1094  if(sizeMap!=NULL){
1095    zWrite(f,vData->value,atoi(sizeMap->value)*sizeof(char));
1096  }
1097  else{
1098    zWrite(f,vData->value,(strlen(vData->value)+1)*sizeof(char));
1099  }
1100  close(f);
1101  addToMap(outputs->content,"storage",pszDataSource);
1102  free(pszDataSource);
1103
1104  /*
1105   * Create an empty map, set name, default size and extent
1106   */
1107  mapObj *myMap=msNewMapObj();
1108  free(myMap->name);
1109  myMap->name=zStrdup("ZOO-Project_WXS_Server");
1110  msMapSetSize(myMap,2048,2048);
1111  msMapSetExtent(myMap,-1,-1,1,1);
1112 
1113  /*
1114   * Set imagepath and imageurl using tmpPath and tmpUrl from main.cfg
1115   */
1116  map *tmp1=getMapFromMaps(conf,"main","tmpPath");
1117  myMap->web.imagepath=zStrdup(tmp1->value);
1118  tmp1=getMapFromMaps(conf,"main","tmpUrl");
1119  myMap->web.imageurl=zStrdup(tmp1->value);
1120 
1121  /*
1122   * Define supported output formats
1123   */
1124  outputFormatObj *o1=msCreateDefaultOutputFormat(NULL,"AGG/PNG","png");
1125  o1->imagemode=MS_IMAGEMODE_RGBA;
1126  o1->transparent=MS_TRUE;
1127  o1->inmapfile=MS_TRUE;
1128  msAppendOutputFormat(myMap,msCloneOutputFormat(o1));
1129  msFreeOutputFormat(o1);
1130
1131#ifdef USE_KML
1132  outputFormatObj *o2=msCreateDefaultOutputFormat(NULL,"KML","kml");
1133  if(!o2){
1134    perror("Unable to initialize KML driver");
1135    fprintf(stderr,"Unable to initialize KML driver !\n");
1136  }else{
1137    o2->inmapfile=MS_TRUE; 
1138    msAppendOutputFormat(myMap,msCloneOutputFormat(o2));
1139    msFreeOutputFormat(o2);
1140  }
1141#endif
1142
1143  outputFormatObj *o3=msCreateDefaultOutputFormat(NULL,"GDAL/GTiff","tiff");
1144  if(!o3)
1145    fprintf(stderr,"Unable to initialize GDAL driver !\n");
1146  else{
1147    o3->imagemode=MS_IMAGEMODE_BYTE;
1148    o3->inmapfile=MS_TRUE; 
1149    msAppendOutputFormat(myMap,msCloneOutputFormat(o3));
1150    msFreeOutputFormat(o3);
1151  }
1152
1153  outputFormatObj *o4=msCreateDefaultOutputFormat(NULL,"GDAL/AAIGRID","grd");
1154  if(!o4)
1155    fprintf(stderr,"Unable to initialize GDAL driver !\n");
1156  else{
1157    o4->imagemode=MS_IMAGEMODE_INT16;
1158    o4->inmapfile=MS_TRUE; 
1159    msAppendOutputFormat(myMap,msCloneOutputFormat(o4));
1160    msFreeOutputFormat(o4);
1161  }
1162
1163#ifdef USE_CAIRO
1164  outputFormatObj *o5=msCreateDefaultOutputFormat(NULL,"CAIRO/PNG","cairopng");
1165  if(!o5)
1166    fprintf(stderr,"Unable to initialize CAIRO driver !\n");
1167  else{
1168    o5->imagemode=MS_IMAGEMODE_RGBA;
1169    o5->transparent=MS_TRUE;
1170    o5->inmapfile=MS_TRUE;
1171    msAppendOutputFormat(myMap,msCloneOutputFormat(o5));
1172    msFreeOutputFormat(o5);
1173  }
1174#endif
1175
1176  /*
1177   * Set default projection to EPSG:4326
1178   */
1179  msLoadProjectionStringEPSG(&myMap->projection,"EPSG:4326");
1180  myMap->transparent=1;
1181
1182  /**
1183   * Set metadata extracted from main.cfg file maps
1184   */
1185  maps* cursor=conf;
1186  map* correspondance=getCorrespondance();
1187  while(cursor!=NULL){
1188    map* _cursor=cursor->content;
1189    map* vMap;
1190    while(_cursor!=NULL){
1191      if((vMap=getMap(correspondance,_cursor->name))!=NULL){
1192        if (msInsertHashTable(&(myMap->web.metadata), vMap->value, _cursor->value) == NULL){
1193#ifdef DEBUGMS
1194          fprintf(stderr,"Unable to add metadata");
1195#endif
1196          return;
1197        }
1198      }
1199      _cursor=_cursor->next;
1200    }
1201    cursor=cursor->next;
1202  }
1203  freeMap(&correspondance);
1204  free(correspondance);
1205
1206  /*
1207   * Set mapserver PROJ_LIB/GDAL_DATA or any other config parameter from
1208   * the main.cfg [mapserver] section if any
1209   */
1210  maps *tmp3=getMaps(conf,"mapserver");
1211  if(tmp3!=NULL){
1212    map* tmp4=tmp3->content;
1213    while(tmp4!=NULL){
1214      msSetConfigOption(myMap,tmp4->name,tmp4->value);
1215      tmp4=tmp4->next;
1216    }
1217  }
1218
1219  /**
1220   * Set a ows_rootlayer_title, 
1221   */
1222  if (msInsertHashTable(&(myMap->web.metadata), "ows_rootlayer_name", "ZOO_Project_Layer") == NULL){
1223#ifdef DEBUGMS
1224    fprintf(stderr,"Unable to add metadata");
1225#endif
1226    return;
1227  }
1228  if (msInsertHashTable(&(myMap->web.metadata), "ows_rootlayer_title", "ZOO_Project_Layer") == NULL){
1229#ifdef DEBUGMS
1230    fprintf(stderr,"Unable to add metadata");
1231#endif
1232    return;
1233  }
1234
1235  /**
1236   * Enable all the WXS requests using ows_enable_request
1237   * see http://mapserver.org/trunk/development/rfc/ms-rfc-67.html
1238   */
1239  if (msInsertHashTable(&(myMap->web.metadata), "ows_enable_request", "*") == NULL){
1240#ifdef DEBUGMS
1241    fprintf(stderr,"Unable to add metadata");
1242#endif
1243    return;
1244  }
1245  msInsertHashTable(&(myMap->web.metadata), "ows_srs", "EPSG:4326");
1246
1247  if(tryOgr(conf,outputs,myMap)<0)
1248    if(tryGdal(conf,outputs,myMap)<0)
1249      return ;
1250
1251  tmp1=getMapFromMaps(conf,"main","dataPath");
1252  char *tmpPath=(char*)malloc((13+strlen(tmp1->value))*sizeof(char));
1253  sprintf(tmpPath,"%s/symbols.sym",tmp1->value);
1254  msInitSymbolSet(&myMap->symbolset);
1255  myMap->symbolset.filename=zStrdup(tmpPath);
1256  free(tmpPath);
1257
1258  map* sid=getMapFromMaps(conf,"lenv","usid");
1259  char *mapPath=
1260    (char*)malloc((7+strlen(sid->value)+strlen(outputs->name)+strlen(tmp1->value))*sizeof(char));
1261  sprintf(mapPath,"%s/%s_%s.map",tmp1->value,outputs->name,sid->value);
1262  msSaveMap(myMap,mapPath);
1263  free(mapPath);
1264  msGDALCleanup();
1265  msFreeMap(myMap);
1266}
1267
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