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

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

Add the capability to publish heatmap or any templated mapfile using the epecific msInclude and msLayer keys for an output. For MapServer? published output, define 4096 as the default maxsize and use pixel width or height for raster files. use the correct MapServer? imagemode depending on GDALGetRasterDataType (MS_IMAGEMODE_BYTE for GDT_Byte, MS_IMAGEMODE_INT16 for GDT_Int16 and MS_IMAGEMODE_FLOAT32 for GDT_Float32). Create a text file (.maps) listing every mapfiles created for a MapServer? published output (or inputs) using saveMapNames function. Fixes in ulinet, use uuid for naming temporary files. Add dialect input to the ogr2ogr service. Use the .maps file for removing a file from the DeleteData? service

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 48.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/**
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 * Return the current publish_id value
79 * @param elem and maps pointer on which the search occur
80 * @return the integer value of the publish_id field, if any, 0 otherwise
81 */ 
82int getPublishedId(maps* elem){
83  map* myIndex=getMap(elem->content,"published_id");
84  if(myIndex!=NULL){
85    return atoi(myIndex->value);
86  }
87  return 0;
88}
89/**
90 * Add width and height keys to an output maps containing the maximum width
91 * and height for displaying the full data extent.
92 * Restriction to an image having a size of 640x480 (width * height)
93 *
94 * @param output
95 * @param minx the lower left x coordinate
96 * @param miny the lower left y coordinate
97 * @param maxx the upper right x coordinate
98 * @param maxy the upper right y coordinate
99 */
100void setMapSize(maps* output,double minx,double miny,double maxx,double maxy){
101  int imyIndex=getPublishedId(output);
102  double maxWidth=640;
103  double maxHeight=480;
104  double deltaX=maxx-minx;
105  double deltaY=maxy-miny;
106  double qWidth;
107  qWidth=maxWidth/deltaX;
108  double qHeight;
109  qHeight=maxHeight/deltaY;
110#ifdef DEBUGMS
111  fprintf(stderr,"deltaX : %.15f \ndeltaY : %.15f\n",deltaX,deltaY);
112  fprintf(stderr,"qWidth : %.15f \nqHeight : %.15f\n",qWidth,qHeight);
113#endif
114
115  double width=deltaX*qWidth;
116  double height=height=deltaY*qWidth;
117  if(deltaX<deltaY){
118    width=deltaX*qHeight;
119    height=deltaY*qHeight;
120  }
121  if(height<0)
122    height=-height;
123  if(width<0)
124    width=-width;
125  char sWidth[1024];
126  char sHeight[1024];
127  sprintf(sWidth,"%.3f",width);
128  sprintf(sHeight,"%.3f",height);
129#ifdef DEBUGMS
130  fprintf(stderr,"sWidth : %.15f \nsHeight : %.15f\n",sWidth,sHeight);
131#endif
132  if(output!=NULL){
133    setMapArray(output->content,"width",imyIndex,sWidth);
134    setMapArray(output->content,"height",imyIndex,sHeight);
135  }
136}
137
138/**
139 * Add a Reference key to an output containing the WMFS/WFS/WCS request for
140 * accessing service result
141 *
142 * @param m the conf maps containing the main.cfg settings
143 * @param tmpI the specific output maps to add the Reference key
144 */
145void setReferenceUrl(maps* m,maps* tmpI){
146  int imyIndex=getPublishedId(tmpI);
147  if(getMapArray(tmpI->content,"ref_wms_link",imyIndex)!=NULL)
148    return;
149  outputMapfile(m,tmpI);
150  map *msUrl=getMapFromMaps(m,"main","mapserverAddress");
151  if(msUrl==NULL){
152    errorException (m, _("Unable to find any mapserverAddress defined in the main.cfg file"),
153                    "InternalError", NULL);
154    exit(-1);
155  }
156  if(getMapArray(tmpI->content,"ref_wms_link",imyIndex)!=NULL)
157    return;
158  int finalProto=-1;
159  map *msOgcVersion=getMapFromMaps(m,"main","msOgcVersion");
160  map *dataPath=getMapFromMaps(m,"main","dataPath");
161  map *sid=getMapFromMaps(m,"lenv","usid");
162  map* format=getMapArray(tmpI->content,"mimeType",imyIndex);
163  map* rformat=getMapArray(tmpI->content,"requestedMimeType",imyIndex);
164  map* width=getMapArray(tmpI->content,"width",imyIndex);
165  map* height=getMapArray(tmpI->content,"height",imyIndex);
166  map* protoMap=getMapArray(tmpI->content,"msOgc",imyIndex);
167  map* versionMap=getMapArray(tmpI->content,"msOgcVersion",imyIndex);
168  map* datatype=getMapArray(tmpI->content,"geodatatype",imyIndex);
169  map* layerName=getMapArray(tmpI->content,"msLayer",imyIndex);
170  char options[4][5][25]={
171    {"WMS","1.3.0","GetMap","layers=%s","wms_extent"},
172    {"WFS","1.0.0","GetFeature","typename=%s","wcs_extent"},
173    {"WCS","2.0.0","GetCoverage","coverageid=%s","wcs_extent"},
174    {"WCS","1.0.0","GetCoverage","coverage=%s","wcs_extent"}
175  };
176  map *nbElements=getMapArray(tmpI->content,"nb_features",imyIndex);
177  if(nbElements==NULL)
178    nbElements=getMapArray(tmpI->content,"nb_pixels",imyIndex);
179  if(datatype==NULL || strncmp(datatype->value,"other",5)==0 || (nbElements!=NULL && atoi(nbElements->value)==0)){
180    map* minNb=getMap(tmpI->content,"minoccurs");
181    map* useMs=getMap(tmpI->content,"useMapserver");
182    if((minNb==NULL || atoi(minNb->value)>=1) && useMs!=NULL && strncasecmp(useMs->value,"true",4)==0){
183      setMapInMaps(m,"lenv","mapError","true");
184      setMapInMaps(m,"lenv","locator",tmpI->name);
185      if(nbElements==NULL)
186        setMapInMaps(m,"lenv","message",_("The ZOO-Kernel was able to retrieve the data but could not read it as geographic data."));
187      else
188        setMapInMaps(m,"lenv","message",_("The ZOO-Kernel was able to retrieve the data but could not access any feature or pixel in te resulting file."));
189      if(getMapFromMaps(m,"lenv","state")==NULL)
190        errorException (m, _("Unable to find any geographic data"), "WrongInputData", tmpI->name);
191    }
192    return;
193  }
194  int proto=0;
195  if(rformat==NULL){
196    rformat=getMapArray(tmpI->content,"mimeType",imyIndex);
197  }
198  if(strncasecmp(rformat->value,"text/xml",8)==0)
199    proto=1;
200  if(strncasecmp(rformat->value,"image/tiff",10)==0 ||
201     strncasecmp(rformat->value,"image/geotiff",10)==0)
202    proto=2;
203  int hasFormat=-1;
204  if(protoMap!=NULL){
205    hasFormat=1;
206    if(strncasecmp(protoMap->value,"WMS",3)==0){
207      proto=0;
208      rformat=createMap("value","image/png");
209    }
210    else{
211      if(strncasecmp(protoMap->value,"WFS",3)==0){
212        proto=1;
213        rformat=createMap("value","text/xml");
214      }
215      else {
216        proto=2;
217        rformat=createMap("value","image/tiff");
218      }
219    }
220  }
221  char *protoVersion=options[proto][1];
222  if(proto==1){
223    if(msOgcVersion!=NULL)
224      protoVersion=msOgcVersion->value;
225    if(versionMap!=NULL)
226      protoVersion=versionMap->value;
227  }
228
229
230  map* extent=getMapArray(tmpI->content,options[proto][4],imyIndex);
231  map* crs=getMapArray(tmpI->content,"crs",imyIndex);
232  int hasCRS=1;
233  if(crs==NULL){
234    crs=getMapFromMaps(m,"main","crs");
235    if(crs==NULL){
236      crs=createMap("crs","epsg:4326");
237      hasCRS=0;
238    }
239  }
240  char layers[128];
241  if(layerName==NULL)
242    sprintf(layers,options[proto][3],tmpI->name);
243  else
244    sprintf(layers,options[proto][3],layerName->value);
245 
246  if(format==NULL || width==NULL || height==NULL || extent==NULL){
247    char tmpStr[1024];
248    sprintf(tmpStr,_("Unable to create the mapfile for %s because of missing values."),tmpI->name);
249    errorException (m, tmpStr,
250                    "InternalError", NULL);
251    exit(-1);
252    return;
253  }
254
255  if(proto==0){
256    hasFormat=1;
257    rformat=createMap("mimeType","image/png");
258  }else{
259    if(proto==1){
260      rformat=createMap("mimeType","text/xml");
261      hasFormat=1;
262    }
263    else
264      if(proto==2){
265        rformat=createMap("mimeType","image/tiff");
266        hasFormat=1;
267        finalProto=1;
268      }
269  }
270 
271  char* webService_url=(char*)malloc((strlen(msUrl->value)+strlen(rformat->value)+strlen(tmpI->name)+strlen(width->value)+strlen(height->value)+strlen(extent->value)+256)*sizeof(char));
272
273
274  if(proto>0){
275    if(proto==2)
276      finalProto=1;
277    sprintf(webService_url,
278            "%s?map=%s/%s_%d_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
279            msUrl->value,
280            dataPath->value,
281            tmpI->name,
282            imyIndex,
283            sid->value,
284            options[proto][2],
285            options[proto][0],
286            protoVersion,
287            layers,
288            rformat->value,
289            extent->value,
290            crs->value
291            );
292    if(datatype!=NULL && strncasecmp(datatype->value,"raster",6)==0){
293      setMapArray(tmpI->content,"ref_wcs_link",imyIndex,webService_url);
294    }
295    else{
296      setMapArray(tmpI->content,"ref_wfs_link",imyIndex,webService_url);
297    }
298    proto=0;
299    freeMap(&rformat);
300    free(rformat);
301    rformat=createMap("mimeType","image/png");
302  }
303  else{
304    sprintf(webService_url,
305            "%s?map=%s/%s_%d_%s.map&request=%s&service=%s&version=%s&%s&width=%s&height=%s&format=%s&bbox=%s&crs=%s",
306            msUrl->value,
307            dataPath->value,
308            tmpI->name,
309            imyIndex,
310            sid->value,
311            options[proto][2],
312            options[proto][0],
313            protoVersion,
314            layers,
315            width->value,
316            height->value,
317            rformat->value,
318            extent->value,
319            crs->value
320            );
321    setMapArray(tmpI->content,"ref_wms_link",imyIndex,webService_url);
322    if(datatype!=NULL && strncasecmp(datatype->value,"raster",6)==0){
323      proto=2;
324      freeMap(&rformat);
325      free(rformat);
326      rformat=createMap("mimeType","image/tiff");
327    }
328    else{
329      proto=1;
330      freeMap(&rformat);
331      free(rformat);
332      rformat=createMap("mimeType","text/xml");
333    }
334  }
335  setMapArray(tmpI->content,"Reference",imyIndex,webService_url);
336  memset(layers,0,128);
337  sprintf(layers,options[proto][3],tmpI->name);
338  protoVersion=options[proto][1];
339  extent=getMapArray(tmpI->content,options[proto][4],imyIndex);
340  memset(webService_url,0,strlen(webService_url));
341  if(proto>0){
342    if(proto==2)
343      finalProto=1;
344    sprintf(webService_url,
345            "%s?map=%s/%s_%d_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
346            msUrl->value,
347            dataPath->value,
348            tmpI->name,
349            imyIndex,
350            sid->value,
351            options[proto][2],
352            options[proto][0],
353            protoVersion,
354            layers,
355            rformat->value,
356            extent->value,
357            crs->value
358            );
359    if(datatype!=NULL && strncasecmp(datatype->value,"raster",6)==0){
360      setMapArray(tmpI->content,"ref_wcs_link",imyIndex,webService_url);
361    }
362    else{
363      setMapArray(tmpI->content,"ref_wfs_link",imyIndex,webService_url);
364    }
365  }else{
366    sprintf(webService_url,
367            "%s?map=%s/%s_%d_%s.map&request=%s&service=%s&version=%s&%s&width=%s&height=%s&format=%s&bbox=%s&crs=%s",
368            msUrl->value,
369            dataPath->value,
370            tmpI->name,
371            imyIndex,
372            sid->value,
373            options[proto][2],
374            options[proto][0],
375            protoVersion,
376            layers,
377            width->value,
378            height->value,
379            rformat->value,
380            extent->value,
381            crs->value
382            );
383    setMapArray(tmpI->content,"ref_wms_link",imyIndex,webService_url);
384  }
385  if(finalProto>0){
386    proto=3;
387    memset(layers,0,128);
388    sprintf(layers,options[proto][3],tmpI->name);
389    protoVersion=options[proto][1];
390    extent=getMapArray(tmpI->content,options[proto][4],imyIndex);
391    memset(webService_url,0,strlen(webService_url));
392    freeMap(&rformat);
393    free(rformat);
394    rformat=createMap("value","image/tiff");
395    sprintf(webService_url,
396            "%s?map=%s/%s_%d_%s.map&request=%s&service=%s&version=%s&%s&format=%s&bbox=%s&crs=%s",
397            msUrl->value,
398            dataPath->value,
399            tmpI->name,
400            imyIndex,
401            sid->value,
402            options[proto][2],
403            options[proto][0],
404            protoVersion,
405            layers,
406            rformat->value,
407            extent->value,
408            crs->value
409            );
410    setMapArray(tmpI->content,"ref_wcs_preview_link",imyIndex,webService_url);
411  }
412  if(hasCRS==0){
413    freeMap(&crs);
414    free(crs);
415  }
416  freeMap(&rformat);
417  free(rformat);
418  free(webService_url);
419}
420
421/**
422 * Set projection for a layer in a MAPFILE using Authority Code and Name if
423 * available or fallback to proj4 definition if available or fallback to
424 * default EPSG:4326
425 *
426 * @param output the output maps
427 * @param m the opened mapObj
428 * @param myLayer the layerObj
429 * @param pszProjection a char* containing the SRS definition in WKT format
430 */
431void setSrsInformations(maps* output,mapObj* m,layerObj* myLayer,
432                        char* pszProjection){
433  OGRSpatialReferenceH  hSRS;
434  map* msSrs=NULL;
435  int imyIndex=getPublishedId(output);
436  hSRS = OSRNewSpatialReference(NULL);
437  if( pszProjection!=NULL && strlen(pszProjection)>1){
438    if(OSRImportFromWkt( hSRS, &pszProjection ) == CE_None ){
439      char *proj4Str=NULL;
440      if(OSRGetAuthorityName(hSRS,NULL)!=NULL && 
441         OSRGetAuthorityCode(hSRS,NULL)!=NULL){
442        char tmpSrs[20];
443        sprintf(tmpSrs,"%s:%s",
444                OSRGetAuthorityName(hSRS,NULL),OSRGetAuthorityCode(hSRS,NULL));
445        msLoadProjectionStringEPSG(&m->projection,tmpSrs);
446        msLoadProjectionStringEPSG(&myLayer->projection,tmpSrs);
447       
448        char tmpSrss[256];
449        sprintf(tmpSrss,"EPSG:4326 EPSG:900913 EPSG:3857 %s",tmpSrs);
450       
451        msInsertHashTable(&(m->web.metadata), "ows_srs", tmpSrss);
452        msInsertHashTable(&(myLayer->metadata), "ows_srs", tmpSrss);
453       
454#ifdef DEBUGMS
455        fprintf(stderr,"isGeo %b\n\n",OSRIsGeographic(hSRS)==TRUE);
456#endif
457        if(output!=NULL){
458          if(OSRIsGeographic(hSRS)==TRUE)
459            setMapArray(output->content,"crs_isGeographic",imyIndex,"true");
460          else
461            setMapArray(output->content,"crs_isGeographic",imyIndex,"false");
462          setMapArray(output->content,"crs",imyIndex,tmpSrs);
463        }
464      }
465      else{
466        OSRExportToProj4(hSRS,&proj4Str);
467        if(proj4Str!=NULL && strlen(proj4Str)>0){
468#ifdef DEBUGMS
469          fprintf(stderr,"PROJ (%s)\n",proj4Str);
470#endif
471          msLoadProjectionString(&(m->projection),proj4Str);
472          msLoadProjectionString(&(myLayer->projection),proj4Str);
473          if(output!=NULL){ 
474            if(OSRIsGeographic(hSRS)==TRUE)
475              setMapArray(output->content,"crs_isGeographic",imyIndex,"true");
476            else
477              setMapArray(output->content,"crs_isGeographic",imyIndex,"false");
478          }
479          free(proj4Str);
480        }
481        else{
482          msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
483          msLoadProjectionStringEPSG(&myLayer->projection,"EPSG:4326");
484          if(output!=NULL){
485            setMapArray(output->content,"crs_isGeographic",imyIndex,"true");
486          }
487        }
488        if(output!=NULL){
489          setMapArray(output->content,"crs",imyIndex,"EPSG:4326");
490          setMapArray(output->content,"real_extent",imyIndex,"true");
491        }
492        msInsertHashTable(&(m->web.metadata),"ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
493        msInsertHashTable(&(myLayer->metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
494      }
495    }
496  }
497  else{
498    if(output!=NULL){
499      msSrs=getMapArray(output->content,"msSrs",imyIndex);
500    }
501    if(msSrs!=NULL){
502      msLoadProjectionStringEPSG(&m->projection,msSrs->value);
503      msLoadProjectionStringEPSG(&myLayer->projection,msSrs->value);
504      char tmpSrs[128];
505      sprintf(tmpSrs,"%s EPSG:4326 EPSG:900913 EPSG:3857",msSrs->value);
506      msInsertHashTable(&(m->web.metadata),"ows_srs",tmpSrs);
507      msInsertHashTable(&(myLayer->metadata),"ows_srs",tmpSrs);
508    }else{
509      msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
510      msLoadProjectionStringEPSG(&myLayer->projection,"EPSG:4326");
511      msInsertHashTable(&(m->web.metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
512      msInsertHashTable(&(myLayer->metadata),"ows_srs","EPSG:4326 EPSG:900913 EPSG:3857");
513    }
514    if(output!=NULL){
515      setMapArray(output->content,"crs",imyIndex,msSrs->value);
516      setMapArray(output->content,"crs_isGeographic",imyIndex,"true");
517    }
518  }
519
520  OSRDestroySpatialReference( hSRS );
521}
522
523/**
524 * Set the MAPFILE extent, the the ows_extent for the layer, add wms_extent and
525 * wfs_extent to the output maps and call setMapSize.
526 *
527 * @param output the specific output
528 * @param m the mapObj
529 * @param myLayer the layerObj
530 * @param minX the lower left x coordinate
531 * @param minY the lower left y coordinate
532 * @param maxX the upper right x coordinate
533 * @param maxY the upper right y coordinate
534 * @see setMapSize
535 */
536void setMsExtent(maps* output,mapObj* m,layerObj* myLayer,
537                 double minX,double minY,double maxX,double maxY){
538  int imyIndex=getPublishedId(output);
539  msMapSetExtent(m,minX,minY,maxX,maxY);
540  m->maxsize=4096;
541#ifdef DEBUGMS
542  fprintf(stderr,"Extent %.15f %.15f %.15f %.15f\n",minX,minY,maxX,maxY);
543#endif
544  char tmpExtent[1024];
545  sprintf(tmpExtent,"%.15f %.15f %.15f %.15f",minX,minY,maxX,maxY);
546#ifdef DEBUGMS
547  fprintf(stderr,"Extent %s\n",tmpExtent);
548#endif
549  msInsertHashTable(&(myLayer->metadata), "ows_extent", tmpExtent);
550 
551  if(output!=NULL){
552    map* test=getMapArray(output->content,"real_extent",imyIndex);
553    pointObj min, max;
554    projectionObj tempSrs;
555    min.x = m->extent.minx;
556    min.y = m->extent.miny;
557    max.x = m->extent.maxx;
558    max.y = m->extent.maxy;
559    char tmpSrsStr[1024];
560    msInitProjection(&tempSrs);
561    msLoadProjectionStringEPSG(&tempSrs,"EPSG:4326");
562
563    msProjectPoint(&(myLayer->projection),&tempSrs,&min);
564    msProjectPoint(&myLayer->projection,&tempSrs,&max);
565 
566    if(test!=NULL){
567      sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",min.y,min.x,max.y,max.x);
568      map* isGeo=getMapArray(output->content,"crs_isGeographic",imyIndex);
569#ifdef DEBUGMS
570      fprintf(stderr,"isGeo = %s\n",isGeo->value);
571#endif
572      if(isGeo!=NULL && strcasecmp("true",isGeo->value)==0){
573        sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",min.y,min.x,max.y,max.x);
574        setMapArray(output->content,"wgs84_extent",imyIndex,tmpExtent);
575        sprintf(tmpExtent,"%f,%f,%f,%f", minY,minX, maxY, maxX);
576      }else{
577        sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",min.x,min.y,max.x,max.y);
578        setMapArray(output->content,"wgs84_extent",imyIndex,tmpExtent);
579      }
580      setMapArray(output->content,"wms_extent",imyIndex,tmpExtent);
581      sprintf(tmpSrsStr,"%.3f,%.3f,%.3f,%.3f",min.x,min.y,max.x,max.y);
582      setMapArray(output->content,"wcs_extent",imyIndex,tmpExtent);
583    }else{
584      sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",min.x,min.y,max.x,max.y);
585      setMapArray(output->content,"wgs84_extent",imyIndex,tmpExtent);
586      sprintf(tmpExtent,"%f,%f,%f,%f",minX, minY, maxX, maxY);
587      map* isGeo=getMapArray(output->content,"crs_isGeographic",imyIndex);
588      if(isGeo!=NULL){
589#ifdef DEBUGMS
590        fprintf(stderr,"isGeo = %s\n",isGeo->value);
591#endif
592        if(isGeo!=NULL && strcasecmp("true",isGeo->value)==0)
593          sprintf(tmpExtent,"%f,%f,%f,%f", minY,minX, maxY, maxX);
594      }
595      setMapArray(output->content,"wms_extent",imyIndex,tmpExtent); 
596      sprintf(tmpExtent,"%.3f,%.3f,%.3f,%.3f",minX,minY,maxX,maxY);
597      setMapArray(output->content,"wcs_extent",imyIndex,tmpExtent);
598    }
599  }
600
601  setMapSize(output,minX,minY,maxX,maxY);
602}
603
604/**
605 * Try to open a vector output and define the corresponding layer in the MAPFILE
606 *
607 * @param conf the conf maps containing the main.cfg settings
608 * @param output the specific output maps
609 * @param m the mapObj
610 */
611int tryOgr(maps* conf,maps* output,mapObj* m){
612  int imyIndex=getPublishedId(output);
613  map* tmpMap=getMapArray(output->content,"storage",imyIndex);
614  char *pszDataSource=tmpMap->value;
615
616  /**
617   * Try to open the DataSource using OGR
618   */
619  OGRRegisterAll();
620  /**
621   * Try to load the file as ZIP
622   *
623  OGRDataSourceH poDS1 = NULL;
624  OGRSFDriverH *poDriver1 = NULL;
625  char *dsName=(char*)malloc((8+strlen(pszDataSource)+1)*sizeof(char));
626  char *odsName=zStrdup(pszDataSource);
627  char *sdsName=zStrdup(pszDataSource);
628  char *demo=".data";
629  sdsName[strlen(sdsName)-(strlen(demo)-1)]='d';
630  sdsName[strlen(sdsName)-(strlen(demo)-2)]='i';
631  sdsName[strlen(sdsName)-(strlen(demo)-3)]='r';
632  sdsName[strlen(sdsName)-(strlen(demo)-4)]=0;
633
634  odsName[strlen(odsName)-(strlen(demo)-1)]='z';
635  odsName[strlen(odsName)-(strlen(demo)-2)]='i';
636  odsName[strlen(odsName)-(strlen(demo)-3)]='p';
637  odsName[strlen(odsName)-(strlen(demo)-4)]=0;
638  sprintf(dsName,"/vsizip/%s",odsName);
639
640#ifdef DEBUGMS
641  fprintf(stderr,"Try loading %s, %s, %s\n",dsName,odsName,dsName);
642#endif
643
644  FILE* file = fopen(pszDataSource, "rb");
645  FILE* fileZ = fopen(odsName, "wb");
646  free(odsName);
647  fseek(file, 0, SEEK_END);
648  unsigned long fileLen=ftell(file);
649  fseek(file, 0, SEEK_SET);
650  char *buffer=(char *)malloc(fileLen+1);
651  fread(buffer, fileLen, 1, file);
652  fwrite(buffer,fileLen, 1, fileZ);
653  fclose(file);
654  fclose(fileZ);
655  free(buffer);
656#ifdef DEBUGMS
657  fprintf(stderr,"Try loading %s",dsName);
658#endif
659  poDS1 = OGROpen( dsName, FALSE, poDriver1 );
660  if( poDS1 == NULL ){
661    fprintf(stderr,"Unable to access the DataSource as ZIP File\n");
662    setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
663    fprintf(stderr,"Remove ZIP File!\n");
664    unlink(odsName);
665    //OGR_DS_Destroy(poDS1);
666  }else{
667#ifdef DEBUGMS
668    fprintf(stderr,"The DataSource is a  ZIP File\n");
669#endif
670    char** demo=VSIReadDir(dsName);
671    int i=0;
672    zMkdir(sdsName
673#ifndef WIN32
674                ,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
675#endif
676                );
677    while(demo[i]!=NULL){
678#ifdef DEBUGMS
679      fprintf(stderr,"ZIP File content : %s\n",demo[i]);
680#endif
681      char *tmpDs=(char*)malloc((strlen(dsName)+strlen(demo[i])+2)*sizeof(char));
682      sprintf(tmpDs,"%s/%s",dsName,demo[i]);
683      fprintf(stderr,"read : %s\n",tmpDs);
684     
685      VSILFILE* vsif=VSIFOpenL(tmpDs,"rb");
686#ifdef DEBUGMS
687      fprintf(stderr,"open : %s\n",tmpDs);
688#endif
689      VSIFSeekL(vsif,0,SEEK_END);
690      vsi_l_offset size=VSIFTellL(vsif);
691#ifdef DEBUGMS
692      fprintf(stderr,"size : %d\n",size);
693#endif
694      VSIFSeekL(vsif,0,SEEK_SET);
695      char *vsifcontent=(char*) malloc(((int)size+1)*sizeof(char));
696      VSIFReadL(vsifcontent,1,(size_t)size,vsif);
697      char *fpath=(char*) malloc((strlen(sdsName)+strlen(demo[1])+2)*sizeof(char));
698      sprintf(fpath,"%s/%s",sdsName,demo[i]);
699      int f=zOpen(fpath,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
700      zWrite(f,vsifcontent,(int)size);
701      close(f);
702      chmod(fpath,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH);
703      char* tmpP=strstr(fpath,".shp");
704      if(tmpP==NULL)
705        tmpP=strstr(fpath,".SHP");
706      if(tmpP!=NULL){
707#ifdef DEBUGMS
708        fprintf(stderr,"*** DEBUG %s\n",strstr(tmpP,"."));
709#endif
710        if( strcmp(tmpP,".shp")==0 || strcmp(tmpP,".SHP")==0 ){
711          tmpMap=getMap(output->content,"storage");
712          free(tmpMap->value);
713          tmpMap->value=(char*) malloc((strlen(fpath)+1)*sizeof(char));
714          sprintf(tmpMap->value,"%s",fpath);
715          pszDataSource=tmpMap->value;
716#ifdef DEBUGMS
717          fprintf(stderr,"*** DEBUG %s\n",pszDataSource);
718#endif
719        }
720      }
721      VSIFCloseL(vsif);
722      i++;
723    }
724    OGR_DS_Destroy(poDS1);
725    }
726  free(sdsName);
727  free(dsName);*/
728 
729  OGRDataSourceH poDS = NULL;
730  OGRSFDriverH *poDriver = NULL;
731  poDS = OGROpen( pszDataSource, FALSE, poDriver );
732  if( poDS == NULL ){
733#ifdef DEBUGMS
734    fprintf(stderr,"Unable to access the DataSource %s\n",pszDataSource);
735#endif
736    setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
737#ifdef DEBUGMS
738    fprintf(stderr,"Unable to access the DataSource, exit! \n"); 
739#endif
740    return -1;
741  }
742
743  setMapArray(output->content,"geodatatype",imyIndex,"vector");
744  int iLayer = 0;
745  for( iLayer=0; iLayer < OGR_DS_GetLayerCount(poDS); iLayer++ ){
746    OGRLayerH poLayer = OGR_DS_GetLayer(poDS,iLayer);
747
748    if( poLayer == NULL ){
749#ifdef DEBUGMS
750      fprintf(stderr,"Unable to access the DataSource Layer \n");
751#endif
752      setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
753      return -1;
754    }
755
756    /**
757     * Add a new layer set name, data
758     */
759    layerObj* myLayer=NULL;
760    if(getMapArray(output->content,"msInclude",imyIndex)==NULL){
761      if(msGrowMapLayers(m)==NULL){
762        return -1;
763      }
764      if(initLayer((m->layers[m->numlayers]), m) == -1){
765        return -1;
766      }
767      myLayer=m->layers[m->numlayers];
768    }else{
769      myLayer=m->layers[m->numlayers-1];
770    }
771   
772#ifdef DEBUGMS
773    dumpMaps(output);
774#endif
775    myLayer->name = zStrdup(output->name);
776    myLayer->tileitem=NULL;
777    myLayer->data = zStrdup(OGR_L_GetName(poLayer));
778    myLayer->connection = zStrdup(pszDataSource);
779    myLayer->index = m->numlayers;
780    myLayer->dump = MS_TRUE;
781    myLayer->status = MS_ON;
782    msConnectLayer(myLayer,MS_OGR,pszDataSource);
783
784    addIntToMapArray(output->content,"nb_features",imyIndex,OGR_L_GetFeatureCount(poLayer,1));
785
786    /**
787     * Detect the Geometry Type or use Polygon
788     */
789    if(OGR_L_GetGeomType(poLayer) != wkbUnknown){
790      switch(OGR_L_GetGeomType(poLayer)){
791      case wkbPoint:
792      case wkbMultiPoint:
793      case wkbPoint25D:
794      case wkbMultiPoint25D:
795#ifdef DEBUGMS
796        fprintf(stderr,"POINT DataSource Layer \n");
797#endif
798        myLayer->type = MS_LAYER_POINT;
799        break;
800      case wkbLineString :
801      case wkbMultiLineString :
802      case wkbLineString25D:
803      case wkbMultiLineString25D:
804#ifdef DEBUGMS
805        fprintf(stderr,"LINE DataSource Layer \n");
806#endif
807        myLayer->type = MS_LAYER_LINE;
808        break;
809      case wkbPolygon:
810      case wkbMultiPolygon:
811      case wkbPolygon25D:
812      case wkbMultiPolygon25D:
813#ifdef DEBUGMS
814        fprintf(stderr,"POLYGON DataSource Layer \n");
815#endif
816        myLayer->type = MS_LAYER_POLYGON;
817        break;
818      default:
819        myLayer->type = MS_LAYER_POLYGON;
820        break;
821      }
822    }else
823      myLayer->type = MS_LAYER_POLYGON;
824
825    /**
826     * Detect spatial reference or use WGS84
827     */
828    OGRSpatialReferenceH srs=OGR_L_GetSpatialRef(poLayer);
829    if(srs!=NULL){
830      char *wkt=NULL;
831      OSRExportToWkt(srs,&wkt);
832      setSrsInformations(output,m,myLayer,wkt);
833      free(wkt);
834    }
835    else{
836      setMapArray(output->content,"crs",imyIndex,"EPSG:4326");
837      setMapArray(output->content,"crs_isGeographic",imyIndex,"true");
838      msLoadProjectionStringEPSG(&m->projection,"EPSG:4326");
839      msInsertHashTable(&(m->web.metadata), "ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
840      msInsertHashTable(&(myLayer->metadata), "ows_srs", "EPSG:4326 EPSG:900913 EPSG:3857");
841    }
842
843    OGREnvelope oExt;
844    if (OGR_L_GetExtent(poLayer,&oExt, TRUE) == OGRERR_NONE){
845      setMsExtent(output,m,myLayer,oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
846      char extent[1024];
847      memset(&extent,0,1024);
848      sprintf(extent,"%d,%d,%d,%d",oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
849      setMapArray(output->content,"boundingbox",imyIndex,extent);
850    }
851 
852    /**
853     * Detect the FID column or use the first attribute field as FID
854     */
855    char *fid=(char*)OGR_L_GetFIDColumn(poLayer);
856    if(strlen(fid)==0){
857      OGRFeatureDefnH def=OGR_L_GetLayerDefn(poLayer);
858      int fIndex=0;
859      for(fIndex=0;fIndex<OGR_FD_GetFieldCount(def);fIndex++){
860        OGRFieldDefnH fdef=OGR_FD_GetFieldDefn(def,fIndex);
861        fid=(char*)OGR_Fld_GetNameRef(fdef);
862        break;
863      }
864    }
865    msInsertHashTable(&(myLayer->metadata), "gml_featureid", fid);
866    msInsertHashTable(&(myLayer->metadata), "gml_include_items", "all");
867    msInsertHashTable(&(myLayer->metadata), "ows_name", output->name);
868    map* tmpMap=getMapArray(output->content,"title",imyIndex);
869    if(tmpMap!=NULL)
870      msInsertHashTable(&(myLayer->metadata), "ows_title", tmpMap->value);
871    else
872      msInsertHashTable(&(myLayer->metadata), "ows_title", "Default Title");
873
874    if(getMapArray(output->content,"msInclude",imyIndex)==NULL){
875      if(msGrowLayerClasses(myLayer) == NULL)
876        return -1;
877      if(initClass((myLayer->CLASS[myLayer->numclasses])) == -1)
878        return -1;
879      if(msGrowClassStyles(myLayer->CLASS[myLayer->numclasses]) == NULL)
880        return -1;
881      if(initStyle(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]) == -1)
882        return -1;
883      /**
884       * Apply msStyle else fallback to the default style
885       */
886      tmpMap=getMap(output->content,"msStyle");
887      if(tmpMap!=NULL)
888        msUpdateStyleFromString(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles],tmpMap->value,0);
889      else{
890        /**
891         * Set style
892         */
893        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.red=125;
894        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.green=125;
895        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.blue=255;
896        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.red=80;
897        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.green=80;
898        myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.blue=80;
899       
900        /**
901         * Set specific style depending on type
902         */
903        if(myLayer->type == MS_LAYER_POLYGON)
904          myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
905        if(myLayer->type == MS_LAYER_LINE){
906          myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
907          myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinewidth=1.5;
908        }
909        if(myLayer->type == MS_LAYER_POINT){
910          myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->symbol=1;
911          myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->size=15;
912        }
913       
914      }
915      myLayer->CLASS[myLayer->numclasses]->numstyles++;
916      myLayer->numclasses++;
917   
918      m->layerorder[m->numlayers] = m->numlayers;
919      m->numlayers++;
920
921    }
922  }
923  OGR_DS_Destroy(poDS);
924  //OGRCleanupAll();
925
926  return 1;
927}
928
929/**
930 * Try to open a raster output and define the corresponding layer in the MAPFILE
931 *
932 * @param conf the conf maps containing the main.cfg settings
933 * @param output the specific output maps
934 * @param m the mapObj
935 */
936int tryGdal(maps* conf,maps* output,mapObj* m){
937  int imyIndex=getPublishedId(output);
938  map* tmpMap=getMapArray(output->content,"storage",imyIndex);
939  char *pszFilename=tmpMap->value;
940  GDALDatasetH hDataset;
941  GDALRasterBandH hBand;
942  double adfGeoTransform[6];
943  int i, iBand;
944 
945  /**
946   * Try to open the DataSource using GDAL
947   */
948  GDALAllRegister();
949  hDataset = GDALOpen( pszFilename, GA_Update );
950  if( hDataset == NULL ){
951#ifdef DEBUGMS
952    fprintf(stderr,"Unable to access the DataSource %s \n",pszFilename);
953#endif
954    setMapArray(output->content,"geodatatype",imyIndex,"other");
955    setMapInMaps(conf,"lenv","message","gdalinfo failed - unable to open");
956    GDALDestroyDriverManager();
957    return -1;
958  }
959#ifdef DEBUGMS
960  fprintf(stderr,"Accessing the DataSource %s %d\n",pszFilename,__LINE__);
961#endif
962
963  setMapArray(output->content,"geodatatype",imyIndex,"raster");
964  /**
965   * Add a new layer set name, data
966   */
967  if(msGrowMapLayers(m)==NULL){
968    return -1;
969  }
970  if(initLayer((m->layers[m->numlayers]), m) == -1){
971    return -1;
972  }
973  m->layers[m->numlayers]->index=m->numlayers;
974
975  layerObj* myLayer=m->layers[m->numlayers];
976  myLayer->name = zStrdup(output->name);
977  myLayer->tileitem=NULL;
978  myLayer->data = zStrdup(pszFilename);
979  myLayer->index = m->numlayers;
980  myLayer->dump = MS_TRUE;
981  myLayer->status = MS_ON;
982  myLayer->type = MS_LAYER_RASTER;
983
984  char *title=output->name;
985  tmpMap=getMapArray(output->content,"title",imyIndex);
986  if(tmpMap!=NULL)
987    title=tmpMap->value;
988  char *abstract=output->name;
989  tmpMap=getMapArray(output->content,"abstract",imyIndex);
990  if(tmpMap!=NULL)
991    abstract=tmpMap->value;
992
993  msInsertHashTable(&(myLayer->metadata), "ows_label", title);
994  msInsertHashTable(&(myLayer->metadata), "ows_title", title);
995  msInsertHashTable(&(myLayer->metadata), "ows_abstract", abstract);
996  msInsertHashTable(&(myLayer->metadata), "ows_rangeset_name", output->name);
997  msInsertHashTable(&(myLayer->metadata), "ows_rangeset_label", title);
998
999  /**
1000   * Set Map Size to the raster size
1001   */
1002  m->width=GDALGetRasterXSize( hDataset );
1003  m->height=GDALGetRasterYSize( hDataset );
1004  if(m->width>4096 || m->height>4096){
1005    if(m->width>m->height)
1006      m->maxsize=m->width;
1007    else 
1008      m->maxsize=m->height;
1009  }else
1010    m->maxsize=4096;
1011  addIntToMapArray(output->content,"nb_pixels",imyIndex,GDALGetRasterXSize( hDataset )*GDALGetRasterYSize( hDataset ));
1012  int pixel_type=GDALGetRasterDataType( hDataset );
1013  addIntToMapArray(output->content,"pixel_data_type",imyIndex,pixel_type);
1014
1015  int outputIndex=msGetOutputFormatIndex(m,"tiff");
1016  if(outputIndex>=0){
1017    m->outputformatlist[outputIndex]->imagemode=((pixel_type==GDT_Byte)?MS_IMAGEMODE_BYTE:((pixel_type==GDT_Int16 || pixel_type==GDT_UInt16)?MS_IMAGEMODE_INT16:((pixel_type!=GDT_Float32)?MS_IMAGEMODE_FLOAT32:MS_IMAGEMODE_BYTE)));
1018    outputIndex=msGetOutputFormatIndex(m,"geotiff");
1019    m->outputformatlist[outputIndex]->imagemode=((pixel_type==GDT_Byte)?MS_IMAGEMODE_BYTE:((pixel_type==GDT_Int16 || pixel_type==GDT_UInt16)?MS_IMAGEMODE_INT16:((pixel_type!=GDT_Float32)?MS_IMAGEMODE_FLOAT32:MS_IMAGEMODE_BYTE)));   
1020  }
1021  //
1022   
1023  /**
1024   * Set projection using Authority Code and Name if available or fallback to
1025   * proj4 definition if available or fallback to default EPSG:4326
1026   */
1027  const char *tRef=GDALGetProjectionRef( hDataset );
1028  if( tRef != NULL && strlen(tRef)>0 ){
1029    char *pszProjection;
1030    pszProjection = (char *) GDALGetProjectionRef( hDataset );
1031#ifdef DEBUGMS
1032    fprintf(stderr,"Accessing the DataSource %s\n",GDALGetProjectionRef( hDataset ));
1033#endif
1034    setSrsInformations(output,m,myLayer,pszProjection);
1035  }else{
1036    fprintf(stderr,"NO SRS FOUND ! %s\n",GDALGetProjectionRef( hDataset ));
1037    CPLErr sp=GDALSetProjection( hDataset , "+init=epsg:4326" );
1038    if(sp!=CE_None){
1039      fprintf(stderr,"NO SRS FOUND ! %s\n",CPLGetLastErrorMsg());
1040    }
1041  }
1042
1043  /**
1044   * Set extent
1045   */
1046  if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ){
1047    if( adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0 ){
1048
1049      double minX = adfGeoTransform[0]
1050        + adfGeoTransform[2] * GDALGetRasterYSize(hDataset);
1051      double minY = adfGeoTransform[3]
1052        + adfGeoTransform[5] * GDALGetRasterYSize(hDataset);
1053
1054      double maxX = adfGeoTransform[0]
1055        + adfGeoTransform[1] * GDALGetRasterXSize(hDataset);
1056      double maxY = adfGeoTransform[3]
1057        + adfGeoTransform[4] * GDALGetRasterXSize(hDataset);
1058
1059      setMsExtent(output,m,myLayer,minX,minY,maxX,maxY);
1060      char extent[1024];
1061      memset(&extent,0,1024);
1062      sprintf(extent,"%d,%d,%d,%d",minX,minY,maxX,maxY);
1063      setMapArray(output->content,"boundingbox",imyIndex,extent);
1064    }
1065  }else{
1066    int scale=1;
1067    if(m->width>2048){
1068      addIntToMapArray(output->content,"width",imyIndex,2048);
1069      scale=2048/m->width;
1070    }else
1071      addIntToMapArray(output->content,"width",imyIndex,m->width);
1072    addIntToMapArray(output->content,"height",imyIndex,m->height*scale);
1073  }
1074
1075  /**
1076   * Extract information about available bands to set the bandcount and the
1077   * processing directive
1078   */
1079  char nBands[3];
1080  memset(&nBands,0,3);
1081  int nBandsI=GDALGetRasterCount( hDataset );
1082  if(nBandsI<100){
1083    sprintf(nBands,"%d",GDALGetRasterCount( hDataset ));
1084    msInsertHashTable(&(myLayer->metadata), "ows_bandcount", nBands);
1085  }
1086  if(nBandsI>=3)
1087    if(nBandsI==4)
1088      msLayerAddProcessing(myLayer,"BANDS=1,2,3,4");
1089    else
1090      msLayerAddProcessing(myLayer,"BANDS=1,2,3");
1091  else if(nBandsI>=2)
1092    msLayerAddProcessing(myLayer,"BANDS=1,2");
1093  else
1094    msLayerAddProcessing(myLayer,"BANDS=1");
1095 
1096
1097  /**
1098   * Name available Bands
1099   */
1100  char lBands[7];
1101  char *nameBands=NULL;
1102  for( iBand = 0; iBand < nBandsI; iBand++ ){
1103    memset(&lBands,0,7);
1104    sprintf(lBands,"Band%d",iBand+1);   
1105    if(nameBands==NULL){
1106      nameBands=(char*)malloc((strlen(lBands)+1)*sizeof(char));
1107      sprintf(nameBands,"%s",lBands);
1108    }else{
1109      /*if(iBand<4)*/{
1110        char *tmpS=zStrdup(nameBands);
1111        nameBands=(char*)realloc(nameBands,(strlen(tmpS)+strlen(lBands)+2)*sizeof(char));
1112        sprintf(nameBands,"%s %s",tmpS,lBands);
1113        free(tmpS);
1114      }
1115    }
1116  }
1117  if(nameBands!=NULL){
1118    msInsertHashTable(&(myLayer->metadata), "ows_bandnames", nameBands);
1119    free(nameBands);
1120  }
1121
1122  /**
1123   * Loops over metadata information to setup specific information
1124   */
1125  for( iBand = 0; iBand < nBandsI; iBand++ ){
1126    double      pdfMin, pdfMax, pdfMean, pdfStdDev;
1127    hBand = GDALGetRasterBand( hDataset, iBand+1 );
1128
1129    CPLErrorReset();
1130    GDALComputeRasterStatistics( hBand, TRUE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev, NULL,NULL);
1131    char tmpN[21];
1132    sprintf(tmpN,"Band%d",iBand+1);
1133    if (CPLGetLastErrorType() == CE_None){
1134      char tmpMm[100],tmpMp[100];
1135      sprintf(tmpMm,"%.3f %.3f",pdfMin,pdfMax);
1136      sprintf(tmpMp,"SCALE_%d=%.3f,%.3f",iBand+1,pdfMean-(2*pdfStdDev),pdfMean+(2*pdfStdDev));
1137      char tmpI[31];     
1138      sprintf(tmpI,"%s_interval",tmpN);
1139      msInsertHashTable(&(myLayer->metadata), tmpI, tmpMm);
1140      if(pdfMax>255)
1141        msLayerAddProcessing(myLayer,tmpMp);
1142      map* test=getMap(output->content,"msClassify");
1143      if(test!=NULL && strncasecmp(test->value,"true",4)==0){
1144        /**
1145         * Classify one band raster pixel value using regular interval
1146         */
1147        int _tmpColors[10][3]={
1148          {102,153,204},
1149          {51,102,153},
1150          {102,102,204},
1151          {51,204,0},
1152          {153,255,102},
1153          {204,255,102},
1154          {102,204,153},
1155          {255,69,64},
1156          {255,192,115},
1157          {255,201,115}
1158        };
1159         
1160        if(nBandsI==1){
1161          double delta=pdfMax-pdfMin;
1162          double interval=delta/10;
1163          double cstep=pdfMin;
1164          for(i=0;i<10;i++){
1165            /**
1166             * Create a new class
1167             */
1168            if(msGrowLayerClasses(myLayer) == NULL)
1169              return -1;
1170            if(initClass((myLayer->CLASS[myLayer->numclasses])) == -1)
1171              return -1;
1172            if(msGrowClassStyles(myLayer->CLASS[myLayer->numclasses]) == NULL)
1173              return -1;
1174            if(initStyle(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]) == -1)
1175              return -1;
1176           
1177            /**
1178             * Set class name
1179             */
1180            char className[7];
1181            sprintf(className,"class%d",i);
1182            myLayer->CLASS[myLayer->numclasses]->name=zStrdup(className);
1183           
1184            /**
1185             * Set expression
1186             */
1187            char expression[1024];
1188            if(i+1<10)
1189              sprintf(expression,"([pixel]>=%.3f AND [pixel]<%.3f)",cstep,cstep+interval);
1190            else
1191              sprintf(expression,"([pixel]>=%.3f AND [pixel]<=%.3f)",cstep,cstep+interval);
1192            msLoadExpressionString(&myLayer->CLASS[myLayer->numclasses]->expression,expression);
1193           
1194            /**
1195             * Set color
1196             */
1197            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.red=_tmpColors[i][0];
1198            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.green=_tmpColors[i][1];
1199            myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.blue=_tmpColors[i][2];
1200            cstep+=interval;
1201            myLayer->CLASS[myLayer->numclasses]->numstyles++;
1202            myLayer->numclasses++;
1203           
1204          }
1205         
1206          char tmpMm[100];
1207          sprintf(tmpMm,"%.3f %.3f",pdfMin,pdfMax);
1208         
1209        }
1210      }
1211      else{
1212        if(nBandsI==1){
1213          myLayer->offsite.red=0;
1214          myLayer->offsite.green=0;
1215          myLayer->offsite.blue=0;
1216        }
1217      }
1218    }
1219    if( strlen(GDALGetRasterUnitType(hBand)) > 0 ){
1220      char tmpU[31];
1221      sprintf(tmpU,"%s_band_uom",tmpN);
1222      msInsertHashTable(&(myLayer->metadata), tmpU, GDALGetRasterUnitType(hBand));
1223    }
1224
1225  }
1226  msLayerAddProcessing(myLayer,"RESAMPLE=BILINEAR");
1227
1228  m->layerorder[m->numlayers] = m->numlayers;
1229  m->numlayers++;
1230  GDALClose( hDataset );
1231  GDALDestroyDriverManager();
1232  CPLCleanupTLS();
1233  return 1;
1234}
1235
1236/**
1237 * Create a MapFile for WMS, WFS or WCS Service output
1238 *
1239 * @param conf the conf maps containing the main.cfg settings
1240 * @param outputs a specific output maps
1241 */
1242void outputMapfile(maps* conf,maps* outputs){
1243  /**
1244   * First store the value on disk
1245   */
1246  int imyIndex=getPublishedId(outputs);
1247  map* mime=getMapArray(outputs->content,"mimeType",imyIndex);
1248  char *ext="data";
1249  if(mime!=NULL)
1250    if(strncasecmp(mime->value,"application/json",16)==0)
1251      ext="json";
1252
1253  map* storage=getMapArray(outputs->content,"storage",imyIndex);
1254  if(storage==NULL){
1255    map* tmpMap=getMapFromMaps(conf,"main","dataPath");
1256    map* sidMap=getMapFromMaps(conf,"lenv","usid");
1257    char *pszDataSource=(char*)malloc((strlen(tmpMap->value)+strlen(sidMap->value)+strlen(outputs->name)+17)*sizeof(char));
1258    sprintf(pszDataSource,"%s/ZOO_DATA_%d_%s_%s.%s",tmpMap->value,imyIndex,outputs->name,sidMap->value,ext);
1259    int f=zOpen(pszDataSource,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
1260    map *gfile=getMapArray(outputs->content,"generated_file",imyIndex);
1261    if(gfile!=NULL){
1262      readGeneratedFile(conf,outputs->content,gfile->value);       
1263    }
1264    map* sizeMap=getMapArray(outputs->content,"size",imyIndex);
1265    map* vData=getMapArray(outputs->content,"value",imyIndex);
1266    if(sizeMap!=NULL){
1267      zWrite(f,vData->value,atoi(sizeMap->value)*sizeof(char));
1268    }
1269    else{
1270      zWrite(f,vData->value,(strlen(vData->value)+1)*sizeof(char));
1271    }
1272    close(f);
1273    setMapArray(outputs->content,"storage",imyIndex,pszDataSource);
1274    free(pszDataSource);
1275  }
1276
1277  /*
1278   * Create an empty map, set name, default size and extent
1279   */
1280  map* mapfileTemplate=getMapArray(outputs->content,"msInclude",imyIndex);
1281  mapObj *myMap=NULL;
1282  if(mapfileTemplate==NULL){
1283    myMap=msNewMapObj();
1284  }
1285  else{
1286    map* dataPath=getMapFromMaps(conf,"main","dataPath");
1287    map* sid=getMapFromMaps(conf,"lenv","sid");
1288    char *mapfileTemplatePath=(char*)malloc(((strlen(dataPath->value)+strlen(sid->value)+strlen(outputs->name)+10)*sizeof(char)));
1289    sprintf(mapfileTemplatePath,"%s/%s_%s.map",dataPath->value,outputs->name,sid->value);
1290    myMap=msLoadMap(mapfileTemplate->value,mapfileTemplatePath);
1291    if(myMap==NULL){
1292      setMapInMaps(conf,"lenv","message",_("Unable to open your template mapfile!"));
1293      return ;
1294    }
1295  }
1296  free(myMap->name);
1297  myMap->name=zStrdup("ZOO-Project_WXS_Server");
1298  msMapSetSize(myMap,2048,2048);
1299  msMapSetExtent(myMap,-1,-1,1,1);
1300 
1301  /*
1302   * Set imagepath and imageurl using tmpPath and tmpUrl from main.cfg
1303   */
1304  map *tmp1=getMapFromMaps(conf,"main","tmpPath");
1305  myMap->web.imagepath=zStrdup(tmp1->value);
1306  tmp1=getMapFromMaps(conf,"main","tmpUrl");
1307  myMap->web.imageurl=zStrdup(tmp1->value);
1308 
1309  /*
1310   * Define supported output formats
1311   */
1312  outputFormatObj *o1=msCreateDefaultOutputFormat(NULL,"AGG/PNG","png");
1313  o1->imagemode=MS_IMAGEMODE_RGBA;
1314  o1->transparent=MS_TRUE;
1315  o1->inmapfile=MS_TRUE;
1316  msAppendOutputFormat(myMap,msCloneOutputFormat(o1));
1317  msFreeOutputFormat(o1);
1318
1319#ifdef USE_KML
1320  outputFormatObj *o2=msCreateDefaultOutputFormat(NULL,"KML","kml");
1321  if(!o2){
1322    perror("Unable to initialize KML driver");
1323    fprintf(stderr,"Unable to initialize KML driver !\n");
1324  }else{
1325    o2->inmapfile=MS_TRUE; 
1326    msAppendOutputFormat(myMap,msCloneOutputFormat(o2));
1327    msFreeOutputFormat(o2);
1328  }
1329#endif
1330
1331  outputFormatObj *o3=msCreateDefaultOutputFormat(NULL,"GDAL/GTiff","tiff");
1332  if(!o3)
1333    fprintf(stderr,"Unable to initialize GDAL driver !\n");
1334  else{
1335    o3->imagemode=MS_IMAGEMODE_INT16;
1336    o3->inmapfile=MS_TRUE; 
1337    msAppendOutputFormat(myMap,msCloneOutputFormat(o3));
1338    msFreeOutputFormat(o3);
1339  }
1340
1341  outputFormatObj *o4=msCreateDefaultOutputFormat(NULL,"GDAL/AAIGRID","grd");
1342  if(!o4)
1343    fprintf(stderr,"Unable to initialize GDAL driver !\n");
1344  else{
1345    o4->imagemode=MS_IMAGEMODE_BYTE;
1346    o4->inmapfile=MS_TRUE; 
1347    msAppendOutputFormat(myMap,msCloneOutputFormat(o4));
1348    msFreeOutputFormat(o4);
1349  }
1350
1351#ifdef USE_CAIRO
1352  outputFormatObj *o5=msCreateDefaultOutputFormat(NULL,"CAIRO/PNG","cairopng");
1353  if(!o5)
1354    fprintf(stderr,"Unable to initialize CAIRO driver !\n");
1355  else{
1356    o5->imagemode=MS_IMAGEMODE_RGBA;
1357    o5->transparent=MS_TRUE;
1358    o5->inmapfile=MS_TRUE;
1359    msAppendOutputFormat(myMap,msCloneOutputFormat(o5));
1360    msFreeOutputFormat(o5);
1361  }
1362#endif
1363
1364 
1365  outputFormatObj *o6=msCreateDefaultOutputFormat(NULL,"GDAL/GTiff","geotiff");
1366  if(!o6)
1367    fprintf(stderr,"Unable to initialize GDAL driver !\n");
1368  else{
1369    o6->imagemode=MS_IMAGEMODE_BYTE;
1370    o6->mimetype=strdup("image/geotiff");
1371    o6->inmapfile=MS_TRUE;
1372    msAppendOutputFormat(myMap,msCloneOutputFormat(o6));
1373    msFreeOutputFormat(o6);
1374  }
1375
1376 
1377  /*
1378   * Set default projection to EPSG:4326
1379   */
1380  msLoadProjectionStringEPSG(&myMap->projection,"EPSG:4326");
1381  myMap->transparent=1;
1382
1383  /**
1384   * Set metadata extracted from main.cfg file maps
1385   */
1386  maps* cursor=conf;
1387  map* correspondance=getCorrespondance();
1388  while(cursor!=NULL){
1389    map* _cursor=cursor->content;
1390    map* vMap;
1391    while(_cursor!=NULL){
1392      if((vMap=getMap(correspondance,_cursor->name))!=NULL){
1393        if (msInsertHashTable(&(myMap->web.metadata), vMap->value, _cursor->value) == NULL){
1394#ifdef DEBUGMS
1395          fprintf(stderr,"Unable to add metadata");
1396#endif
1397          return;
1398        }
1399      }
1400      _cursor=_cursor->next;
1401    }
1402    cursor=cursor->next;
1403  }
1404  freeMap(&correspondance);
1405  free(correspondance);
1406
1407  /*
1408   * Set mapserver PROJ_LIB/GDAL_DATA or any other config parameter from
1409   * the main.cfg [mapserver] section if any
1410   */
1411  maps *tmp3=getMaps(conf,"mapserver");
1412  if(tmp3!=NULL){
1413    map* tmp4=tmp3->content;
1414    while(tmp4!=NULL){
1415      msSetConfigOption(myMap,tmp4->name,tmp4->value);
1416      tmp4=tmp4->next;
1417    }
1418  }
1419
1420  /**
1421   * Set a ows_rootlayer_title, 
1422   */
1423  if (msInsertHashTable(&(myMap->web.metadata), "ows_rootlayer_name", "ZOO_Project_Layer") == NULL){
1424#ifdef DEBUGMS
1425    fprintf(stderr,"Unable to add metadata");
1426#endif
1427    return;
1428  }
1429  if (msInsertHashTable(&(myMap->web.metadata), "ows_rootlayer_title", "ZOO_Project_Layer") == NULL){
1430#ifdef DEBUGMS
1431    fprintf(stderr,"Unable to add metadata");
1432#endif
1433    return;
1434  }
1435
1436  /**
1437   * Enable all the WXS requests using ows_enable_request
1438   * see http://mapserver.org/trunk/development/rfc/ms-rfc-67.html
1439   */
1440  if (msInsertHashTable(&(myMap->web.metadata), "ows_enable_request", "*") == NULL){
1441#ifdef DEBUGMS
1442    fprintf(stderr,"Unable to add metadata");
1443#endif
1444    return;
1445  }
1446  msInsertHashTable(&(myMap->web.metadata), "ows_srs", "EPSG:4326");
1447
1448  if(tryOgr(conf,outputs,myMap)<0)
1449    if(tryGdal(conf,outputs,myMap)<0)
1450      return ;
1451
1452  tmp1=getMapFromMaps(conf,"main","dataPath");
1453  char *tmpPath=(char*)malloc((13+strlen(tmp1->value))*sizeof(char));
1454  sprintf(tmpPath,"%s/symbols.sym",tmp1->value);
1455  msInitSymbolSet(&myMap->symbolset);
1456  myMap->symbolset.filename=zStrdup(tmpPath);
1457  free(tmpPath);
1458
1459  map* sid=getMapFromMaps(conf,"lenv","usid");
1460  char *mapPath=
1461    (char*)malloc((14+strlen(sid->value)+strlen(outputs->name)+strlen(tmp1->value))*sizeof(char));
1462  sprintf(mapPath,"%s/%s_%d_%s.map",tmp1->value,outputs->name,imyIndex,sid->value);
1463  msSaveMap(myMap,mapPath);
1464  saveMapNames(conf,outputs,mapPath);
1465  free(mapPath);
1466  //free(myMap->symbolset.filename);
1467  //msFreeSymbolSet(&myMap->symbolset);
1468  msFreeMap(myMap);
1469  //msFree(myMap);
1470  msGDALCleanup();
1471}
1472
1473/**
1474 * Save the map fullpath in a text file (.maps)
1475 * @param conf the main configuration map pointer
1476 * @param output the current output for which a mapfile has been generated
1477 * @param mapfile the mapfile saved to store in the text file
1478 */
1479void saveMapNames(maps* conf,maps* output,char* mapfile){
1480  map* storage=getMap(output->content,"storage");
1481  char *tmp=zStrdup(storage->value);
1482  tmp[strlen(tmp)-strlen(strrchr(tmp,'.'))]=0;
1483  char* mapName=(char*)malloc((strlen(tmp)+6)*sizeof(char*));
1484  sprintf(mapName,"%s.maps",tmp);
1485  FILE* myMaps=fopen(mapName,"a");
1486  if(myMaps!=NULL){
1487    fprintf(myMaps,"%s\n",mapfile);
1488    fclose(myMaps);
1489  } 
1490}
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