source: trunk/zoo-project/zoo-services/ogr/base-vect-ops/service.c @ 984

Last change on this file since 984 was 984, checked in by djay, 3 years ago

Take into account building ZOO-Project on ubuntu focal (cf. https://trac.osgeo.org/osgeolive/ticket/2282). Add the OTB and SAGA support to the Dockerfile to make the corresponding demos using docker WPS server. Add OGC API - Processes demo to the demos list.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 57.1 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright 2008-2009 GeoLabs SARL. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "cpl_conv.h"
26#include "ogr_api.h"
27#include "ogr_geometry.h"
28#if GDAL_VERSION_MAJOR >= 2
29#include <gdal_priv.h>
30#endif
31
32#include "cpl_minixml.h"
33#include "ogr_api.h"
34#include "ogrsf_frmts.h"
35
36#include "geos_c.h"
37#include "service.h"
38#include "service_internal.h"
39
40#include <libxml/tree.h>
41#include <libxml/parser.h>
42#include <libxml/xpath.h>
43#include <libxml/xpathInternals.h>
44
45extern "C" {
46
47  void printExceptionReportResponse(maps*,map*);
48  char *base64(const char *input, int length);
49
50  OGRGeometryH createGeometryFromGML(maps* conf,char* inputStr){
51    xmlInitParser();
52    xmlDocPtr doc = xmlParseMemory(inputStr,strlen(inputStr));
53    xmlChar *xmlbuff;
54    int buffersize;
55    xmlXPathContextPtr xpathCtx;
56    xmlXPathObjectPtr xpathObj;
57    const char * xpathExpr="/*/*/*/*/*[local-name()='Polygon' or local-name()='MultiPolygon']";
58    xpathCtx = xmlXPathNewContext(doc);
59    xpathObj = xmlXPathEvalExpression(BAD_CAST xpathExpr,xpathCtx);
60    if(!xpathObj->nodesetval){
61      setMapInMaps(conf,"lenv","message",_ss("Unable to parse Input Polygon"));
62      setMapInMaps(conf,"lenv","code","InvalidParameterValue");
63      return NULL;
64    }
65    int size = (xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0;
66    /**
67     * Create a temporary XML document
68     */
69    xmlDocPtr ndoc = xmlNewDoc(BAD_CAST "1.0");
70    /**
71     * Only one polygon should be provided so we use it as the root node.
72     */
73    for(int k=size-1;k>=0;k--){ 
74      xmlDocSetRootElement(ndoc, xpathObj->nodesetval->nodeTab[k]);
75    }
76    xmlDocDumpFormatMemory(ndoc, &xmlbuff, &buffersize, 1);
77    char *tmp=(char*)calloc((xmlStrlen(xmlStrstr(xmlbuff,BAD_CAST "?>"))-1),sizeof(char));
78    sprintf(tmp,"%s",xmlStrstr(xmlbuff,BAD_CAST "?>")+2);
79    xmlXPathFreeObject(xpathObj);
80    xmlXPathFreeContext(xpathCtx);
81    xmlFree(xmlbuff);
82    xmlFreeDoc(doc);
83    xmlFreeDoc(ndoc);
84#ifndef WIN32
85    xmlCleanupParser();
86#endif
87#ifdef DEBUG
88    fprintf(stderr,"\nService internal print\n Loading the geometry from GML string ...");
89#endif
90    OGRGeometryH res=OGR_G_CreateFromGML(tmp);
91    free(tmp);
92    if(res==NULL){
93      setMapInMaps(conf,"lenv","message",_ss("Unable to call OGR_G_CreatFromGML"));
94      return NULL;
95    }
96    else
97      return res;
98  }
99
100#ifdef WIN32
101  __declspec(dllexport)
102#endif
103  int Simplify(maps*& conf,maps*& inputs,maps*& outputs){
104    OGRRegisterAll();
105
106    double tolerance;
107    map* tmp0=getMapFromMaps(inputs,"Tolerance","value");
108    if(tmp0==NULL){
109      tolerance=atof("2.0");
110    }
111    else
112      tolerance=atof(tmp0->value);
113
114    maps* cursor=inputs;
115    OGRGeometryH geometry;
116    OGRGeometry *res;
117    OGRLayer *poDstLayer;
118    const char *oDriver1;
119#if GDAL_VERSION_MAJOR >= 2
120    GDALDataset *poODS;
121#else
122    OGRDataSource *poODS;
123#endif
124    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
125    if(!tmp){
126      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
127      return SERVICE_FAILED;
128    }
129    char filename[1024];
130    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
131    const char *oDriver;
132    oDriver="GeoJSON";
133    sprintf(filename,"/vsimem/input_%d.json",getpid());
134    if(tmp1!=NULL){
135      if(strcmp(tmp1->value,"text/xml")==0){
136        sprintf(filename,"/vsimem/input_%d.xml",getpid());
137        oDriver="GML";
138      }
139    }
140    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
141    VSIFCloseL(ifile);
142
143#if GDAL_VERSION_MAJOR >= 2
144      GDALDataset *ipoDS =
145        (GDALDataset*) GDALOpenEx( filename,
146                                   GDAL_OF_READONLY | GDAL_OF_VECTOR,
147                                   NULL, NULL, NULL );
148      GDALDriverManager* poR=GetGDALDriverManager();
149      GDALDriver          *poDriver = NULL;
150#else
151      OGRDataSource* ipoDS = 
152        OGRSFDriverRegistrar::Open(filename,FALSE);
153      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
154      OGRSFDriver          *poDriver = NULL;
155#endif
156    char pszDestDataSource[100];
157    if( ipoDS == NULL )
158      {
159        fprintf( stderr, "FAILURE:\n"
160                 "Unable to open datasource `%s' with the following drivers.\n",
161                 filename );
162       
163        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
164          {
165#if GDAL_VERSION_MAJOR >= 2
166            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
167#else
168            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
169#endif
170          }
171        char tmp[1024];
172        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
173        setMapInMaps(conf,"lenv","message",tmp);
174        return SERVICE_FAILED;
175      }
176    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
177         iLayer++ )
178      {
179        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
180       
181        if( poLayer == NULL )
182          {
183            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
184                     iLayer );
185            char tmp[1024];
186            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
187            setMapInMaps(conf,"lenv","message",tmp);
188            return SERVICE_FAILED;
189          }
190       
191        OGRFeature  *poFeature;
192
193        int                  iDriver;
194       
195        map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
196        oDriver1="GeoJSON";
197        sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
198        if(tmpMap!=NULL){
199          if(strcmp(tmpMap->value,"text/xml")==0){
200            sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
201            oDriver1="GML";
202          }
203        }
204       
205        for( iDriver = 0;
206             iDriver < poR->GetDriverCount() && poDriver == NULL;
207             iDriver++ )
208          {
209#if GDAL_VERSION_MAJOR >=2
210            if( EQUAL(poR->GetDriver(iDriver)->GetDescription(),oDriver1) )
211#else
212            if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver1) )
213#endif
214              {
215                poDriver = poR->GetDriver(iDriver);
216              }
217          }
218       
219        if( poDriver == NULL )
220          {
221            char emessage[8192];
222            sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
223            sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
224           
225            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
226              {
227#if GDAL_VERSION_MAJOR >=2
228                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() );
229#else
230                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
231#endif
232              }
233           
234            setMapInMaps(conf,"lenv","message",emessage);
235            return SERVICE_FAILED;
236           
237          }
238       
239#if GDAL_VERSION_MAJOR >=2
240        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
241#else
242        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
243#endif
244          {
245            char emessage[1024];
246            sprintf( emessage,  "%s driver does not support data source creation.\n",
247              "json" );
248            setMapInMaps(conf,"lenv","message",emessage);
249            return SERVICE_FAILED;
250          }
251       
252        char **papszDSCO=NULL;
253#if GDAL_VERSION_MAJOR >=2
254        poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO );
255#else
256        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
257#endif
258        if( poODS == NULL ){
259          char emessage[1024];     
260          sprintf( emessage,  "%s driver failed to create %s\n", 
261                   "json", pszDestDataSource );
262          setMapInMaps(conf,"lenv","message",emessage);
263          return SERVICE_FAILED;
264        }
265       
266        if( !poODS->TestCapability( ODsCCreateLayer ) )
267          {
268            char emessage[1024];
269            sprintf( emessage, 
270                     "Layer %s not found, and CreateLayer not supported by driver.", 
271                     "Result" );
272            setMapInMaps(conf,"lenv","message",emessage);
273            return SERVICE_FAILED;
274          }
275       
276        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
277        if( poDstLayer == NULL ){
278          setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
279          return SERVICE_FAILED;
280        }
281       
282        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
283        int iField;
284        int hasMmField=0;
285       
286        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
287          {
288            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
289            if (iField >= 0)
290                poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
291            else
292            {
293                fprintf( stderr, "Field '%s' not found in source layer.\n", 
294                        iField );
295                return SERVICE_FAILED;
296            }
297          }
298
299        while(TRUE){
300          OGRFeature      *poDstFeature = NULL;
301          poFeature = poLayer->GetNextFeature();
302          if( poFeature == NULL )
303            break;
304          if(poFeature->GetGeometryRef() != NULL){
305            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
306            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
307              {
308                char tmpMsg[1024];
309                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
310                         poFeature->GetFID(), poFDefn->GetName() );
311               
312                OGRFeature::DestroyFeature( poFeature );
313                OGRFeature::DestroyFeature( poDstFeature );
314                return SERVICE_FAILED;
315              }
316            geometry=poFeature->GetGeometryRef();
317#if GDAL_VERSION_MAJOR == 1 && GDAL_VERSION_MINOR < 11
318            GEOSGeometry* ggeometry=((OGRGeometry *) geometry)->exportToGEOS();
319            GEOSGeometry* gres=GEOSTopologyPreserveSimplify(ggeometry,tolerance);
320            if(gres!=NULL)
321              res=(OGRGeometry*)OGRGeometryFactory::createFromGEOS(gres);
322#else
323            res=((OGRGeometry *) geometry)->SimplifyPreserveTopology(tolerance);
324#endif
325            if(poDstFeature->SetGeometryDirectly(res) != OGRERR_NONE )
326              {
327                char tmpMsg[1024];
328                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
329                         poFeature->GetFID(), poFDefn->GetName() );
330               
331                OGRFeature::DestroyFeature( poFeature );
332                OGRFeature::DestroyFeature( poDstFeature );
333                return SERVICE_FAILED;
334              }
335            OGRFeature::DestroyFeature( poFeature );
336            if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
337              {         
338                OGRFeature::DestroyFeature( poDstFeature );
339                return SERVICE_FAILED;
340              }
341            OGRFeature::DestroyFeature( poDstFeature );
342#if GDAL_VERSION_MAJOR == 1 && GDAL_VERSION_MINOR < 11
343            GEOSGeom_destroy( ggeometry);
344            GEOSGeom_destroy( gres);
345#endif
346          }
347        }
348      }
349
350    delete poODS;
351    delete ipoDS;
352
353    char *res1=readVSIFile(conf,pszDestDataSource);
354    if(res1==NULL)
355      return SERVICE_FAILED;
356    setMapInMaps(outputs,"Result","value",res1);
357    free(res1);
358
359    OGRCleanupAll();
360    dumpMaps(outputs);
361    return SERVICE_SUCCEEDED;
362
363}
364
365int applyOne(maps*& conf,maps*& inputs,maps*& outputs,OGRGeometry* (OGRGeometry::*myFunc)() const,const char* schema){
366    OGRRegisterAll();
367
368    maps* cursor=inputs;
369    OGRGeometryH geometry,res;
370    OGRLayer *poDstLayer;
371    const char *oDriver1;
372#if GDAL_VERSION_MAJOR >= 2
373    GDALDataset *poODS;
374#else
375    OGRDataSource *poODS;
376#endif
377    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
378    if(!tmp){
379      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
380      return SERVICE_FAILED;
381    }
382    char filename[1024];
383    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
384    const char *oDriver;
385    oDriver="GeoJSON";
386    sprintf(filename,"/vsimem/input_%d.json",getpid());
387    if(tmp1!=NULL){
388      if(strcmp(tmp1->value,"text/xml")==0){
389        sprintf(filename,"/vsimem/input_%d.xml",getpid());
390        oDriver="GML";
391      }
392    }
393    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
394    VSIFCloseL(ifile);
395#if GDAL_VERSION_MAJOR >= 2
396    GDALDataset *ipoDS =
397      (GDALDataset*) GDALOpenEx( filename,
398                                 GDAL_OF_READONLY | GDAL_OF_VECTOR,
399                                 NULL, NULL, NULL );
400    GDALDriverManager* poR=GetGDALDriverManager();
401    GDALDriver          *poDriver = NULL;
402#else
403    OGRDataSource* ipoDS = 
404      OGRSFDriverRegistrar::Open(filename,FALSE);
405    OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
406    OGRSFDriver          *poDriver = NULL;
407#endif
408    char pszDestDataSource[100];
409    if( ipoDS == NULL )
410      {
411        fprintf( stderr, "FAILURE:\n"
412                 "Unable to open datasource `%s' with the following drivers.\n",
413                 filename );
414        char emessage[1024];   
415        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
416          {
417#if GDAL_VERSION_MAJOR >=2
418                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() );
419                fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
420#else
421                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
422                fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
423#endif
424          }
425        char tmp[1024];
426        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.\n%s",filename,emessage);
427        setMapInMaps(conf,"lenv","message",tmp);
428        return SERVICE_FAILED;
429      }
430
431    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
432         iLayer++ )
433      {
434        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
435       
436        if( poLayer == NULL )
437          {
438            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
439                     iLayer );
440            char tmp[1024];
441            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
442            setMapInMaps(conf,"lenv","message",tmp);
443            return SERVICE_FAILED;
444          }
445       
446        OGRFeature  *poFeature;
447
448        /* -------------------------------------------------------------------- */
449        /*      Try opening the output datasource as an existing, writable      */
450        /* -------------------------------------------------------------------- */
451       
452        int                  iDriver;
453       
454        map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
455        oDriver1="GeoJSON";
456        sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
457        if(tmpMap!=NULL){
458          if(strcmp(tmpMap->value,"text/xml")==0){
459            sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
460            oDriver1="GML";
461          }
462        }
463       
464        for( iDriver = 0;
465             iDriver < poR->GetDriverCount() && poDriver == NULL;
466             iDriver++ )
467          {
468
469            if( 
470#if GDAL_VERSION_MAJOR >=2
471                EQUAL(poR->GetDriver(iDriver)->GetDescription(),oDriver1)
472#else
473                EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver1)
474#endif
475                )
476              {
477                poDriver = poR->GetDriver(iDriver);
478              }
479          }
480       
481        if( poDriver == NULL )
482          {
483            char emessage[8192];
484            sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
485            sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
486            for( iDriver = 0;iDriver < poR->GetDriverCount();iDriver++ )           
487              {
488#if GDAL_VERSION_MAJOR >=2
489                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() );
490#else
491                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
492#endif
493              }
494           
495            setMapInMaps(conf,"lenv","message",emessage);
496            return SERVICE_FAILED;
497           
498          }
499       
500#if GDAL_VERSION_MAJOR >=2
501        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
502#else
503        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
504#endif
505        {
506          char emessage[1024];
507          sprintf( emessage,  "%s driver does not support data source creation.\n",
508                   "json" );
509          setMapInMaps(conf,"lenv","message",emessage);
510          return SERVICE_FAILED;
511        }
512       
513        /* -------------------------------------------------------------------- */
514        /*      Create the output data source.                                  */
515        /* -------------------------------------------------------------------- */
516        //map* tpath=getMapFromMaps(conf,"main","tmpPath");
517        char **papszDSCO=NULL;
518#if GDAL_VERSION_MAJOR >=2
519        poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO );
520#else
521        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
522#endif
523        if( poODS == NULL ){
524          char emessage[1024];     
525          sprintf( emessage,  "%s driver failed to create %s\n", 
526                   "json", pszDestDataSource );
527          setMapInMaps(conf,"lenv","message",emessage);
528          return SERVICE_FAILED;
529        }
530       
531        /* -------------------------------------------------------------------- */
532        /*      Create the layer.                                               */
533        /* -------------------------------------------------------------------- */
534        if( !poODS->TestCapability( ODsCCreateLayer ) )
535          {
536            char emessage[1024];
537            sprintf( emessage, 
538                     "Layer %s not found, and CreateLayer not supported by driver.", 
539                     "Result" );
540            setMapInMaps(conf,"lenv","message",emessage);
541            return SERVICE_FAILED;
542          }
543       
544       
545        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
546        if( poDstLayer == NULL ){
547          setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
548          return SERVICE_FAILED;
549        }
550       
551        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
552        int iField;
553        int hasMmField=0;
554       
555        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
556          {
557            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
558            if (iField >= 0)
559                poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
560            else
561            {
562                fprintf( stderr, "Field '%s' not found in source layer.\n", 
563                        iField );
564                return SERVICE_FAILED;
565            }
566          }
567
568        while(TRUE){
569          OGRFeature      *poDstFeature = NULL;
570          poFeature = poLayer->GetNextFeature();
571          if( poFeature == NULL )
572            break;
573          if(poFeature->GetGeometryRef() != NULL){
574            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
575            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
576              {
577                char tmpMsg[1024];
578                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
579                         poFeature->GetFID(), poFDefn->GetName() );
580               
581                OGRFeature::DestroyFeature( poFeature );
582                OGRFeature::DestroyFeature( poDstFeature );
583                return SERVICE_FAILED;
584              }
585            if(poDstFeature->SetGeometryDirectly((poDstFeature->GetGeometryRef()->*myFunc)()) != OGRERR_NONE )
586              {
587                char tmpMsg[1024];
588                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
589                         poFeature->GetFID(), poFDefn->GetName() );
590               
591                OGRFeature::DestroyFeature( poFeature );
592                OGRFeature::DestroyFeature( poDstFeature );
593                return SERVICE_FAILED;
594              }
595            OGRFeature::DestroyFeature( poFeature );
596            if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
597              {         
598                OGRFeature::DestroyFeature( poDstFeature );
599                return SERVICE_FAILED;
600              }
601            OGRFeature::DestroyFeature( poDstFeature );
602          }
603        }
604
605      }
606
607    delete poODS;
608    delete ipoDS;
609
610    char *res1=readVSIFile(conf,pszDestDataSource);
611    if(res1==NULL)
612      return SERVICE_FAILED;
613    setMapInMaps(outputs,"Result","value",res1);
614    free(res1);
615
616    OGRCleanupAll();
617    return SERVICE_SUCCEEDED;
618  }
619
620#ifdef WIN32
621  __declspec(dllexport)
622#endif
623int Buffer(maps*& conf,maps*& inputs,maps*& outputs){
624    OGRRegisterAll();
625
626    double bufferDistance;
627    map* tmp0=getMapFromMaps(inputs,"BufferDistance","value");
628    if(tmp0==NULL){
629      bufferDistance=atof("10.0");
630    }
631    else
632      bufferDistance=atof(tmp0->value);
633
634    maps* cursor=inputs;
635    OGRGeometryH geometry,res;
636    OGRLayer *poDstLayer;
637    const char *oDriver1;
638#if GDAL_VERSION_MAJOR >= 2
639    GDALDataset *poODS;
640#else
641    OGRDataSource *poODS;
642#endif
643    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
644    if(!tmp){
645      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
646      return SERVICE_FAILED;
647    }
648    char filename[1024];
649    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
650    const char *oDriver;
651    oDriver="GeoJSON";
652    sprintf(filename,"/vsimem/input_%d.json",getpid());
653    if(tmp1!=NULL){
654      if(strcmp(tmp1->value,"text/xml")==0){
655        sprintf(filename,"/vsimem/input_%d.xml",getpid());
656        oDriver="GML";
657      }
658    }
659    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
660    VSIFCloseL(ifile);
661#if GDAL_VERSION_MAJOR >= 2
662      GDALDataset *ipoDS =
663        (GDALDataset*) GDALOpenEx( filename,
664                                   GDAL_OF_READONLY | GDAL_OF_VECTOR,
665                                   NULL, NULL, NULL );
666      GDALDriverManager* poR=GetGDALDriverManager();
667      GDALDriver          *poDriver = NULL;
668#else
669      OGRDataSource* ipoDS = 
670        OGRSFDriverRegistrar::Open(filename,FALSE);
671      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
672      OGRSFDriver          *poDriver = NULL;
673#endif
674    char pszDestDataSource[100];
675    if( ipoDS == NULL )
676      {
677       
678        fprintf( stderr, "FAILURE:\n"
679                 "Unable to open datasource `%s' with the following drivers.\n",
680                 filename );
681       
682        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
683          {
684#if GDAL_VERSION_MAJOR >= 2
685            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
686#else
687            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
688#endif
689          }
690        char tmp[1024];
691        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
692        setMapInMaps(conf,"lenv","message",tmp);
693        return SERVICE_FAILED;
694      }
695    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
696         iLayer++ )
697      {
698        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
699       
700        if( poLayer == NULL )
701          {
702            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
703                     iLayer );
704            char tmp[1024];
705            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
706            setMapInMaps(conf,"lenv","message",tmp);
707            return SERVICE_FAILED;
708          }
709       
710        OGRFeature  *poFeature;
711
712        /* -------------------------------------------------------------------- */
713        /*      Try opening the output datasource as an existing, writable      */
714        /* -------------------------------------------------------------------- */
715       
716        int                  iDriver;
717       
718        map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
719        oDriver1="GeoJSON";
720        sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
721        if(tmpMap!=NULL){
722          if(strcmp(tmpMap->value,"text/xml")==0){
723            sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
724            oDriver1="GML";
725          }
726        }
727       
728        for( iDriver = 0;
729             iDriver < poR->GetDriverCount() && poDriver == NULL;
730             iDriver++ )
731          {
732#if GDAL_VERSION_MAJOR >=2
733            if( EQUAL(poR->GetDriver(iDriver)->GetDescription(),oDriver1) )
734#else
735            if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver1) )
736#endif
737              {
738                poDriver = poR->GetDriver(iDriver);
739              }
740          }
741       
742        if( poDriver == NULL )
743          {
744            char emessage[8192];
745            sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
746            sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
747           
748            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
749              {
750#if GDAL_VERSION_MAJOR >=2
751                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() );
752#else
753                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
754#endif
755              }
756           
757            setMapInMaps(conf,"lenv","message",emessage);
758            return SERVICE_FAILED;
759           
760          }
761       
762#if GDAL_VERSION_MAJOR >=2
763        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
764#else
765        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
766#endif
767        {
768          char emessage[1024];
769          sprintf( emessage,  "%s driver does not support data source creation.\n",
770                   "json" );
771          setMapInMaps(conf,"lenv","message",emessage);
772          return SERVICE_FAILED;
773        }
774       
775        /* -------------------------------------------------------------------- */
776        /*      Create the output data source.                                  */
777        /* -------------------------------------------------------------------- */
778        //map* tpath=getMapFromMaps(conf,"main","tmpPath");
779        char **papszDSCO=NULL;
780#if GDAL_VERSION_MAJOR >=2
781        poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO );
782#else
783        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
784#endif
785        if( poODS == NULL ){
786          char emessage[1024];     
787          sprintf( emessage,  "%s driver failed to create %s\n", 
788                   "json", pszDestDataSource );
789          setMapInMaps(conf,"lenv","message",emessage);
790          return SERVICE_FAILED;
791        }
792       
793        /* -------------------------------------------------------------------- */
794        /*      Create the layer.                                               */
795        /* -------------------------------------------------------------------- */
796        if( !poODS->TestCapability( ODsCCreateLayer ) )
797          {
798            char emessage[1024];
799            sprintf( emessage, 
800                     "Layer %s not found, and CreateLayer not supported by driver.", 
801                     "Result" );
802            setMapInMaps(conf,"lenv","message",emessage);
803            return SERVICE_FAILED;
804          }
805
806        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
807        if( poDstLayer == NULL ){
808          setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
809          return SERVICE_FAILED;
810        }
811       
812        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
813        int iField;
814        int hasMmField=0;
815       
816        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
817          {
818            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
819            if (iField >= 0)
820                poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
821            else
822            {
823                fprintf( stderr, "Field '%s' not found in source layer.\n", 
824                        iField );
825                return SERVICE_FAILED;
826            }
827          }
828
829        while(TRUE){
830          OGRFeature      *poDstFeature = NULL;
831          poFeature = poLayer->GetNextFeature();
832          if( poFeature == NULL )
833            break;
834          if(poFeature->GetGeometryRef() != NULL){
835            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
836            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
837              {
838                char tmpMsg[1024];
839                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
840                         poFeature->GetFID(), poFDefn->GetName() );
841               
842                OGRFeature::DestroyFeature( poFeature );
843                OGRFeature::DestroyFeature( poDstFeature );
844                return SERVICE_FAILED;
845              }
846            if(poDstFeature->SetGeometryDirectly(poDstFeature->GetGeometryRef()->Buffer(bufferDistance,30)) != OGRERR_NONE )
847              {
848                char tmpMsg[1024];
849                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
850                         poFeature->GetFID(), poFDefn->GetName() );
851               
852                OGRFeature::DestroyFeature( poFeature );
853                OGRFeature::DestroyFeature( poDstFeature );
854                return SERVICE_FAILED;
855              }
856            OGRFeature::DestroyFeature( poFeature );
857            if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
858              {         
859                OGRFeature::DestroyFeature( poDstFeature );
860                return SERVICE_FAILED;
861              }
862            OGRFeature::DestroyFeature( poDstFeature );
863          }
864        }
865
866      }
867
868    delete poODS;
869    delete ipoDS;
870
871    char *res1=readVSIFile(conf,pszDestDataSource);
872    if(res1==NULL)
873      return SERVICE_FAILED;
874    setMapInMaps(outputs,"Result","value",res1);
875    free(res1);
876
877    OGRCleanupAll();
878    return SERVICE_SUCCEEDED;
879
880}
881
882#ifdef WIN32
883  __declspec(dllexport)
884#endif
885int Centroid(maps*& conf,maps*& inputs,maps*& outputs){
886    OGRRegisterAll();
887
888    maps* cursor=inputs;
889    OGRGeometryH geometry,res;
890    OGRLayer *poDstLayer;
891    const char *oDriver1;
892#if GDAL_VERSION_MAJOR >= 2
893    GDALDataset *poODS;
894#else
895    OGRDataSource *poODS;
896#endif
897    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
898    if(!tmp){
899      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
900      return SERVICE_FAILED;
901    }
902    char filename[1024];
903    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
904    const char *oDriver;
905    oDriver="GeoJSON";
906    sprintf(filename,"/vsimem/input_%d.json",getpid());
907    if(tmp1!=NULL){
908      if(strcmp(tmp1->value,"text/xml")==0){
909        sprintf(filename,"/vsimem/input_%d.xml",getpid());
910        oDriver="GML";
911      }
912    }
913    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
914    VSIFCloseL(ifile);
915#if GDAL_VERSION_MAJOR >= 2
916      GDALDataset *ipoDS =
917        (GDALDataset*) GDALOpenEx( filename,
918                                   GDAL_OF_READONLY | GDAL_OF_VECTOR,
919                                   NULL, NULL, NULL );
920      GDALDriverManager* poR=GetGDALDriverManager();
921      GDALDriver          *poDriver = NULL;
922#else
923      OGRDataSource* ipoDS = 
924        OGRSFDriverRegistrar::Open(filename,FALSE);
925      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
926      OGRSFDriver          *poDriver = NULL;
927#endif
928    char pszDestDataSource[100];
929    if( ipoDS == NULL )
930      {
931       
932        fprintf( stderr, "FAILURE:\n"
933                 "Unable to open datasource `%s' with the following drivers.\n",
934                 filename );
935       
936        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
937          {
938#if GDAL_VERSION_MAJOR >= 2
939            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
940#else
941            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
942#endif
943          }
944        char tmp[1024];
945        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
946        setMapInMaps(conf,"lenv","message",tmp);
947        return SERVICE_FAILED;
948      }
949    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
950         iLayer++ )
951      {
952        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
953       
954        if( poLayer == NULL )
955          {
956            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
957                     iLayer );
958            char tmp[1024];
959            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
960            setMapInMaps(conf,"lenv","message",tmp);
961            return SERVICE_FAILED;
962          }
963       
964        OGRFeature  *poFeature;
965
966        /* -------------------------------------------------------------------- */
967        /*      Try opening the output datasource as an existing, writable      */
968        /* -------------------------------------------------------------------- */
969       
970        int                  iDriver;
971       
972        map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
973        oDriver1="GeoJSON";
974        sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
975        if(tmpMap!=NULL){
976          if(strcmp(tmpMap->value,"text/xml")==0){
977            sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
978            oDriver1="GML";
979          }
980        }
981       
982        for( iDriver = 0;
983             iDriver < poR->GetDriverCount() && poDriver == NULL;
984             iDriver++ )
985          {
986#if GDAL_VERSION_MAJOR >=2
987            if( EQUAL(poR->GetDriver(iDriver)->GetDescription(),oDriver1) )
988#else
989            if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver1) )
990#endif
991              {
992                poDriver = poR->GetDriver(iDriver);
993              }
994          }
995       
996        if( poDriver == NULL )
997          {
998            char emessage[8192];
999            sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
1000            sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
1001           
1002            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
1003              {
1004#if GDAL_VERSION_MAJOR >=2
1005                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() );
1006#else
1007                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
1008#endif
1009              }
1010           
1011            setMapInMaps(conf,"lenv","message",emessage);
1012            return SERVICE_FAILED;
1013           
1014          }
1015       
1016#if GDAL_VERSION_MAJOR >=2
1017        if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
1018#else
1019        if( !poDriver->TestCapability( ODrCCreateDataSource ) )
1020#endif
1021        {
1022          char emessage[1024];
1023          sprintf( emessage,  "%s driver does not support data source creation.\n",
1024                   "json" );
1025          setMapInMaps(conf,"lenv","message",emessage);
1026          return SERVICE_FAILED;
1027        }
1028       
1029        /* -------------------------------------------------------------------- */
1030        /*      Create the output data source.                                  */
1031        /* -------------------------------------------------------------------- */
1032        //map* tpath=getMapFromMaps(conf,"main","tmpPath");
1033        char **papszDSCO=NULL;
1034#if GDAL_VERSION_MAJOR >=2
1035        poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO );
1036#else
1037        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
1038#endif
1039        if( poODS == NULL ){
1040          char emessage[1024];     
1041          sprintf( emessage,  "%s driver failed to create %s\n", 
1042                   "json", pszDestDataSource );
1043          setMapInMaps(conf,"lenv","message",emessage);
1044          return SERVICE_FAILED;
1045        }
1046       
1047        /* -------------------------------------------------------------------- */
1048        /*      Create the layer.                                               */
1049        /* -------------------------------------------------------------------- */
1050        if( !poODS->TestCapability( ODsCCreateLayer ) )
1051          {
1052            char emessage[1024];
1053            sprintf( emessage, 
1054                     "Layer %s not found, and CreateLayer not supported by driver.", 
1055                     "Result" );
1056            setMapInMaps(conf,"lenv","message",emessage);
1057            return SERVICE_FAILED;
1058          }
1059
1060        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
1061        if( poDstLayer == NULL ){
1062          setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
1063          return SERVICE_FAILED;
1064        }
1065       
1066        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
1067        int iField;
1068        int hasMmField=0;
1069       
1070        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
1071          {
1072            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
1073            if (iField >= 0)
1074                poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
1075            else
1076            {
1077                fprintf( stderr, "Field '%s' not found in source layer.\n", 
1078                        iField );
1079                return SERVICE_FAILED;
1080            }
1081          }
1082
1083        while(TRUE){
1084          OGRFeature      *poDstFeature = NULL;
1085          poFeature = poLayer->GetNextFeature();
1086          if( poFeature == NULL )
1087            break;
1088          if(poFeature->GetGeometryRef() != NULL){
1089            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
1090            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
1091              {
1092                char tmpMsg[1024];
1093                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
1094                         poFeature->GetFID(), poFDefn->GetName() );
1095               
1096                OGRFeature::DestroyFeature( poFeature );
1097                OGRFeature::DestroyFeature( poDstFeature );
1098                return SERVICE_FAILED;
1099              }
1100            OGRPoint* poPoint=new OGRPoint();
1101            poDstFeature->GetGeometryRef()->Centroid(poPoint);
1102            if(poDstFeature->SetGeometryDirectly(poPoint)!= OGRERR_NONE ){
1103              char tmpMsg[1024];
1104              sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
1105                       poFeature->GetFID(), poFDefn->GetName() );
1106             
1107              OGRFeature::DestroyFeature( poFeature );
1108              OGRFeature::DestroyFeature( poDstFeature );
1109              return SERVICE_FAILED;
1110            }
1111            OGRFeature::DestroyFeature( poFeature );
1112            if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
1113              {         
1114                OGRFeature::DestroyFeature( poDstFeature );
1115                return SERVICE_FAILED;
1116              }
1117            OGRFeature::DestroyFeature( poDstFeature );
1118          }
1119        }
1120
1121      }
1122
1123    delete poODS;
1124    delete ipoDS;
1125
1126    char *res1=readVSIFile(conf,pszDestDataSource);
1127    if(res1==NULL)
1128      return SERVICE_FAILED;
1129    setMapInMaps(outputs,"Result","value",res1);
1130    free(res1);
1131
1132    OGRCleanupAll();
1133    return SERVICE_SUCCEEDED;
1134
1135}
1136
1137#ifdef WIN32
1138  __declspec(dllexport)
1139#endif
1140  int Boundary(maps*& conf,maps*& inputs,maps*& outputs){
1141    return applyOne(conf,inputs,outputs,&OGRGeometry::Boundary,"http://fooa/gml/3.1.0/polygon.xsd");
1142  }
1143
1144#ifdef WIN32
1145  __declspec(dllexport)
1146#endif
1147  int ConvexHull(maps*& conf,maps*& inputs,maps*& outputs){
1148    return applyOne(conf,inputs,outputs,&OGRGeometry::ConvexHull,"http://fooa/gml/3.1.0/polygon.xsd");
1149  }
1150
1151#if GDAL_VERSION_MAJOR >= 2
1152GDALDataset*
1153#else
1154OGRDataSource* 
1155#endif
1156  loadEntity(maps* conf,maps* inputs,char **filename,const char **oDriver,const char *entity,int iter){
1157    map* tmp=getMapFromMaps(inputs,entity,"value");
1158    map* tmp1=getMapFromMaps(inputs,entity,"mimeType");
1159    *oDriver="GeoJSON";
1160    sprintf(*filename,"/vsimem/input_%d.json",getpid()+iter);
1161    if(tmp1!=NULL){
1162      if(strcmp(tmp1->value,"text/xml")==0){
1163        sprintf(*filename,"/vsimem/input_%d.xml",getpid()+iter);
1164        *oDriver="GML";
1165      }
1166    }
1167    VSILFILE *ifile=VSIFileFromMemBuffer(*filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
1168    VSIFCloseL(ifile);
1169#if GDAL_VERSION_MAJOR >= 2
1170    return (GDALDataset*) GDALOpenEx( *filename,
1171                                      GDAL_OF_READONLY | GDAL_OF_VECTOR,
1172                                      NULL, NULL, NULL );
1173#else
1174    return OGRSFDriverRegistrar::Open(*filename,FALSE);   
1175#endif
1176  }
1177
1178  int applyOneBool(maps*& conf,maps*& inputs,maps*& outputs,OGRBoolean (OGRGeometry::*myFunc)() const){
1179#ifdef DEBUG
1180    fprintf(stderr,"\nService internal print\n");
1181#endif
1182    OGRRegisterAll();
1183
1184    maps* cursor=inputs;
1185    OGRGeometryH geometry,res;
1186    OGRLayer *poDstLayer;
1187    const char *oDriver1;
1188    OGRDataSource       *poODS;
1189#ifdef DEBUG
1190    dumpMaps(cursor);
1191#endif
1192    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
1193    if(!tmp){
1194      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
1195      return SERVICE_FAILED;
1196    }
1197    char filename[1024];
1198    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
1199    const char *oDriver;
1200    oDriver="GeoJSON";
1201    sprintf(filename,"/vsimem/input_%d.json",getpid());
1202    if(tmp1!=NULL){
1203      if(strcmp(tmp1->value,"text/xml")==0){
1204        sprintf(filename,"/vsimem/input_%d.xml",getpid());
1205        oDriver="GML";
1206      }
1207    }
1208    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
1209    VSIFCloseL(ifile);
1210
1211#if GDAL_VERSION_MAJOR >= 2
1212      GDALDataset *ipoDS =
1213        (GDALDataset*) GDALOpenEx( filename,
1214                                   GDAL_OF_READONLY | GDAL_OF_VECTOR,
1215                                   NULL, NULL, NULL );
1216      GDALDriverManager* poR=GetGDALDriverManager();
1217      GDALDriver          *poDriver = NULL;
1218#else
1219      OGRDataSource* ipoDS = 
1220        OGRSFDriverRegistrar::Open(filename,FALSE);
1221      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
1222      OGRSFDriver          *poDriver = NULL;
1223#endif
1224    char pszDestDataSource[100];
1225    if( ipoDS == NULL )
1226      { 
1227        fprintf( stderr, "FAILURE:\n"
1228                 "Unable to open datasource `%s' with the following drivers.\n",
1229                 filename );
1230       
1231        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
1232          {
1233#if GDAL_VERSION_MAJOR >= 2
1234            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
1235#else
1236            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
1237#endif
1238          }
1239        char tmp[1024];
1240        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
1241        setMapInMaps(conf,"lenv","message",tmp);
1242        return SERVICE_FAILED;
1243      }
1244    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
1245         iLayer++ )
1246      {
1247        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
1248       
1249        if( poLayer == NULL )
1250          {
1251            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
1252                     iLayer );
1253            char tmp[1024];
1254            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
1255            setMapInMaps(conf,"lenv","message",tmp);
1256            return SERVICE_FAILED;
1257          }
1258       
1259        OGRFeature  *poFeature;
1260
1261
1262        while(TRUE){
1263          OGRFeature      *poDstFeature = NULL;
1264          poFeature = poLayer->GetNextFeature();
1265          if( poFeature == NULL )
1266            break;
1267          if(poFeature->GetGeometryRef() != NULL){
1268            if((poFeature->GetGeometryRef()->*myFunc)()==0){
1269              setMapInMaps(outputs,"Result","value","false");
1270              OGRFeature::DestroyFeature( poFeature );
1271              delete ipoDS;
1272              return SERVICE_SUCCEEDED;
1273            }
1274          }
1275          OGRFeature::DestroyFeature( poFeature );
1276        }
1277
1278      }
1279
1280    delete ipoDS;
1281    setMapInMaps(outputs,"Result","value","true");
1282
1283    OGRCleanupAll();
1284    return SERVICE_SUCCEEDED;
1285  }
1286
1287#ifdef WIN32
1288  __declspec(dllexport)
1289#endif
1290  int IsSimple(maps*& conf,maps*& inputs,maps*& outputs){
1291    return applyOneBool(conf,inputs,outputs,&OGRGeometry::IsSimple);
1292  }
1293
1294#ifdef WIN32
1295  __declspec(dllexport)
1296#endif
1297  int IsClosed(maps*& conf,maps*& inputs,maps*& outputs){
1298    return applyOneBool(conf,inputs,outputs,&OGRGeometry::IsRing);
1299  }
1300
1301#ifdef WIN32
1302  __declspec(dllexport)
1303#endif
1304  int IsValid(maps*& conf,maps*& inputs,maps*& outputs){
1305    return applyOneBool(conf,inputs,outputs,&OGRGeometry::IsValid);
1306  }
1307
1308 
1309  int applyTwo(maps*& conf,maps*& inputs,maps*& outputs,OGRGeometry* (OGRGeometry::*myFunc)(const OGRGeometry*) const){
1310#ifdef DEBUG
1311    fprintf(stderr,"\nService internal print\n");
1312#endif
1313    OGRRegisterAll();
1314
1315    maps* cursor=inputs;
1316    OGRGeometryH geometry,res;
1317    OGRLayer *poDstLayer;
1318    //const char *oDriver1;
1319#ifdef DEBUG
1320    dumpMaps(cursor);
1321#endif
1322
1323    char *filename=(char*)malloc(1024*sizeof(char));
1324    const char *oDriver1;
1325#if GDAL_VERSION_MAJOR >= 2
1326    GDALDataset*
1327#else
1328    OGRDataSource* 
1329#endif
1330      ipoDS1 = loadEntity(conf,inputs,&filename,&oDriver1,"InputEntity1",1);
1331    char *filename1=(char*)malloc(1024*sizeof(char));
1332    const char *oDriver2;
1333#if GDAL_VERSION_MAJOR >= 2
1334    GDALDataset*
1335#else
1336    OGRDataSource* 
1337#endif
1338      ipoDS2 = loadEntity(conf,inputs,&filename1,&oDriver2,"InputEntity2",2);
1339    const char *oDriver3;
1340    char pszDestDataSource[100];
1341#if GDAL_VERSION_MAJOR >= 2
1342      GDALDriverManager* poR=GetGDALDriverManager();
1343      GDALDriver          *poDriver = NULL;
1344      GDALDataset *poODS;
1345#else
1346      OGRDataSource       *poODS;
1347      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
1348      OGRSFDriver          *poDriver = NULL;
1349#endif
1350    if( ipoDS1 == NULL || ipoDS2 == NULL )
1351      {
1352       
1353        fprintf( stderr, "FAILURE:\n"
1354                 "Unable to open datasource `%s' with the following drivers.\n",
1355                 filename );
1356       
1357        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
1358          {
1359#if GDAL_VERSION_MAJOR >= 2
1360            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
1361#else
1362            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
1363#endif
1364          }
1365        char tmp[1024];
1366        if( ipoDS1 == NULL )
1367          sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
1368        if( ipoDS2 == NULL )
1369          sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename1);
1370        setMapInMaps(conf,"lenv","message",tmp);
1371        return SERVICE_FAILED;
1372      }
1373    for( int iLayer = 0; iLayer < ipoDS1->GetLayerCount();
1374         iLayer++ )
1375      {
1376        OGRLayer        *poLayer1 = ipoDS1->GetLayer(iLayer);
1377       
1378        if( poLayer1 == NULL )
1379          {
1380            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
1381                     iLayer );
1382            char tmp[1024];
1383            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
1384            setMapInMaps(conf,"lenv","message",tmp);
1385            return SERVICE_FAILED;
1386          }
1387
1388        for( int iLayer1 = 0; iLayer1 < ipoDS2->GetLayerCount();
1389             iLayer1++ )
1390          {
1391            OGRLayer        *poLayer2 = ipoDS2->GetLayer(iLayer1);
1392           
1393            if( poLayer1 == NULL )
1394              {
1395                fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
1396                         iLayer1 );
1397                char tmp[1024];
1398                sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer1);
1399                setMapInMaps(conf,"lenv","message",tmp);
1400                return SERVICE_FAILED;
1401              }
1402       
1403            OGRFeature  *poFeature1,*poFeature2;
1404
1405            /* -------------------------------------------------------------------- */
1406            /*      Try opening the output datasource as an existing, writable      */
1407            /* -------------------------------------------------------------------- */
1408           
1409            int                  iDriver;
1410           
1411            map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
1412            oDriver3="GeoJSON";
1413            sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
1414            if(tmpMap!=NULL){
1415              if(strcmp(tmpMap->value,"text/xml")==0){
1416                sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
1417                oDriver3="GML";
1418              }
1419            }
1420
1421            for( iDriver = 0;
1422                 iDriver < poR->GetDriverCount() && poDriver == NULL;
1423                 iDriver++ )
1424              {
1425#if GDAL_VERSION_MAJOR >=2
1426                if( EQUAL(poR->GetDriver(iDriver)->GetDescription(),oDriver3) )
1427#else
1428                if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver3) )
1429#endif
1430                  {
1431                    poDriver = poR->GetDriver(iDriver);
1432                  }
1433              }
1434           
1435            if( poDriver == NULL )
1436              {
1437                char emessage[8192];
1438                sprintf( emessage, "Unable to find driver `%s'.\n", oDriver1 );
1439                sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
1440               
1441                for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
1442                  {
1443#if GDAL_VERSION_MAJOR >= 2
1444                    sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() );
1445#else
1446                    sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
1447#endif
1448                  }
1449               
1450                setMapInMaps(conf,"lenv","message",emessage);
1451                return SERVICE_FAILED;
1452               
1453              }
1454           
1455#if GDAL_VERSION_MAJOR >= 2
1456            if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
1457#else
1458            if( !poDriver->TestCapability( ODrCCreateDataSource ) )
1459#endif
1460              {
1461                char emessage[1024];
1462                sprintf( emessage,  "%s driver does not support data source creation.\n",
1463                         "json" );
1464                setMapInMaps(conf,"lenv","message",emessage);
1465                return SERVICE_FAILED;
1466              }
1467           
1468            /* -------------------------------------------------------------------- */
1469            /*      Create the output data source.                                  */
1470            /* -------------------------------------------------------------------- */
1471            //map* tpath=getMapFromMaps(conf,"main","tmpPath");
1472            char **papszDSCO=NULL;
1473#if GDAL_VERSION_MAJOR >= 2
1474            poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO );
1475#else
1476            poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
1477#endif
1478            if( poODS == NULL ){
1479              char emessage[1024];     
1480              sprintf( emessage,  "%s driver failed to create %s\n", 
1481                       "json", pszDestDataSource );
1482              setMapInMaps(conf,"lenv","message",emessage);
1483              return SERVICE_FAILED;
1484            }
1485           
1486            /* -------------------------------------------------------------------- */
1487            /*      Create the layer.                                               */
1488            /* -------------------------------------------------------------------- */
1489            if( !poODS->TestCapability( ODsCCreateLayer ) )
1490              {
1491                char emessage[1024];
1492                sprintf( emessage, 
1493                         "Layer %s not found, and CreateLayer not supported by driver.", 
1494                         "Result" );
1495                setMapInMaps(conf,"lenv","message",emessage);
1496                return SERVICE_FAILED;
1497              }
1498           
1499            //CPLErrorReset();
1500           
1501            poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
1502            if( poDstLayer == NULL ){
1503              setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
1504              return SERVICE_FAILED;
1505            }
1506           
1507            OGRFeatureDefn *poFDefn = poLayer2->GetLayerDefn();
1508            int iField;
1509            int hasMmField=0;
1510           
1511            for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
1512              {
1513                OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
1514                if (iField >= 0)
1515                  poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
1516                else
1517                  {
1518                    fprintf( stderr, "Field '%s' not found in source layer.\n", 
1519                             iField );
1520                    return SERVICE_FAILED;
1521                  }
1522              }
1523           
1524            while(TRUE){
1525              OGRFeature      *poDstFeature = NULL;
1526              poFeature1 = poLayer1->GetNextFeature();
1527              if( poFeature1 == NULL )
1528                break;
1529              while(TRUE){
1530                poFeature2 = poLayer2->GetNextFeature();
1531                if( poFeature2 == NULL )
1532                  break;
1533
1534                if(poFeature1->GetGeometryRef() != NULL && poFeature2->GetGeometryRef() != NULL){
1535                  poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
1536                  if( poDstFeature->SetFrom( poFeature2, TRUE ) != OGRERR_NONE )
1537                    {
1538                      char tmpMsg[1024];
1539                      sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
1540                               poFeature2->GetFID(), poFDefn->GetName() );
1541                     
1542                      OGRFeature::DestroyFeature( poFeature1 );
1543                      OGRFeature::DestroyFeature( poFeature2 );
1544                      OGRFeature::DestroyFeature( poDstFeature );
1545                      return SERVICE_FAILED;
1546                    }
1547                  if(poDstFeature->SetGeometryDirectly((poFeature1->GetGeometryRef()->*myFunc)(poFeature2->GetGeometryRef())) != OGRERR_NONE )
1548                    {
1549                      char tmpMsg[1024];
1550                      sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
1551                               poFeature2->GetFID(), poFDefn->GetName() );
1552                     
1553                      OGRFeature::DestroyFeature( poFeature1 );
1554                      OGRFeature::DestroyFeature( poFeature2 );
1555                      OGRFeature::DestroyFeature( poDstFeature );
1556                      return SERVICE_FAILED;
1557                    }
1558                  OGRFeature::DestroyFeature( poFeature2 );
1559                  if(!poDstFeature->GetGeometryRef()->IsEmpty())
1560                    if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
1561                      {         
1562                        OGRFeature::DestroyFeature( poDstFeature );
1563                        return SERVICE_FAILED;
1564                      }
1565                  OGRFeature::DestroyFeature( poDstFeature );
1566                }
1567              }
1568            }
1569            OGRFeature::DestroyFeature( poFeature1 );
1570          }
1571      }
1572
1573    delete poODS;
1574    delete ipoDS1;
1575    delete ipoDS2;
1576    free(filename);
1577    free(filename1);
1578
1579    char *res1=readVSIFile(conf,pszDestDataSource);
1580    if(res1==NULL)
1581      return SERVICE_FAILED;
1582    setMapInMaps(outputs,"Result","value",res1);
1583    free(res1);
1584    OGRCleanupAll();
1585    return SERVICE_SUCCEEDED;
1586  }
1587 
1588#ifdef WIN32
1589  __declspec(dllexport)
1590#endif
1591  int Difference(maps*& conf,maps*& inputs,maps*& outputs){
1592    return applyTwo(conf,inputs,outputs,&OGRGeometry::Difference);
1593  }
1594
1595#ifdef WIN32
1596  __declspec(dllexport)
1597#endif
1598  int SymDifference(maps*& conf,maps*& inputs,maps*& outputs){
1599    return applyTwo(conf,inputs,outputs,&OGRGeometry::SymDifference);
1600  }
1601
1602#ifdef WIN32
1603  __declspec(dllexport)
1604#endif
1605  int Intersection(maps*& conf,maps*& inputs,maps*& outputs){
1606    return applyTwo(conf,inputs,outputs,&OGRGeometry::Intersection);
1607  }
1608
1609#ifdef WIN32
1610  __declspec(dllexport)
1611#endif
1612  int Union(maps*& conf,maps*& inputs,maps*& outputs){
1613    return applyTwo(conf,inputs,outputs,&OGRGeometry::Union);
1614  }
1615
1616  int applyTwoBool(maps*& conf,maps*& inputs,maps*& outputs,OGRBoolean (OGRGeometry::*myFunc)(const OGRGeometry*) const){
1617    OGRRegisterAll();
1618
1619    maps* cursor=inputs;
1620    OGRGeometryH geometry,res;
1621    OGRLayer *poDstLayer;
1622#if GDAL_VERSION_MAJOR >= 2
1623    GDALDataset *poODS;
1624#else
1625    OGRDataSource *poODS;
1626#endif
1627
1628    char *filename=(char*)malloc(1024*sizeof(char));
1629    const char *oDriver1;
1630#if GDAL_VERSION_MAJOR >= 2
1631    GDALDataset*
1632#else
1633    OGRDataSource*
1634#endif
1635      ipoDS1 = loadEntity(conf,inputs,&filename,&oDriver1,"InputEntity1",1);
1636
1637    char *filename1=(char*)malloc(1024*sizeof(char));
1638    const char *oDriver2;
1639#if GDAL_VERSION_MAJOR >= 2
1640    GDALDataset*
1641#else
1642    OGRDataSource*
1643#endif
1644      ipoDS2 = loadEntity(conf,inputs,&filename1,&oDriver2,"InputEntity2",2);
1645    const char *oDriver3;
1646    char pszDestDataSource[100];
1647#if GDAL_VERSION_MAJOR >= 2
1648      GDALDriverManager* poR=GetGDALDriverManager();
1649      GDALDriver          *poDriver = NULL;
1650#else
1651      OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
1652      OGRSFDriver          *poDriver = NULL;
1653#endif
1654
1655    if( ipoDS1 == NULL || ipoDS2 == NULL )
1656      {
1657        fprintf( stderr, "FAILURE:\n"
1658                 "Unable to open datasource `%s' with the following drivers.\n",
1659                 filename );
1660       
1661        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
1662          {
1663#if GDAL_VERSION_MAJOR >= 2
1664            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
1665#else
1666            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
1667#endif
1668          }
1669        char tmp[1024];
1670        if( ipoDS1 == NULL )
1671          sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
1672        if( ipoDS2 == NULL )
1673          sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename1);
1674        setMapInMaps(conf,"lenv","message",tmp);
1675        return SERVICE_FAILED;
1676      }
1677    for( int iLayer = 0; iLayer < ipoDS1->GetLayerCount();
1678         iLayer++ )
1679      {
1680        OGRLayer        *poLayer1 = ipoDS1->GetLayer(iLayer);
1681       
1682        if( poLayer1 == NULL )
1683          {
1684            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
1685                     iLayer );
1686            char tmp[1024];
1687            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
1688            setMapInMaps(conf,"lenv","message",tmp);
1689            return SERVICE_FAILED;
1690          }
1691
1692        for( int iLayer1 = 0; iLayer1 < ipoDS2->GetLayerCount();
1693             iLayer1++ )
1694          {
1695            OGRLayer        *poLayer2 = ipoDS2->GetLayer(iLayer1);
1696           
1697            if( poLayer1 == NULL )
1698              {
1699                fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
1700                         iLayer1 );
1701                char tmp[1024];
1702                sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer1);
1703                setMapInMaps(conf,"lenv","message",tmp);
1704                return SERVICE_FAILED;
1705              }
1706       
1707            OGRFeature  *poFeature1,*poFeature2;
1708
1709
1710            while(TRUE){
1711              OGRFeature      *poDstFeature = NULL;
1712              poFeature1 = poLayer1->GetNextFeature();
1713              if( poFeature1 == NULL )
1714                break;
1715              while(TRUE){
1716                poFeature2 = poLayer2->GetNextFeature();
1717                if( poFeature2 == NULL )
1718                  break;
1719                if(poFeature1->GetGeometryRef() != NULL && poFeature2->GetGeometryRef() != NULL){
1720                  if((poFeature1->GetGeometryRef()->*myFunc)(poFeature2->GetGeometryRef())==0){
1721                    setMapInMaps(outputs,"Result","value","false");
1722                    OGRFeature::DestroyFeature( poFeature1 );
1723                    OGRFeature::DestroyFeature( poFeature2 );
1724                    delete ipoDS1;
1725                    delete ipoDS2;
1726                    free(filename);
1727                    free(filename1);
1728                    return SERVICE_SUCCEEDED;
1729                  }
1730                }
1731                OGRFeature::DestroyFeature( poFeature2 );
1732              }
1733              OGRFeature::DestroyFeature( poFeature1 );
1734            }
1735          }
1736      }
1737
1738    delete ipoDS1;
1739    delete ipoDS2;
1740    free(filename);
1741    free(filename1);
1742
1743    setMapInMaps(outputs,"Result","value","true");
1744
1745    OGRCleanupAll();
1746    return SERVICE_SUCCEEDED;
1747  }
1748
1749
1750#ifdef WIN32
1751  __declspec(dllexport)
1752#endif
1753  int Equals(maps*& conf,maps*& inputs,maps*& outputs){
1754    return applyTwoBool(conf,inputs,outputs,(OGRBoolean (OGRGeometry::*)(const OGRGeometry *) const)&OGRGeometry::Equals);
1755  }
1756
1757#ifdef WIN32
1758  __declspec(dllexport)
1759#endif
1760  int Disjoint(maps*& conf,maps*& inputs,maps*& outputs){
1761    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Disjoint);
1762  }
1763
1764#ifdef WIN32
1765  __declspec(dllexport)
1766#endif
1767  int Touches(maps*& conf,maps*& inputs,maps*& outputs){
1768    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Touches);
1769  }
1770
1771#ifdef WIN32
1772  __declspec(dllexport)
1773#endif
1774  int Crosses(maps*& conf,maps*& inputs,maps*& outputs){
1775    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Crosses);
1776  }
1777
1778#ifdef WIN32
1779  __declspec(dllexport)
1780#endif
1781  int Within(maps*& conf,maps*& inputs,maps*& outputs){
1782    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Within);
1783  }
1784
1785#ifdef WIN32
1786  __declspec(dllexport)
1787#endif
1788  int Contains(maps*& conf,maps*& inputs,maps*& outputs){
1789    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Contains);
1790  }
1791
1792#ifdef WIN32
1793  __declspec(dllexport)
1794#endif
1795  int Overlaps(maps*& conf,maps*& inputs,maps*& outputs){
1796    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Overlaps);
1797  }
1798
1799#ifdef WIN32
1800  __declspec(dllexport)
1801#endif
1802  int Intersects(maps*& conf,maps*& inputs,maps*& outputs){
1803    return applyTwoBool(conf,inputs,outputs,(OGRBoolean (OGRGeometry::*)(const OGRGeometry *) const)&OGRGeometry::Intersects);
1804  }
1805
1806#ifdef WIN32
1807  __declspec(dllexport)
1808#endif
1809  int Distance(maps*& conf,maps*& inputs,maps*& outputs){
1810#ifdef DEBUG
1811    fprintf(stderr,"\nService internal print1\n");
1812#endif
1813    fflush(stderr);
1814    maps* cursor=inputs;
1815    OGRGeometryH geometry1,geometry2;
1816    double res;
1817    {
1818      map* tmp=getMapFromMaps(inputs,"InputEntity1","value");
1819      map* tmp1=getMapFromMaps(inputs,"InputEntity1","mimeType");
1820#ifdef DEBUG
1821      fprintf(stderr,"MY MAP\n");
1822      dumpMap(tmp1);
1823      dumpMaps(inputs);
1824      fprintf(stderr,"MY MAP\n");
1825#endif
1826      if(tmp1!=NULL){
1827        if(strncmp(tmp1->value,"application/json",16)==0)
1828          geometry1=OGR_G_CreateGeometryFromJson(tmp->value);
1829        else
1830          geometry1=createGeometryFromGML(conf,tmp->value);
1831      }
1832      else
1833        geometry1=createGeometryFromGML(conf,tmp->value);
1834    }
1835    if(geometry1==NULL){
1836      setMapInMaps(conf,"lenv","message",_ss("Unable to parse input geometry for InputEntity1."));
1837      fprintf(stderr,"SERVICE FAILED !\n");
1838      return SERVICE_FAILED;
1839    }
1840    {
1841      map* tmp=getMapFromMaps(inputs,"InputEntity2","value");
1842      map* tmp1=getMapFromMaps(inputs,"InputEntity2","mimeType");
1843#ifdef DEBUG
1844      fprintf(stderr,"MY MAP\n");
1845      dumpMap(tmp1);
1846      dumpMaps(inputs);
1847      fprintf(stderr,"MY MAP\n");
1848#endif
1849      if(tmp1!=NULL){
1850        if(strncmp(tmp1->value,"application/json",16)==0)
1851          geometry2=OGR_G_CreateGeometryFromJson(tmp->value);
1852        else
1853          geometry2=createGeometryFromGML(conf,tmp->value);
1854      }
1855      else
1856        geometry2=createGeometryFromGML(conf,tmp->value);
1857    }
1858    if(geometry2==NULL){
1859      setMapInMaps(conf,"lenv","message",_ss("Unable to parse input geometry for InputEntity2."));
1860      fprintf(stderr,"SERVICE FAILED !\n");
1861      return SERVICE_FAILED;
1862    }
1863    res=OGR_G_Distance(geometry1,geometry2);   
1864    char tmpres[100];
1865    sprintf(tmpres,"%f",res);
1866    setMapInMaps(outputs,"Distance","value",tmpres);
1867    setMapInMaps(outputs,"Distance","dataType","float");
1868#ifdef DEBUG
1869    dumpMaps(outputs);
1870    fprintf(stderr,"\nService internal print\n===\n");
1871#endif
1872    return SERVICE_SUCCEEDED;
1873  }
1874
1875#ifdef WIN32
1876  __declspec(dllexport)
1877#endif
1878  int GetArea(maps*& conf,maps*& inputs,maps*& outputs){
1879    fprintf(stderr,"GETAREA \n");
1880    double res;
1881    /**
1882     * Extract Geometry from the InputPolygon value
1883     */
1884    OGRGeometryH geometry;
1885    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
1886    if(tmp==NULL){
1887      setMapInMaps(conf,"lenv","message",_ss("Unable to parse input geometry from InputPolygon"));
1888      return SERVICE_FAILED;
1889    }
1890    fprintf(stderr,"geometry creation %s \n",tmp->value);
1891    geometry=createGeometryFromGML(conf,tmp->value);
1892    if(geometry==NULL){
1893      setMapInMaps(conf,"lenv","message",_ss("Unable to parse input geometry from InputPolygon"));
1894      return SERVICE_FAILED;
1895    }
1896    fprintf(stderr,"geometry created %s \n",tmp->value);
1897    res=OGR_G_Area(geometry);
1898    fprintf(stderr,"area %d \n",res);
1899    /**
1900     * Filling the outputs
1901     */
1902    char tmp1[100];
1903    sprintf(tmp1,"%f",res);
1904    setMapInMaps(outputs,"Area","value",tmp1);
1905    setMapInMaps(outputs,"Area","dataType","float");
1906#ifdef DEBUG
1907    dumpMaps(outputs);
1908#endif
1909    return SERVICE_SUCCEEDED;
1910  }
1911
1912}
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