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