[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 | /* -------------------------------------------------------------------- */ |
---|
| 69 | OGRDataSource *poODS; |
---|
| 70 | |
---|
| 71 | OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar(); |
---|
| 72 | OGRSFDriver *poDriver = NULL; |
---|
| 73 | int iDriver; |
---|
| 74 | |
---|
[469] | 75 | map* tmpMap=getMapFromMaps(outputs,"Result","mimeType"); |
---|
[115] | 76 | const char *oDriver; |
---|
[1] | 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 | } |
---|
[115] | 107 | |
---|
| 108 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 109 | return SERVICE_FAILED; |
---|
| 110 | |
---|
[1] | 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" ); |
---|
[115] | 117 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 118 | return SERVICE_FAILED; |
---|
[1] | 119 | } |
---|
| 120 | |
---|
| 121 | /* -------------------------------------------------------------------- */ |
---|
| 122 | /* Create the output data source. */ |
---|
| 123 | /* -------------------------------------------------------------------- */ |
---|
| 124 | map* tpath=getMapFromMaps(conf,"main","tmpPath"); |
---|
[469] | 125 | char *pszDestDataSource=(char*)malloc(100); |
---|
[1] | 126 | char **papszDSCO=NULL; |
---|
[469] | 127 | sprintf(pszDestDataSource,"/vsimem/result_%d.json",tpath->value,getpid()); |
---|
[1] | 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 ); |
---|
[115] | 133 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 134 | return SERVICE_FAILED; |
---|
[1] | 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" ); |
---|
[115] | 146 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 147 | return SERVICE_FAILED; |
---|
[1] | 148 | } |
---|
| 149 | |
---|
| 150 | CPLErrorReset(); |
---|
| 151 | |
---|
| 152 | OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbLineString,NULL); |
---|
| 153 | if( poDstLayer == NULL ){ |
---|
[115] | 154 | setMapInMaps(conf,"lenv","message","Layer creation failed.\n"); |
---|
| 155 | return SERVICE_FAILED; |
---|
[1] | 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); |
---|
[469] | 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(); |
---|
[1] | 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 ){ |
---|
[115] | 177 | setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n"); |
---|
| 178 | return SERVICE_FAILED; |
---|
[1] | 179 | } |
---|
| 180 | OGR_F_Destroy( hFeature ); |
---|
| 181 | ++ns ; |
---|
| 182 | } |
---|
[469] | 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 | } |
---|
[1] | 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 | |
---|
[469] | 209 | char *res1=readVSIFile(conf,pszDestDataSource); |
---|
| 210 | if(res1==NULL) |
---|
[115] | 211 | return SERVICE_FAILED; |
---|
[1] | 212 | |
---|
[115] | 213 | setMapInMaps(outputs,"Result","value",res1); |
---|
[1] | 214 | |
---|
| 215 | if(strcmp(oDriver,"GML")==0) |
---|
[115] | 216 | setMapInMaps(outputs,"Result","mimeType","text/xml"); |
---|
[1] | 217 | else |
---|
[546] | 218 | setMapInMaps(outputs,"Result","mimeType","application/json"); |
---|
[1] | 219 | |
---|
[115] | 220 | setMapInMaps(outputs,"Result","encoding","UTF-8"); |
---|
| 221 | #ifdef DEBUG |
---|
[1] | 222 | fprintf(stderr,"\nService internal print\n===\n"); |
---|
[115] | 223 | #endif |
---|
[469] | 224 | OGRCleanupAll(); |
---|
[1] | 225 | return SERVICE_SUCCEEDED; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | } |
---|