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 | * **************************************************************************** |
---|
9 | * Copyright (c) 2010-2011, GeoLabs SARL |
---|
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" |
---|
32 | #include "service_internal.h" |
---|
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 | |
---|
42 | #ifdef ZOO_SERVICE |
---|
43 | int GdalExtractProfile(maps*& conf,maps*& inputs,maps*& outputs) |
---|
44 | #else |
---|
45 | int main(int argc,char** argv) |
---|
46 | #endif |
---|
47 | { |
---|
48 | char *pszFilename; |
---|
49 | #ifdef ZOO_SERVICE |
---|
50 | map* tmp=NULL; |
---|
51 | map* tmp1=NULL; |
---|
52 | tmp=getMapFromMaps(conf,"main","dataPath"); |
---|
53 | tmp1=getMapFromMaps(inputs,"RasterFile","value"); |
---|
54 | pszFilename=(char *)malloc((2+strlen(tmp->value)+strlen(tmp1->value))*sizeof(char)); |
---|
55 | sprintf(pszFilename,"%s/%s",tmp->value,tmp1->value); |
---|
56 | #else |
---|
57 | pszFilename=argv[1]; |
---|
58 | #endif |
---|
59 | GDALDatasetH hDataset; |
---|
60 | GDALAllRegister(); |
---|
61 | OGRRegisterAll(); |
---|
62 | |
---|
63 | hDataset = GDALOpen( pszFilename, GA_ReadOnly ); |
---|
64 | free(pszFilename); |
---|
65 | if( hDataset != NULL ) |
---|
66 | { |
---|
67 | GDALDriverH hDriver; |
---|
68 | double adfGeoTransform[6]; |
---|
69 | |
---|
70 | if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None ) |
---|
71 | { |
---|
72 | |
---|
73 | |
---|
74 | GDALRasterBandH hBand; |
---|
75 | int nBlockXSize, nBlockYSize; |
---|
76 | int bGotMin, bGotMax; |
---|
77 | double adfMinMax[2]; |
---|
78 | |
---|
79 | hBand = GDALGetRasterBand( hDataset, 1 ); |
---|
80 | |
---|
81 | adfMinMax[0] = GDALGetRasterMinimum( hBand, &bGotMin ); |
---|
82 | adfMinMax[1] = GDALGetRasterMaximum( hBand, &bGotMax ); |
---|
83 | if( ! (bGotMin && bGotMax) ) |
---|
84 | GDALComputeRasterMinMax( hBand, TRUE, adfMinMax ); |
---|
85 | |
---|
86 | #ifdef ZOO_SERVICE |
---|
87 | tmp1=getMapFromMaps(inputs,"Geometry","value"); |
---|
88 | OGRGeometryH geometry=OGR_G_CreateGeometryFromJson(tmp1->value); |
---|
89 | #else |
---|
90 | OGRGeometryH geometry=OGR_G_CreateGeometryFromJson(argv[2]); |
---|
91 | #endif |
---|
92 | OGR_G_Segmentize(geometry, adfGeoTransform[1]); |
---|
93 | int nbGeom=OGR_G_GetPointCount(geometry); |
---|
94 | int k=0; |
---|
95 | double ppx=0,ppy=0; |
---|
96 | double value; |
---|
97 | char *buffer=NULL; |
---|
98 | int length=0; |
---|
99 | buffer=(char*)malloc(37*sizeof(char)); |
---|
100 | sprintf(buffer,"{\"type\":\"LineString\",\"coordinates\":["); |
---|
101 | length+=strlen(buffer); |
---|
102 | for(k=0;k<nbGeom;k++){ |
---|
103 | //OGRGeometryH point; |
---|
104 | double prx,pry,prz; |
---|
105 | OGR_G_GetPoint(geometry,k,&prx,&pry,&prz); |
---|
106 | float *pafScanline; |
---|
107 | pafScanline = (float *) CPLMalloc(sizeof(float)); |
---|
108 | int px=(int)floor((prx-adfGeoTransform[0])/adfGeoTransform[1]); |
---|
109 | int py=(int)floor((pry-adfGeoTransform[3])/adfGeoTransform[5]); |
---|
110 | if(px!=ppx || py!=ppy){ |
---|
111 | if(GDALRasterIO( hBand, GF_Read, px, py, 1, 1, |
---|
112 | pafScanline, 1, 1, GDT_Float32, |
---|
113 | 0, 0 ) != CE_None){ |
---|
114 | char *tmp; |
---|
115 | tmp=(char*) malloc(300*sizeof(char)); |
---|
116 | sprintf(tmp,"GDALRasterIO failed for point (%d,%d)",px,py); |
---|
117 | setMapInMaps(conf,"lenv","message",_ss(tmp)); |
---|
118 | CPLFree(pafScanline); |
---|
119 | free(tmp); |
---|
120 | return SERVICE_FAILED; |
---|
121 | } |
---|
122 | if(buffer!=NULL){ |
---|
123 | int len=strlen(buffer); |
---|
124 | buffer=(char*)realloc(buffer,(len+50+1)*sizeof(char)); |
---|
125 | } |
---|
126 | else |
---|
127 | buffer=(char*)malloc((51)*sizeof(char)); |
---|
128 | char *tmpValue=(char *)malloc(50*sizeof(char)); |
---|
129 | sprintf(tmpValue,"[%.6f,%.6f,%.6f]%c",prx,pry,pafScanline[0],(k+1==nbGeom?' ':',')); |
---|
130 | strncpy(buffer+length,tmpValue,strlen(tmpValue)); |
---|
131 | length+=strlen(tmpValue); |
---|
132 | buffer[length]=0; |
---|
133 | value=pafScanline[0]; |
---|
134 | free(tmpValue); |
---|
135 | //Usefull if we can export 3D JSON string at the end |
---|
136 | //OGR_G_SetPoint(geometry,k,prx,pry,pafScanline[0]); |
---|
137 | } |
---|
138 | else{ |
---|
139 | if(buffer!=NULL) |
---|
140 | buffer=(char*)realloc(buffer,(strlen(buffer)+50+1)*sizeof(char)); |
---|
141 | else |
---|
142 | buffer=(char*)malloc((51)*sizeof(char)); |
---|
143 | char *tmpValue=(char *)malloc(50*sizeof(char)); |
---|
144 | sprintf(tmpValue,"[%.6f,%.6f,%.6f]%c",prx,pry,value,(k+1==nbGeom?' ':',')); |
---|
145 | strncpy(buffer+length,tmpValue,strlen(tmpValue)); |
---|
146 | length+=strlen(tmpValue); |
---|
147 | buffer[length]=0; |
---|
148 | free(tmpValue); |
---|
149 | value=value; |
---|
150 | } |
---|
151 | CPLFree(pafScanline); |
---|
152 | ppx=px; |
---|
153 | ppy=py; |
---|
154 | } |
---|
155 | buffer=(char*)realloc(buffer,(strlen(buffer)+3)*sizeof(char)); |
---|
156 | char *tmpValue=(char *)malloc(3*sizeof(char)); |
---|
157 | sprintf(tmpValue,"]}"); |
---|
158 | tmpValue[2]=0; |
---|
159 | strncpy(buffer+length,tmpValue,strlen(tmpValue)); |
---|
160 | length+=strlen(tmpValue); |
---|
161 | buffer[length]=0; |
---|
162 | #ifdef ZOO_SERVICE |
---|
163 | setMapInMaps(outputs,"Profile","value",buffer); |
---|
164 | setMapInMaps(outputs,"Profile","mimeType","text/plain"); |
---|
165 | #else |
---|
166 | fprintf(stderr,"%s\n",buffer); |
---|
167 | #endif |
---|
168 | free(buffer); |
---|
169 | free(tmpValue); |
---|
170 | OGR_G_DestroyGeometry(geometry); |
---|
171 | } |
---|
172 | } |
---|
173 | else{ |
---|
174 | #ifdef ZOO_SERVICE |
---|
175 | setMapInMaps(conf,"lenv","message",_ss("Unable to load your raster file !")); |
---|
176 | return SERVICE_FAILED; |
---|
177 | #else |
---|
178 | printf("Unable to load your raster file %s !\n",argv[1]); |
---|
179 | #endif |
---|
180 | } |
---|
181 | OGRCleanupAll(); |
---|
182 | GDALClose(hDataset); |
---|
183 | GDALDestroyDriverManager(); |
---|
184 | #ifdef ZOO_SERVICE |
---|
185 | return SERVICE_SUCCEEDED; |
---|
186 | #endif |
---|
187 | } |
---|
188 | |
---|
189 | #ifdef ZOO_SERVICE |
---|
190 | } |
---|
191 | #endif |
---|