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

Last change on this file since 775 was 775, checked in by djay, 8 years ago

Add GDAL 2 support to the cgal services. Updadte otb support to build against OTB 5.4.

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