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

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

Fix various memory leaks and enhance the callback support. Add the prohibited keyword to the callback section to avoid calling callback for such services.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 38.3 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,"datatype");
197 
198  sprintf(webService_url,
199          "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
200          msUrl->value,
201          dataPath->value,
202          tmpI->name,
203          sid->value,
204          options[proto][2],
205          options[proto][0],
206          protoVersion,
207          layers,
208          rformat->value,
209          extent->value,
210          crs->value
211          );
212  if(proto>0){
213    addToMap(tmpI->content,"Reference",webService_url);
214    proto=0;
215    rformat=createMap("mimeType","image/png");
216  }
217  else{
218    if(datatype!=NULL && strncasecmp(datatype->value,"raster",6)==0){
219      proto=2;
220      rformat=createMap("mimeType","image/tiff");
221    }
222    else{
223      proto=1;
224      rformat=createMap("mimeType","text/xml");
225    }
226  }
227  if(datatype!=NULL && strncasecmp(datatype->value,"raster",6)==0){
228    addToMap(tmpI->content,"ref_wcs_link",webService_url);
229  }
230  else{
231    addToMap(tmpI->content,"ref_wfs_link",webService_url);
232  }
233  protoVersion=options[proto][1];
234  extent=getMap(tmpI->content,options[proto][4]);
235  memset(webService_url,0,strlen(webService_url));
236  sprintf(webService_url,
237          "%s?map=%s/%s_%s.map&request=%s&service=%s&version=%s&%s&width=%s&height=%s&format=%s&bbox=%s&crs=%s",
238          msUrl->value,
239          dataPath->value,
240          tmpI->name,
241          sid->value,
242          options[proto][2],
243          options[proto][0],
244          protoVersion,
245          layers,
246          width->value,
247          height->value,
248          rformat->value,
249          extent->value,
250          crs->value
251          );
252  if(proto>0){
253    addToMap(tmpI->content,"Reference",webService_url);
254  }
255  addToMap(tmpI->content,"ref_wms_link",webService_url);
256  if(hasCRS==0){
257    freeMap(&crs);
258    free(crs);
259  }
260  free(webService_url);
261}
262
263/**
264 * Set projection for a layer in a MAPFILE using Authority Code and Name if
265 * available or fallback to proj4 definition if available or fallback to
266 * default EPSG:4326
267 *
268 * @param output the output maps
269 * @param m the opened mapObj
270 * @param myLayer the layerObj
271 * @param pszProjection a char* containing the SRS definition in WKT format
272 */
273void setSrsInformations(maps* output,mapObj* m,layerObj* myLayer,
274                        char* pszProjection){
275  OGRSpatialReferenceH  hSRS;
276  map* msSrs=NULL;
277  hSRS = OSRNewSpatialReference(NULL);
278  if( pszProjection!=NULL && strlen(pszProjection)>1){
279    if(OSRImportFromWkt( hSRS, &pszProjection ) == CE_None ){
280      char *proj4Str=NULL;
281      if(OSRGetAuthorityName(hSRS,NULL)!=NULL && 
282         OSRGetAuthorityCode(hSRS,NULL)!=NULL){
283        char tmpSrs[20];
284        sprintf(tmpSrs,"%s:%s",
285                OSRGetAuthorityName(hSRS,NULL),OSRGetAuthorityCode(hSRS,NULL));
286        msLoadProjectionStringEPSG(&m->projection,tmpSrs);
287        msLoadProjectionStringEPSG(&myLayer->projection,tmpSrs);
288       
289        char tmpSrss[256];
290        sprintf(tmpSrss,"EPSG:4326 EPSG:900913 EPSG:3857 %s",tmpSrs);
291       
292        msInsertHashTable(&(m->web.metadata), "ows_srs", tmpSrss);
293        msInsertHashTable(&(myLayer->metadata), "ows_srs", tmpSrss);
294       
295#ifdef DEBUGMS
296        fprintf(stderr,"isGeo %b\n\n",OSRIsGeographic(hSRS)==TRUE);
297#endif
298        if(output!=NULL){
299          if(OSRIsGeographic(hSRS)==TRUE)
300            addToMap(output->content,"crs_isGeographic","true");
301          else
302            addToMap(output->content,"crs_isGeographic","false");
303          addToMap(output->content,"crs",tmpSrs);
304        }
305      }
306      else{
307        OSRExportToProj4(hSRS,&proj4Str);
308        if(proj4Str!=NULL && strlen(proj4Str)>0){
309#ifdef DEBUGMS
310          fprintf(stderr,"PROJ (%s)\n",proj4Str);
311#endif
312          msLoadProjectionString(&(m->projection),proj4Str);
313          msLoadProjectionString(&(myLayer->projection),proj4Str);
314          if(output!=NULL){ 
315            if(OSRIsGeographic(hSRS)==TRUE)
316              addToMap(output->content,"crs_isGeographic","true");
317            else
318              addToMap(output->content,"crs_isGeographic","false");
319          }
320          free(proj4Str);
321        }
322        else{
323          msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
324          msLoadProjectionStringEPSG(&myLayer->projection,"EPSG:4326");
325          if(output!=NULL){
326            addToMap(output->content,"crs_isGeographic","true");
327          }
328        }
329        if(output!=NULL){
330          addToMap(output->content,"crs","EPSG:4326");
331          addToMap(output->content,"real_extent","true");
332        }
333        msInsertHashTable(&(m->web.metadata),"ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
334        msInsertHashTable(&(myLayer->metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
335      }
336    }
337  }
338  else{
339    if(output!=NULL){
340      msSrs=getMap(output->content,"msSrs");
341    }
342    if(msSrs!=NULL){
343      msLoadProjectionStringEPSG(&m->projection,msSrs->value);
344      msLoadProjectionStringEPSG(&myLayer->projection,msSrs->value);
345      char tmpSrs[128];
346      sprintf(tmpSrs,"%s EPSG:4326 EPSG:900913 EPSG:3857",msSrs->value);
347      msInsertHashTable(&(m->web.metadata),"ows_srs",tmpSrs);
348      msInsertHashTable(&(myLayer->metadata),"ows_srs",tmpSrs);
349    }else{
350      msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
351      msLoadProjectionStringEPSG(&myLayer->projection,"EPSG:4326");
352      msInsertHashTable(&(m->web.metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
353      msInsertHashTable(&(myLayer->metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
354    }
355    if(output!=NULL){
356      addToMap(output->content,"crs",msSrs->value);
357      addToMap(output->content,"crs_isGeographic","true");
358    }
359  }
360
361  OSRDestroySpatialReference( hSRS );
362}
363
364/**
365 * Set the MAPFILE extent, the the ows_extent for the layer, add wms_extent and
366 * wfs_extent to the output maps and call setMapSize.
367 *
368 * @param output the specific output
369 * @param m the mapObj
370 * @param myLayer the layerObj
371 * @param minX the lower left x coordinate
372 * @param minY the lower left y coordinate
373 * @param maxX the upper right x coordinate
374 * @param maxY the upper right y coordinate
375 * @see setMapSize
376 */
377void setMsExtent(maps* output,mapObj* m,layerObj* myLayer,
378                 double minX,double minY,double maxX,double maxY){
379  msMapSetExtent(m,minX,minY,maxX,maxY);
380#ifdef DEBUGMS
381  fprintf(stderr,"Extent %.15f %.15f %.15f %.15f\n",minX,minY,maxX,maxY);
382#endif
383  char tmpExtent[1024];
384  sprintf(tmpExtent,"%.15f %.15f %.15f %.15f",minX,minY,maxX,maxY);
385#ifdef DEBUGMS
386  fprintf(stderr,"Extent %s\n",tmpExtent);
387#endif
388  msInsertHashTable(&(myLayer->metadata), "ows_extent", tmpExtent);
389 
390  if(output!=NULL){
391    map* test=getMap(output->content,"real_extent");
392    if(test!=NULL){
393      pointObj min, max;
394      projectionObj tempSrs;
395      min.x = m->extent.minx;
396      min.y = m->extent.miny;
397      max.x = m->extent.maxx;
398      max.y = m->extent.maxy;
399      char tmpSrsStr[1024];
400      msInitProjection(&tempSrs);
401      msLoadProjectionStringEPSG(&tempSrs,"EPSG:4326");
402
403      msProjectPoint(&(m->projection),&tempSrs,&min);
404      msProjectPoint(&m->projection,&tempSrs,&max);
405     
406      sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",min.y,min.x,max.y,max.x);
407      map* isGeo=getMap(output->content,"crs_isGeographic");
408#ifdef DEBUGMS
409      fprintf(stderr,"isGeo = %s\n",isGeo->value);
410#endif
411      if(isGeo!=NULL && strcasecmp("true",isGeo->value)==0)
412        sprintf(tmpExtent,"%f,%f,%f,%f", minY,minX, maxY, maxX);
413      addToMap(output->content,"wms_extent",tmpExtent);
414      sprintf(tmpSrsStr,"%.3f,%.3f,%.3f,%.3f",min.x,min.y,max.x,max.y);
415      addToMap(output->content,"wcs_extent",tmpExtent);
416    }else{
417      sprintf(tmpExtent,"%f,%f,%f,%f",minX, minY, maxX, maxY);
418      map* isGeo=getMap(output->content,"crs_isGeographic");
419      if(isGeo!=NULL){
420#ifdef DEBUGMS
421        fprintf(stderr,"isGeo = %s\n",isGeo->value);
422#endif
423        if(isGeo!=NULL && strcasecmp("true",isGeo->value)==0)
424          sprintf(tmpExtent,"%f,%f,%f,%f", minY,minX, maxY, maxX);
425      }
426      addToMap(output->content,"wms_extent",tmpExtent); 
427      sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",minX,minY,maxX,maxY);
428      addToMap(output->content,"wcs_extent",tmpExtent);
429    }
430  }
431
432  setMapSize(output,minX,minY,maxX,maxY);
433}
434
435/**
436 * Try to open a vector output and define the corresponding layer in the MAPFILE
437 *
438 * @param conf the conf maps containing the main.cfg settings
439 * @param output the specific output maps
440 * @param m the mapObj
441 */
442int tryOgr(maps* conf,maps* output,mapObj* m){
443
444  map* tmpMap=getMap(output->content,"storage");
445  char *pszDataSource=tmpMap->value;
446
447  /**
448   * Try to open the DataSource using OGR
449   */
450  OGRRegisterAll();
451  /**
452   * Try to load the file as ZIP
453   */
454
455  OGRDataSourceH poDS1 = NULL;
456  OGRSFDriverH *poDriver1 = NULL;
457  char *dsName=(char*)malloc((8+strlen(pszDataSource)+1)*sizeof(char));
458  char *odsName=zStrdup(pszDataSource);
459  char *sdsName=zStrdup(pszDataSource);
460  char *demo=".data";
461  sdsName[strlen(sdsName)-(strlen(demo)-1)]='d';
462  sdsName[strlen(sdsName)-(strlen(demo)-2)]='i';
463  sdsName[strlen(sdsName)-(strlen(demo)-3)]='r';
464  sdsName[strlen(sdsName)-(strlen(demo)-4)]=0;
465
466  odsName[strlen(odsName)-(strlen(demo)-1)]='z';
467  odsName[strlen(odsName)-(strlen(demo)-2)]='i';
468  odsName[strlen(odsName)-(strlen(demo)-3)]='p';
469  odsName[strlen(odsName)-(strlen(demo)-4)]=0;
470  sprintf(dsName,"/vsizip/%s",odsName);
471
472#ifdef DEBUGMS
473  fprintf(stderr,"Try loading %s, %s, %s\n",dsName,odsName,dsName);
474#endif
475
476  FILE* file = fopen(pszDataSource, "rb");
477  FILE* fileZ = fopen(odsName, "wb");
478  free(odsName);
479  fseek(file, 0, SEEK_END);
480  unsigned long fileLen=ftell(file);
481  fseek(file, 0, SEEK_SET);
482  char *buffer=(char *)malloc(fileLen+1);
483  fread(buffer, fileLen, 1, file);
484  fwrite(buffer,fileLen, 1, fileZ);
485  fclose(file);
486  fclose(fileZ);
487  free(buffer);
488#ifdef DEBUGMS
489  fprintf(stderr,"Try loading %s",dsName);
490#endif
491  poDS1 = OGROpen( dsName, FALSE, poDriver1 );
492  if( poDS1 == NULL ){
493    fprintf(stderr,"Unable to access the DataSource as ZIP File\n");
494    setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
495    //OGR_DS_Destroy(poDS1);
496  }else{
497#ifdef DEBUGMS
498    fprintf(stderr,"The DataSource is a  ZIP File\n");
499#endif
500    char** demo=VSIReadDir(dsName);
501    int i=0;
502    zMkdir(sdsName
503#ifndef WIN32
504                ,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
505#endif
506                );
507    while(demo[i]!=NULL){
508#ifdef DEBUGMS
509      fprintf(stderr,"ZIP File content : %s\n",demo[i]);
510#endif
511      char *tmpDs=(char*)malloc((strlen(dsName)+strlen(demo[i])+2)*sizeof(char));
512      sprintf(tmpDs,"%s/%s",dsName,demo[i]);
513      fprintf(stderr,"read : %s\n",tmpDs);
514     
515      VSILFILE* vsif=VSIFOpenL(tmpDs,"rb");
516#ifdef DEBUGMS
517      fprintf(stderr,"open : %s\n",tmpDs);
518#endif
519      VSIFSeekL(vsif,0,SEEK_END);
520      vsi_l_offset size=VSIFTellL(vsif);
521#ifdef DEBUGMS
522      fprintf(stderr,"size : %d\n",size);
523#endif
524      VSIFSeekL(vsif,0,SEEK_SET);
525      char *vsifcontent=(char*) malloc(((int)size+1)*sizeof(char));
526      VSIFReadL(vsifcontent,1,(size_t)size,vsif);
527      char *fpath=(char*) malloc((strlen(sdsName)+strlen(demo[1])+2)*sizeof(char));
528      sprintf(fpath,"%s/%s",sdsName,demo[i]);
529      int f=zOpen(fpath,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
530      zWrite(f,vsifcontent,(int)size);
531      close(f);
532      chmod(fpath,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH);
533      char* tmpP=strstr(fpath,".shp");
534      if(tmpP==NULL)
535        tmpP=strstr(fpath,".SHP");
536      if(tmpP!=NULL){
537#ifdef DEBUGMS
538        fprintf(stderr,"*** DEBUG %s\n",strstr(tmpP,"."));
539#endif
540        if( strcmp(tmpP,".shp")==0 || strcmp(tmpP,".SHP")==0 ){
541          tmpMap=getMap(output->content,"storage");
542          free(tmpMap->value);
543          tmpMap->value=(char*) malloc((strlen(fpath)+1)*sizeof(char));
544          sprintf(tmpMap->value,"%s",fpath);
545          pszDataSource=tmpMap->value;
546#ifdef DEBUGMS
547          fprintf(stderr,"*** DEBUG %s\n",pszDataSource);
548#endif
549        }
550      }
551      VSIFCloseL(vsif);
552      i++;
553    }
554  }
555  free(sdsName);
556  free(dsName);
557 
558  OGRDataSourceH poDS = NULL;
559  OGRSFDriverH *poDriver = NULL;
560  poDS = OGROpen( pszDataSource, FALSE, poDriver );
561  if( poDS == NULL ){
562#ifdef DEBUGMS
563    fprintf(stderr,"Unable to access the DataSource %s\n",pszDataSource);
564#endif
565    setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
566    //OGR_DS_Destroy(poDS);
567    //OGRCleanupAll();
568#ifdef DEBUGMS
569    fprintf(stderr,"Unable to access the DataSource, exit! \n"); 
570#endif
571    return -1;
572  }
573
574  addToMap(output->content,"datatype","vector");
575  int iLayer = 0;
576  for( iLayer=0; iLayer < OGR_DS_GetLayerCount(poDS); iLayer++ ){
577    OGRLayerH poLayer = OGR_DS_GetLayer(poDS,iLayer);
578
579    if( poLayer == NULL ){
580#ifdef DEBUGMS
581      fprintf(stderr,"Unable to access the DataSource Layer \n");
582#endif
583      setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
584      return -1;
585    }
586
587    /**
588     * Add a new layer set name, data
589     */
590    if(msGrowMapLayers(m)==NULL){
591      return -1;
592    }
593    if(initLayer((m->layers[m->numlayers]), m) == -1){
594      return -1;
595    }
596
597    layerObj* myLayer=m->layers[m->numlayers];
598#ifdef DEBUGMS
599    dumpMaps(output);
600#endif
601    myLayer->name = zStrdup(output->name);
602    myLayer->tileitem=NULL;
603    myLayer->data = zStrdup(OGR_L_GetName(poLayer));
604    myLayer->connection = zStrdup(pszDataSource);
605    myLayer->index = m->numlayers;
606    myLayer->dump = MS_TRUE;
607    myLayer->status = MS_ON;
608    msConnectLayer(myLayer,MS_OGR,pszDataSource);
609
610    /**
611     * Detect the Geometry Type or use Polygon
612     */
613    if(OGR_L_GetGeomType(poLayer) != wkbUnknown){
614      switch(OGR_L_GetGeomType(poLayer)){
615      case wkbPoint:
616      case wkbMultiPoint:
617      case wkbPoint25D:
618      case wkbMultiPoint25D:
619#ifdef DEBUGMS
620        fprintf(stderr,"POINT DataSource Layer \n");
621#endif
622        myLayer->type = MS_LAYER_POINT;
623        break;
624      case wkbLineString :
625      case wkbMultiLineString :
626      case wkbLineString25D:
627      case wkbMultiLineString25D:
628#ifdef DEBUGMS
629        fprintf(stderr,"LINE DataSource Layer \n");
630#endif
631        myLayer->type = MS_LAYER_LINE;
632        break;
633      case wkbPolygon:
634      case wkbMultiPolygon:
635      case wkbPolygon25D:
636      case wkbMultiPolygon25D:
637#ifdef DEBUGMS
638        fprintf(stderr,"POLYGON DataSource Layer \n");
639#endif
640        myLayer->type = MS_LAYER_POLYGON;
641        break;
642      default:
643        myLayer->type = MS_LAYER_POLYGON;
644        break;
645      }
646    }else
647      myLayer->type = MS_LAYER_POLYGON;
648
649    /**
650     * Detect spatial reference or use WGS84
651     */
652    OGRSpatialReferenceH srs=OGR_L_GetSpatialRef(poLayer);
653    if(srs!=NULL){
654      char *wkt=NULL;
655      OSRExportToWkt(srs,&wkt);
656      setSrsInformations(output,m,myLayer,wkt);
657      free(wkt);
658    }
659    else{
660      addToMap(output->content,"crs","EPSG:4326");
661      addToMap(output->content,"crs_isGeographic","true");
662      msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
663      msInsertHashTable(&(m->web.metadata), "ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
664      msInsertHashTable(&(myLayer->metadata), "ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
665    }
666
667    OGREnvelope oExt;
668    if (OGR_L_GetExtent(poLayer,&oExt, TRUE) == OGRERR_NONE){
669      setMsExtent(output,m,myLayer,oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
670      char extent[1024];
671      memset(&extent,0,1024);
672      sprintf(extent,"%d,%d,%d,%d",oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
673      addToMap(output->content,"boundingbox",extent);
674    }
675 
676    /**
677     * Detect the FID column or use the first attribute field as FID
678     */
679    char *fid=(char*)OGR_L_GetFIDColumn(poLayer);
680    if(strlen(fid)==0){
681      OGRFeatureDefnH def=OGR_L_GetLayerDefn(poLayer);
682      int fIndex=0;
683      for(fIndex=0;fIndex<OGR_FD_GetFieldCount(def);fIndex++){
684        OGRFieldDefnH fdef=OGR_FD_GetFieldDefn(def,fIndex);
685        fid=(char*)OGR_Fld_GetNameRef(fdef);
686        break;
687      }
688    }
689    msInsertHashTable(&(myLayer->metadata), "gml_featureid", fid);
690    msInsertHashTable(&(myLayer->metadata), "gml_include_items", "all");
691    msInsertHashTable(&(myLayer->metadata), "ows_name", output->name);
692    map* tmpMap=getMap(output->content,"title");
693    if(tmpMap!=NULL)
694      msInsertHashTable(&(myLayer->metadata), "ows_title", tmpMap->value);
695    else
696      msInsertHashTable(&(myLayer->metadata), "ows_title", "Default Title");
697   
698    if(msGrowLayerClasses(myLayer) == NULL)
699      return -1;
700    if(initClass((myLayer->CLASS[myLayer->numclasses])) == -1)
701      return -1;
702    if(msGrowClassStyles(myLayer->CLASS[myLayer->numclasses]) == NULL)
703      return -1;
704    if(initStyle(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]) == -1)
705      return -1;
706    /**
707     * Apply msStyle else fallback to the default style
708     */
709    tmpMap=getMap(output->content,"msStyle");
710    if(tmpMap!=NULL)
711      msUpdateStyleFromString(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles],tmpMap->value,0);
712    else{
713      /**
714       * Set style
715       */
716      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.red=125;
717      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.green=125;
718      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.blue=255;
719      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.red=80;
720      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.green=80;
721      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.blue=80;
722     
723      /**
724       * Set specific style depending on type
725       */
726      if(myLayer->type == MS_LAYER_POLYGON)
727        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
728      if(myLayer->type == MS_LAYER_LINE){
729        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
730        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinewidth=1.5;
731      }
732      if(myLayer->type == MS_LAYER_POINT){
733        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->symbol=1;
734        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->size=15;
735      }
736     
737    }
738    myLayer->CLASS[myLayer->numclasses]->numstyles++;
739    myLayer->numclasses++;
740   
741    m->layerorder[m->numlayers] = m->numlayers;
742    m->numlayers++;
743
744  }
745
746  //OGR_DS_Destroy(poDS);
747  //OGRCleanupAll();
748
749  return 1;
750}
751
752/**
753 * Try to open a raster output and define the corresponding layer in the MAPFILE
754 *
755 * @param conf the conf maps containing the main.cfg settings
756 * @param output the specific output maps
757 * @param m the mapObj
758 */
759int tryGdal(maps* conf,maps* output,mapObj* m){
760  map* tmpMap=getMap(output->content,"storage");
761  char *pszFilename=tmpMap->value;
762  GDALDatasetH hDataset;
763  GDALRasterBandH hBand;
764  double adfGeoTransform[6];
765  int i, iBand;
766 
767  /**
768   * Try to open the DataSource using GDAL
769   */
770  GDALAllRegister();
771  hDataset = GDALOpen( pszFilename, GA_ReadOnly );
772  if( hDataset == NULL ){
773#ifdef DEBUGMS
774    fprintf(stderr,"Unable to access the DataSource %s \n",pszFilename);
775#endif
776    setMapInMaps(conf,"lenv","message","gdalinfo failed - unable to open");
777    GDALDestroyDriverManager();
778    return -1;
779  }
780#ifdef DEBUGMS
781  fprintf(stderr,"Accessing the DataSource %s %d\n",pszFilename,__LINE__);
782#endif
783
784  addToMap(output->content,"datatype","raster");
785  /**
786   * Add a new layer set name, data
787   */
788  if(msGrowMapLayers(m)==NULL){
789    return -1;
790  }
791  if(initLayer((m->layers[m->numlayers]), m) == -1){
792    return -1;
793  }
794  m->layers[m->numlayers]->index=m->numlayers;
795
796  layerObj* myLayer=m->layers[m->numlayers];
797  myLayer->name = zStrdup(output->name);
798  myLayer->tileitem=NULL;
799  myLayer->data = zStrdup(pszFilename);
800  myLayer->index = m->numlayers;
801  myLayer->dump = MS_TRUE;
802  myLayer->status = MS_ON;
803  myLayer->type = MS_LAYER_RASTER;
804
805  char *title=output->name;
806  tmpMap=getMap(output->content,"title");
807  if(tmpMap!=NULL)
808    title=tmpMap->value;
809  char *abstract=output->name;
810  tmpMap=getMap(output->content,"abstract");
811  if(tmpMap!=NULL)
812    abstract=tmpMap->value;
813
814  msInsertHashTable(&(myLayer->metadata), "ows_label", title);
815  msInsertHashTable(&(myLayer->metadata), "ows_title", title);
816  msInsertHashTable(&(myLayer->metadata), "ows_abstract", abstract);
817  msInsertHashTable(&(myLayer->metadata), "ows_rangeset_name", output->name);
818  msInsertHashTable(&(myLayer->metadata), "ows_rangeset_label", title);
819
820  /**
821   * Set Map Size to the raster size
822   */
823  m->width=GDALGetRasterXSize( hDataset );
824  m->height=GDALGetRasterYSize( hDataset );
825 
826  /**
827   * Set projection using Authority Code and Name if available or fallback to
828   * proj4 definition if available or fallback to default EPSG:4326
829   */
830  const char *tRef=GDALGetProjectionRef( hDataset );
831  if( tRef != NULL && strlen(tRef)>0 ){
832    char *pszProjection;
833    pszProjection = (char *) GDALGetProjectionRef( hDataset );
834#ifdef DEBUGMS
835    fprintf(stderr,"Accessing the DataSource %s\n",GDALGetProjectionRef( hDataset ));
836#endif
837    setSrsInformations(output,m,myLayer,pszProjection);
838  }else{
839    fprintf(stderr,"NO SRS FOUND ! %s\n",GDALGetProjectionRef( hDataset ));   
840  }
841
842  /**
843   * Set extent
844   */
845  if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ){
846    if( adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0 ){
847
848      double minX = adfGeoTransform[0]
849        + adfGeoTransform[2] * GDALGetRasterYSize(hDataset);
850      double minY = adfGeoTransform[3]
851        + adfGeoTransform[5] * GDALGetRasterYSize(hDataset);
852
853      double maxX = adfGeoTransform[0]
854        + adfGeoTransform[1] * GDALGetRasterXSize(hDataset);
855      double maxY = adfGeoTransform[3]
856        + adfGeoTransform[4] * GDALGetRasterXSize(hDataset);
857
858      setMsExtent(output,m,myLayer,minX,minY,maxX,maxY);
859      char extent[1024];
860      memset(&extent,0,1024);
861      sprintf(extent,"%d,%d,%d,%d",minX,minY,maxX,maxY);
862      addToMap(output->content,"boundingbox",extent);
863    }
864  }
865
866  /**
867   * Extract information about available bands to set the bandcount and the
868   * processing directive
869   */
870  char nBands[3];
871  memset(&nBands,0,3);
872  int nBandsI=GDALGetRasterCount( hDataset );
873  if(nBandsI<100){
874    sprintf(nBands,"%d",GDALGetRasterCount( hDataset ));
875    msInsertHashTable(&(myLayer->metadata), "ows_bandcount", nBands);
876  }
877  if(nBandsI>=3)
878    msLayerAddProcessing(myLayer,"BANDS=1,2,3");
879  else if(nBandsI>=2)
880    msLayerAddProcessing(myLayer,"BANDS=1,2");
881  else
882    msLayerAddProcessing(myLayer,"BANDS=1");
883
884  /**
885   * Name available Bands
886   */
887  char lBands[7];
888  memset(&nBands,0,7);
889  char *nameBands=NULL;
890  for( iBand = 0; iBand < nBandsI; iBand++ ){
891    sprintf(lBands,"Band%d",iBand+1);   
892    if(nameBands==NULL){
893      nameBands=(char*)malloc((strlen(lBands)+1)*sizeof(char));
894      sprintf(nameBands,"%s",lBands);
895    }else{
896      if(iBand<4){
897        char *tmpS=zStrdup(nameBands);
898        nameBands=(char*)realloc(nameBands,(strlen(tmpS)+strlen(lBands)+2)*sizeof(char));
899        sprintf(nameBands,"%s %s",tmpS,lBands);
900        free(tmpS);
901      }
902    }
903  }
904  if(nameBands!=NULL){
905    msInsertHashTable(&(myLayer->metadata), "ows_bandnames", nameBands);
906    free(nameBands);
907  }
908
909  /**
910   * Loops over metadata information to setup specific information
911   */
912  for( iBand = 0; iBand < nBandsI; iBand++ ){
913    //int         bGotNodata;//, bSuccess;
914    double      adfCMinMax[2]/*, dfNoData*/;
915    //int         nBlockXSize, nBlockYSize, nMaskFlags;
916    //double      /*dfMean, dfStdDev*/;
917    hBand = GDALGetRasterBand( hDataset, iBand+1 );
918
919    CPLErrorReset();
920    GDALComputeRasterMinMax( hBand, FALSE, adfCMinMax );
921    char tmpN[21];
922    sprintf(tmpN,"Band%d",iBand+1);
923    if (CPLGetLastErrorType() == CE_None){
924      char tmpMm[100];
925      sprintf(tmpMm,"%.3f %.3f",adfCMinMax[0],adfCMinMax[1]);
926      char tmpI[31];     
927      sprintf(tmpI,"%s_interval",tmpN);
928      msInsertHashTable(&(myLayer->metadata), tmpI, tmpMm);
929
930      map* test=getMap(output->content,"msClassify");
931      if(test!=NULL && strncasecmp(test->value,"true",4)==0){
932        /**
933         * Classify one band raster pixel value using regular interval
934         */
935        int _tmpColors[10][3]={
936          {102,153,204},
937          {51,102,153},
938          {102,102,204},
939          {51,204,0},
940          {153,255,102},
941          {204,255,102},
942          {102,204,153},
943          {255,69,64},
944          {255,192,115},
945          {255,201,115}
946        };
947         
948        if(nBandsI==1){
949          double delta=adfCMinMax[1]-adfCMinMax[0];
950          double interval=delta/10;
951          double cstep=adfCMinMax[0];
952          for(i=0;i<10;i++){
953            /**
954             * Create a new class
955             */
956            if(msGrowLayerClasses(myLayer) == NULL)
957              return -1;
958            if(initClass((myLayer->CLASS[myLayer->numclasses])) == -1)
959              return -1;
960            if(msGrowClassStyles(myLayer->CLASS[myLayer->numclasses]) == NULL)
961              return -1;
962            if(initStyle(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]) == -1)
963              return -1;
964           
965            /**
966             * Set class name
967             */
968            char className[7];
969            sprintf(className,"class%d",i);
970            myLayer->CLASS[myLayer->numclasses]->name=zStrdup(className);
971           
972            /**
973             * Set expression
974             */
975            char expression[1024];
976            if(i+1<10)
977              sprintf(expression,"([pixel]>=%.3f AND [pixel]<%.3f)",cstep,cstep+interval);
978            else
979              sprintf(expression,"([pixel]>=%.3f AND [pixel]<=%.3f)",cstep,cstep+interval);
980            msLoadExpressionString(&myLayer->CLASS[myLayer->numclasses]->expression,expression);
981           
982            /**
983             * Set color
984             */
985            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.red=_tmpColors[i][0];
986            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.green=_tmpColors[i][1];
987            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.blue=_tmpColors[i][2];
988            cstep+=interval;
989            myLayer->CLASS[myLayer->numclasses]->numstyles++;
990            myLayer->numclasses++;
991           
992          }
993         
994          char tmpMm[100];
995          sprintf(tmpMm,"%.3f %.3f",adfCMinMax[0],adfCMinMax[1]);
996         
997        }
998      }
999      else{
1000        if(nBandsI==1){
1001          myLayer->offsite.red=0;
1002          myLayer->offsite.green=0;
1003          myLayer->offsite.blue=0;
1004        }
1005        msLayerAddProcessing(myLayer,"RESAMPLE=BILINEAR");
1006      }
1007    }
1008    if( strlen(GDALGetRasterUnitType(hBand)) > 0 ){
1009      char tmpU[31];
1010      sprintf(tmpU,"%s_band_uom",tmpN);
1011      msInsertHashTable(&(myLayer->metadata), tmpU, GDALGetRasterUnitType(hBand));
1012    }
1013
1014  }
1015
1016  m->layerorder[m->numlayers] = m->numlayers;
1017  m->numlayers++;
1018  GDALClose( hDataset );
1019  GDALDestroyDriverManager();
1020
1021  CPLCleanupTLS();
1022  return 1;
1023}
1024
1025/**
1026 * Create a MapFile for WMS, WFS or WCS Service output
1027 *
1028 * @param conf the conf maps containing the main.cfg settings
1029 * @param outputs a specific output maps
1030 */
1031void outputMapfile(maps* conf,maps* outputs){
1032  /**
1033   * First store the value on disk
1034   */
1035  map* mime=getMap(outputs->content,"mimeType");
1036  char *ext="data";
1037  if(mime!=NULL)
1038    if(strncasecmp(mime->value,"application/json",16)==0)
1039      ext="json";
1040
1041  map* tmpMap=getMapFromMaps(conf,"main","dataPath");
1042  map* sidMap=getMapFromMaps(conf,"lenv","usid");
1043  char *pszDataSource=(char*)malloc((strlen(tmpMap->value)+strlen(sidMap->value)+strlen(outputs->name)+17)*sizeof(char));
1044  sprintf(pszDataSource,"%s/ZOO_DATA_%s_%s.%s",tmpMap->value,outputs->name,sidMap->value,ext);
1045  int f=zOpen(pszDataSource,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
1046  map *gfile=getMap(outputs->content,"generated_file");
1047  if(gfile!=NULL){
1048    readGeneratedFile(conf,outputs->content,gfile->value);         
1049  }
1050  map* sizeMap=getMap(outputs->content,"size");
1051  map* vData=getMap(outputs->content,"value");
1052  if(sizeMap!=NULL){
1053    zWrite(f,vData->value,atoi(sizeMap->value)*sizeof(char));
1054  }
1055  else{
1056    zWrite(f,vData->value,(strlen(vData->value)+1)*sizeof(char));
1057  }
1058  close(f);
1059  addToMap(outputs->content,"storage",pszDataSource);
1060  free(pszDataSource);
1061
1062  /*
1063   * Create an empty map, set name, default size and extent
1064   */
1065  mapObj *myMap=msNewMapObj();
1066  free(myMap->name);
1067  myMap->name=zStrdup("ZOO-Project_WXS_Server");
1068  msMapSetSize(myMap,2048,2048);
1069  msMapSetExtent(myMap,-1,-1,1,1);
1070 
1071  /*
1072   * Set imagepath and imageurl using tmpPath and tmpUrl from main.cfg
1073   */
1074  map *tmp1=getMapFromMaps(conf,"main","tmpPath");
1075  myMap->web.imagepath=zStrdup(tmp1->value);
1076  tmp1=getMapFromMaps(conf,"main","tmpUrl");
1077  myMap->web.imageurl=zStrdup(tmp1->value);
1078 
1079  /*
1080   * Define supported output formats
1081   */
1082  outputFormatObj *o1=msCreateDefaultOutputFormat(NULL,"AGG/PNG","png");
1083  o1->imagemode=MS_IMAGEMODE_RGBA;
1084  o1->transparent=MS_TRUE;
1085  o1->inmapfile=MS_TRUE;
1086  msAppendOutputFormat(myMap,msCloneOutputFormat(o1));
1087  msFreeOutputFormat(o1);
1088
1089#ifdef USE_KML
1090  outputFormatObj *o2=msCreateDefaultOutputFormat(NULL,"KML","kml");
1091  if(!o2){
1092    perror("Unable to initialize KML driver");
1093    fprintf(stderr,"Unable to initialize KML driver !\n");
1094  }else{
1095    o2->inmapfile=MS_TRUE; 
1096    msAppendOutputFormat(myMap,msCloneOutputFormat(o2));
1097    msFreeOutputFormat(o2);
1098  }
1099#endif
1100
1101  outputFormatObj *o3=msCreateDefaultOutputFormat(NULL,"GDAL/GTiff","tiff");
1102  if(!o3)
1103    fprintf(stderr,"Unable to initialize GDAL driver !\n");
1104  else{
1105    o3->imagemode=MS_IMAGEMODE_BYTE;
1106    o3->inmapfile=MS_TRUE; 
1107    msAppendOutputFormat(myMap,msCloneOutputFormat(o3));
1108    msFreeOutputFormat(o3);
1109  }
1110
1111  outputFormatObj *o4=msCreateDefaultOutputFormat(NULL,"GDAL/AAIGRID","grd");
1112  if(!o4)
1113    fprintf(stderr,"Unable to initialize GDAL driver !\n");
1114  else{
1115    o4->imagemode=MS_IMAGEMODE_INT16;
1116    o4->inmapfile=MS_TRUE; 
1117    msAppendOutputFormat(myMap,msCloneOutputFormat(o4));
1118    msFreeOutputFormat(o4);
1119  }
1120
1121#ifdef USE_CAIRO
1122  outputFormatObj *o5=msCreateDefaultOutputFormat(NULL,"CAIRO/PNG","cairopng");
1123  if(!o5)
1124    fprintf(stderr,"Unable to initialize CAIRO driver !\n");
1125  else{
1126    o5->imagemode=MS_IMAGEMODE_RGBA;
1127    o5->transparent=MS_TRUE;
1128    o5->inmapfile=MS_TRUE;
1129    msAppendOutputFormat(myMap,msCloneOutputFormat(o5));
1130    msFreeOutputFormat(o5);
1131  }
1132#endif
1133
1134  /*
1135   * Set default projection to EPSG:4326
1136   */
1137  msLoadProjectionStringEPSG(&myMap->projection,"EPSG:4326");
1138  myMap->transparent=1;
1139
1140  /**
1141   * Set metadata extracted from main.cfg file maps
1142   */
1143  maps* cursor=conf;
1144  map* correspondance=getCorrespondance();
1145  while(cursor!=NULL){
1146    map* _cursor=cursor->content;
1147    map* vMap;
1148    while(_cursor!=NULL){
1149      if((vMap=getMap(correspondance,_cursor->name))!=NULL){
1150        if (msInsertHashTable(&(myMap->web.metadata), vMap->value, _cursor->value) == NULL){
1151#ifdef DEBUGMS
1152          fprintf(stderr,"Unable to add metadata");
1153#endif
1154          return;
1155        }
1156      }
1157      _cursor=_cursor->next;
1158    }
1159    cursor=cursor->next;
1160  }
1161  freeMap(&correspondance);
1162  free(correspondance);
1163
1164  /*
1165   * Set mapserver PROJ_LIB/GDAL_DATA or any other config parameter from
1166   * the main.cfg [mapserver] section if any
1167   */
1168  maps *tmp3=getMaps(conf,"mapserver");
1169  if(tmp3!=NULL){
1170    map* tmp4=tmp3->content;
1171    while(tmp4!=NULL){
1172      msSetConfigOption(myMap,tmp4->name,tmp4->value);
1173      tmp4=tmp4->next;
1174    }
1175  }
1176
1177  /**
1178   * Set a ows_rootlayer_title, 
1179   */
1180  if (msInsertHashTable(&(myMap->web.metadata), "ows_rootlayer_name", "ZOO_Project_Layer") == NULL){
1181#ifdef DEBUGMS
1182    fprintf(stderr,"Unable to add metadata");
1183#endif
1184    return;
1185  }
1186  if (msInsertHashTable(&(myMap->web.metadata), "ows_rootlayer_title", "ZOO_Project_Layer") == NULL){
1187#ifdef DEBUGMS
1188    fprintf(stderr,"Unable to add metadata");
1189#endif
1190    return;
1191  }
1192
1193  /**
1194   * Enable all the WXS requests using ows_enable_request
1195   * see http://mapserver.org/trunk/development/rfc/ms-rfc-67.html
1196   */
1197  if (msInsertHashTable(&(myMap->web.metadata), "ows_enable_request", "*") == NULL){
1198#ifdef DEBUGMS
1199    fprintf(stderr,"Unable to add metadata");
1200#endif
1201    return;
1202  }
1203  msInsertHashTable(&(myMap->web.metadata), "ows_srs", "EPSG:4326");
1204
1205  if(tryOgr(conf,outputs,myMap)<0)
1206    if(tryGdal(conf,outputs,myMap)<0)
1207      return ;
1208
1209  tmp1=getMapFromMaps(conf,"main","dataPath");
1210  char *tmpPath=(char*)malloc((13+strlen(tmp1->value))*sizeof(char));
1211  sprintf(tmpPath,"%s/symbols.sym",tmp1->value);
1212  msInitSymbolSet(&myMap->symbolset);
1213  myMap->symbolset.filename=zStrdup(tmpPath);
1214  free(tmpPath);
1215
1216  map* sid=getMapFromMaps(conf,"lenv","usid");
1217  char *mapPath=
1218    (char*)malloc((7+strlen(sid->value)+strlen(outputs->name)+strlen(tmp1->value))*sizeof(char));
1219  sprintf(mapPath,"%s/%s_%s.map",tmp1->value,outputs->name,sid->value);
1220  msSaveMap(myMap,mapPath);
1221  free(mapPath);
1222  msGDALCleanup();
1223  msFreeMap(myMap);
1224}
1225
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