[50] | 1 | /* **************************************************************************** |
---|
| 2 | * $Id$ |
---|
| 3 | * |
---|
| 4 | * Project: GdalExtractProfile |
---|
| 5 | * Purpose: Extract Profile from a Raster file for an Input Geometry (LINE) |
---|
| 6 | * Author: Gérald Fenoy, gerald.fenoy@geolabs.fr |
---|
| 7 | * |
---|
| 8 | * **************************************************************************** |
---|
[102] | 9 | * Copyright (c) 2010-2011, GeoLabs SARL |
---|
[50] | 10 | * |
---|
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
| 12 | * copy of this software and associated documentation files (the "Software"), |
---|
| 13 | * to deal in the Software without restriction, including without limitation |
---|
| 14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
| 15 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
| 16 | * Software is furnished to do so, subject to the following conditions: |
---|
| 17 | * |
---|
| 18 | * The above copyright notice and this permission notice shall be included |
---|
| 19 | * in all copies or substantial portions of the Software. |
---|
| 20 | * |
---|
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
| 22 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
| 24 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
| 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 27 | * DEALINGS IN THE SOFTWARE. |
---|
| 28 | ****************************************************************************/ |
---|
| 29 | |
---|
| 30 | #ifdef ZOO_SERVICE |
---|
| 31 | #include "service.h" |
---|
[102] | 32 | #include "service_internal.h" |
---|
[50] | 33 | #endif |
---|
| 34 | #include "gdal.h" |
---|
| 35 | #include "cpl_conv.h" |
---|
| 36 | #include "ogr_api.h" |
---|
| 37 | |
---|
| 38 | #ifdef ZOO_SERVICE |
---|
| 39 | extern "C" { |
---|
| 40 | #endif |
---|
| 41 | |
---|
[371] | 42 | #ifdef WIN32 |
---|
| 43 | __declspec(dllexport) |
---|
| 44 | #endif |
---|
[50] | 45 | #ifdef ZOO_SERVICE |
---|
| 46 | int GdalExtractProfile(maps*& conf,maps*& inputs,maps*& outputs) |
---|
| 47 | #else |
---|
| 48 | int main(int argc,char** argv) |
---|
| 49 | #endif |
---|
| 50 | { |
---|
| 51 | char *pszFilename; |
---|
| 52 | #ifdef ZOO_SERVICE |
---|
| 53 | map* tmp=NULL; |
---|
| 54 | map* tmp1=NULL; |
---|
| 55 | tmp=getMapFromMaps(conf,"main","dataPath"); |
---|
| 56 | tmp1=getMapFromMaps(inputs,"RasterFile","value"); |
---|
| 57 | pszFilename=(char *)malloc((2+strlen(tmp->value)+strlen(tmp1->value))*sizeof(char)); |
---|
| 58 | sprintf(pszFilename,"%s/%s",tmp->value,tmp1->value); |
---|
| 59 | #else |
---|
| 60 | pszFilename=argv[1]; |
---|
| 61 | #endif |
---|
| 62 | GDALDatasetH hDataset; |
---|
| 63 | GDALAllRegister(); |
---|
| 64 | OGRRegisterAll(); |
---|
| 65 | |
---|
| 66 | hDataset = GDALOpen( pszFilename, GA_ReadOnly ); |
---|
[102] | 67 | free(pszFilename); |
---|
[50] | 68 | if( hDataset != NULL ) |
---|
| 69 | { |
---|
| 70 | GDALDriverH hDriver; |
---|
| 71 | double adfGeoTransform[6]; |
---|
| 72 | |
---|
| 73 | if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ) |
---|
| 74 | { |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | GDALRasterBandH hBand; |
---|
| 78 | int nBlockXSize, nBlockYSize; |
---|
| 79 | int bGotMin, bGotMax; |
---|
| 80 | double adfMinMax[2]; |
---|
| 81 | |
---|
| 82 | hBand = GDALGetRasterBand( hDataset, 1 ); |
---|
| 83 | |
---|
| 84 | adfMinMax[0] = GDALGetRasterMinimum( hBand, &bGotMin ); |
---|
| 85 | adfMinMax[1] = GDALGetRasterMaximum( hBand, &bGotMax ); |
---|
| 86 | if( ! (bGotMin && bGotMax) ) |
---|
[371] | 87 | GDALComputeRasterMinMax( hBand, TRUE, adfMinMax ); |
---|
[50] | 88 | #ifdef ZOO_SERVICE |
---|
| 89 | tmp1=getMapFromMaps(inputs,"Geometry","value"); |
---|
| 90 | OGRGeometryH geometry=OGR_G_CreateGeometryFromJson(tmp1->value); |
---|
| 91 | #else |
---|
| 92 | OGRGeometryH geometry=OGR_G_CreateGeometryFromJson(argv[2]); |
---|
| 93 | #endif |
---|
[371] | 94 | |
---|
[50] | 95 | OGR_G_Segmentize(geometry, adfGeoTransform[1]); |
---|
| 96 | int nbGeom=OGR_G_GetPointCount(geometry); |
---|
| 97 | int k=0; |
---|
| 98 | double ppx=0,ppy=0; |
---|
| 99 | double value; |
---|
| 100 | char *buffer=NULL; |
---|
| 101 | int length=0; |
---|
| 102 | buffer=(char*)malloc(37*sizeof(char)); |
---|
| 103 | sprintf(buffer,"{\"type\":\"LineString\",\"coordinates\":["); |
---|
| 104 | length+=strlen(buffer); |
---|
| 105 | for(k=0;k<nbGeom;k++){ |
---|
[102] | 106 | //OGRGeometryH point; |
---|
[50] | 107 | double prx,pry,prz; |
---|
| 108 | OGR_G_GetPoint(geometry,k,&prx,&pry,&prz); |
---|
| 109 | float *pafScanline; |
---|
| 110 | pafScanline = (float *) CPLMalloc(sizeof(float)); |
---|
| 111 | int px=(int)floor((prx-adfGeoTransform[0])/adfGeoTransform[1]); |
---|
| 112 | int py=(int)floor((pry-adfGeoTransform[3])/adfGeoTransform[5]); |
---|
| 113 | if(px!=ppx || py!=ppy){ |
---|
| 114 | if(GDALRasterIO( hBand, GF_Read, px, py, 1, 1, |
---|
| 115 | pafScanline, 1, 1, GDT_Float32, |
---|
| 116 | 0, 0 ) != CE_None){ |
---|
| 117 | char *tmp; |
---|
| 118 | tmp=(char*) malloc(300*sizeof(char)); |
---|
| 119 | sprintf(tmp,"GDALRasterIO failed for point (%d,%d)",px,py); |
---|
| 120 | setMapInMaps(conf,"lenv","message",_ss(tmp)); |
---|
[102] | 121 | CPLFree(pafScanline); |
---|
| 122 | free(tmp); |
---|
[50] | 123 | return SERVICE_FAILED; |
---|
| 124 | } |
---|
[102] | 125 | if(buffer!=NULL){ |
---|
| 126 | int len=strlen(buffer); |
---|
| 127 | buffer=(char*)realloc(buffer,(len+50+1)*sizeof(char)); |
---|
| 128 | } |
---|
[50] | 129 | else |
---|
| 130 | buffer=(char*)malloc((51)*sizeof(char)); |
---|
| 131 | char *tmpValue=(char *)malloc(50*sizeof(char)); |
---|
| 132 | sprintf(tmpValue,"[%.6f,%.6f,%.6f]%c",prx,pry,pafScanline[0],(k+1==nbGeom?' ':',')); |
---|
[102] | 133 | strncpy(buffer+length,tmpValue,strlen(tmpValue)); |
---|
[50] | 134 | length+=strlen(tmpValue); |
---|
[102] | 135 | buffer[length]=0; |
---|
[50] | 136 | value=pafScanline[0]; |
---|
[102] | 137 | free(tmpValue); |
---|
[50] | 138 | //Usefull if we can export 3D JSON string at the end |
---|
| 139 | //OGR_G_SetPoint(geometry,k,prx,pry,pafScanline[0]); |
---|
| 140 | } |
---|
| 141 | else{ |
---|
[102] | 142 | if(buffer!=NULL) |
---|
[50] | 143 | buffer=(char*)realloc(buffer,(strlen(buffer)+50+1)*sizeof(char)); |
---|
| 144 | else |
---|
| 145 | buffer=(char*)malloc((51)*sizeof(char)); |
---|
| 146 | char *tmpValue=(char *)malloc(50*sizeof(char)); |
---|
| 147 | sprintf(tmpValue,"[%.6f,%.6f,%.6f]%c",prx,pry,value,(k+1==nbGeom?' ':',')); |
---|
[102] | 148 | strncpy(buffer+length,tmpValue,strlen(tmpValue)); |
---|
[50] | 149 | length+=strlen(tmpValue); |
---|
[102] | 150 | buffer[length]=0; |
---|
| 151 | free(tmpValue); |
---|
[50] | 152 | value=value; |
---|
| 153 | } |
---|
[102] | 154 | CPLFree(pafScanline); |
---|
[50] | 155 | ppx=px; |
---|
| 156 | ppy=py; |
---|
| 157 | } |
---|
| 158 | buffer=(char*)realloc(buffer,(strlen(buffer)+3)*sizeof(char)); |
---|
| 159 | char *tmpValue=(char *)malloc(3*sizeof(char)); |
---|
| 160 | sprintf(tmpValue,"]}"); |
---|
[102] | 161 | tmpValue[2]=0; |
---|
| 162 | strncpy(buffer+length,tmpValue,strlen(tmpValue)); |
---|
| 163 | length+=strlen(tmpValue); |
---|
| 164 | buffer[length]=0; |
---|
[50] | 165 | #ifdef ZOO_SERVICE |
---|
| 166 | setMapInMaps(outputs,"Profile","value",buffer); |
---|
| 167 | setMapInMaps(outputs,"Profile","mimeType","text/plain"); |
---|
| 168 | #else |
---|
| 169 | fprintf(stderr,"%s\n",buffer); |
---|
| 170 | #endif |
---|
[102] | 171 | free(buffer); |
---|
| 172 | free(tmpValue); |
---|
| 173 | OGR_G_DestroyGeometry(geometry); |
---|
[50] | 174 | } |
---|
| 175 | } |
---|
| 176 | else{ |
---|
| 177 | #ifdef ZOO_SERVICE |
---|
| 178 | setMapInMaps(conf,"lenv","message",_ss("Unable to load your raster file !")); |
---|
| 179 | return SERVICE_FAILED; |
---|
| 180 | #else |
---|
| 181 | printf("Unable to load your raster file %s !\n",argv[1]); |
---|
| 182 | #endif |
---|
| 183 | } |
---|
[102] | 184 | OGRCleanupAll(); |
---|
[50] | 185 | GDALClose(hDataset); |
---|
[102] | 186 | GDALDestroyDriverManager(); |
---|
[50] | 187 | #ifdef ZOO_SERVICE |
---|
| 188 | return SERVICE_SUCCEEDED; |
---|
| 189 | #endif |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | #ifdef ZOO_SERVICE |
---|
| 193 | } |
---|
| 194 | #endif |
---|