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

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

Commit the minimal requirements for remote HPC support

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