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

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

Merge prototype-v0 branch in trunk

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 9.1 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    Face_iterator fit =T.faces_begin();
201    FILE* f=fopen("/tmp/toto.log","w+");
202    for ( ; fit !=T.faces_end(); ++fit) {
203      fprintf(stderr," *** %s %d %d %d\n",__FILE__,__LINE__,nf,fit.info());
204      fflush(stderr);
205     
206      nf++;
207    }
208    fclose(f);
209    Edge_iterator eit =T.edges_begin();
210    for ( ; eit !=T.edges_end(); ++eit) {
211      CGAL::Object o = T.dual(eit);
212      if (const Kernel::Segment_2 *tmp=CGAL::object_cast<Kernel::Segment_2>(&o)) {
213        const Pointz p1=tmp->source();
214        const Pointz p2=tmp->target();
215        fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y());
216#ifdef DEBUG
217        fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y());
218#endif
219        OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) );
220        OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString);
221        OGR_G_AddPoint_2D(currLine,p1.x(),p1.y());
222        OGR_G_AddPoint_2D(currLine,p2.x(),p2.y());
223        OGR_F_SetGeometry( hFeature, currLine ); 
224        OGR_G_DestroyGeometry(currLine);
225        if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){
226          setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n");
227          return SERVICE_FAILED;
228        }
229        OGR_F_Destroy( hFeature );
230        ++ns ;
231      }
232      else if (const Kernel::Ray_2 *tmp=CGAL::object_cast<Kernel::Ray_2>(&o)) { 
233        const Pointz p1=tmp->source();
234        const Pointz p2=tmp->point(2);
235        OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) );
236        OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString);
237        OGR_G_AddPoint_2D(currLine,p1.x(),p1.y());
238        OGR_G_AddPoint_2D(currLine,p2.x(),p2.y());
239        OGR_F_SetGeometry( hFeature, currLine );
240        OGR_G_DestroyGeometry(currLine);
241        if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){
242          setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n");
243          return SERVICE_FAILED;
244        }
245        OGR_F_Destroy( hFeature );
246        ++nr; 
247      }
248    }
249    OGR_DS_Destroy( poODS );
250
251#ifdef DEBUG
252    std::cerr << "The Voronoi diagram has " << ns << " finite edges "
253              << " and " << nr << " rays" << std::endl;
254    sprintf(tmp1,"%d finite edges, %d rays",ns,nr);
255#endif
256   
257
258    char *res1=readVSIFile(conf,pszDestDataSource);
259    if(res1==NULL)
260      return SERVICE_FAILED;
261   
262    setMapInMaps(outputs,"Result","value",res1);
263   
264    if(strcmp(oDriver,"GML")==0)
265      setMapInMaps(outputs,"Result","mimeType","text/xml");
266    else
267      setMapInMaps(outputs,"Result","mimeType","application/json");
268
269    setMapInMaps(outputs,"Result","encoding","UTF-8");
270#ifdef DEBUG
271    fprintf(stderr,"\nService internal print\n===\n");
272#endif
273    //OGRCleanupAll();
274    return SERVICE_SUCCEEDED;
275  }
276
277}
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