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

Last change on this file since 491 was 371, checked in by djay, 12 years ago

Make GdalExtractProfile? Service working on windows platforms.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 6.1 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          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
94
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++){
106            //OGRGeometryH point;
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));
121                CPLFree(pafScanline);
122                free(tmp);
123                return SERVICE_FAILED;
124              }
125              if(buffer!=NULL){
126                int len=strlen(buffer);
127                buffer=(char*)realloc(buffer,(len+50+1)*sizeof(char));
128              }
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?' ':','));
133              strncpy(buffer+length,tmpValue,strlen(tmpValue));
134              length+=strlen(tmpValue);
135              buffer[length]=0;
136              value=pafScanline[0];
137              free(tmpValue);
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{
142              if(buffer!=NULL)
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?' ':','));
148              strncpy(buffer+length,tmpValue,strlen(tmpValue));
149              length+=strlen(tmpValue);
150              buffer[length]=0;
151              free(tmpValue);
152              value=value;
153            }
154            CPLFree(pafScanline);
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,"]}");
161          tmpValue[2]=0;
162          strncpy(buffer+length,tmpValue,strlen(tmpValue));
163          length+=strlen(tmpValue);
164          buffer[length]=0;
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
171          free(buffer);
172          free(tmpValue);
173          OGR_G_DestroyGeometry(geometry);
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  }
184  OGRCleanupAll();
185  GDALClose(hDataset);
186  GDALDestroyDriverManager();
187#ifdef ZOO_SERVICE
188  return SERVICE_SUCCEEDED;
189#endif
190}
191
192#ifdef ZOO_SERVICE
193}
194#endif
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