[425] | 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 | |
---|
[1] | 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" |
---|
[469] | 38 | #include "cgal_service.h" |
---|
[1] | 39 | |
---|
[469] | 40 | typedef CGAL::Delaunay_triangulation_2<Kernel> Triangulation; |
---|
[1] | 41 | typedef Triangulation::Edge_iterator Edge_iterator; |
---|
| 42 | typedef Triangulation::Vertex_circulator Vertex_circulator; |
---|
| 43 | |
---|
| 44 | extern "C" { |
---|
| 45 | |
---|
[115] | 46 | int Voronoi(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
| 47 | #ifdef DEBUG |
---|
[1] | 48 | fprintf(stderr,"\nService internal print\nStarting\n"); |
---|
[115] | 49 | #endif |
---|
[1] | 50 | maps* cursor=inputs; |
---|
| 51 | OGRGeometryH geometry,res; |
---|
| 52 | int bufferDistance; |
---|
| 53 | map* tmpm=NULL; |
---|
| 54 | tmpm=getMapFromMaps(inputs,"InputPoints","value"); |
---|
| 55 | |
---|
[469] | 56 | OGRRegisterAll(); |
---|
[1] | 57 | |
---|
[469] | 58 | std::vector<Point> points; |
---|
| 59 | if(int res=parseInput(conf,inputs,&points,"/vsimem/tmp")!=SERVICE_SUCCEEDED) |
---|
| 60 | return res; |
---|
| 61 | |
---|
[1] | 62 | Triangulation T; |
---|
[469] | 63 | T.insert(points.begin(), points.end()); |
---|
[1] | 64 | |
---|
| 65 | OGRRegisterAll(); |
---|
| 66 | /* -------------------------------------------------------------------- */ |
---|
| 67 | /* Try opening the output datasource as an existing, writable */ |
---|
| 68 | /* -------------------------------------------------------------------- */ |
---|
[775] | 69 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 70 | GDALDataset *poODS; |
---|
| 71 | GDALDriverManager* poR=GetGDALDriverManager(); |
---|
| 72 | GDALDriver *poDriver = NULL; |
---|
| 73 | #else |
---|
| 74 | OGRDataSource *poODS; |
---|
[1] | 75 | OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar(); |
---|
| 76 | OGRSFDriver *poDriver = NULL; |
---|
[775] | 77 | #endif |
---|
[1] | 78 | int iDriver; |
---|
[469] | 79 | map* tmpMap=getMapFromMaps(outputs,"Result","mimeType"); |
---|
[115] | 80 | const char *oDriver; |
---|
[1] | 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 |
---|
[775] | 93 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 94 | fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetDescription()); |
---|
| 95 | #else |
---|
[1] | 96 | fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName()); |
---|
| 97 | #endif |
---|
[775] | 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) ) |
---|
[1] | 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 | { |
---|
[775] | 120 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 121 | sprintf( emessage, "%s -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() ); |
---|
| 122 | #else |
---|
[1] | 123 | sprintf( emessage, "%s -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() ); |
---|
[775] | 124 | #endif |
---|
[1] | 125 | } |
---|
[115] | 126 | |
---|
| 127 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 128 | return SERVICE_FAILED; |
---|
| 129 | |
---|
[1] | 130 | } |
---|
| 131 | |
---|
[775] | 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 | } |
---|
[1] | 144 | |
---|
| 145 | /* -------------------------------------------------------------------- */ |
---|
| 146 | /* Create the output data source. */ |
---|
| 147 | /* -------------------------------------------------------------------- */ |
---|
| 148 | map* tpath=getMapFromMaps(conf,"main","tmpPath"); |
---|
[469] | 149 | char *pszDestDataSource=(char*)malloc(100); |
---|
[1] | 150 | char **papszDSCO=NULL; |
---|
[469] | 151 | sprintf(pszDestDataSource,"/vsimem/result_%d.json",tpath->value,getpid()); |
---|
[775] | 152 | #if GDAL_VERSION_MAJOR >=2 |
---|
| 153 | poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO ); |
---|
| 154 | #else |
---|
[1] | 155 | poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO ); |
---|
[775] | 156 | #endif |
---|
[1] | 157 | if( poODS == NULL ){ |
---|
| 158 | char emessage[1024]; |
---|
| 159 | sprintf( emessage, "%s driver failed to create %s\n", |
---|
| 160 | "json", pszDestDataSource ); |
---|
[115] | 161 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 162 | return SERVICE_FAILED; |
---|
[1] | 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" ); |
---|
[115] | 174 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 175 | return SERVICE_FAILED; |
---|
[1] | 176 | } |
---|
| 177 | |
---|
| 178 | CPLErrorReset(); |
---|
| 179 | |
---|
| 180 | OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbLineString,NULL); |
---|
| 181 | if( poDstLayer == NULL ){ |
---|
[115] | 182 | setMapInMaps(conf,"lenv","message","Layer creation failed.\n"); |
---|
| 183 | return SERVICE_FAILED; |
---|
[1] | 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); |
---|
[469] | 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(); |
---|
[1] | 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 ){ |
---|
[115] | 205 | setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n"); |
---|
| 206 | return SERVICE_FAILED; |
---|
[1] | 207 | } |
---|
| 208 | OGR_F_Destroy( hFeature ); |
---|
| 209 | ++ns ; |
---|
| 210 | } |
---|
[469] | 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 | } |
---|
[1] | 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 | |
---|
[469] | 237 | char *res1=readVSIFile(conf,pszDestDataSource); |
---|
| 238 | if(res1==NULL) |
---|
[115] | 239 | return SERVICE_FAILED; |
---|
[1] | 240 | |
---|
[115] | 241 | setMapInMaps(outputs,"Result","value",res1); |
---|
[1] | 242 | |
---|
| 243 | if(strcmp(oDriver,"GML")==0) |
---|
[115] | 244 | setMapInMaps(outputs,"Result","mimeType","text/xml"); |
---|
[1] | 245 | else |
---|
[546] | 246 | setMapInMaps(outputs,"Result","mimeType","application/json"); |
---|
[1] | 247 | |
---|
[115] | 248 | setMapInMaps(outputs,"Result","encoding","UTF-8"); |
---|
| 249 | #ifdef DEBUG |
---|
[1] | 250 | fprintf(stderr,"\nService internal print\n===\n"); |
---|
[115] | 251 | #endif |
---|
[469] | 252 | OGRCleanupAll(); |
---|
[1] | 253 | return SERVICE_SUCCEEDED; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | } |
---|