source: branches/branch-1.4/zoo-project/zoo-services/cgal/delaunay.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

File size: 6.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
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include "cgal_service.h"
26typedef CGAL::Triangulation_vertex_base_with_info_2<unsigned int, Kernel> Vb;
27typedef CGAL::Triangulation_data_structure_2<Vb>                       Tds;
28typedef CGAL::Delaunay_triangulation_2<Kernel, Tds>                    DelaunayT;
29
30extern "C" {
31
32  int Delaunay(maps*& conf,maps*& inputs,maps*& outputs){
33#ifdef DEBUG
34    fprintf(stderr,"\nService internal print\nStarting\n");
35#endif
36    maps* cursor=inputs;
37    OGRGeometryH geometry,res;
38    int bufferDistance;
39    map* tmpm=NULL;
40    OGRRegisterAll();
41
42    std::vector<Point> points;
43    if(int res=parseInput(conf,inputs,&points,"/vsimem/tmp")!=SERVICE_SUCCEEDED)
44      return res;
45
46    DelaunayT T;
47    T.insert(points.begin(), points.end());
48
49    /* -------------------------------------------------------------------- */
50    /*      Try opening the output datasource as an existing, writable      */
51    /* -------------------------------------------------------------------- */
52    OGRDataSource       *poODS;
53   
54    OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
55    OGRSFDriver          *poDriver = NULL;
56    int                  iDriver;
57
58    map *tmpMap=getMapFromMaps(outputs,"Result","mimeType");
59    const char *oDriver;
60    oDriver="GeoJSON";
61    if(tmpMap!=NULL){
62      if(strcmp(tmpMap->value,"text/xml")==0){
63        oDriver="GML";
64      }
65    }
66   
67    for( iDriver = 0;
68         iDriver < poR->GetDriverCount() && poDriver == NULL;
69         iDriver++ )
70      {
71#ifdef DEBUG
72        fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName());
73#endif
74        if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver) )
75          {
76            poDriver = poR->GetDriver(iDriver);
77          }
78      }
79
80    if( poDriver == NULL )
81      {
82        char emessage[8192];
83        sprintf( emessage, "Unable to find driver `%s'.\n", oDriver );
84        sprintf( emessage,  "%sThe following drivers are available:\n",emessage );
85       
86        for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
87          {
88            sprintf( emessage,  "%s  -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() );
89          }
90
91        setMapInMaps(conf,"lenv","message",emessage);
92        return SERVICE_FAILED;
93
94      }
95
96    if( !poDriver->TestCapability( ODrCCreateDataSource ) ){
97      char emessage[1024];
98      sprintf( emessage,  "%s driver does not support data source creation.\n",
99               "json" );
100      setMapInMaps(conf,"lenv","message",emessage);
101      return SERVICE_FAILED;
102    }
103
104    /* -------------------------------------------------------------------- */
105    /*      Create the output data source.                                  */
106    /* -------------------------------------------------------------------- */
107    char *pszDestDataSource=(char*)malloc(100);
108    char **papszDSCO=NULL;
109    sprintf(pszDestDataSource,"/vsimem/result_%d",getpid());
110    poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO );
111    if( poODS == NULL ){
112      char emessage[1024];     
113      sprintf( emessage,  "%s driver failed to create %s\n", 
114               "json", pszDestDataSource );
115      setMapInMaps(conf,"lenv","message",emessage);
116      return SERVICE_FAILED;
117    }
118
119    /* -------------------------------------------------------------------- */
120    /*      Create the layer.                                               */
121    /* -------------------------------------------------------------------- */
122    if( !poODS->TestCapability( ODsCCreateLayer ) )
123      {
124        char emessage[1024];
125        sprintf( emessage, 
126                 "Layer %s not found, and CreateLayer not supported by driver.", 
127                 "Result" );
128        setMapInMaps(conf,"lenv","message",emessage);
129        return SERVICE_FAILED;
130      }
131   
132    CPLErrorReset();
133   
134    OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbPolygon,NULL);
135    if( poDstLayer == NULL ){
136      setMapInMaps(conf,"lenv","message","Layer creation failed.\n");
137      return SERVICE_FAILED;
138    }
139
140
141    for(DelaunayT::Finite_faces_iterator fit = T.finite_faces_begin();
142        fit != T.finite_faces_end(); ++fit) {
143      DelaunayT::Face_handle face = fit;
144      OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) );
145      OGRGeometryH hCollection = OGR_G_CreateGeometry( wkbGeometryCollection );
146      OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLinearRing);
147      OGRGeometryH currPoly=OGR_G_CreateGeometry(wkbPolygon);
148      OGR_G_AddPoint_2D(currLine,T.triangle(face)[0].x(),T.triangle(face)[0].y());
149      OGR_G_AddPoint_2D(currLine,T.triangle(face)[1].x(),T.triangle(face)[1].y());
150      OGR_G_AddPoint_2D(currLine,T.triangle(face)[2].x(),T.triangle(face)[2].y());
151      OGR_G_AddPoint_2D(currLine,T.triangle(face)[0].x(),T.triangle(face)[0].y());
152      OGR_G_AddGeometryDirectly( currPoly, currLine ); 
153      OGR_G_AddGeometryDirectly( hCollection, currPoly ); 
154      OGR_F_SetGeometry( hFeature, hCollection ); 
155      OGR_G_DestroyGeometry(hCollection);
156      if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){
157        setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n");
158        return SERVICE_FAILED;
159      }
160      OGR_F_Destroy( hFeature );
161    }
162    OGR_DS_Destroy( poODS );
163
164#ifdef DEBUG
165    std::cerr << "The Voronoi diagram has " << ns << " finite edges "
166              << " and " << nr << " rays" << std::endl;
167#endif
168
169    char *res1=readVSIFile(conf,pszDestDataSource);
170    if(res1==NULL)
171      return SERVICE_FAILED;
172
173    setMapInMaps(outputs,"Result","value",res1);
174    free(res1);
175
176    if(strcmp(oDriver,"GML")==0)
177      setMapInMaps(outputs,"Result","mimeType","text/xml");
178    else
179      setMapInMaps(outputs,"Result","mimeType","application/json");
180
181    setMapInMaps(outputs,"Result","encoding","UTF-8");
182    OGRCleanupAll();
183   
184    return SERVICE_SUCCEEDED;
185  }
186
187}
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