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 | int Voronoi(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
49 | #ifdef DEBUG |
---|
50 | fprintf(stderr,"\nService internal print\nStarting\n"); |
---|
51 | #endif |
---|
52 | maps* cursor=inputs; |
---|
53 | OGRGeometryH geometry,res; |
---|
54 | int bufferDistance; |
---|
55 | xmlInitParser(); |
---|
56 | map* tmpm=NULL; |
---|
57 | tmpm=getMapFromMaps(inputs,"InputPoints","value"); |
---|
58 | |
---|
59 | xmlInitParser(); |
---|
60 | xmlDocPtr doc = |
---|
61 | xmlParseMemory(tmpm->value,strlen(tmpm->value)); |
---|
62 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
---|
63 | /** |
---|
64 | * Parse every Input in DataInputs node. |
---|
65 | */ |
---|
66 | maps* tempMaps=NULL; |
---|
67 | xmlXPathContextPtr xpathCtx; |
---|
68 | xmlXPathObjectPtr xpathObj; |
---|
69 | xpathCtx = xmlXPathNewContext(doc); |
---|
70 | xpathObj = xmlXPathEvalExpression(BAD_CAST "/*/*[local-name()='featureMember']/*/*/*[local-name()='Point']/*[local-name()='coordinates']",xpathCtx); |
---|
71 | xmlXPathFreeContext(xpathCtx); |
---|
72 | xmlNodeSet* nSet=xpathObj->nodesetval; |
---|
73 | |
---|
74 | if(nSet==NULL){ |
---|
75 | setMapInMaps(conf,"lenv","message","Unable to continue !!!"); |
---|
76 | return SERVICE_FAILED; |
---|
77 | } |
---|
78 | char filepath[2048]; |
---|
79 | map* tmpMap=getMapFromMaps(conf,"main","tmpPath"); |
---|
80 | if(tmpMap!=NULL){ |
---|
81 | sprintf(filepath,"%s/varonoi_%d.tmp",tmpMap->value,getpid()); |
---|
82 | } |
---|
83 | FILE *fo=fopen(filepath,"w"); |
---|
84 | #ifdef DEBUG |
---|
85 | fprintf(stderr,"File Creation (%s) OK\nPrinting %d Points.\n",filepath,nSet->nodeNr); |
---|
86 | #endif |
---|
87 | for(int k=0;k<nSet->nodeNr;k++){ |
---|
88 | xmlNodePtr cur=nSet->nodeTab[k]; |
---|
89 | char *val= |
---|
90 | (char*)xmlNodeListGetString(doc,cur->xmlChildrenNode,1); |
---|
91 | char *tmp=strstr(val,","); |
---|
92 | char tmp1[1024]; |
---|
93 | strncpy(tmp1,val,strlen(val)-strlen(tmp)); |
---|
94 | tmp1[strlen(val)-strlen(tmp)]=0; |
---|
95 | char buff[1024]; |
---|
96 | sprintf(buff,"%s %s\n",tmp1,tmp+1); |
---|
97 | fwrite(buff,1,strlen(buff)*sizeof(char),fo); |
---|
98 | } |
---|
99 | fclose(fo); |
---|
100 | #ifdef DEBUG |
---|
101 | fprintf(stderr,"File Close (%s) OK\n",filepath); |
---|
102 | #endif |
---|
103 | |
---|
104 | std::ifstream in(filepath); |
---|
105 | std::istream_iterator<Point> begin(in); |
---|
106 | std::istream_iterator<Point> end; |
---|
107 | Triangulation T; |
---|
108 | T.insert(begin, end); |
---|
109 | |
---|
110 | OGRRegisterAll(); |
---|
111 | /* -------------------------------------------------------------------- */ |
---|
112 | /* Try opening the output datasource as an existing, writable */ |
---|
113 | /* -------------------------------------------------------------------- */ |
---|
114 | OGRDataSource *poODS; |
---|
115 | |
---|
116 | OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar(); |
---|
117 | OGRSFDriver *poDriver = NULL; |
---|
118 | int iDriver; |
---|
119 | |
---|
120 | tmpMap=getMapFromMaps(outputs,"Result","mimeType"); |
---|
121 | const char *oDriver; |
---|
122 | oDriver="GeoJSON"; |
---|
123 | if(tmpMap!=NULL){ |
---|
124 | if(strcmp(tmpMap->value,"text/xml")==0){ |
---|
125 | oDriver="GML"; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | for( iDriver = 0; |
---|
130 | iDriver < poR->GetDriverCount() && poDriver == NULL; |
---|
131 | iDriver++ ) |
---|
132 | { |
---|
133 | #ifdef DEBUG |
---|
134 | fprintf(stderr,"D:%s\n",poR->GetDriver(iDriver)->GetName()); |
---|
135 | #endif |
---|
136 | if( EQUAL(poR->GetDriver(iDriver)->GetName(),oDriver) ) |
---|
137 | { |
---|
138 | poDriver = poR->GetDriver(iDriver); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | if( poDriver == NULL ) |
---|
143 | { |
---|
144 | char emessage[8192]; |
---|
145 | sprintf( emessage, "Unable to find driver `%s'.\n", oDriver ); |
---|
146 | sprintf( emessage, "%sThe following drivers are available:\n",emessage ); |
---|
147 | |
---|
148 | for( iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ ) |
---|
149 | { |
---|
150 | sprintf( emessage, "%s -> `%s'\n", emessage, poR->GetDriver(iDriver)->GetName() ); |
---|
151 | } |
---|
152 | |
---|
153 | setMapInMaps(conf,"lenv","message",emessage); |
---|
154 | return SERVICE_FAILED; |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | if( !poDriver->TestCapability( ODrCCreateDataSource ) ){ |
---|
159 | char emessage[1024]; |
---|
160 | sprintf( emessage, "%s driver does not support data source creation.\n", |
---|
161 | "json" ); |
---|
162 | setMapInMaps(conf,"lenv","message",emessage); |
---|
163 | return SERVICE_FAILED; |
---|
164 | } |
---|
165 | |
---|
166 | /* -------------------------------------------------------------------- */ |
---|
167 | /* Create the output data source. */ |
---|
168 | /* -------------------------------------------------------------------- */ |
---|
169 | map* tpath=getMapFromMaps(conf,"main","tmpPath"); |
---|
170 | char *pszDestDataSource=(char*)malloc(strlen(tpath->value)+20); |
---|
171 | char **papszDSCO=NULL; |
---|
172 | sprintf(pszDestDataSource,"%s/result_%d.json",tpath->value,getpid()); |
---|
173 | poODS = poDriver->CreateDataSource( pszDestDataSource, papszDSCO ); |
---|
174 | if( poODS == NULL ){ |
---|
175 | char emessage[1024]; |
---|
176 | sprintf( emessage, "%s driver failed to create %s\n", |
---|
177 | "json", pszDestDataSource ); |
---|
178 | setMapInMaps(conf,"lenv","message",emessage); |
---|
179 | return SERVICE_FAILED; |
---|
180 | } |
---|
181 | |
---|
182 | /* -------------------------------------------------------------------- */ |
---|
183 | /* Create the layer. */ |
---|
184 | /* -------------------------------------------------------------------- */ |
---|
185 | if( !poODS->TestCapability( ODsCCreateLayer ) ) |
---|
186 | { |
---|
187 | char emessage[1024]; |
---|
188 | sprintf( emessage, |
---|
189 | "Layer %s not found, and CreateLayer not supported by driver.", |
---|
190 | "Result" ); |
---|
191 | setMapInMaps(conf,"lenv","message",emessage); |
---|
192 | return SERVICE_FAILED; |
---|
193 | } |
---|
194 | |
---|
195 | CPLErrorReset(); |
---|
196 | |
---|
197 | OGRLayer *poDstLayer = poODS->CreateLayer( "Result", NULL,wkbLineString,NULL); |
---|
198 | if( poDstLayer == NULL ){ |
---|
199 | setMapInMaps(conf,"lenv","message","Layer creation failed.\n"); |
---|
200 | return SERVICE_FAILED; |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | int ns = 0; |
---|
205 | int nr = 0; |
---|
206 | Edge_iterator eit =T.edges_begin(); |
---|
207 | for ( ; eit !=T.edges_end(); ++eit) { |
---|
208 | CGAL::Object o = T.dual(eit); |
---|
209 | if (const K::Segment_2 *tmp=CGAL::object_cast<K::Segment_2>(&o)) { |
---|
210 | const K::Point_2 p1=tmp->source(); |
---|
211 | const K::Point_2 p2=tmp->target(); |
---|
212 | #ifdef DEBUG |
---|
213 | fprintf(stderr,"P1 %d %d | P2 %d %d\n",p1.x(),p1.y(),p2.x(),p2.y()); |
---|
214 | #endif |
---|
215 | OGRFeatureH hFeature = OGR_F_Create( OGR_L_GetLayerDefn( poDstLayer ) ); |
---|
216 | OGRGeometryH currLine=OGR_G_CreateGeometry(wkbLineString); |
---|
217 | OGR_G_AddPoint_2D(currLine,p1.x(),p1.y()); |
---|
218 | OGR_G_AddPoint_2D(currLine,p2.x(),p2.y()); |
---|
219 | OGR_F_SetGeometry( hFeature, currLine ); |
---|
220 | OGR_G_DestroyGeometry(currLine); |
---|
221 | if( OGR_L_CreateFeature( poDstLayer, hFeature ) != OGRERR_NONE ){ |
---|
222 | setMapInMaps(conf,"lenv","message","Failed to create feature in file.\n"); |
---|
223 | return SERVICE_FAILED; |
---|
224 | } |
---|
225 | OGR_F_Destroy( hFeature ); |
---|
226 | ++ns ; |
---|
227 | } |
---|
228 | else if (CGAL::object_cast<K::Ray_2>(&o)) {++nr;} |
---|
229 | } |
---|
230 | OGR_DS_Destroy( poODS ); |
---|
231 | OGRCleanupAll(); |
---|
232 | |
---|
233 | #ifdef DEBUG |
---|
234 | std::cerr << "The Voronoi diagram has " << ns << " finite edges " |
---|
235 | << " and " << nr << " rays" << std::endl; |
---|
236 | sprintf(tmp1,"%d finite edges, %d rays",ns,nr); |
---|
237 | #endif |
---|
238 | |
---|
239 | char tmp1[1024]; |
---|
240 | |
---|
241 | FILE * fichier=fopen(pszDestDataSource,"r"); |
---|
242 | struct stat file_status; |
---|
243 | stat(pszDestDataSource, &file_status); |
---|
244 | char *res1=(char *)malloc(file_status.st_size*sizeof(char)); |
---|
245 | if(fichier==NULL){ |
---|
246 | char tmp[1024]; |
---|
247 | sprintf(tmp,"Failed to open file %s for reading purpose.\n", |
---|
248 | pszDestDataSource); |
---|
249 | setMapInMaps(conf,"lenv","message",tmp); |
---|
250 | return SERVICE_FAILED; |
---|
251 | } |
---|
252 | fread(res1,1,(file_status.st_size)*sizeof(char),fichier); |
---|
253 | res1[strlen(res1)]=0; |
---|
254 | fclose(fichier); |
---|
255 | unlink(pszDestDataSource); |
---|
256 | |
---|
257 | setMapInMaps(outputs,"Result","value",res1); |
---|
258 | |
---|
259 | if(strcmp(oDriver,"GML")==0) |
---|
260 | setMapInMaps(outputs,"Result","mimeType","text/xml"); |
---|
261 | else |
---|
262 | setMapInMaps(outputs,"Result","mimeType","text/plain"); |
---|
263 | |
---|
264 | setMapInMaps(outputs,"Result","encoding","UTF-8"); |
---|
265 | #ifdef DEBUG |
---|
266 | fprintf(stderr,"\nService internal print\n===\n"); |
---|
267 | #endif |
---|
268 | xmlCleanupParser(); |
---|
269 | return SERVICE_SUCCEEDED; |
---|
270 | } |
---|
271 | |
---|
272 | } |
---|