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

Last change on this file since 917 was 917, checked in by djay, 5 years ago

Merge prototype-v0 branch in trunk

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