[469] | 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 |
---|
[546] | 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. |
---|
[469] | 23 | */ |
---|
| 24 | |
---|
| 25 | #include "cgal_service.h" |
---|
| 26 | typedef CGAL::Triangulation_vertex_base_with_info_2<unsigned int, Kernel> Vb; |
---|
| 27 | typedef CGAL::Triangulation_data_structure_2<Vb> Tds; |
---|
| 28 | typedef CGAL::Delaunay_triangulation_2<Kernel, Tds> DelaunayT; |
---|
| 29 | |
---|
| 30 | extern "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 | |
---|
[917] | 42 | std::vector<Pointz> points; |
---|
[469] | 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 | /* -------------------------------------------------------------------- */ |
---|
[775] | 52 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 53 | GDALDataset *poODS; |
---|
| 54 | GDALDriverManager* poR=GetGDALDriverManager(); |
---|
| 55 | GDALDriver *poDriver = NULL; |
---|
| 56 | #else |
---|
| 57 | OGRDataSource *poODS; |
---|
[469] | 58 | OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar(); |
---|
| 59 | OGRSFDriver *poDriver = NULL; |
---|
[775] | 60 | #endif |
---|
[469] | 61 | int iDriver; |
---|
| 62 | map *tmpMap=getMapFromMaps(outputs,"Result","mimeType"); |
---|
| 63 | const char *oDriver; |
---|
| 64 | oDriver="GeoJSON"; |
---|
| 65 | if(tmpMap!=NULL){ |
---|
| 66 | if(strcmp(tmpMap->value,"text/xml")==0){ |
---|
| 67 | oDriver="GML"; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | for( iDriver = 0; |
---|
| 72 | iDriver < poR->GetDriverCount() && poDriver == NULL; |
---|
| 73 | iDriver++ ) |
---|
| 74 | { |
---|
| 75 | #ifdef DEBUG |
---|
[775] | 76 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 77 | fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetDescription()); |
---|
| 78 | #else |
---|
[469] | 79 | fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName()); |
---|
| 80 | #endif |
---|
[775] | 81 | #endif |
---|
| 82 | if( EQUAL( |
---|
| 83 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 84 | poR->GetDriver(iDriver)->GetDescription() |
---|
| 85 | #else |
---|
| 86 | poR->GetDriver(iDriver)->GetName() |
---|
| 87 | #endif |
---|
| 88 | , |
---|
| 89 | oDriver) ) |
---|
[469] | 90 | { |
---|
| 91 | poDriver = poR->GetDriver(iDriver); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | if( poDriver == NULL ) |
---|
| 96 | { |
---|
| 97 | char emessage[8192]; |
---|
| 98 | sprintf( emessage, "Unable to find driver `%s'.\n", oDriver ); |
---|
| 99 | sprintf( emessage, "%sThe following drivers are available:\n",emessage ); |
---|
| 100 | |
---|
| 101 | for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ ) |
---|
| 102 | { |
---|
[775] | 103 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 104 | sprintf( emessage, "%s -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetDescription() ); |
---|
| 105 | #else |
---|
[469] | 106 | sprintf( emessage, "%s -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() ); |
---|
[775] | 107 | #endif |
---|
[469] | 108 | } |
---|
| 109 | |
---|
| 110 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 111 | return SERVICE_FAILED; |
---|
| 112 | |
---|
| 113 | } |
---|
| 114 | |
---|
[775] | 115 | #if GDAL_VERSION_MAJOR >=2 |
---|
| 116 | if( !CPLTestBool( CSLFetchNameValueDef(poDriver->GetMetadata(), GDAL_DCAP_CREATE, "FALSE") ) ) |
---|
| 117 | #else |
---|
| 118 | if( !poDriver->TestCapability( ODrCCreateDataSource ) ) |
---|
| 119 | #endif |
---|
| 120 | { |
---|
| 121 | char emessage[1024]; |
---|
| 122 | sprintf( emessage, "%s driver does not support data source creation.\n", |
---|
| 123 | "json" ); |
---|
| 124 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 125 | return SERVICE_FAILED; |
---|
| 126 | } |
---|
[469] | 127 | |
---|
| 128 | /* -------------------------------------------------------------------- */ |
---|
| 129 | /* Create the output data source. */ |
---|
| 130 | /* -------------------------------------------------------------------- */ |
---|
| 131 | char *pszDestDataSource=(char*)malloc(100); |
---|
| 132 | char **papszDSCO=NULL; |
---|
| 133 | sprintf(pszDestDataSource,"/vsimem/result_%d",getpid()); |
---|
[775] | 134 | #if GDAL_VERSION_MAJOR >=2 |
---|
| 135 | poODS = poDriver->Create( pszDestDataSource, 0, 0, 0, GDT_Unknown, papszDSCO ); |
---|
| 136 | #else |
---|
[469] | 137 | poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO ); |
---|
[775] | 138 | #endif |
---|
[469] | 139 | if( poODS == NULL ){ |
---|
| 140 | char emessage[1024]; |
---|
| 141 | sprintf( emessage, "%s driver failed to create %s\n", |
---|
| 142 | "json", pszDestDataSource ); |
---|
| 143 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 144 | return SERVICE_FAILED; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /* -------------------------------------------------------------------- */ |
---|
| 148 | /* Create the layer. */ |
---|
| 149 | /* -------------------------------------------------------------------- */ |
---|
| 150 | if( !poODS->TestCapability( ODsCCreateLayer ) ) |
---|
| 151 | { |
---|
| 152 | char emessage[1024]; |
---|
| 153 | sprintf( emessage, |
---|
| 154 | "Layer %s not found, and CreateLayer not supported by driver.", |
---|
| 155 | "Result" ); |
---|
| 156 | setMapInMaps(conf,"lenv","message",emessage); |
---|
| 157 | return SERVICE_FAILED; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | CPLErrorReset(); |
---|
| 161 | |
---|
| 162 | OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbPolygon,NULL); |
---|
| 163 | if( poDstLayer == NULL ){ |
---|
| 164 | setMapInMaps(conf,"lenv","message","Layer creation failed.\n"); |
---|
| 165 | return SERVICE_FAILED; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | for(DelaunayT::Finite_faces_iterator fit = T.finite_faces_begin(); |
---|
| 170 | fit != T.finite_faces_end(); ++fit) { |
---|
| 171 | DelaunayT::Face_handle face = fit; |
---|
| 172 | OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) ); |
---|
| 173 | OGRGeometryH hCollection = OGR_G_CreateGeometry( wkbGeometryCollection ); |
---|
| 174 | OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLinearRing); |
---|
| 175 | OGRGeometryH currPoly=OGR_G_CreateGeometry(wkbPolygon); |
---|
| 176 | OGR_G_AddPoint_2D(currLine,T.triangle(face)[0].x(),T.triangle(face)[0].y()); |
---|
| 177 | OGR_G_AddPoint_2D(currLine,T.triangle(face)[1].x(),T.triangle(face)[1].y()); |
---|
| 178 | OGR_G_AddPoint_2D(currLine,T.triangle(face)[2].x(),T.triangle(face)[2].y()); |
---|
| 179 | OGR_G_AddPoint_2D(currLine,T.triangle(face)[0].x(),T.triangle(face)[0].y()); |
---|
| 180 | OGR_G_AddGeometryDirectly( currPoly, currLine ); |
---|
| 181 | OGR_G_AddGeometryDirectly( hCollection, currPoly ); |
---|
| 182 | OGR_F_SetGeometry( hFeature, hCollection ); |
---|
| 183 | OGR_G_DestroyGeometry(hCollection); |
---|
| 184 | if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){ |
---|
| 185 | setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n"); |
---|
| 186 | return SERVICE_FAILED; |
---|
| 187 | } |
---|
| 188 | OGR_F_Destroy( hFeature ); |
---|
| 189 | } |
---|
| 190 | OGR_DS_Destroy( poODS ); |
---|
| 191 | |
---|
| 192 | #ifdef DEBUG |
---|
| 193 | std::cerr << "The Voronoi diagram has " << ns << " finite edges " |
---|
| 194 | << " and " << nr << " rays" << std::endl; |
---|
| 195 | #endif |
---|
| 196 | |
---|
| 197 | char *res1=readVSIFile(conf,pszDestDataSource); |
---|
| 198 | if(res1==NULL) |
---|
| 199 | return SERVICE_FAILED; |
---|
| 200 | |
---|
| 201 | setMapInMaps(outputs,"Result","value",res1); |
---|
| 202 | free(res1); |
---|
| 203 | |
---|
| 204 | if(strcmp(oDriver,"GML")==0) |
---|
| 205 | setMapInMaps(outputs,"Result","mimeType","text/xml"); |
---|
| 206 | else |
---|
[546] | 207 | setMapInMaps(outputs,"Result","mimeType","application/json"); |
---|
[469] | 208 | |
---|
| 209 | setMapInMaps(outputs,"Result","encoding","UTF-8"); |
---|
| 210 | OGRCleanupAll(); |
---|
| 211 | |
---|
| 212 | return SERVICE_SUCCEEDED; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | } |
---|