source: trunk/zoo-project/zoo-services/gdal/profile/service.c

Last change on this file was 980, checked in by djay, 4 years ago

Remove wrong tag and set 1.8.0 version in configure.ac and Makefile.in

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 7.3 KB
Line 
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
39extern "C" {
40#endif
41
42#ifdef WIN32
43__declspec(dllexport)
44#endif
45#ifdef ZOO_SERVICE
46int GdalExtractProfile(maps*& conf,maps*& inputs,maps*& outputs)
47#else
48int 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 );
67  free(pszFilename);
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) )
87          GDALComputeRasterMinMax( hBand, TRUE, adfMinMax );
88#ifdef ZOO_SERVICE
89        OGRGeometryH geometry=NULL;
90        // Verify if there is a cache file for this input, use it if available
91        tmp1=getMapFromMaps(inputs,"Geometry","cache_file");
92        if(tmp1!=NULL){
93#if GDAL_VERSION_MAJOR >= 2
94          char* pszDataSource=strdup(tmp1->value);
95          GDALDatasetH poDS
96            = GDALOpenEx( pszDataSource,
97                          GDAL_OF_READONLY | GDAL_OF_VECTOR,
98                          NULL, NULL, NULL );
99#endif
100          for( int iLayer = 0; iLayer < OGR_DS_GetLayerCount(poDS) ;
101               iLayer++ )
102            {
103              OGRLayerH        poLayer = OGR_DS_GetLayer(poDS,iLayer);
104
105              if( poLayer == NULL )
106                {
107                  fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
108                           iLayer );
109                  char tmp[1024];
110                  sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
111                  setMapInMaps(conf,"lenv","message",tmp);
112                  return SERVICE_FAILED;
113                }
114              OGRFeatureH  poFeature;
115              int         nFeaturesInTransaction = 0;
116              int fCount=0;
117              OGR_L_ResetReading(poLayer);
118              while( TRUE )
119                {
120                  poFeature = OGR_L_GetNextFeature(poLayer);
121                  if( poFeature == NULL ){
122                    break;
123                  }
124                  geometry=OGR_G_Clone(OGR_F_GetGeometryRef(poFeature));
125                  OGR_F_Destroy( poFeature );
126                }
127             
128            }
129        }
130        else{
131          tmp1=getMapFromMaps(inputs,"Geometry","value");
132          geometry=OGR_G_CreateGeometryFromJson(tmp1->value);
133        }
134#else
135          OGRGeometryH geometry=OGR_G_CreateGeometryFromJson(argv[2]);
136#endif
137
138          OGR_G_Segmentize(geometry, adfGeoTransform[1]);
139          int nbGeom=OGR_G_GetPointCount(geometry);
140          int k=0;
141          double ppx=0,ppy=0;
142          double value;
143          char *buffer=NULL;
144          int length=0;
145          buffer=(char*)malloc(37*sizeof(char));
146          sprintf(buffer,"{\"type\":\"LineString\",\"coordinates\":[");
147          length+=strlen(buffer);
148          for(k=0;k<nbGeom;k++){
149            //OGRGeometryH point;
150            double prx,pry,prz;
151            OGR_G_GetPoint(geometry,k,&prx,&pry,&prz);
152            float *pafScanline;
153            pafScanline = (float *) CPLMalloc(sizeof(float));
154            int px=(int)floor((prx-adfGeoTransform[0])/adfGeoTransform[1]);
155            int py=(int)floor((pry-adfGeoTransform[3])/adfGeoTransform[5]);
156            if(px!=ppx || py!=ppy){
157              if(GDALRasterIO( hBand, GF_Read, px, py, 1, 1, 
158                            pafScanline, 1, 1, GDT_Float32, 
159                               0, 0 ) != CE_None){
160                char *tmp;
161                tmp=(char*) malloc(300*sizeof(char));
162                sprintf(tmp,"GDALRasterIO failed for point (%d,%d)",px,py);
163                setMapInMaps(conf,"lenv","message",_ss(tmp));
164                CPLFree(pafScanline);
165                free(tmp);
166                return SERVICE_FAILED;
167              }
168              if(buffer!=NULL){
169                int len=strlen(buffer);
170                buffer=(char*)realloc(buffer,(len+50+1)*sizeof(char));
171              }
172              else
173                buffer=(char*)malloc((51)*sizeof(char));
174              char *tmpValue=(char *)malloc(50*sizeof(char));
175              sprintf(tmpValue,"[%.6f,%.6f,%.6f]%c",prx,pry,pafScanline[0],(k+1==nbGeom?' ':','));
176              strncpy(buffer+length,tmpValue,strlen(tmpValue));
177              length+=strlen(tmpValue);
178              buffer[length]=0;
179              value=pafScanline[0];
180              free(tmpValue);
181              //Usefull if we can export 3D JSON string at the end
182              //OGR_G_SetPoint(geometry,k,prx,pry,pafScanline[0]);           
183            }
184            else{
185              if(buffer!=NULL)
186                buffer=(char*)realloc(buffer,(strlen(buffer)+50+1)*sizeof(char));
187              else
188                buffer=(char*)malloc((51)*sizeof(char));
189              char *tmpValue=(char *)malloc(50*sizeof(char));
190              sprintf(tmpValue,"[%.6f,%.6f,%.6f]%c",prx,pry,value,(k+1==nbGeom?' ':','));
191              strncpy(buffer+length,tmpValue,strlen(tmpValue));
192              length+=strlen(tmpValue);
193              buffer[length]=0;
194              free(tmpValue);
195              value=value;
196            }
197            CPLFree(pafScanline);
198            ppx=px;
199            ppy=py;
200          }
201          buffer=(char*)realloc(buffer,(strlen(buffer)+3)*sizeof(char));
202          char *tmpValue=(char *)malloc(3*sizeof(char));
203          sprintf(tmpValue,"]}");
204          tmpValue[2]=0;
205          strncpy(buffer+length,tmpValue,strlen(tmpValue));
206          length+=strlen(tmpValue);
207          buffer[length]=0;
208#ifdef ZOO_SERVICE
209          setMapInMaps(outputs,"Profile","value",buffer);
210          setMapInMaps(outputs,"Profile","mimeType","text/plain");
211#else
212          fprintf(stderr,"%s\n",buffer);
213#endif
214          free(buffer);
215          free(tmpValue);
216          OGR_G_DestroyGeometry(geometry);
217        }
218    }
219  else{
220#ifdef ZOO_SERVICE
221    setMapInMaps(conf,"lenv","message",_ss("Unable to load your raster file !"));
222    return SERVICE_FAILED;
223#else
224    printf("Unable to load your raster file %s !\n",argv[1]);
225#endif
226  }
227  GDALClose(hDataset);
228  OGRCleanupAll();
229  GDALDestroyDriverManager();
230#ifdef ZOO_SERVICE
231  return SERVICE_SUCCEEDED;
232#endif
233}
234
235#ifdef ZOO_SERVICE
236}
237#endif
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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