source: branches/branch-1.4/zoo-project/zoo-services/cgal/voronoi.c @ 822

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

Created branch/tags for the 1.4.0 release

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