source: branches/branch-1.3/zoo-project/zoo-kernel/service_internal_ms.c @ 986

Last change on this file since 986 was 434, checked in by djay, 11 years ago

Fixed verbose output when using W*S output in r434 (ticket #!0).

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