source: trunk/zoo-project/zoo-services/cgal/voronoi.c @ 982

Last change on this file since 982 was 982, checked in by djay, 9 months ago

Update version 1.8.1-dev, use demos from github and build basic services in the Dockerfile

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 8.8 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_2.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#include "cgal_service.h"
39
40typedef CGAL::Delaunay_triangulation_2<Kernel>  Triangulation;
41typedef Triangulation::Face_iterator  Face_iterator;
42typedef Triangulation::Edge_iterator  Edge_iterator;
43typedef Triangulation::Vertex_circulator Vertex_circulator;
44
45extern "C" {
46
47  int Voronoi(maps*& conf,maps*& inputs,maps*& outputs){
48#ifdef DEBUG
49    fprintf(stderr,"\nService internal print\nStarting\n");
50#endif
51    //return SERVICE_FAILED;
52    maps* cursor=inputs;
53    OGRGeometryH geometry,res;
54    int bufferDistance;
55    map* tmpm=NULL;
56    tmpm=getMapFromMaps(inputs,"InputPoints","value");
57
58#ifdef DEBUG
59    fprintf(stderr," **** %s %d\n",__FILE__,__LINE__);
60    fflush(stderr);
61#endif
62
63    OGRRegisterAll();
64
65    std::vector<Pointz> points;
66    if(int res=parseInput(conf,inputs,&points,"/vsimem/tmp")!=SERVICE_SUCCEEDED){
67      fprintf(stderr," **** %s %d\n",__FILE__,__LINE__);
68      fflush(stderr);
69      return SERVICE_FAILED;
70    }
71#ifdef DEBUG
72    fprintf(stderr," **** %s %d\n",__FILE__,__LINE__);
73    fflush(stderr);
74#endif
75   
76    Triangulation T;
77    T.insert(points.begin(), points.end());
78
79    //OGRRegisterAll();
80    /* -------------------------------------------------------------------- */
81    /*      Try opening the output datasource as an existing, writable      */
82    /* -------------------------------------------------------------------- */
83#if GDAL_VERSION_MAJOR >= 2
84    GDALDataset *poODS;
85    GDALDriverManager* poR=GetGDALDriverManager();
86    GDALDriver          *poDriver = NULL;
87#else
88    OGRDataSource       *poODS;   
89    OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
90    OGRSFDriver          *poDriver = NULL;
91#endif
92    int                  iDriver;
93    map* tmpMap=getMapFromMaps(outputs,"Result","mimeType");
94    const char *oDriver;
95    oDriver="GeoJSON";
96    if(tmpMap!=NULL){
97      if(strcmp(tmpMap->value,"text/xml")==0){
98        oDriver="GML";
99      }
100    }
101   
102    for( iDriver = 0;
103         iDriver < poR->GetDriverCount() && poDriver == NULL;
104         iDriver++ )
105      {
106#ifdef DEBUG
107#if GDAL_VERSION_MAJOR >= 2
108        fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetDescription());
109#else
110        fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName());
111#endif
112#endif
113        if( EQUAL(
114#if GDAL_VERSION_MAJOR >= 2
115                  poR->GetDriver(iDriver)->GetDescription()
116#else
117                  poR->GetDriver(iDriver)->GetName()
118#endif
119                  ,
120                  oDriver) )
121          {
122            poDriver = poR->GetDriver(iDriver);
123          }
124      }
125
126    if( poDriver == NULL )
127      {
128        char emessage[8192];
129        sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
130        sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
131       
132        for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
133          {
134#if GDAL_VERSION_MAJOR >= 2
135            sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() );
136#else
137            sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
138#endif
139          }
140
141        setMapInMaps(conf,"lenv","message",emessage);
142        return SERVICE_FAILED;
143
144      }
145
146#if GDAL_VERSION_MAJOR >=2
147    if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) )
148#else
149    if( !poDriver->TestCapability( ODrCCreateDataSource ) )
150#endif
151      {
152        char emessage[1024];
153        sprintf( emessage,  "%s driver does not support data source creation.\n",
154                 "json" );
155        setMapInMaps(conf,"lenv","message",emessage);
156        return SERVICE_FAILED;
157      }
158
159    /* -------------------------------------------------------------------- */
160    /*      Create the output data source.                                  */
161    /* -------------------------------------------------------------------- */
162    map* tpath=getMapFromMaps(conf,"main","tmpPath");
163    char *pszDestDataSource=(char*)malloc(100);
164    char **papszDSCO=NULL;
165    sprintf(pszDestDataSource,"/vsimem/result_%d.json",tpath->value,getpid());
166#if GDAL_VERSION_MAJOR >=2
167    poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO );
168#else
169    poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
170#endif
171    if( poODS == NULL ){
172      char emessage[1024];     
173      sprintf( emessage,  "%s driver failed to create %s\n", 
174               "json", pszDestDataSource );
175      setMapInMaps(conf,"lenv","message",emessage);
176      return SERVICE_FAILED;
177    }
178
179    /* -------------------------------------------------------------------- */
180    /*      Create the layer.                                               */
181    /* -------------------------------------------------------------------- */
182    if( !poODS->TestCapability( ODsCCreateLayer ) )
183      {
184        char emessage[1024];
185        sprintf( emessage, 
186                 "Layer %s not found, and CreateLayer not supported by driver.", 
187                 "Result" );
188        setMapInMaps(conf,"lenv","message",emessage);
189        return SERVICE_FAILED;
190      }
191   
192    CPLErrorReset();
193   
194    OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbLineString,NULL);
195    if( poDstLayer == NULL ){
196      setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
197      return SERVICE_FAILED;
198    }
199
200
201    int ns = 0;
202    int nr = 0;
203    int nf = 0;
204    Edge_iterator eit =T.edges_begin();
205    for ( ; eit !=T.edges_end(); ++eit) {
206      CGAL::Object o = T.dual(eit);
207      if (const Kernel::Segment_2 *tmp=CGAL::object_cast<Kernel::Segment_2>(&o)) {
208        const Pointz p1=tmp->source();
209        const Pointz p2=tmp->target();
210#ifdef DEBUG
211        fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y());
212#endif
213        OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) );
214        OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString);
215        OGR_G_AddPoint_2D(currLine,p1.x(),p1.y());
216        OGR_G_AddPoint_2D(currLine,p2.x(),p2.y());
217        OGR_F_SetGeometry( hFeature, currLine ); 
218        OGR_G_DestroyGeometry(currLine);
219        if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){
220          setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n");
221          return SERVICE_FAILED;
222        }
223        OGR_F_Destroy( hFeature );
224        ++ns ;
225      }
226      else if (const Kernel::Ray_2 *tmp=CGAL::object_cast<Kernel::Ray_2>(&o)) { 
227        const Pointz p1=tmp->source();
228        const Pointz p2=tmp->point(2);
229        OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) );
230        OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString);
231        OGR_G_AddPoint_2D(currLine,p1.x(),p1.y());
232        OGR_G_AddPoint_2D(currLine,p2.x(),p2.y());
233        OGR_F_SetGeometry( hFeature, currLine );
234        OGR_G_DestroyGeometry(currLine);
235        if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){
236          setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n");
237          return SERVICE_FAILED;
238        }
239        OGR_F_Destroy( hFeature );
240        ++nr; 
241      }
242    }
243    OGR_DS_Destroy( poODS );
244
245#ifdef DEBUG
246    std::cerr << "The Voronoi diagram has " << ns << " finite edges "
247              << " and " << nr << " rays" << std::endl;
248    sprintf(tmp1,"%d finite edges, %d rays",ns,nr);
249#endif
250   
251
252    char *res1=readVSIFile(conf,pszDestDataSource);
253    if(res1==NULL)
254      return SERVICE_FAILED;
255   
256    setMapInMaps(outputs,"Result","value",res1);
257   
258    if(strcmp(oDriver,"GML")==0)
259      setMapInMaps(outputs,"Result","mimeType","text/xml");
260    else
261      setMapInMaps(outputs,"Result","mimeType","application/json");
262
263    setMapInMaps(outputs,"Result","encoding","UTF-8");
264#ifdef DEBUG
265    fprintf(stderr,"\nService internal print\n===\n");
266#endif
267    //OGRCleanupAll();
268    return SERVICE_SUCCEEDED;
269  }
270
271}
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