source: branches/prototype-v0/zoo-project/zoo-services/cgal/voronoi.c @ 862

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

Add the capability to publish heatmap or any templated mapfile using the epecific msInclude and msLayer keys for an output. For MapServer? published output, define 4096 as the default maxsize and use pixel width or height for raster files. use the correct MapServer? imagemode depending on GDALGetRasterDataType (MS_IMAGEMODE_BYTE for GDT_Byte, MS_IMAGEMODE_INT16 for GDT_Int16 and MS_IMAGEMODE_FLOAT32 for GDT_Float32). Create a text file (.maps) listing every mapfiles created for a MapServer? published output (or inputs) using saveMapNames function. Fixes in ulinet, use uuid for naming temporary files. Add dialect input to the ogr2ogr service. Use the .maps file for removing a file from the DeleteData? service

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