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

Last change on this file since 497 was 497, checked in by djay, 10 years ago

Fix Simplify service to first build against GDAL 1.11.0 and to be able to handle FeatureCollection? as other services.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 44.7 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
29#include "cpl_minixml.h"
30#include "ogr_api.h"
31#include "ogrsf_frmts.h"
32
33#include "geos_c.h"
34#include "service.h"
35#include "service_internal.h"
36
37extern "C" {
38#include <libxml/tree.h>
39#include <libxml/parser.h>
40#include <libxml/xpath.h>
41#include <libxml/xpathInternals.h>
42
43#include <openssl/sha.h>
44#include <openssl/hmac.h>
45#include <openssl/evp.h>
46#include <openssl/bio.h>
47#include <openssl/buffer.h>
48
49  void printExceptionReportResponse(maps*,map*);
50  char *base64(const char *input, int length);
51
52  OGRGeometryH createGeometryFromGML(maps* conf,char* inputStr){
53    xmlInitParser();
54    xmlDocPtr doc = xmlParseMemory(inputStr,strlen(inputStr));
55    xmlChar *xmlbuff;
56    int buffersize;
57    xmlXPathContextPtr xpathCtx;
58    xmlXPathObjectPtr xpathObj;
59    const char * xpathExpr="/*/*/*/*/*[local-name()='Polygon' or local-name()='MultiPolygon']";
60    xpathCtx = xmlXPathNewContext(doc);
61    xpathObj = xmlXPathEvalExpression(BAD_CAST xpathExpr,xpathCtx);
62    if(!xpathObj->nodesetval){
63      setMapInMaps(conf,"lenv","message",_ss("Unable to parse Input Polygon"));
64      setMapInMaps(conf,"lenv","code","InvalidParameterValue");
65      return NULL;
66    }
67    int size = (xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0;
68    /**
69     * Create a temporary XML document
70     */
71    xmlDocPtr ndoc = xmlNewDoc(BAD_CAST "1.0");
72    /**
73     * Only one polygon should be provided so we use it as the root node.
74     */
75    for(int k=size-1;k>=0;k--){ 
76      xmlDocSetRootElement(ndoc, xpathObj->nodesetval->nodeTab[k]);
77    }
78    xmlDocDumpFormatMemory(ndoc, &xmlbuff, &buffersize, 1);
79    char *tmp=(char*)calloc((xmlStrlen(xmlStrstr(xmlbuff,BAD_CAST "?>"))-1),sizeof(char));
80    sprintf(tmp,"%s",xmlStrstr(xmlbuff,BAD_CAST "?>")+2);
81    xmlXPathFreeObject(xpathObj);
82    xmlXPathFreeContext(xpathCtx);
83    xmlFree(xmlbuff);
84    xmlFreeDoc(doc);
85    xmlFreeDoc(ndoc);
86#ifndef WIN32
87    xmlCleanupParser();
88#endif
89#ifdef DEBUG
90    fprintf(stderr,"\nService internal print\n Loading the geometry from GML string ...");
91#endif
92    OGRGeometryH res=OGR_G_CreateFromGML(tmp);
93    free(tmp);
94    if(res==NULL){
95      setMapInMaps(conf,"lenv","message",_ss("Unable to call OGR_G_CreatFromGML"));
96      return NULL;
97    }
98    else
99      return res;
100  }
101
102#ifdef WIN32
103  __declspec(dllexport)
104#endif
105  int Simplify(maps*& conf,maps*& inputs,maps*& outputs){
106    OGRRegisterAll();
107
108    double tolerance;
109    map* tmp0=getMapFromMaps(inputs,"Tolerance","value");
110    if(tmp0==NULL){
111      tolerance=atof("2.0");
112    }
113    else
114      tolerance=atof(tmp0->value);
115
116    maps* cursor=inputs;
117    OGRGeometryH geometry;
118    OGRGeometry *res;
119    OGRLayer *poDstLayer;
120    const char *oDriver1;
121    OGRDataSource       *poODS;
122    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
123    if(!tmp){
124      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
125      return SERVICE_FAILED;
126    }
127    char filename[1024];
128    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
129    const char *oDriver;
130    oDriver="GeoJSON";
131    sprintf(filename,"/vsimem/input_%d.json",getpid());
132    if(tmp1!=NULL){
133      if(strcmp(tmp1->value,"text/xml")==0){
134        sprintf(filename,"/vsimem/input_%d.xml",getpid());
135        oDriver="GML";
136      }
137    }
138    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
139    VSIFCloseL(ifile);
140    OGRDataSource* ipoDS = OGRSFDriverRegistrar::Open(filename,FALSE);
141    char pszDestDataSource[100];
142    if( ipoDS == NULL )
143      {
144        OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
145       
146        fprintf( stderr, "FAILURE:\n"
147                 "Unable to open datasource `%s' with the following drivers.\n",
148                 filename );
149       
150        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
151          {
152            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
153          }
154        char tmp[1024];
155        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
156        setMapInMaps(conf,"lenv","message",tmp);
157        return SERVICE_FAILED;
158      }
159    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
160         iLayer++ )
161      {
162        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
163       
164        if( poLayer == NULL )
165          {
166            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
167                     iLayer );
168            char tmp[1024];
169            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
170            setMapInMaps(conf,"lenv","message",tmp);
171            return SERVICE_FAILED;
172          }
173       
174        OGRFeature  *poFeature;
175       
176        OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
177        OGRSFDriver          *poDriver = NULL;
178        int                  iDriver;
179       
180        map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
181        oDriver1="GeoJSON";
182        sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
183        if(tmpMap!=NULL){
184          if(strcmp(tmpMap->value,"text/xml")==0){
185            sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
186            oDriver1="GML";
187          }
188        }
189       
190        for( iDriver = 0;
191             iDriver < poR->GetDriverCount() && poDriver == NULL;
192             iDriver++ )
193          {
194            if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver1) )
195              {
196                poDriver = poR->GetDriver(iDriver);
197              }
198          }
199       
200        if( poDriver == NULL )
201          {
202            char emessage[8192];
203            sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
204            sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
205           
206            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
207              {
208                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
209              }
210           
211            setMapInMaps(conf,"lenv","message",emessage);
212            return SERVICE_FAILED;
213           
214          }
215       
216        if( !poDriver->TestCapability( ODrCCreateDataSource ) ){
217          char emessage[1024];
218          sprintf( emessage,  "%s driver does not support data source creation.\n",
219                   "json" );
220          setMapInMaps(conf,"lenv","message",emessage);
221          return SERVICE_FAILED;
222        }
223       
224        char **papszDSCO=NULL;
225        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
226        if( poODS == NULL ){
227          char emessage[1024];     
228          sprintf( emessage,  "%s driver failed to create %s\n", 
229                   "json", pszDestDataSource );
230          setMapInMaps(conf,"lenv","message",emessage);
231          return SERVICE_FAILED;
232        }
233       
234        if( !poODS->TestCapability( ODsCCreateLayer ) )
235          {
236            char emessage[1024];
237            sprintf( emessage, 
238                     "Layer %s not found, and CreateLayer not supported by driver.", 
239                     "Result" );
240            setMapInMaps(conf,"lenv","message",emessage);
241            return SERVICE_FAILED;
242          }
243       
244        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
245        if( poDstLayer == NULL ){
246          setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
247          return SERVICE_FAILED;
248        }
249       
250        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
251        int iField;
252        int hasMmField=0;
253       
254        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
255          {
256            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
257            if (iField >= 0)
258                poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
259            else
260            {
261                fprintf( stderr, "Field '%s' not found in source layer.\n", 
262                        iField );
263                return SERVICE_FAILED;
264            }
265          }
266
267        while(TRUE){
268          OGRFeature      *poDstFeature = NULL;
269          poFeature = poLayer->GetNextFeature();
270          if( poFeature == NULL )
271            break;
272          if(poFeature->GetGeometryRef() != NULL){
273            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
274            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
275              {
276                char tmpMsg[1024];
277                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
278                         poFeature->GetFID(), poFDefn->GetName() );
279               
280                OGRFeature::DestroyFeature( poFeature );
281                OGRFeature::DestroyFeature( poDstFeature );
282                return SERVICE_FAILED;
283              }
284            geometry=poFeature->GetGeometryRef();
285#if GDAL_VERSION_MAJOR == 1 && GDAL_VERSION_MINOR < 11
286            GEOSGeometry* ggeometry=((OGRGeometry *) geometry)->exportToGEOS();
287            GEOSGeometry* gres=GEOSTopologyPreserveSimplify(ggeometry,tolerance);
288            if(gres!=NULL)
289              res=(OGRGeometry*)OGRGeometryFactory::createFromGEOS(gres);
290#else
291            res=((OGRGeometry *) geometry)->SimplifyPreserveTopology(tolerance);
292#endif
293            if(poDstFeature->SetGeometryDirectly(res) != OGRERR_NONE )
294              {
295                char tmpMsg[1024];
296                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
297                         poFeature->GetFID(), poFDefn->GetName() );
298               
299                OGRFeature::DestroyFeature( poFeature );
300                OGRFeature::DestroyFeature( poDstFeature );
301                return SERVICE_FAILED;
302              }
303            OGRFeature::DestroyFeature( poFeature );
304            if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
305              {         
306                OGRFeature::DestroyFeature( poDstFeature );
307                return SERVICE_FAILED;
308              }
309            OGRFeature::DestroyFeature( poDstFeature );
310#if GDAL_VERSION_MAJOR == 1 && GDAL_VERSION_MINOR < 11
311            GEOSGeom_destroy( ggeometry);
312            GEOSGeom_destroy( gres);
313#endif
314          }
315        }
316      }
317
318    delete poODS;
319    delete ipoDS;
320
321    char *res1=readVSIFile(conf,pszDestDataSource);
322    if(res1==NULL)
323      return SERVICE_FAILED;
324    setMapInMaps(outputs,"Result","value",res1);
325    free(res1);
326
327    OGRCleanupAll();
328    return SERVICE_SUCCEEDED;
329
330}
331
332  int applyOne(maps*& conf,maps*& inputs,maps*& outputs,OGRGeometry* (OGRGeometry::*myFunc)() const,const char* schema){
333    OGRRegisterAll();
334
335    maps* cursor=inputs;
336    OGRGeometryH geometry,res;
337    OGRLayer *poDstLayer;
338    const char *oDriver1;
339    OGRDataSource       *poODS;
340    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
341    if(!tmp){
342      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
343      return SERVICE_FAILED;
344    }
345    char filename[1024];
346    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
347    const char *oDriver;
348    oDriver="GeoJSON";
349    sprintf(filename,"/vsimem/input_%d.json",getpid());
350    if(tmp1!=NULL){
351      if(strcmp(tmp1->value,"text/xml")==0){
352        sprintf(filename,"/vsimem/input_%d.xml",getpid());
353        oDriver="GML";
354      }
355    }
356    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
357    VSIFCloseL(ifile);
358    OGRDataSource* ipoDS = OGRSFDriverRegistrar::Open(filename,FALSE);
359    char pszDestDataSource[100];
360    if( ipoDS == NULL )
361      {
362        OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
363       
364        fprintf( stderr, "FAILURE:\n"
365                 "Unable to open datasource `%s' with the following drivers.\n",
366                 filename );
367       
368        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
369          {
370            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
371          }
372        char tmp[1024];
373        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
374        setMapInMaps(conf,"lenv","message",tmp);
375        return SERVICE_FAILED;
376      }
377    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
378         iLayer++ )
379      {
380        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
381       
382        if( poLayer == NULL )
383          {
384            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
385                     iLayer );
386            char tmp[1024];
387            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
388            setMapInMaps(conf,"lenv","message",tmp);
389            return SERVICE_FAILED;
390          }
391       
392        OGRFeature  *poFeature;
393
394        /* -------------------------------------------------------------------- */
395        /*      Try opening the output datasource as an existing, writable      */
396        /* -------------------------------------------------------------------- */
397       
398        OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
399        OGRSFDriver          *poDriver = NULL;
400        int                  iDriver;
401       
402        map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
403        oDriver1="GeoJSON";
404        sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
405        if(tmpMap!=NULL){
406          if(strcmp(tmpMap->value,"text/xml")==0){
407            sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
408            oDriver1="GML";
409          }
410        }
411       
412        for( iDriver = 0;
413             iDriver < poR->GetDriverCount() && poDriver == NULL;
414             iDriver++ )
415          {
416            if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver1) )
417              {
418                poDriver = poR->GetDriver(iDriver);
419              }
420          }
421       
422        if( poDriver == NULL )
423          {
424            char emessage[8192];
425            sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
426            sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
427           
428            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
429              {
430                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
431              }
432           
433            setMapInMaps(conf,"lenv","message",emessage);
434            return SERVICE_FAILED;
435           
436          }
437       
438        if( !poDriver->TestCapability( ODrCCreateDataSource ) ){
439          char emessage[1024];
440          sprintf( emessage,  "%s driver does not support data source creation.\n",
441                   "json" );
442          setMapInMaps(conf,"lenv","message",emessage);
443          return SERVICE_FAILED;
444        }
445       
446        /* -------------------------------------------------------------------- */
447        /*      Create the output data source.                                  */
448        /* -------------------------------------------------------------------- */
449        //map* tpath=getMapFromMaps(conf,"main","tmpPath");
450        char **papszDSCO=NULL;
451        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
452        if( poODS == NULL ){
453          char emessage[1024];     
454          sprintf( emessage,  "%s driver failed to create %s\n", 
455                   "json", pszDestDataSource );
456          setMapInMaps(conf,"lenv","message",emessage);
457          return SERVICE_FAILED;
458        }
459       
460        /* -------------------------------------------------------------------- */
461        /*      Create the layer.                                               */
462        /* -------------------------------------------------------------------- */
463        if( !poODS->TestCapability( ODsCCreateLayer ) )
464          {
465            char emessage[1024];
466            sprintf( emessage, 
467                     "Layer %s not found, and CreateLayer not supported by driver.", 
468                     "Result" );
469            setMapInMaps(conf,"lenv","message",emessage);
470            return SERVICE_FAILED;
471          }
472       
473        //CPLErrorReset();
474       
475        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
476        if( poDstLayer == NULL ){
477          setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
478          return SERVICE_FAILED;
479        }
480       
481        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
482        int iField;
483        int hasMmField=0;
484       
485        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
486          {
487            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
488            if (iField >= 0)
489                poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
490            else
491            {
492                fprintf( stderr, "Field '%s' not found in source layer.\n", 
493                        iField );
494                return SERVICE_FAILED;
495            }
496          }
497
498        while(TRUE){
499          OGRFeature      *poDstFeature = NULL;
500          poFeature = poLayer->GetNextFeature();
501          if( poFeature == NULL )
502            break;
503          if(poFeature->GetGeometryRef() != NULL){
504            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
505            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
506              {
507                char tmpMsg[1024];
508                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
509                         poFeature->GetFID(), poFDefn->GetName() );
510               
511                OGRFeature::DestroyFeature( poFeature );
512                OGRFeature::DestroyFeature( poDstFeature );
513                return SERVICE_FAILED;
514              }
515            if(poDstFeature->SetGeometryDirectly((poDstFeature->GetGeometryRef()->*myFunc)()) != OGRERR_NONE )
516              {
517                char tmpMsg[1024];
518                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
519                         poFeature->GetFID(), poFDefn->GetName() );
520               
521                OGRFeature::DestroyFeature( poFeature );
522                OGRFeature::DestroyFeature( poDstFeature );
523                return SERVICE_FAILED;
524              }
525            OGRFeature::DestroyFeature( poFeature );
526            if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
527              {         
528                OGRFeature::DestroyFeature( poDstFeature );
529                return SERVICE_FAILED;
530              }
531            OGRFeature::DestroyFeature( poDstFeature );
532          }
533        }
534
535      }
536
537    delete poODS;
538    delete ipoDS;
539
540    char *res1=readVSIFile(conf,pszDestDataSource);
541    if(res1==NULL)
542      return SERVICE_FAILED;
543    setMapInMaps(outputs,"Result","value",res1);
544    free(res1);
545
546    OGRCleanupAll();
547    return SERVICE_SUCCEEDED;
548  }
549
550#ifdef WIN32
551  __declspec(dllexport)
552#endif
553int Buffer(maps*& conf,maps*& inputs,maps*& outputs){
554    OGRRegisterAll();
555
556    double bufferDistance;
557    map* tmp0=getMapFromMaps(inputs,"BufferDistance","value");
558    if(tmp0==NULL){
559      bufferDistance=atof("10.0");
560    }
561    else
562      bufferDistance=atof(tmp0->value);
563
564    maps* cursor=inputs;
565    OGRGeometryH geometry,res;
566    OGRLayer *poDstLayer;
567    const char *oDriver1;
568    OGRDataSource       *poODS;
569    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
570    if(!tmp){
571      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
572      return SERVICE_FAILED;
573    }
574    char filename[1024];
575    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
576    const char *oDriver;
577    oDriver="GeoJSON";
578    sprintf(filename,"/vsimem/input_%d.json",getpid());
579    if(tmp1!=NULL){
580      if(strcmp(tmp1->value,"text/xml")==0){
581        sprintf(filename,"/vsimem/input_%d.xml",getpid());
582        oDriver="GML";
583      }
584    }
585    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
586    VSIFCloseL(ifile);
587    OGRDataSource* ipoDS = OGRSFDriverRegistrar::Open(filename,FALSE);
588    char pszDestDataSource[100];
589    if( ipoDS == NULL )
590      {
591        OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
592       
593        fprintf( stderr, "FAILURE:\n"
594                 "Unable to open datasource `%s' with the following drivers.\n",
595                 filename );
596       
597        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
598          {
599            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
600          }
601        char tmp[1024];
602        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
603        setMapInMaps(conf,"lenv","message",tmp);
604        return SERVICE_FAILED;
605      }
606    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
607         iLayer++ )
608      {
609        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
610       
611        if( poLayer == NULL )
612          {
613            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
614                     iLayer );
615            char tmp[1024];
616            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
617            setMapInMaps(conf,"lenv","message",tmp);
618            return SERVICE_FAILED;
619          }
620       
621        OGRFeature  *poFeature;
622
623        /* -------------------------------------------------------------------- */
624        /*      Try opening the output datasource as an existing, writable      */
625        /* -------------------------------------------------------------------- */
626       
627        OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
628        OGRSFDriver          *poDriver = NULL;
629        int                  iDriver;
630       
631        map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
632        oDriver1="GeoJSON";
633        sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
634        if(tmpMap!=NULL){
635          if(strcmp(tmpMap->value,"text/xml")==0){
636            sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
637            oDriver1="GML";
638          }
639        }
640       
641        for( iDriver = 0;
642             iDriver < poR->GetDriverCount() && poDriver == NULL;
643             iDriver++ )
644          {
645            if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver1) )
646              {
647                poDriver = poR->GetDriver(iDriver);
648              }
649          }
650       
651        if( poDriver == NULL )
652          {
653            char emessage[8192];
654            sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
655            sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
656           
657            for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
658              {
659                sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
660              }
661           
662            setMapInMaps(conf,"lenv","message",emessage);
663            return SERVICE_FAILED;
664           
665          }
666       
667        if( !poDriver->TestCapability( ODrCCreateDataSource ) ){
668          char emessage[1024];
669          sprintf( emessage,  "%s driver does not support data source creation.\n",
670                   "json" );
671          setMapInMaps(conf,"lenv","message",emessage);
672          return SERVICE_FAILED;
673        }
674       
675        /* -------------------------------------------------------------------- */
676        /*      Create the output data source.                                  */
677        /* -------------------------------------------------------------------- */
678        //map* tpath=getMapFromMaps(conf,"main","tmpPath");
679        char **papszDSCO=NULL;
680        poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
681        if( poODS == NULL ){
682          char emessage[1024];     
683          sprintf( emessage,  "%s driver failed to create %s\n", 
684                   "json", pszDestDataSource );
685          setMapInMaps(conf,"lenv","message",emessage);
686          return SERVICE_FAILED;
687        }
688       
689        /* -------------------------------------------------------------------- */
690        /*      Create the layer.                                               */
691        /* -------------------------------------------------------------------- */
692        if( !poODS->TestCapability( ODsCCreateLayer ) )
693          {
694            char emessage[1024];
695            sprintf( emessage, 
696                     "Layer %s not found, and CreateLayer not supported by driver.", 
697                     "Result" );
698            setMapInMaps(conf,"lenv","message",emessage);
699            return SERVICE_FAILED;
700          }
701
702        poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
703        if( poDstLayer == NULL ){
704          setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
705          return SERVICE_FAILED;
706        }
707       
708        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
709        int iField;
710        int hasMmField=0;
711       
712        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
713          {
714            OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
715            if (iField >= 0)
716                poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
717            else
718            {
719                fprintf( stderr, "Field '%s' not found in source layer.\n", 
720                        iField );
721                return SERVICE_FAILED;
722            }
723          }
724
725        while(TRUE){
726          OGRFeature      *poDstFeature = NULL;
727          poFeature = poLayer->GetNextFeature();
728          if( poFeature == NULL )
729            break;
730          if(poFeature->GetGeometryRef() != NULL){
731            poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
732            if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
733              {
734                char tmpMsg[1024];
735                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
736                         poFeature->GetFID(), poFDefn->GetName() );
737               
738                OGRFeature::DestroyFeature( poFeature );
739                OGRFeature::DestroyFeature( poDstFeature );
740                return SERVICE_FAILED;
741              }
742            if(poDstFeature->SetGeometryDirectly(poDstFeature->GetGeometryRef()->Buffer(bufferDistance,30)) != OGRERR_NONE )
743              {
744                char tmpMsg[1024];
745                sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
746                         poFeature->GetFID(), poFDefn->GetName() );
747               
748                OGRFeature::DestroyFeature( poFeature );
749                OGRFeature::DestroyFeature( poDstFeature );
750                return SERVICE_FAILED;
751              }
752            OGRFeature::DestroyFeature( poFeature );
753            if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
754              {         
755                OGRFeature::DestroyFeature( poDstFeature );
756                return SERVICE_FAILED;
757              }
758            OGRFeature::DestroyFeature( poDstFeature );
759          }
760        }
761
762      }
763
764    delete poODS;
765    delete ipoDS;
766
767    char *res1=readVSIFile(conf,pszDestDataSource);
768    if(res1==NULL)
769      return SERVICE_FAILED;
770    setMapInMaps(outputs,"Result","value",res1);
771    free(res1);
772
773    OGRCleanupAll();
774    return SERVICE_SUCCEEDED;
775
776}
777
778#ifdef WIN32
779  __declspec(dllexport)
780#endif
781  int Boundary(maps*& conf,maps*& inputs,maps*& outputs){
782    return applyOne(conf,inputs,outputs,&OGRGeometry::Boundary,"http://fooa/gml/3.1.0/polygon.xsd");
783  }
784
785#ifdef WIN32
786  __declspec(dllexport)
787#endif
788  int ConvexHull(maps*& conf,maps*& inputs,maps*& outputs){
789    return applyOne(conf,inputs,outputs,&OGRGeometry::ConvexHull,"http://fooa/gml/3.1.0/polygon.xsd");
790  }
791
792
793  OGRDataSource* loadEntity(maps* conf,maps* inputs,char **filename,const char **oDriver,const char *entity,int iter){
794    map* tmp=getMapFromMaps(inputs,entity,"value");
795    map* tmp1=getMapFromMaps(inputs,entity,"mimeType");
796    *oDriver="GeoJSON";
797    sprintf(*filename,"/vsimem/input_%d.json",getpid()+iter);
798    if(tmp1!=NULL){
799      if(strcmp(tmp1->value,"text/xml")==0){
800        sprintf(*filename,"/vsimem/input_%d.xml",getpid()+iter);
801        *oDriver="GML";
802      }
803    }
804    VSILFILE *ifile=VSIFileFromMemBuffer(*filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
805    VSIFCloseL(ifile);
806    return OGRSFDriverRegistrar::Open(*filename,FALSE);   
807  }
808
809  int applyOneBool(maps*& conf,maps*& inputs,maps*& outputs,OGRBoolean (OGRGeometry::*myFunc)() const){
810#ifdef DEBUG
811    fprintf(stderr,"\nService internal print\n");
812#endif
813    OGRRegisterAll();
814
815    maps* cursor=inputs;
816    OGRGeometryH geometry,res;
817    OGRLayer *poDstLayer;
818    const char *oDriver1;
819    OGRDataSource       *poODS;
820#ifdef DEBUG
821    dumpMaps(cursor);
822#endif
823    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
824    if(!tmp){
825      setMapInMaps(conf,"lenv","message",_ss("Unable to parse the input geometry from InputPolygon"));
826      return SERVICE_FAILED;
827    }
828    char filename[1024];
829    map* tmp1=getMapFromMaps(inputs,"InputPolygon","mimeType");
830    const char *oDriver;
831    oDriver="GeoJSON";
832    sprintf(filename,"/vsimem/input_%d.json",getpid());
833    if(tmp1!=NULL){
834      if(strcmp(tmp1->value,"text/xml")==0){
835        sprintf(filename,"/vsimem/input_%d.xml",getpid());
836        oDriver="GML";
837      }
838    }
839    VSILFILE *ifile=VSIFileFromMemBuffer(filename,(GByte*)tmp->value,strlen(tmp->value),FALSE);
840    VSIFCloseL(ifile);
841    OGRDataSource* ipoDS = OGRSFDriverRegistrar::Open(filename,FALSE);
842    char pszDestDataSource[100];
843    if( ipoDS == NULL )
844      {
845        OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
846       
847        fprintf( stderr, "FAILURE:\n"
848                 "Unable to open datasource `%s' with the following drivers.\n",
849                 filename );
850       
851        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
852          {
853            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
854          }
855        char tmp[1024];
856        sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
857        setMapInMaps(conf,"lenv","message",tmp);
858        return SERVICE_FAILED;
859      }
860    for( int iLayer = 0; iLayer < ipoDS->GetLayerCount();
861         iLayer++ )
862      {
863        OGRLayer        *poLayer = ipoDS->GetLayer(iLayer);
864       
865        if( poLayer == NULL )
866          {
867            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
868                     iLayer );
869            char tmp[1024];
870            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
871            setMapInMaps(conf,"lenv","message",tmp);
872            return SERVICE_FAILED;
873          }
874       
875        OGRFeature  *poFeature;
876
877
878        while(TRUE){
879          OGRFeature      *poDstFeature = NULL;
880          poFeature = poLayer->GetNextFeature();
881          if( poFeature == NULL )
882            break;
883          if(poFeature->GetGeometryRef() != NULL){
884            if((poFeature->GetGeometryRef()->*myFunc)()==0){
885              setMapInMaps(outputs,"Result","value","false");
886              OGRFeature::DestroyFeature( poFeature );
887              delete ipoDS;
888              return SERVICE_SUCCEEDED;
889            }
890          }
891          OGRFeature::DestroyFeature( poFeature );
892        }
893
894      }
895
896    delete ipoDS;
897    setMapInMaps(outputs,"Result","value","true");
898
899    OGRCleanupAll();
900    return SERVICE_SUCCEEDED;
901  }
902
903#ifdef WIN32
904  __declspec(dllexport)
905#endif
906  int IsSimple(maps*& conf,maps*& inputs,maps*& outputs){
907    return applyOneBool(conf,inputs,outputs,&OGRGeometry::IsSimple);
908  }
909
910#ifdef WIN32
911  __declspec(dllexport)
912#endif
913  int IsClosed(maps*& conf,maps*& inputs,maps*& outputs){
914    return applyOneBool(conf,inputs,outputs,&OGRGeometry::IsRing);
915  }
916
917#ifdef WIN32
918  __declspec(dllexport)
919#endif
920  int IsValid(maps*& conf,maps*& inputs,maps*& outputs){
921    return applyOneBool(conf,inputs,outputs,&OGRGeometry::IsValid);
922  }
923
924 
925  int applyTwo(maps*& conf,maps*& inputs,maps*& outputs,OGRGeometry* (OGRGeometry::*myFunc)(const OGRGeometry*) const){
926#ifdef DEBUG
927    fprintf(stderr,"\nService internal print\n");
928#endif
929    OGRRegisterAll();
930
931    maps* cursor=inputs;
932    OGRGeometryH geometry,res;
933    OGRLayer *poDstLayer;
934    //const char *oDriver1;
935    OGRDataSource       *poODS;
936#ifdef DEBUG
937    dumpMaps(cursor);
938#endif
939
940    char *filename=(char*)malloc(1024*sizeof(char));
941    const char *oDriver1;
942    OGRDataSource* ipoDS1 = loadEntity(conf,inputs,&filename,&oDriver1,"InputEntity1",1);
943
944    char *filename1=(char*)malloc(1024*sizeof(char));
945    const char *oDriver2;
946    OGRDataSource* ipoDS2 = loadEntity(conf,inputs,&filename1,&oDriver2,"InputEntity2",2);
947    const char *oDriver3;
948    char pszDestDataSource[100];
949    if( ipoDS1 == NULL || ipoDS2 == NULL )
950      {
951        OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
952       
953        fprintf( stderr, "FAILURE:\n"
954                 "Unable to open datasource `%s' with the following drivers.\n",
955                 filename );
956       
957        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
958          {
959            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
960          }
961        char tmp[1024];
962        if( ipoDS1 == NULL )
963          sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
964        if( ipoDS2 == NULL )
965          sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename1);
966        setMapInMaps(conf,"lenv","message",tmp);
967        return SERVICE_FAILED;
968      }
969    for( int iLayer = 0; iLayer < ipoDS1->GetLayerCount();
970         iLayer++ )
971      {
972        OGRLayer        *poLayer1 = ipoDS1->GetLayer(iLayer);
973       
974        if( poLayer1 == NULL )
975          {
976            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
977                     iLayer );
978            char tmp[1024];
979            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
980            setMapInMaps(conf,"lenv","message",tmp);
981            return SERVICE_FAILED;
982          }
983
984        for( int iLayer1 = 0; iLayer1 < ipoDS2->GetLayerCount();
985             iLayer1++ )
986          {
987            OGRLayer        *poLayer2 = ipoDS2->GetLayer(iLayer1);
988           
989            if( poLayer1 == NULL )
990              {
991                fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
992                         iLayer1 );
993                char tmp[1024];
994                sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer1);
995                setMapInMaps(conf,"lenv","message",tmp);
996                return SERVICE_FAILED;
997              }
998       
999            OGRFeature  *poFeature1,*poFeature2;
1000
1001            /* -------------------------------------------------------------------- */
1002            /*      Try opening the output datasource as an existing, writable      */
1003            /* -------------------------------------------------------------------- */
1004           
1005            OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
1006            OGRSFDriver          *poDriver = NULL;
1007            int                  iDriver;
1008           
1009            map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
1010            oDriver3="GeoJSON";
1011            sprintf(pszDestDataSource,"/vsimem/result_%d.json",getpid());
1012            if(tmpMap!=NULL){
1013              if(strcmp(tmpMap->value,"text/xml")==0){
1014                sprintf(pszDestDataSource,"/vsimem/result_%d.xml",getpid());
1015                oDriver3="GML";
1016              }
1017            }
1018           
1019            for( iDriver = 0;
1020                 iDriver < poR->GetDriverCount() && poDriver == NULL;
1021                 iDriver++ )
1022              {
1023#ifdef DEBUG
1024                fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName());
1025#endif
1026                if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver3) )
1027                  {
1028                    poDriver = poR->GetDriver(iDriver);
1029                  }
1030              }
1031           
1032            if( poDriver == NULL )
1033              {
1034                char emessage[8192];
1035                sprintf( emessage, "Unable to find driver `%s'.\n", oDriver1 );
1036                sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
1037               
1038                for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
1039                  {
1040                    sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
1041                  }
1042               
1043                setMapInMaps(conf,"lenv","message",emessage);
1044                return SERVICE_FAILED;
1045               
1046              }
1047           
1048            if( !poDriver->TestCapability( ODrCCreateDataSource ) ){
1049              char emessage[1024];
1050              sprintf( emessage,  "%s driver does not support data source creation.\n",
1051                       "json" );
1052              setMapInMaps(conf,"lenv","message",emessage);
1053              return SERVICE_FAILED;
1054            }
1055           
1056            /* -------------------------------------------------------------------- */
1057            /*      Create the output data source.                                  */
1058            /* -------------------------------------------------------------------- */
1059            //map* tpath=getMapFromMaps(conf,"main","tmpPath");
1060            char **papszDSCO=NULL;
1061            poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
1062            if( poODS == NULL ){
1063              char emessage[1024];     
1064              sprintf( emessage,  "%s driver failed to create %s\n", 
1065                       "json", pszDestDataSource );
1066              setMapInMaps(conf,"lenv","message",emessage);
1067              return SERVICE_FAILED;
1068            }
1069           
1070            /* -------------------------------------------------------------------- */
1071            /*      Create the layer.                                               */
1072            /* -------------------------------------------------------------------- */
1073            if( !poODS->TestCapability( ODsCCreateLayer ) )
1074              {
1075                char emessage[1024];
1076                sprintf( emessage, 
1077                         "Layer %s not found, and CreateLayer not supported by driver.", 
1078                         "Result" );
1079                setMapInMaps(conf,"lenv","message",emessage);
1080                return SERVICE_FAILED;
1081              }
1082           
1083            //CPLErrorReset();
1084           
1085            poDstLayer = poODS->CreateLayer( "Result", NULL,wkbUnknown,NULL);
1086            if( poDstLayer == NULL ){
1087              setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
1088              return SERVICE_FAILED;
1089            }
1090           
1091            OGRFeatureDefn *poFDefn = poLayer1->GetLayerDefn();
1092            int iField;
1093            int hasMmField=0;
1094           
1095            for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
1096              {
1097                OGRFieldDefn *tmp=poFDefn->GetFieldDefn(iField);
1098                if (iField >= 0)
1099                  poDstLayer->CreateField( poFDefn->GetFieldDefn(iField) );
1100                else
1101                  {
1102                    fprintf( stderr, "Field '%s' not found in source layer.\n", 
1103                             iField );
1104                    return SERVICE_FAILED;
1105                  }
1106              }
1107           
1108            while(TRUE){
1109              OGRFeature      *poDstFeature = NULL;
1110              poFeature1 = poLayer1->GetNextFeature();
1111              if( poFeature1 == NULL )
1112                break;
1113              while(TRUE){
1114                poFeature2 = poLayer2->GetNextFeature();
1115                if( poFeature2 == NULL )
1116                  break;
1117
1118                if(poFeature1->GetGeometryRef() != NULL && poFeature2->GetGeometryRef() != NULL){
1119                  poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
1120                  if( poDstFeature->SetFrom( poFeature2, TRUE ) != OGRERR_NONE )
1121                    {
1122                      char tmpMsg[1024];
1123                      sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
1124                               poFeature2->GetFID(), poFDefn->GetName() );
1125                     
1126                      OGRFeature::DestroyFeature( poFeature1 );
1127                      OGRFeature::DestroyFeature( poFeature2 );
1128                      OGRFeature::DestroyFeature( poDstFeature );
1129                      return SERVICE_FAILED;
1130                    }
1131                  if(poDstFeature->SetGeometryDirectly((poFeature1->GetGeometryRef()->*myFunc)(poFeature2->GetGeometryRef())) != OGRERR_NONE )
1132                    {
1133                      char tmpMsg[1024];
1134                      sprintf( tmpMsg,"Unable to translate feature %ld from layer %s.\n",
1135                               poFeature2->GetFID(), poFDefn->GetName() );
1136                     
1137                      OGRFeature::DestroyFeature( poFeature1 );
1138                      OGRFeature::DestroyFeature( poFeature2 );
1139                      OGRFeature::DestroyFeature( poDstFeature );
1140                      return SERVICE_FAILED;
1141                    }
1142                  OGRFeature::DestroyFeature( poFeature1 );
1143                  OGRFeature::DestroyFeature( poFeature2 );
1144                  if(!poDstFeature->GetGeometryRef()->IsEmpty())
1145                    if( poDstLayer->CreateFeature( poDstFeature ) != OGRERR_NONE )
1146                      {         
1147                        OGRFeature::DestroyFeature( poDstFeature );
1148                        return SERVICE_FAILED;
1149                      }
1150                  OGRFeature::DestroyFeature( poDstFeature );
1151                }
1152              }
1153            }
1154          }
1155      }
1156
1157    delete poODS;
1158    delete ipoDS1;
1159    delete ipoDS2;
1160    free(filename);
1161    free(filename1);
1162
1163    char *res1=readVSIFile(conf,pszDestDataSource);
1164    if(res1==NULL)
1165      return SERVICE_FAILED;
1166    setMapInMaps(outputs,"Result","value",res1);
1167    free(res1);
1168    OGRCleanupAll();
1169    return SERVICE_SUCCEEDED;
1170  }
1171 
1172#ifdef WIN32
1173  __declspec(dllexport)
1174#endif
1175  int Difference(maps*& conf,maps*& inputs,maps*& outputs){
1176    return applyTwo(conf,inputs,outputs,&OGRGeometry::Difference);
1177  }
1178
1179#ifdef WIN32
1180  __declspec(dllexport)
1181#endif
1182  int SymDifference(maps*& conf,maps*& inputs,maps*& outputs){
1183    return applyTwo(conf,inputs,outputs,&OGRGeometry::SymDifference);
1184  }
1185
1186#ifdef WIN32
1187  __declspec(dllexport)
1188#endif
1189  int Intersection(maps*& conf,maps*& inputs,maps*& outputs){
1190    return applyTwo(conf,inputs,outputs,&OGRGeometry::Intersection);
1191  }
1192
1193#ifdef WIN32
1194  __declspec(dllexport)
1195#endif
1196  int Union(maps*& conf,maps*& inputs,maps*& outputs){
1197    return applyTwo(conf,inputs,outputs,&OGRGeometry::Union);
1198  }
1199
1200  int applyTwoBool(maps*& conf,maps*& inputs,maps*& outputs,OGRBoolean (OGRGeometry::*myFunc)(const OGRGeometry*) const){
1201    OGRRegisterAll();
1202
1203    maps* cursor=inputs;
1204    OGRGeometryH geometry,res;
1205    OGRLayer *poDstLayer;
1206    OGRDataSource       *poODS;
1207
1208    char *filename=(char*)malloc(1024*sizeof(char));
1209    const char *oDriver1;
1210    OGRDataSource* ipoDS1 = loadEntity(conf,inputs,&filename,&oDriver1,"InputEntity1",1);
1211
1212    char *filename1=(char*)malloc(1024*sizeof(char));
1213    const char *oDriver2;
1214    OGRDataSource* ipoDS2 = loadEntity(conf,inputs,&filename1,&oDriver2,"InputEntity2",2);
1215    const char *oDriver3;
1216    char pszDestDataSource[100];
1217    if( ipoDS1 == NULL || ipoDS2 == NULL )
1218      {
1219        OGRSFDriverRegistrar    *poR = OGRSFDriverRegistrar::GetRegistrar();
1220       
1221        fprintf( stderr, "FAILURE:\n"
1222                 "Unable to open datasource `%s' with the following drivers.\n",
1223                 filename );
1224       
1225        for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
1226          {
1227            fprintf( stderr, "  -> %s\n", poR->GetDriver(iDriver)->GetName() );
1228          }
1229        char tmp[1024];
1230        if( ipoDS1 == NULL )
1231          sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename);
1232        if( ipoDS2 == NULL )
1233          sprintf(tmp,"Unable to open datasource `%s' with the following drivers.",filename1);
1234        setMapInMaps(conf,"lenv","message",tmp);
1235        return SERVICE_FAILED;
1236      }
1237    for( int iLayer = 0; iLayer < ipoDS1->GetLayerCount();
1238         iLayer++ )
1239      {
1240        OGRLayer        *poLayer1 = ipoDS1->GetLayer(iLayer);
1241       
1242        if( poLayer1 == NULL )
1243          {
1244            fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
1245                     iLayer );
1246            char tmp[1024];
1247            sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer);
1248            setMapInMaps(conf,"lenv","message",tmp);
1249            return SERVICE_FAILED;
1250          }
1251
1252        for( int iLayer1 = 0; iLayer1 < ipoDS2->GetLayerCount();
1253             iLayer1++ )
1254          {
1255            OGRLayer        *poLayer2 = ipoDS2->GetLayer(iLayer1);
1256           
1257            if( poLayer1 == NULL )
1258              {
1259                fprintf( stderr, "FAILURE: Couldn't fetch advertised layer %d!\n",
1260                         iLayer1 );
1261                char tmp[1024];
1262                sprintf(tmp,"Couldn't fetch advertised layer %d!",iLayer1);
1263                setMapInMaps(conf,"lenv","message",tmp);
1264                return SERVICE_FAILED;
1265              }
1266       
1267            OGRFeature  *poFeature1,*poFeature2;
1268
1269
1270            while(TRUE){
1271              OGRFeature      *poDstFeature = NULL;
1272              poFeature1 = poLayer1->GetNextFeature();
1273              if( poFeature1 == NULL )
1274                break;
1275              while(TRUE){
1276                poFeature2 = poLayer2->GetNextFeature();
1277                if( poFeature2 == NULL )
1278                  break;
1279                if(poFeature1->GetGeometryRef() != NULL && poFeature2->GetGeometryRef() != NULL){
1280                  if((poFeature1->GetGeometryRef()->*myFunc)(poFeature2->GetGeometryRef())==0){
1281                    setMapInMaps(outputs,"Result","value","false");
1282                    OGRFeature::DestroyFeature( poFeature1 );
1283                    OGRFeature::DestroyFeature( poFeature2 );
1284                    delete ipoDS1;
1285                    delete ipoDS2;
1286                    free(filename);
1287                    free(filename1);
1288                    return SERVICE_SUCCEEDED;
1289                  }
1290                }
1291                OGRFeature::DestroyFeature( poFeature2 );
1292              }
1293              OGRFeature::DestroyFeature( poFeature1 );
1294            }
1295          }
1296      }
1297
1298    delete ipoDS1;
1299    delete ipoDS2;
1300    free(filename);
1301    free(filename1);
1302
1303    setMapInMaps(outputs,"Result","value","true");
1304
1305    OGRCleanupAll();
1306    return SERVICE_SUCCEEDED;
1307  }
1308
1309
1310#ifdef WIN32
1311  __declspec(dllexport)
1312#endif
1313  int Equals(maps*& conf,maps*& inputs,maps*& outputs){
1314    return applyTwoBool(conf,inputs,outputs,(OGRBoolean (OGRGeometry::*)(const OGRGeometry *) const)&OGRGeometry::Equals);
1315  }
1316
1317#ifdef WIN32
1318  __declspec(dllexport)
1319#endif
1320  int Disjoint(maps*& conf,maps*& inputs,maps*& outputs){
1321    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Disjoint);
1322  }
1323
1324#ifdef WIN32
1325  __declspec(dllexport)
1326#endif
1327  int Touches(maps*& conf,maps*& inputs,maps*& outputs){
1328    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Touches);
1329  }
1330
1331#ifdef WIN32
1332  __declspec(dllexport)
1333#endif
1334  int Crosses(maps*& conf,maps*& inputs,maps*& outputs){
1335    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Crosses);
1336  }
1337
1338#ifdef WIN32
1339  __declspec(dllexport)
1340#endif
1341  int Within(maps*& conf,maps*& inputs,maps*& outputs){
1342    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Within);
1343  }
1344
1345#ifdef WIN32
1346  __declspec(dllexport)
1347#endif
1348  int Contains(maps*& conf,maps*& inputs,maps*& outputs){
1349    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Contains);
1350  }
1351
1352#ifdef WIN32
1353  __declspec(dllexport)
1354#endif
1355  int Overlaps(maps*& conf,maps*& inputs,maps*& outputs){
1356    return applyTwoBool(conf,inputs,outputs,&OGRGeometry::Overlaps);
1357  }
1358
1359#ifdef WIN32
1360  __declspec(dllexport)
1361#endif
1362  int Intersects(maps*& conf,maps*& inputs,maps*& outputs){
1363    return applyTwoBool(conf,inputs,outputs,(OGRBoolean (OGRGeometry::*)(const OGRGeometry *) const)&OGRGeometry::Intersects);
1364  }
1365
1366#ifdef WIN32
1367  __declspec(dllexport)
1368#endif
1369  int Distance(maps*& conf,maps*& inputs,maps*& outputs){
1370#ifdef DEBUG
1371    fprintf(stderr,"\nService internal print1\n");
1372#endif
1373    fflush(stderr);
1374    maps* cursor=inputs;
1375    OGRGeometryH geometry1,geometry2;
1376    double res;
1377    {
1378      map* tmp=getMapFromMaps(inputs,"InputEntity1","value");
1379      map* tmp1=getMapFromMaps(inputs,"InputEntity1","mimeType");
1380#ifdef DEBUG
1381      fprintf(stderr,"MY MAP\n");
1382      dumpMap(tmp1);
1383      dumpMaps(inputs);
1384      fprintf(stderr,"MY MAP\n");
1385#endif
1386      if(tmp1!=NULL){
1387        if(strncmp(tmp1->value,"application/json",16)==0)
1388          geometry1=OGR_G_CreateGeometryFromJson(tmp->value);
1389        else
1390          geometry1=createGeometryFromGML(conf,tmp->value);
1391      }
1392      else
1393        geometry1=createGeometryFromGML(conf,tmp->value);
1394    }
1395    if(geometry1==NULL){
1396      setMapInMaps(conf,"lenv","message",_ss("Unable to parse input geometry for InputEntity1."));
1397      fprintf(stderr,"SERVICE FAILED !\n");
1398      return SERVICE_FAILED;
1399    }
1400    {
1401      map* tmp=getMapFromMaps(inputs,"InputEntity2","value");
1402      map* tmp1=getMapFromMaps(inputs,"InputEntity2","mimeType");
1403#ifdef DEBUG
1404      fprintf(stderr,"MY MAP\n");
1405      dumpMap(tmp1);
1406      dumpMaps(inputs);
1407      fprintf(stderr,"MY MAP\n");
1408#endif
1409      if(tmp1!=NULL){
1410        if(strncmp(tmp1->value,"application/json",16)==0)
1411          geometry2=OGR_G_CreateGeometryFromJson(tmp->value);
1412        else
1413          geometry2=createGeometryFromGML(conf,tmp->value);
1414      }
1415      else
1416        geometry2=createGeometryFromGML(conf,tmp->value);
1417    }
1418    if(geometry2==NULL){
1419      setMapInMaps(conf,"lenv","message",_ss("Unable to parse input geometry for InputEntity2."));
1420      fprintf(stderr,"SERVICE FAILED !\n");
1421      return SERVICE_FAILED;
1422    }
1423    res=OGR_G_Distance(geometry1,geometry2);   
1424    char tmpres[100];
1425    sprintf(tmpres,"%f",res);
1426    setMapInMaps(outputs,"Distance","value",tmpres);
1427    setMapInMaps(outputs,"Distance","dataType","float");
1428#ifdef DEBUG
1429    dumpMaps(outputs);
1430    fprintf(stderr,"\nService internal print\n===\n");
1431#endif
1432    return SERVICE_SUCCEEDED;
1433  }
1434
1435#ifdef WIN32
1436  __declspec(dllexport)
1437#endif
1438  int GetArea(maps*& conf,maps*& inputs,maps*& outputs){
1439    fprintf(stderr,"GETAREA \n");
1440    double res;
1441    /**
1442     * Extract Geometry from the InputPolygon value
1443     */
1444    OGRGeometryH geometry;
1445    map* tmp=getMapFromMaps(inputs,"InputPolygon","value");
1446    if(tmp==NULL){
1447      setMapInMaps(conf,"lenv","message",_ss("Unable to parse input geometry from InputPolygon"));
1448      return SERVICE_FAILED;
1449    }
1450    fprintf(stderr,"geometry creation %s \n",tmp->value);
1451    geometry=createGeometryFromGML(conf,tmp->value);
1452    if(geometry==NULL){
1453      setMapInMaps(conf,"lenv","message",_ss("Unable to parse input geometry from InputPolygon"));
1454      return SERVICE_FAILED;
1455    }
1456    fprintf(stderr,"geometry created %s \n",tmp->value);
1457    res=OGR_G_Area(geometry);
1458    fprintf(stderr,"area %d \n",res);
1459    /**
1460     * Filling the outputs
1461     */
1462    char tmp1[100];
1463    sprintf(tmp1,"%f",res);
1464    setMapInMaps(outputs,"Area","value",tmp1);
1465    setMapInMaps(outputs,"Area","dataType","float");
1466#ifdef DEBUG
1467    dumpMaps(outputs);
1468#endif
1469    return SERVICE_SUCCEEDED;
1470  }
1471
1472}
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