| 1 | #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> |
|---|
| 2 | #include <CGAL/Triangulation_euclidean_traits_xy_3.h> |
|---|
| 3 | #include <CGAL/Delaunay_triangulation_2.h> |
|---|
| 4 | #include <CGAL/Constrained_Delaunay_triangulation_2.h> |
|---|
| 5 | #include <CGAL/Triangulation_conformer_2.h> |
|---|
| 6 | #include <CGAL/Triangulation_face_base_2.h> |
|---|
| 7 | |
|---|
| 8 | #include <fstream> |
|---|
| 9 | |
|---|
| 10 | #include "cpl_minixml.h" |
|---|
| 11 | #include "ogr_api.h" |
|---|
| 12 | #include "ogrsf_frmts.h" |
|---|
| 13 | #include "service.h" |
|---|
| 14 | |
|---|
| 15 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K; |
|---|
| 16 | |
|---|
| 17 | typedef CGAL::Delaunay_triangulation_2<K> Triangulation; |
|---|
| 18 | typedef Triangulation::Edge_iterator Edge_iterator; |
|---|
| 19 | typedef Triangulation::Point Point; |
|---|
| 20 | |
|---|
| 21 | typedef CGAL::Constrained_Delaunay_triangulation_2<K> CDT; |
|---|
| 22 | typedef CDT::Point Point; |
|---|
| 23 | typedef CDT::Vertex_handle Vertex_handle; |
|---|
| 24 | |
|---|
| 25 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K; |
|---|
| 26 | typedef CGAL::Triangulation_euclidean_traits_xy_3<K> Gt; |
|---|
| 27 | typedef CGAL::Delaunay_triangulation_2<Gt> DelaunayTriangulation; |
|---|
| 28 | |
|---|
| 29 | typedef Triangulation::Vertex_circulator Vertex_circulator; |
|---|
| 30 | |
|---|
| 31 | typedef K::Point_3 Point1; |
|---|
| 32 | |
|---|
| 33 | extern "C" { |
|---|
| 34 | #include <libxml/tree.h> |
|---|
| 35 | #include <libxml/parser.h> |
|---|
| 36 | #include <libxml/xpath.h> |
|---|
| 37 | #include <libxml/xpathInternals.h> |
|---|
| 38 | |
|---|
| 39 | #include <openssl/sha.h> |
|---|
| 40 | #include <openssl/hmac.h> |
|---|
| 41 | #include <openssl/evp.h> |
|---|
| 42 | #include <openssl/bio.h> |
|---|
| 43 | #include <openssl/buffer.h> |
|---|
| 44 | |
|---|
| 45 | xmlNodeSet* extractFromDoc(xmlDocPtr,char*); |
|---|
| 46 | void printExceptionReportResponse(maps*,map*); |
|---|
| 47 | |
|---|
| 48 | char *base64(const unsigned char *input, int length){ |
|---|
| 49 | BIO *bmem, *b64; |
|---|
| 50 | BUF_MEM *bptr; |
|---|
| 51 | |
|---|
| 52 | b64 = BIO_new(BIO_f_base64()); |
|---|
| 53 | bmem = BIO_new(BIO_s_mem()); |
|---|
| 54 | b64 = BIO_push(b64, bmem); |
|---|
| 55 | BIO_write(b64, input, length); |
|---|
| 56 | BIO_flush(b64); |
|---|
| 57 | BIO_get_mem_ptr(b64, &bptr); |
|---|
| 58 | |
|---|
| 59 | char *buff = (char *)malloc(bptr->length+1); |
|---|
| 60 | memcpy(buff, bptr->data, bptr->length); |
|---|
| 61 | buff[bptr->length] = 0; |
|---|
| 62 | |
|---|
| 63 | BIO_free_all(b64); |
|---|
| 64 | |
|---|
| 65 | return buff; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | OGRGeometryH createGeometryFromGML(maps* conf,char* inputStr){ |
|---|
| 69 | xmlInitParser(); |
|---|
| 70 | xmlDocPtr doc = xmlParseMemory(inputStr,strlen(inputStr)); |
|---|
| 71 | xmlChar *xmlbuff; |
|---|
| 72 | int buffersize; |
|---|
| 73 | xmlXPathContextPtr xpathCtx; |
|---|
| 74 | xmlXPathObjectPtr xpathObj; |
|---|
| 75 | char * xpathExpr="/*/*/*/*/*[local-name()='Polygon' or local-name()='MultiPolygon']"; |
|---|
| 76 | xpathCtx = xmlXPathNewContext(doc); |
|---|
| 77 | xpathObj = xmlXPathEvalExpression(BAD_CAST xpathExpr,xpathCtx); |
|---|
| 78 | if(!xpathObj->nodesetval){ |
|---|
| 79 | map* tmp=createMap("text","Unable to parse Input Polygon"); |
|---|
| 80 | addToMap(tmp,"code","InvalidParameterValue"); |
|---|
| 81 | printExceptionReportResponse(conf,tmp); |
|---|
| 82 | exit(0); |
|---|
| 83 | } |
|---|
| 84 | int size = (xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0; |
|---|
| 85 | /** |
|---|
| 86 | * Create a temporary XML document |
|---|
| 87 | */ |
|---|
| 88 | xmlDocPtr ndoc = xmlNewDoc(BAD_CAST "1.0"); |
|---|
| 89 | /** |
|---|
| 90 | * Only one polygon should be provided so we use it as the root node. |
|---|
| 91 | */ |
|---|
| 92 | for(int k=size-1;k>=0;k--){ |
|---|
| 93 | xmlDocSetRootElement(ndoc, xpathObj->nodesetval->nodeTab[k]); |
|---|
| 94 | } |
|---|
| 95 | xmlDocDumpFormatMemory(ndoc, &xmlbuff, &buffersize, 1); |
|---|
| 96 | char *tmp=strdup(strstr((char*)xmlbuff,"?>")+2); |
|---|
| 97 | xmlXPathFreeObject(xpathObj); |
|---|
| 98 | xmlXPathFreeContext(xpathCtx); |
|---|
| 99 | xmlFree(xmlbuff); |
|---|
| 100 | xmlFreeDoc(doc); |
|---|
| 101 | xmlCleanupParser(); |
|---|
| 102 | fprintf(stderr,"\nService internal print\n Loading the geometry from GML string ..."); |
|---|
| 103 | OGRGeometryH res=OGR_G_CreateFromGML(tmp); |
|---|
| 104 | if(res==NULL){ |
|---|
| 105 | map* tmp=createMap("text","Unable to call OGR_G_CreatFromGML"); |
|---|
| 106 | addToMap(tmp,"code","NoApplicableCode"); |
|---|
| 107 | printExceptionReportResponse(conf,tmp); |
|---|
| 108 | exit(0); |
|---|
| 109 | } |
|---|
| 110 | else |
|---|
| 111 | return OGR_G_CreateFromGML(tmp); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | int Delaunay(maps*& conf,maps*& inputs,maps*& outputs){ |
|---|
| 116 | fprintf(stderr,"\nService internal print\nStarting\n"); |
|---|
| 117 | maps* cursor=inputs; |
|---|
| 118 | OGRGeometryH geometry,res; |
|---|
| 119 | int bufferDistance; |
|---|
| 120 | xmlInitParser(); |
|---|
| 121 | map* tmpm=NULL; |
|---|
| 122 | tmpm=getMapFromMaps(inputs,"InputPoints","value"); |
|---|
| 123 | |
|---|
| 124 | xmlInitParser(); |
|---|
| 125 | xmlDocPtr doc = |
|---|
| 126 | xmlParseMemory(tmpm->value,strlen(tmpm->value)); |
|---|
| 127 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
|---|
| 128 | /** |
|---|
| 129 | * Parse every Input in DataInputs node. |
|---|
| 130 | */ |
|---|
| 131 | maps* tempMaps=NULL; |
|---|
| 132 | xmlXPathContextPtr xpathCtx; |
|---|
| 133 | xmlXPathObjectPtr xpathObj; |
|---|
| 134 | xpathCtx = xmlXPathNewContext(doc); |
|---|
| 135 | xpathObj = xmlXPathEvalExpression(BAD_CAST "/*/*[local-name()='featureMember']/*/*/*[local-name()='Point']/*[local-name()='coordinates']",xpathCtx); |
|---|
| 136 | xmlXPathFreeContext(xpathCtx); |
|---|
| 137 | xmlNodeSet* toto=xpathObj->nodesetval; |
|---|
| 138 | |
|---|
| 139 | if(toto==NULL) |
|---|
| 140 | fprintf(stderr,"IMPOSSIBLE DE CONTINUER !!!!\n"); |
|---|
| 141 | char filepath[2048]; |
|---|
| 142 | map* tmpMap=getMapFromMaps(conf,"main","tmpPath"); |
|---|
| 143 | if(tmpMap!=NULL){ |
|---|
| 144 | sprintf(filepath,"%s/varonoi_%d.tmp",tmpMap->value,getpid()); |
|---|
| 145 | } |
|---|
| 146 | FILE *fo=fopen(filepath,"w"); |
|---|
| 147 | fprintf(stderr,"File Creation (%s) OK\nPrinting %d Points.\n",filepath,toto->nodeNr); |
|---|
| 148 | for(int k=0;k<toto->nodeNr;k++){ |
|---|
| 149 | xmlNodePtr cur=toto->nodeTab[k]; |
|---|
| 150 | char *val= |
|---|
| 151 | (char*)xmlNodeListGetString(doc,cur->xmlChildrenNode,1); |
|---|
| 152 | char *tmp=strstr(val,","); |
|---|
| 153 | char tmp1[1024]; |
|---|
| 154 | strncpy(tmp1,val,strlen(val)-strlen(tmp)); |
|---|
| 155 | tmp1[strlen(val)-strlen(tmp)]=0; |
|---|
| 156 | char buff[1024]; |
|---|
| 157 | sprintf(buff,"%s %s\n",tmp1,tmp+1); |
|---|
| 158 | fwrite(buff,1,strlen(buff)*sizeof(char),fo); |
|---|
| 159 | } |
|---|
| 160 | fclose(fo); |
|---|
| 161 | fprintf(stderr,"File Close (%s) OK\n",filepath); |
|---|
| 162 | |
|---|
| 163 | std::ifstream in(filepath); |
|---|
| 164 | |
|---|
| 165 | std::istream_iterator<Point1> begin(in); |
|---|
| 166 | std::istream_iterator<Point1> end; |
|---|
| 167 | |
|---|
| 168 | DelaunayTriangulation dt1; |
|---|
| 169 | dt1.insert(begin, end); |
|---|
| 170 | |
|---|
| 171 | OGRRegisterAll(); |
|---|
| 172 | /* -------------------------------------------------------------------- */ |
|---|
| 173 | /* Try opening the output datasource as an existing, writable */ |
|---|
| 174 | /* -------------------------------------------------------------------- */ |
|---|
| 175 | OGRDataSource *poODS; |
|---|
| 176 | |
|---|
| 177 | OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar(); |
|---|
| 178 | OGRSFDriver *poDriver = NULL; |
|---|
| 179 | int iDriver; |
|---|
| 180 | |
|---|
| 181 | tmpMap=getMapFromMaps(outputs,"Result","mimeType"); |
|---|
| 182 | char *oDriver=(char*)malloc(7*sizeof(char)); |
|---|
| 183 | oDriver="GeoJSON"; |
|---|
| 184 | if(tmpMap!=NULL){ |
|---|
| 185 | if(strcmp(tmpMap->value,"text/xml")==0){ |
|---|
| 186 | fprintf(stderr,"USEING GML \n"); |
|---|
| 187 | oDriver="GML"; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | for( iDriver = 0; |
|---|
| 192 | iDriver < poR->GetDriverCount() && poDriver == NULL; |
|---|
| 193 | iDriver++ ) |
|---|
| 194 | { |
|---|
| 195 | #ifdef DEBUG |
|---|
| 196 | fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName()); |
|---|
| 197 | #endif |
|---|
| 198 | if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver) ) |
|---|
| 199 | { |
|---|
| 200 | poDriver = poR->GetDriver(iDriver); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | if( poDriver == NULL ) |
|---|
| 205 | { |
|---|
| 206 | char emessage[8192]; |
|---|
| 207 | sprintf( emessage, "Unable to find driver `%s'.\n", oDriver ); |
|---|
| 208 | sprintf( emessage, "%sThe following drivers are available:\n",emessage ); |
|---|
| 209 | |
|---|
| 210 | for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ ) |
|---|
| 211 | { |
|---|
| 212 | sprintf( emessage, "%s -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() ); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | fprintf(stderr,"{\"type\": \"error\", \"%s\"}",emessage); |
|---|
| 216 | exit( 1 ); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | if( !poDriver->TestCapability( ODrCCreateDataSource ) ){ |
|---|
| 220 | char emessage[1024]; |
|---|
| 221 | sprintf( emessage, "%s driver does not support data source creation.\n", |
|---|
| 222 | "json" ); |
|---|
| 223 | fprintf(stderr,"{\"type\": \"error\", \"%s\"}",emessage); |
|---|
| 224 | exit( 1 ); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | /* -------------------------------------------------------------------- */ |
|---|
| 228 | /* Create the output data source. */ |
|---|
| 229 | /* -------------------------------------------------------------------- */ |
|---|
| 230 | map* tpath=getMapFromMaps(conf,"main","tmpPath"); |
|---|
| 231 | char *pszDestDataSource=(char*)malloc(strlen(tpath->value)+20); |
|---|
| 232 | char **papszDSCO=NULL; |
|---|
| 233 | sprintf(pszDestDataSource,"%s/result_%d.json",tpath->value,getpid()); |
|---|
| 234 | fprintf(stderr,"\n *%s* \n",pszDestDataSource); |
|---|
| 235 | poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO ); |
|---|
| 236 | if( poODS == NULL ){ |
|---|
| 237 | char emessage[1024]; |
|---|
| 238 | sprintf( emessage, "%s driver failed to create %s\n", |
|---|
| 239 | "json", pszDestDataSource ); |
|---|
| 240 | fprintf(stderr,"{\"type\": \"error\", \"%s\"}",emessage); |
|---|
| 241 | exit( 1 ); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /* -------------------------------------------------------------------- */ |
|---|
| 245 | /* Create the layer. */ |
|---|
| 246 | /* -------------------------------------------------------------------- */ |
|---|
| 247 | if( !poODS->TestCapability( ODsCCreateLayer ) ) |
|---|
| 248 | { |
|---|
| 249 | char emessage[1024]; |
|---|
| 250 | sprintf( emessage, |
|---|
| 251 | "Layer %s not found, and CreateLayer not supported by driver.", |
|---|
| 252 | "Result" ); |
|---|
| 253 | fprintf(stderr,"{\"type\": \"error\",\"message\": \"%s\"}",emessage); |
|---|
| 254 | return FALSE; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | CPLErrorReset(); |
|---|
| 258 | |
|---|
| 259 | OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbLineString,NULL); |
|---|
| 260 | if( poDstLayer == NULL ){ |
|---|
| 261 | fprintf( stderr,"Layer creation failed.\n" ); |
|---|
| 262 | exit( 1 ); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | Vertex_circulator vc = dt1.incident_vertices(dt1.infinite_vertex()), |
|---|
| 266 | done(vc); |
|---|
| 267 | if (vc != 0) { |
|---|
| 268 | do {std::cout << vc->point() << std::endl;}while(++vc != done); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | DelaunayTriangulation::Edge_iterator eit=dt1.edges_begin(); |
|---|
| 272 | int i=0; |
|---|
| 273 | for ( ; eit !=dt1.edges_end(); ++eit) { |
|---|
| 274 | fprintf(stderr,"Edge %d\n",i); |
|---|
| 275 | i++; |
|---|
| 276 | /*CGAL::Object o = dt1.dual(eit); |
|---|
| 277 | if (const K::Segment_2 *tmp=CGAL::object_cast<K::Segment_2>(&o)) { |
|---|
| 278 | const K::Point_2 p1=tmp->source(); |
|---|
| 279 | const K::Point_2 p2=tmp->target(); |
|---|
| 280 | //#ifdef DEBUG |
|---|
| 281 | fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y()); |
|---|
| 282 | //#endif |
|---|
| 283 | }*/ |
|---|
| 284 | /*if (const K::Segment_3 *tmp=CGAL::object_cast<K::Segment_3>(eit)) { |
|---|
| 285 | const K::Point_3 p1=tmp->source(); |
|---|
| 286 | const K::Point_3 p2=tmp->target(); |
|---|
| 287 | #ifdef DEBUG |
|---|
| 288 | fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y()); |
|---|
| 289 | #endif |
|---|
| 290 | OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) ); |
|---|
| 291 | OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString); |
|---|
| 292 | OGR_G_AddPoint_2D(currLine,p1.x(),p1.y()); |
|---|
| 293 | OGR_G_AddPoint_2D(currLine,p2.x(),p2.y()); |
|---|
| 294 | OGR_F_SetGeometry( hFeature, currLine ); |
|---|
| 295 | OGR_G_DestroyGeometry(currLine); |
|---|
| 296 | if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){ |
|---|
| 297 | fprintf( stderr,"Failed to create feature in file.\n" ); |
|---|
| 298 | exit( 1 ); |
|---|
| 299 | } |
|---|
| 300 | OGR_F_Destroy( hFeature );*/ |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | int idg=0; |
|---|
| 304 | for (DelaunayTriangulation::Face_iterator fit = dt1.faces_begin(); fit!= |
|---|
| 305 | dt1.faces_end(); ++fit) { |
|---|
| 306 | int idx = 0; |
|---|
| 307 | int i0, i1, i2; |
|---|
| 308 | |
|---|
| 309 | OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) ); |
|---|
| 310 | OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString); |
|---|
| 311 | |
|---|
| 312 | for(i0=0;i0<3;i0++){ |
|---|
| 313 | if(!dt1.is_infinite((*fit).vertex(i0))) |
|---|
| 314 | OGR_G_AddPoint_2D(currLine,(*fit).vertex(i0)->point().x(), |
|---|
| 315 | (*fit).vertex(i0)->point().y()); |
|---|
| 316 | if(i0==2 && !dt1.is_infinite((*fit).vertex(0))) |
|---|
| 317 | OGR_G_AddPoint_2D(currLine,(*fit).vertex(0)->point().x(), |
|---|
| 318 | (*fit).vertex(0)->point().y()); |
|---|
| 319 | } |
|---|
| 320 | OGR_F_SetGeometry( hFeature, currLine ); |
|---|
| 321 | OGR_G_DestroyGeometry(currLine); |
|---|
| 322 | if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){ |
|---|
| 323 | fprintf( stderr,"Failed to create feature in file.\n" ); |
|---|
| 324 | exit( 1 ); |
|---|
| 325 | } |
|---|
| 326 | OGR_F_Destroy( hFeature ); |
|---|
| 327 | |
|---|
| 328 | /*OGRFeatureH hFeature=NULL; |
|---|
| 329 | OGRGeometryH currLine=NULL; |
|---|
| 330 | if(idg==0) { |
|---|
| 331 | hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) ); |
|---|
| 332 | currLine=OGR_G_CreateGeometry(wkbLineString); |
|---|
| 333 | }*/ |
|---|
| 334 | |
|---|
| 335 | for (DelaunayTriangulation::Vertex_iterator vit = dt1.vertices_begin(); vit!= |
|---|
| 336 | dt1.vertices_end(); ++vit) { |
|---|
| 337 | idx++; |
|---|
| 338 | /*if(idg==0) |
|---|
| 339 | OGR_G_AddPoint_2D(currLine,(*vit).point().x(), |
|---|
| 340 | (*vit).point().y());*/ |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | if ((*vit).point() == (*fit).vertex(0)->point()){ |
|---|
| 344 | i0 = idx; |
|---|
| 345 | /*OGR_G_AddPoint_2D(currLine,(*fit).vertex(i0)->point().x(), |
|---|
| 346 | (*fit).vertex(i0)->point().y());**/ |
|---|
| 347 | } |
|---|
| 348 | if ((*vit).point() == (*fit).vertex(1)->point()){ |
|---|
| 349 | i1 = idx; |
|---|
| 350 | /*OGR_G_AddPoint_2D(currLine,(*fit).vertex(i1)->point().x(), |
|---|
| 351 | (*fit).vertex(i1)->point().y());*/ |
|---|
| 352 | } |
|---|
| 353 | if ((*vit).point() == (*fit).vertex(2)->point()){ |
|---|
| 354 | i2 = idx; |
|---|
| 355 | /*OGR_G_AddPoint_2D(currLine,(*fit).vertex(i2)->point().x(), |
|---|
| 356 | (*fit).vertex(i2)->point().y());*/ |
|---|
| 357 | } |
|---|
| 358 | } |
|---|
| 359 | /*if(idg==0){ |
|---|
| 360 | OGR_F_SetGeometry( hFeature, currLine ); |
|---|
| 361 | OGR_G_DestroyGeometry(currLine); |
|---|
| 362 | if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){ |
|---|
| 363 | fprintf( stderr,"Failed to create feature in file.\n" ); |
|---|
| 364 | exit( 1 ); |
|---|
| 365 | } |
|---|
| 366 | OGR_F_Destroy( hFeature ); |
|---|
| 367 | idg++; |
|---|
| 368 | }*/ |
|---|
| 369 | |
|---|
| 370 | std::cerr << "f " << i0 << " " << dt1.triangle(fit) << std::endl; |
|---|
| 371 | |
|---|
| 372 | } |
|---|
| 373 | OGR_DS_Destroy( poODS ); |
|---|
| 374 | OGRCleanupAll(); |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | char tmp1[1024]; |
|---|
| 378 | outputs=(maps*)malloc(sizeof(maps*)); |
|---|
| 379 | outputs->name="Result"; |
|---|
| 380 | |
|---|
| 381 | FILE * fichier=fopen(pszDestDataSource,"r"); |
|---|
| 382 | struct stat file_status; |
|---|
| 383 | stat(pszDestDataSource, &file_status); |
|---|
| 384 | fprintf(stderr,"%s (%d)",pszDestDataSource,file_status.st_size); |
|---|
| 385 | char *res1=(char *)malloc(file_status.st_size*sizeof(char)); |
|---|
| 386 | if(fichier==NULL) |
|---|
| 387 | fprintf(stderr,"Failed to open file %s for reading purpose.\n",pszDestDataSource); |
|---|
| 388 | fread(res1,1,(file_status.st_size)*sizeof(char),fichier); |
|---|
| 389 | res1[strlen(res1)]=0; |
|---|
| 390 | fclose(fichier); |
|---|
| 391 | //unlink(pszDestDataSource); |
|---|
| 392 | //fprintf(stderr,"Read (%s).\n",res1); |
|---|
| 393 | |
|---|
| 394 | outputs->content=createMap("value",res1); |
|---|
| 395 | |
|---|
| 396 | if(strcmp(oDriver,"GML")==0) |
|---|
| 397 | addMapToMap(&outputs->content,createMap("mimeType","text/xml")); |
|---|
| 398 | else |
|---|
| 399 | addMapToMap(&outputs->content,createMap("mimeType","text/plain")); |
|---|
| 400 | |
|---|
| 401 | addMapToMap(&outputs->content,createMap("encoding","UTF-8")); |
|---|
| 402 | outputs->next=NULL; |
|---|
| 403 | fprintf(stderr,"\nService internal print\n===\n"); |
|---|
| 404 | xmlCleanupParser(); |
|---|
| 405 | |
|---|
| 406 | return SERVICE_SUCCEEDED; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | int Voronoi(maps*& conf,maps*& inputs,maps*& outputs){ |
|---|
| 411 | fprintf(stderr,"\nService internal print\nStarting\n"); |
|---|
| 412 | maps* cursor=inputs; |
|---|
| 413 | OGRGeometryH geometry,res; |
|---|
| 414 | int bufferDistance; |
|---|
| 415 | xmlInitParser(); |
|---|
| 416 | map* tmpm=NULL; |
|---|
| 417 | tmpm=getMapFromMaps(inputs,"InputPoints","value"); |
|---|
| 418 | |
|---|
| 419 | xmlInitParser(); |
|---|
| 420 | xmlDocPtr doc = |
|---|
| 421 | xmlParseMemory(tmpm->value,strlen(tmpm->value)); |
|---|
| 422 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
|---|
| 423 | /** |
|---|
| 424 | * Parse every Input in DataInputs node. |
|---|
| 425 | */ |
|---|
| 426 | maps* tempMaps=NULL; |
|---|
| 427 | xmlXPathContextPtr xpathCtx; |
|---|
| 428 | xmlXPathObjectPtr xpathObj; |
|---|
| 429 | xpathCtx = xmlXPathNewContext(doc); |
|---|
| 430 | xpathObj = xmlXPathEvalExpression(BAD_CAST "/*/*[local-name()='featureMember']/*/*/*[local-name()='Point']/*[local-name()='coordinates']",xpathCtx); |
|---|
| 431 | xmlXPathFreeContext(xpathCtx); |
|---|
| 432 | xmlNodeSet* toto=xpathObj->nodesetval; |
|---|
| 433 | |
|---|
| 434 | if(toto==NULL) |
|---|
| 435 | fprintf(stderr,"IMPOSSIBLE DE CONTINUER !!!!\n"); |
|---|
| 436 | char filepath[2048]; |
|---|
| 437 | map* tmpMap=getMapFromMaps(conf,"main","tmpPath"); |
|---|
| 438 | if(tmpMap!=NULL){ |
|---|
| 439 | sprintf(filepath,"%s/varonoi_%d.tmp",tmpMap->value,getpid()); |
|---|
| 440 | } |
|---|
| 441 | FILE *fo=fopen(filepath,"w"); |
|---|
| 442 | fprintf(stderr,"File Creation (%s) OK\nPrinting %d Points.\n",filepath,toto->nodeNr); |
|---|
| 443 | for(int k=0;k<toto->nodeNr;k++){ |
|---|
| 444 | xmlNodePtr cur=toto->nodeTab[k]; |
|---|
| 445 | char *val= |
|---|
| 446 | (char*)xmlNodeListGetString(doc,cur->xmlChildrenNode,1); |
|---|
| 447 | char *tmp=strstr(val,","); |
|---|
| 448 | char tmp1[1024]; |
|---|
| 449 | strncpy(tmp1,val,strlen(val)-strlen(tmp)); |
|---|
| 450 | tmp1[strlen(val)-strlen(tmp)]=0; |
|---|
| 451 | char buff[1024]; |
|---|
| 452 | sprintf(buff,"%s %s\n",tmp1,tmp+1); |
|---|
| 453 | fwrite(buff,1,strlen(buff)*sizeof(char),fo); |
|---|
| 454 | } |
|---|
| 455 | fclose(fo); |
|---|
| 456 | fprintf(stderr,"File Close (%s) OK\n",filepath); |
|---|
| 457 | |
|---|
| 458 | std::ifstream in(filepath); |
|---|
| 459 | std::istream_iterator<Point> begin(in); |
|---|
| 460 | std::istream_iterator<Point> end; |
|---|
| 461 | Triangulation T; |
|---|
| 462 | T.insert(begin, end); |
|---|
| 463 | |
|---|
| 464 | OGRRegisterAll(); |
|---|
| 465 | /* -------------------------------------------------------------------- */ |
|---|
| 466 | /* Try opening the output datasource as an existing, writable */ |
|---|
| 467 | /* -------------------------------------------------------------------- */ |
|---|
| 468 | OGRDataSource *poODS; |
|---|
| 469 | |
|---|
| 470 | OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar(); |
|---|
| 471 | OGRSFDriver *poDriver = NULL; |
|---|
| 472 | int iDriver; |
|---|
| 473 | |
|---|
| 474 | tmpMap=getMapFromMaps(outputs,"Result","mimeType"); |
|---|
| 475 | char *oDriver=(char*)malloc(7*sizeof(char)); |
|---|
| 476 | oDriver="GeoJSON"; |
|---|
| 477 | if(tmpMap!=NULL){ |
|---|
| 478 | if(strcmp(tmpMap->value,"text/xml")==0){ |
|---|
| 479 | fprintf(stderr,"USEING GML \n"); |
|---|
| 480 | oDriver="GML"; |
|---|
| 481 | } |
|---|
| 482 | } |
|---|
| 483 | |
|---|
| 484 | for( iDriver = 0; |
|---|
| 485 | iDriver < poR->GetDriverCount() && poDriver == NULL; |
|---|
| 486 | iDriver++ ) |
|---|
| 487 | { |
|---|
| 488 | #ifdef DEBUG |
|---|
| 489 | fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName()); |
|---|
| 490 | #endif |
|---|
| 491 | if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver) ) |
|---|
| 492 | { |
|---|
| 493 | poDriver = poR->GetDriver(iDriver); |
|---|
| 494 | } |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | if( poDriver == NULL ) |
|---|
| 498 | { |
|---|
| 499 | char emessage[8192]; |
|---|
| 500 | sprintf( emessage, "Unable to find driver `%s'.\n", oDriver ); |
|---|
| 501 | sprintf( emessage, "%sThe following drivers are available:\n",emessage ); |
|---|
| 502 | |
|---|
| 503 | for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ ) |
|---|
| 504 | { |
|---|
| 505 | sprintf( emessage, "%s -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() ); |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | fprintf(stderr,"{\"type\": \"error\", \"%s\"}",emessage); |
|---|
| 509 | exit( 1 ); |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | if( !poDriver->TestCapability( ODrCCreateDataSource ) ){ |
|---|
| 513 | char emessage[1024]; |
|---|
| 514 | sprintf( emessage, "%s driver does not support data source creation.\n", |
|---|
| 515 | "json" ); |
|---|
| 516 | fprintf(stderr,"{\"type\": \"error\", \"%s\"}",emessage); |
|---|
| 517 | exit( 1 ); |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | /* -------------------------------------------------------------------- */ |
|---|
| 521 | /* Create the output data source. */ |
|---|
| 522 | /* -------------------------------------------------------------------- */ |
|---|
| 523 | map* tpath=getMapFromMaps(conf,"main","tmpPath"); |
|---|
| 524 | char *pszDestDataSource=(char*)malloc(strlen(tpath->value)+20); |
|---|
| 525 | char **papszDSCO=NULL; |
|---|
| 526 | sprintf(pszDestDataSource,"%s/result_%d.json",tpath->value,getpid()); |
|---|
| 527 | poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO ); |
|---|
| 528 | if( poODS == NULL ){ |
|---|
| 529 | char emessage[1024]; |
|---|
| 530 | sprintf( emessage, "%s driver failed to create %s\n", |
|---|
| 531 | "json", pszDestDataSource ); |
|---|
| 532 | fprintf(stderr,"{\"type\": \"error\", \"%s\"}",emessage); |
|---|
| 533 | exit( 1 ); |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | /* -------------------------------------------------------------------- */ |
|---|
| 537 | /* Create the layer. */ |
|---|
| 538 | /* -------------------------------------------------------------------- */ |
|---|
| 539 | if( !poODS->TestCapability( ODsCCreateLayer ) ) |
|---|
| 540 | { |
|---|
| 541 | char emessage[1024]; |
|---|
| 542 | sprintf( emessage, |
|---|
| 543 | "Layer %s not found, and CreateLayer not supported by driver.", |
|---|
| 544 | "Result" ); |
|---|
| 545 | fprintf(stderr,"{\"type\": \"error\",\"message\": \"%s\"}",emessage); |
|---|
| 546 | return FALSE; |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | CPLErrorReset(); |
|---|
| 550 | |
|---|
| 551 | OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbLineString,NULL); |
|---|
| 552 | if( poDstLayer == NULL ){ |
|---|
| 553 | fprintf( stderr,"Layer creation failed.\n" ); |
|---|
| 554 | exit( 1 ); |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | int ns = 0; |
|---|
| 559 | int nr = 0; |
|---|
| 560 | Edge_iterator eit =T.edges_begin(); |
|---|
| 561 | for ( ; eit !=T.edges_end(); ++eit) { |
|---|
| 562 | CGAL::Object o = T.dual(eit); |
|---|
| 563 | if (const K::Segment_2 *tmp=CGAL::object_cast<K::Segment_2>(&o)) { |
|---|
| 564 | const K::Point_2 p1=tmp->source(); |
|---|
| 565 | const K::Point_2 p2=tmp->target(); |
|---|
| 566 | #ifdef DEBUG |
|---|
| 567 | fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y()); |
|---|
| 568 | #endif |
|---|
| 569 | OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) ); |
|---|
| 570 | OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString); |
|---|
| 571 | OGR_G_AddPoint_2D(currLine,p1.x(),p1.y()); |
|---|
| 572 | OGR_G_AddPoint_2D(currLine,p2.x(),p2.y()); |
|---|
| 573 | OGR_F_SetGeometry( hFeature, currLine ); |
|---|
| 574 | OGR_G_DestroyGeometry(currLine); |
|---|
| 575 | if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){ |
|---|
| 576 | fprintf( stderr,"Failed to create feature in file.\n" ); |
|---|
| 577 | exit( 1 ); |
|---|
| 578 | } |
|---|
| 579 | OGR_F_Destroy( hFeature ); |
|---|
| 580 | ++ns ; |
|---|
| 581 | } |
|---|
| 582 | else if (CGAL::object_cast<K::Ray_2>(&o)) {++nr;} |
|---|
| 583 | } |
|---|
| 584 | OGR_DS_Destroy( poODS ); |
|---|
| 585 | OGRCleanupAll(); |
|---|
| 586 | |
|---|
| 587 | #ifdef DEBUG |
|---|
| 588 | std::cerr << "The Voronoi diagram has " << ns << " finite edges " |
|---|
| 589 | << " and " << nr << " rays" << std::endl; |
|---|
| 590 | sprintf(tmp1,"%d finite edges, %d rays",ns,nr); |
|---|
| 591 | #endif |
|---|
| 592 | |
|---|
| 593 | char tmp1[1024]; |
|---|
| 594 | outputs=(maps*)malloc(sizeof(maps*)); |
|---|
| 595 | outputs->name="Result"; |
|---|
| 596 | |
|---|
| 597 | FILE * fichier=fopen(pszDestDataSource,"r"); |
|---|
| 598 | struct stat file_status; |
|---|
| 599 | stat(pszDestDataSource, &file_status); |
|---|
| 600 | //fprintf(stderr,"%s (%d)",pszDestDataSource,file_status.st_size); |
|---|
| 601 | char *res1=(char *)malloc(file_status.st_size*sizeof(char)); |
|---|
| 602 | if(fichier==NULL) |
|---|
| 603 | fprintf(stderr,"Failed to open file %s for reading purpose.\n",pszDestDataSource); |
|---|
| 604 | fread(res1,1,(file_status.st_size)*sizeof(char),fichier); |
|---|
| 605 | res1[strlen(res1)]=0; |
|---|
| 606 | fclose(fichier); |
|---|
| 607 | unlink(pszDestDataSource); |
|---|
| 608 | //fprintf(stderr,"Read (%s).\n",res1); |
|---|
| 609 | |
|---|
| 610 | outputs->content=createMap("value",res1); |
|---|
| 611 | |
|---|
| 612 | if(strcmp(oDriver,"GML")==0) |
|---|
| 613 | addMapToMap(&outputs->content,createMap("mimeType","text/xml")); |
|---|
| 614 | else |
|---|
| 615 | addMapToMap(&outputs->content,createMap("mimeType","text/plain")); |
|---|
| 616 | |
|---|
| 617 | addMapToMap(&outputs->content,createMap("encoding","UTF-8")); |
|---|
| 618 | outputs->next=NULL; |
|---|
| 619 | fprintf(stderr,"\nService internal print\n===\n"); |
|---|
| 620 | xmlCleanupParser(); |
|---|
| 621 | return SERVICE_SUCCEEDED; |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | } |
|---|