source: tags/rel-1.8.0/zoo-project/zoo-services/cgal/voronoi.c @ 981

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

Create tag and update branch for 1.8.0 release

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