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

Last change on this file since 975 was 961, checked in by djay, 4 years ago

Make sure not to try accessing mimetype and provenance in case a local file is used. Fix issue with addMapsToMaps. Produce value string only when memory=load for Python support. Make gdal/profile service usable in case memory=protect.

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