source: branches/branch-1.3/zoo-project/zoo-services/cgal/service.c @ 645

Last change on this file since 645 was 425, checked in by djay, 11 years ago

Add missing header.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 9.7 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright 2009-2013 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 <CGAL/Exact_predicates_inexact_constructions_kernel.h>
26#include <CGAL/Triangulation_euclidean_traits_xy_3.h>
27#include <CGAL/Delaunay_triangulation_2.h>
28#include <CGAL/Constrained_Delaunay_triangulation_2.h>
29#include <CGAL/Triangulation_conformer_2.h>
30#include <CGAL/Triangulation_face_base_2.h>
31
32#include <fstream>
33
34#include "cpl_minixml.h"
35#include "ogr_api.h"
36#include "ogrsf_frmts.h"
37#include "service.h"
38
39typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
40
41typedef CGAL::Delaunay_triangulation_2<K>  Triangulation;
42typedef Triangulation::Edge_iterator  Edge_iterator;
43typedef Triangulation::Point          Point;
44
45typedef CGAL::Constrained_Delaunay_triangulation_2<K> CDT;
46typedef CDT::Point Point;
47typedef CDT::Vertex_handle Vertex_handle;
48
49typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
50typedef CGAL::Triangulation_euclidean_traits_xy_3<K>  Gt;
51typedef CGAL::Delaunay_triangulation_2<Gt> DelaunayTriangulation;
52
53typedef Triangulation::Vertex_circulator Vertex_circulator;
54
55typedef K::Point_3   Point1;
56
57extern "C" {
58#include <libxml/tree.h>
59#include <libxml/parser.h>
60#include <libxml/xpath.h>
61#include <libxml/xpathInternals.h>
62
63#include <openssl/sha.h>
64#include <openssl/hmac.h>
65#include <openssl/evp.h>
66#include <openssl/bio.h>
67#include <openssl/buffer.h>
68
69  xmlNodeSet* extractFromDoc(xmlDocPtr,char*);
70  void printExceptionReportResponse(maps*,map*);
71
72  int Voronoi(maps*& conf,maps*& inputs,maps*& outputs){
73#ifdef DEBUG
74    fprintf(stderr,"\nService internal print\nStarting\n");
75#endif
76    maps* cursor=inputs;
77    OGRGeometryH geometry,res;
78    int bufferDistance;
79    xmlInitParser();
80    map* tmpm=NULL;
81    tmpm=getMapFromMaps(inputs,"InputPoints","value");
82   
83    xmlInitParser();
84    xmlDocPtr doc =
85      xmlParseMemory(tmpm->value,strlen(tmpm->value));
86    xmlNodePtr cur = xmlDocGetRootElement(doc);
87    /**
88     * Parse every Input in DataInputs node.
89     */
90    maps* tempMaps=NULL;
91    xmlXPathContextPtr xpathCtx;
92    xmlXPathObjectPtr xpathObj;
93    xpathCtx = xmlXPathNewContext(doc);
94    xpathObj = xmlXPathEvalExpression(BAD_CAST "/*/*[local-name()='featureMember']/*/*/*[local-name()='Point']/*[local-name()='coordinates']",xpathCtx);
95    xmlXPathFreeContext(xpathCtx); 
96    xmlNodeSet* nSet=xpathObj->nodesetval;
97
98    if(nSet==NULL){
99      setMapInMaps(conf,"lenv","message","Unable to continue !!!");
100      return SERVICE_FAILED;
101    }
102    char filepath[2048];
103    map* tmpMap=getMapFromMaps(conf,"main","tmpPath");
104    if(tmpMap!=NULL){
105      sprintf(filepath,"%s/varonoi_%d.tmp",tmpMap->value,getpid());
106    }
107    FILE *fo=fopen(filepath,"w");
108#ifdef DEBUG
109    fprintf(stderr,"File Creation (%s) OK\nPrinting %d Points.\n",filepath,nSet->nodeNr);
110#endif
111    for(int k=0;k<nSet->nodeNr;k++){
112      xmlNodePtr cur=nSet->nodeTab[k];
113      char *val=
114        (char*)xmlNodeListGetString(doc,cur->xmlChildrenNode,1);
115      char *tmp=strstr(val,",");
116      char tmp1[1024];
117      strncpy(tmp1,val,strlen(val)-strlen(tmp));
118      tmp1[strlen(val)-strlen(tmp)]=0;
119      char buff[1024];
120      sprintf(buff,"%s %s\n",tmp1,tmp+1);
121      fwrite(buff,1,strlen(buff)*sizeof(char),fo);
122    }
123    fclose(fo);
124#ifdef DEBUG
125    fprintf(stderr,"File Close (%s) OK\n",filepath);
126#endif
127
128    std::ifstream in(filepath);
129    std::istream_iterator<Point> begin(in);
130    std::istream_iterator<Point> end;
131    Triangulation T;
132    T.insert(begin, end);
133
134    OGRRegisterAll();
135    /* -------------------------------------------------------------------- */
136    /*      Try opening the output datasource as an existing, writable      */
137    /* -------------------------------------------------------------------- */
138    OGRDataSource       *poODS;
139   
140    OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
141    OGRSFDriver          *poDriver = NULL;
142    int                  iDriver;
143
144    tmpMap=getMapFromMaps(outputs,"Result","mimeType");
145    const char *oDriver;
146    oDriver="GeoJSON";
147    if(tmpMap!=NULL){
148      if(strcmp(tmpMap->value,"text/xml")==0){
149        oDriver="GML";
150      }
151    }
152   
153    for( iDriver = 0;
154         iDriver < poR->GetDriverCount() && poDriver == NULL;
155         iDriver++ )
156      {
157#ifdef DEBUG
158        fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName());
159#endif
160        if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver) )
161          {
162            poDriver = poR->GetDriver(iDriver);
163          }
164      }
165
166    if( poDriver == NULL )
167      {
168        char emessage[8192];
169        sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
170        sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
171       
172        for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
173          {
174            sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
175          }
176
177        setMapInMaps(conf,"lenv","message",emessage);
178        return SERVICE_FAILED;
179
180      }
181
182    if( !poDriver->TestCapability( ODrCCreateDataSource ) ){
183      char emessage[1024];
184      sprintf( emessage,  "%s driver does not support data source creation.\n",
185               "json" );
186      setMapInMaps(conf,"lenv","message",emessage);
187      return SERVICE_FAILED;
188    }
189
190    /* -------------------------------------------------------------------- */
191    /*      Create the output data source.                                  */
192    /* -------------------------------------------------------------------- */
193    map* tpath=getMapFromMaps(conf,"main","tmpPath");
194    char *pszDestDataSource=(char*)malloc(strlen(tpath->value)+20);
195    char **papszDSCO=NULL;
196    sprintf(pszDestDataSource,"%s/result_%d.json",tpath->value,getpid());
197    poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
198    if( poODS == NULL ){
199      char emessage[1024];     
200      sprintf( emessage,  "%s driver failed to create %s\n", 
201               "json", pszDestDataSource );
202      setMapInMaps(conf,"lenv","message",emessage);
203      return SERVICE_FAILED;
204    }
205
206    /* -------------------------------------------------------------------- */
207    /*      Create the layer.                                               */
208    /* -------------------------------------------------------------------- */
209    if( !poODS->TestCapability( ODsCCreateLayer ) )
210      {
211        char emessage[1024];
212        sprintf( emessage, 
213                 "Layer %s not found, and CreateLayer not supported by driver.", 
214                 "Result" );
215        setMapInMaps(conf,"lenv","message",emessage);
216        return SERVICE_FAILED;
217      }
218   
219    CPLErrorReset();
220   
221    OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbLineString,NULL);
222    if( poDstLayer == NULL ){
223      setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
224      return SERVICE_FAILED;
225    }
226
227
228    int ns = 0;
229    int nr = 0;
230    Edge_iterator eit =T.edges_begin();
231    for ( ; eit !=T.edges_end(); ++eit) {
232      CGAL::Object o = T.dual(eit);
233      if (const K::Segment_2 *tmp=CGAL::object_cast<K::Segment_2>(&o)) {
234        const K::Point_2 p1=tmp->source();
235        const K::Point_2 p2=tmp->target();
236#ifdef DEBUG
237        fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y());
238#endif
239        OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) );
240        OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString);
241        OGR_G_AddPoint_2D(currLine,p1.x(),p1.y());
242        OGR_G_AddPoint_2D(currLine,p2.x(),p2.y());
243        OGR_F_SetGeometry( hFeature, currLine ); 
244        OGR_G_DestroyGeometry(currLine);
245        if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){
246          setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n");
247          return SERVICE_FAILED;
248        }
249        OGR_F_Destroy( hFeature );
250        ++ns ;
251      }
252      else if (CGAL::object_cast<K::Ray_2>(&o)) {++nr;}
253    }
254    OGR_DS_Destroy( poODS );
255    OGRCleanupAll();
256
257#ifdef DEBUG
258    std::cerr << "The Voronoi diagram has " << ns << " finite edges "
259              << " and " << nr << " rays" << std::endl;
260    sprintf(tmp1,"%d finite edges, %d rays",ns,nr);
261#endif
262   
263    char tmp1[1024];
264
265    FILE * fichier=fopen(pszDestDataSource,"r"); 
266    struct stat file_status;
267    stat(pszDestDataSource, &file_status);
268    char *res1=(char *)malloc(file_status.st_size*sizeof(char));
269    if(fichier==NULL){
270      char tmp[1024];
271      sprintf(tmp,"Failed to open file %s for reading purpose.\n",
272              pszDestDataSource);
273      setMapInMaps(conf,"lenv","message",tmp);
274      return SERVICE_FAILED;
275    }
276    fread(res1,1,(file_status.st_size)*sizeof(char),fichier);
277    res1[strlen(res1)]=0;
278    fclose(fichier);
279    unlink(pszDestDataSource);
280   
281    setMapInMaps(outputs,"Result","value",res1);
282   
283    if(strcmp(oDriver,"GML")==0)
284      setMapInMaps(outputs,"Result","mimeType","text/xml");
285    else
286      setMapInMaps(outputs,"Result","mimeType","text/plain");
287
288    setMapInMaps(outputs,"Result","encoding","UTF-8");
289#ifdef DEBUG
290    fprintf(stderr,"\nService internal print\n===\n");
291#endif
292    xmlCleanupParser();
293    return SERVICE_SUCCEEDED;
294  }
295
296}
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png