Parent Directory | Revision Log
Asynchronous network io
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of OSM2Go. |
5 | * |
6 | * OSM2Go is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * OSM2Go is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include "appdata.h" |
21 | |
22 | #include <libxml/parser.h> |
23 | #include <libxml/tree.h> |
24 | |
25 | #ifndef LIBXML_TREE_ENABLED |
26 | #error "Tree not enabled in libxml" |
27 | #endif |
28 | |
29 | #define WMS_FORMAT_JPG (1<<0) |
30 | #define WMS_FORMAT_JPEG (1<<1) |
31 | #define WMS_FORMAT_PNG (1<<2) |
32 | #define WMS_FORMAT_GIF (1<<3) |
33 | |
34 | typedef struct { |
35 | pos_t min, max; |
36 | gboolean valid; |
37 | } wms_llbbox_t; |
38 | |
39 | typedef struct wms_layer_s { |
40 | char *title; |
41 | char *name; |
42 | gboolean epsg4326, selected; |
43 | wms_llbbox_t llbbox; |
44 | |
45 | struct wms_layer_s *children; |
46 | struct wms_layer_s *next; |
47 | } wms_layer_t; |
48 | |
49 | typedef struct { |
50 | gulong format; |
51 | } wms_getmap_t; |
52 | |
53 | typedef struct { |
54 | wms_getmap_t *getmap; |
55 | } wms_request_t; |
56 | |
57 | typedef struct { |
58 | char *title; |
59 | } wms_service_t; |
60 | |
61 | typedef struct { |
62 | wms_layer_t *layer; |
63 | wms_request_t *request; |
64 | } wms_cap_t; |
65 | |
66 | typedef struct { |
67 | char *server; |
68 | char *path; |
69 | gint width, height; |
70 | |
71 | wms_service_t *service; |
72 | wms_cap_t *cap; |
73 | } wms_t; |
74 | |
75 | gboolean xmlTextIs(xmlDocPtr doc, xmlNodePtr list, char *str) { |
76 | char *nstr = (char*)xmlNodeListGetString(doc, list, 1); |
77 | if(!nstr) return FALSE; |
78 | |
79 | printf("text = %s\n", nstr); |
80 | |
81 | gboolean match = (strcmp(str, nstr) == 0); |
82 | xmlFree(nstr); |
83 | return match; |
84 | } |
85 | |
86 | gboolean xmlPropIs(xmlNode *node, char *prop, char *str) { |
87 | char *prop_str = (char*)xmlGetProp(node, BAD_CAST prop); |
88 | if(!prop_str) return FALSE; |
89 | |
90 | gboolean match = (strcmp(prop_str, str) == 0); |
91 | xmlFree(prop_str); |
92 | return match; |
93 | } |
94 | |
95 | float xmlGetPropFloat(xmlNode *node, char *prop) { |
96 | char *prop_str = (char*)xmlGetProp(node, BAD_CAST prop); |
97 | if(!prop_str) return NAN; |
98 | |
99 | float retval = g_ascii_strtod(prop_str, NULL); |
100 | xmlFree(prop_str); |
101 | return retval; |
102 | } |
103 | |
104 | static gboolean wms_bbox_is_valid(pos_t *min, pos_t *max) { |
105 | /* all four coordinates are valid? */ |
106 | if(isnan(min->lat)||isnan(min->lon)||isnan(max->lat)||isnan(max->lon)) |
107 | return FALSE; |
108 | |
109 | /* min/max span a useful range? */ |
110 | if(max->lat - min->lat < 1.0) return FALSE; |
111 | if(max->lon - min->lon < 1.0) return FALSE; |
112 | |
113 | /* useful angles? */ |
114 | if(min->lat > 90.0 || min->lat < -90.0) return FALSE; |
115 | if(max->lat > 90.0 || max->lat < -90.0) return FALSE; |
116 | if(min->lon > 180.0 || min->lon < -180.0) return FALSE; |
117 | if(max->lon > 180.0 || max->lon < -180.0) return FALSE; |
118 | |
119 | return TRUE; |
120 | } |
121 | |
122 | static wms_layer_t *wms_cap_parse_layer(xmlDocPtr doc, xmlNode *a_node) { |
123 | wms_layer_t *wms_layer = NULL; |
124 | xmlNode *cur_node = NULL; |
125 | char *str = NULL; |
126 | |
127 | wms_layer = g_new0(wms_layer_t, 1); |
128 | wms_layer->llbbox.min.lon = wms_layer->llbbox.min.lat = NAN; |
129 | wms_layer->llbbox.max.lon = wms_layer->llbbox.max.lat = NAN; |
130 | |
131 | wms_layer_t **children = &(wms_layer->children); |
132 | |
133 | for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) { |
134 | if (cur_node->type == XML_ELEMENT_NODE) { |
135 | if(strcasecmp((char*)cur_node->name, "Layer") == 0) { |
136 | *children = wms_cap_parse_layer(doc, cur_node); |
137 | if(*children) children = &((*children)->next); |
138 | } else if(strcasecmp((char*)cur_node->name, "Name") == 0) { |
139 | str = (char*)xmlNodeListGetString(doc, cur_node->children, 1); |
140 | wms_layer->name = g_strdup(str); |
141 | xmlFree(str); |
142 | } else if(strcasecmp((char*)cur_node->name, "Title") == 0) { |
143 | str = (char*)xmlNodeListGetString(doc, cur_node->children, 1); |
144 | wms_layer->title = g_strdup(str); |
145 | xmlFree(str); |
146 | } else if(strcasecmp((char*)cur_node->name, "SRS") == 0) { |
147 | if(xmlTextIs(doc, cur_node->children, "EPSG:4326")) |
148 | wms_layer->epsg4326 = TRUE; |
149 | } else if(strcasecmp((char*)cur_node->name, "LatLonBoundingBox") == 0) { |
150 | wms_layer->llbbox.min.lat = xmlGetPropFloat(cur_node, "miny"); |
151 | wms_layer->llbbox.min.lon = xmlGetPropFloat(cur_node, "minx"); |
152 | wms_layer->llbbox.max.lat = xmlGetPropFloat(cur_node, "maxy"); |
153 | wms_layer->llbbox.max.lon = xmlGetPropFloat(cur_node, "maxx"); |
154 | } else |
155 | printf("found unhandled WMT_MS_Capabilities/Capability/Layer/%s\n", |
156 | cur_node->name); |
157 | } |
158 | } |
159 | |
160 | wms_layer->llbbox.valid = wms_bbox_is_valid(&wms_layer->llbbox.min, |
161 | &wms_layer->llbbox.max); |
162 | |
163 | printf("------------------- Layer: %s ---------------------------\n", |
164 | wms_layer->title); |
165 | printf("Name: %s\n", wms_layer->name); |
166 | printf("EPSG-4326: %s\n", wms_layer->epsg4326?"yes":"no"); |
167 | if(wms_layer->llbbox.valid) |
168 | printf("LatLonBBox: %f/%f %f/%f\n", |
169 | wms_layer->llbbox.min.lat, wms_layer->llbbox.min.lon, |
170 | wms_layer->llbbox.max.lat, wms_layer->llbbox.max.lon); |
171 | else |
172 | printf("No/invalid LatLonBBox\n"); |
173 | |
174 | return wms_layer; |
175 | } |
176 | |
177 | static wms_getmap_t *wms_cap_parse_getmap(xmlDocPtr doc, xmlNode *a_node) { |
178 | wms_getmap_t *wms_getmap = NULL; |
179 | xmlNode *cur_node = NULL; |
180 | |
181 | wms_getmap = g_new0(wms_getmap_t, 1); |
182 | |
183 | for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) { |
184 | if (cur_node->type == XML_ELEMENT_NODE) { |
185 | if(strcasecmp((char*)cur_node->name, "Format") == 0) { |
186 | if(xmlTextIs(doc, cur_node->children, "image/png")) |
187 | wms_getmap->format |= WMS_FORMAT_PNG; |
188 | if(xmlTextIs(doc, cur_node->children, "image/gif")) |
189 | wms_getmap->format |= WMS_FORMAT_GIF; |
190 | if(xmlTextIs(doc, cur_node->children, "image/jpg")) |
191 | wms_getmap->format |= WMS_FORMAT_JPG; |
192 | if(xmlTextIs(doc, cur_node->children, "image/jpeg")) |
193 | wms_getmap->format |= WMS_FORMAT_JPEG; |
194 | } else |
195 | printf("found unhandled " |
196 | "WMT_MS_Capabilities/Capability/Request/GetMap/%s\n", |
197 | cur_node->name); |
198 | } |
199 | } |
200 | |
201 | printf("Supported formats: %s%s%s\n", |
202 | (wms_getmap->format & WMS_FORMAT_PNG)?"png ":"", |
203 | (wms_getmap->format & WMS_FORMAT_GIF)?"gif ":"", |
204 | (wms_getmap->format & (WMS_FORMAT_JPG | WMS_FORMAT_JPEG))?"jpg ":""); |
205 | return wms_getmap; |
206 | } |
207 | |
208 | static wms_request_t *wms_cap_parse_request(xmlDocPtr doc, xmlNode *a_node) { |
209 | wms_request_t *wms_request = NULL; |
210 | xmlNode *cur_node = NULL; |
211 | |
212 | wms_request = g_new0(wms_request_t, 1); |
213 | |
214 | for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) { |
215 | if (cur_node->type == XML_ELEMENT_NODE) { |
216 | if(strcasecmp((char*)cur_node->name, "GetMap") == 0) { |
217 | wms_request->getmap = wms_cap_parse_getmap(doc, cur_node); |
218 | } else |
219 | printf("found unhandled WMT_MS_Capabilities/Capability/Request/%s\n", |
220 | cur_node->name); |
221 | } |
222 | } |
223 | |
224 | return wms_request; |
225 | } |
226 | |
227 | static wms_cap_t *wms_cap_parse_cap(xmlDocPtr doc, xmlNode *a_node) { |
228 | wms_cap_t *wms_cap = NULL; |
229 | xmlNode *cur_node = NULL; |
230 | |
231 | wms_cap = g_new0(wms_cap_t, 1); |
232 | wms_layer_t **layer = &(wms_cap->layer); |
233 | |
234 | for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) { |
235 | if (cur_node->type == XML_ELEMENT_NODE) { |
236 | if(strcasecmp((char*)cur_node->name, "Request") == 0) { |
237 | wms_cap->request = wms_cap_parse_request(doc, cur_node); |
238 | } else if(strcasecmp((char*)cur_node->name, "Layer") == 0) { |
239 | *layer = wms_cap_parse_layer(doc, cur_node); |
240 | if(*layer) layer = &((*layer)->next); |
241 | } else |
242 | printf("found unhandled WMT_MS_Capabilities/Capability/%s\n", |
243 | cur_node->name); |
244 | } |
245 | } |
246 | |
247 | return wms_cap; |
248 | } |
249 | |
250 | static wms_service_t *wms_cap_parse_service(xmlDocPtr doc, xmlNode *a_node) { |
251 | wms_service_t *wms_service = NULL; |
252 | xmlNode *cur_node = NULL; |
253 | char *str = NULL; |
254 | |
255 | wms_service = g_new0(wms_service_t, 1); |
256 | |
257 | for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) { |
258 | if (cur_node->type == XML_ELEMENT_NODE) { |
259 | if(strcasecmp((char*)cur_node->name, "title") == 0) { |
260 | str = (char*)xmlNodeListGetString(doc, cur_node->children, 1); |
261 | wms_service->title = g_strdup(str); |
262 | xmlFree(str); |
263 | } else |
264 | printf("found unhandled WMT_MS_Capabilities/Service/%s\n", |
265 | cur_node->name); |
266 | } |
267 | } |
268 | |
269 | printf("-- Service --\n"); |
270 | printf("Title: %s\n", wms_service->title); |
271 | |
272 | return wms_service; |
273 | } |
274 | |
275 | static void wms_cap_parse(wms_t *wms, xmlDocPtr doc, xmlNode *a_node) { |
276 | xmlNode *cur_node = NULL; |
277 | |
278 | for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) { |
279 | if (cur_node->type == XML_ELEMENT_NODE) { |
280 | |
281 | if(strcasecmp((char*)cur_node->name, "Service") == 0) { |
282 | if(!wms->service) |
283 | wms->service = wms_cap_parse_service(doc, cur_node); |
284 | } else if(strcasecmp((char*)cur_node->name, "Capability") == 0) { |
285 | if(!wms->cap) |
286 | wms->cap = wms_cap_parse_cap(doc, cur_node); |
287 | } else |
288 | printf("found unhandled WMT_MS_Capabilities/%s\n", cur_node->name); |
289 | } |
290 | } |
291 | } |
292 | |
293 | /* parse root element */ |
294 | static void wms_cap_parse_root(wms_t *wms, xmlDocPtr doc, xmlNode *a_node) { |
295 | xmlNode *cur_node = NULL; |
296 | |
297 | for (cur_node = a_node; cur_node; cur_node = cur_node->next) { |
298 | if (cur_node->type == XML_ELEMENT_NODE) { |
299 | |
300 | if(strcasecmp((char*)cur_node->name, "WMT_MS_Capabilities") == 0) { |
301 | wms_cap_parse(wms, doc, cur_node); |
302 | } else |
303 | printf("found unhandled %s\n", cur_node->name); |
304 | } |
305 | } |
306 | } |
307 | |
308 | static void wms_cap_parse_doc(wms_t *wms, xmlDocPtr doc) { |
309 | /* Get the root element node */ |
310 | xmlNode *root_element = xmlDocGetRootElement(doc); |
311 | |
312 | wms_cap_parse_root(wms, doc, root_element); |
313 | |
314 | /*free the document */ |
315 | xmlFreeDoc(doc); |
316 | |
317 | /* |
318 | * Free the global variables that may |
319 | * have been allocated by the parser. |
320 | */ |
321 | xmlCleanupParser(); |
322 | } |
323 | |
324 | /* get pixel extent of image display */ |
325 | void wms_setup_extent(project_t *project, wms_t *wms) { |
326 | pos_t center; |
327 | lpos_t lcenter, lmin, lmax; |
328 | float scale; |
329 | |
330 | center.lat = (project->min.lat + project->max.lat)/2; |
331 | center.lon = (project->min.lon + project->max.lon)/2; |
332 | |
333 | pos2lpos_center(¢er, &lcenter); |
334 | |
335 | /* the scale is needed to accomodate for "streching" */ |
336 | /* by the mercartor projection */ |
337 | scale = cos(DEG2RAD(center.lat)); |
338 | |
339 | pos2lpos_center(&project->min, &lmin); |
340 | lmin.x -= lcenter.x; |
341 | lmin.y -= lcenter.y; |
342 | lmin.x *= scale; |
343 | lmin.y *= scale; |
344 | |
345 | pos2lpos_center(&project->max, &lmax); |
346 | lmax.x -= lcenter.x; |
347 | lmax.y -= lcenter.y; |
348 | lmax.x *= scale; |
349 | lmax.y *= scale; |
350 | |
351 | wms->width = lmax.x - lmin.x; |
352 | wms->height = lmax.y - lmin.y; |
353 | |
354 | if(wms->width > 2048) wms->width = 2048; |
355 | if(wms->height > 2048) wms->height = 2048; |
356 | |
357 | printf("WMS: required image size = %dx%d\n", |
358 | wms->width, wms->height); |
359 | } |
360 | |
361 | /* --------------- freeing stuff ------------------- */ |
362 | |
363 | static void wms_layer_free(wms_layer_t *layer) { |
364 | while(layer) { |
365 | |
366 | if(layer->title) g_free(layer->title); |
367 | if(layer->name) g_free(layer->name); |
368 | |
369 | if(layer->children) wms_layer_free(layer->children); |
370 | |
371 | wms_layer_t *next = layer->next; |
372 | g_free(layer); |
373 | layer = next; |
374 | } |
375 | } |
376 | |
377 | static void wms_getmap_free(wms_getmap_t *getmap) { |
378 | g_free(getmap); |
379 | } |
380 | |
381 | static void wms_request_free(wms_request_t *request) { |
382 | if(request->getmap) wms_getmap_free(request->getmap); |
383 | g_free(request); |
384 | } |
385 | |
386 | static void wms_cap_free(wms_cap_t *cap) { |
387 | if(cap->layer) wms_layer_free(cap->layer); |
388 | if(cap->request) wms_request_free(cap->request); |
389 | g_free(cap); |
390 | } |
391 | |
392 | static void wms_service_free(wms_service_t *service) { |
393 | if(service->title) g_free(service->title); |
394 | g_free(service); |
395 | } |
396 | |
397 | static void wms_free(wms_t *wms) { |
398 | if(wms->server) g_free(wms->server); |
399 | if(wms->path) g_free(wms->path); |
400 | if(wms->cap) wms_cap_free(wms->cap); |
401 | if(wms->service) wms_service_free(wms->service); |
402 | g_free(wms); |
403 | } |
404 | |
405 | /* ---------------------- use ------------------- */ |
406 | |
407 | static gboolean wms_llbbox_fits(project_t *project, wms_llbbox_t *llbbox) { |
408 | if(!llbbox || !llbbox->valid || |
409 | (project->min.lat < llbbox->min.lat) || |
410 | (project->min.lon < llbbox->min.lon) || |
411 | (project->max.lat > llbbox->max.lat) || |
412 | (project->max.lon > llbbox->max.lon)) |
413 | return FALSE; |
414 | |
415 | return TRUE; |
416 | } |
417 | |
418 | static void wms_get_child_layers(wms_layer_t *layer, |
419 | gint depth, gboolean epsg4326, wms_llbbox_t *llbbox, |
420 | wms_layer_t **c_layer) { |
421 | while(layer) { |
422 | |
423 | /* get a copy of the parents values for the current one ... */ |
424 | wms_llbbox_t *local_llbbox = llbbox; |
425 | gboolean local_epsg4326 = epsg4326; |
426 | |
427 | /* ... and overwrite the inherited stuff with better local stuff */ |
428 | if(layer->llbbox.valid) local_llbbox = &layer->llbbox; |
429 | if(layer->epsg4326) local_epsg4326 = TRUE; |
430 | |
431 | /* only named layers with useful bounding box are added to the list */ |
432 | if(local_llbbox && layer->name) { |
433 | *c_layer = g_new0(wms_layer_t, 1); |
434 | (*c_layer)->name = g_strdup(layer->name); |
435 | (*c_layer)->title = g_strdup(layer->title); |
436 | (*c_layer)->epsg4326 = local_epsg4326; |
437 | (*c_layer)->llbbox = *local_llbbox; |
438 | c_layer = &((*c_layer)->next); |
439 | } |
440 | |
441 | wms_get_child_layers(layer->children, depth+1, |
442 | local_epsg4326, local_llbbox, |
443 | c_layer); |
444 | |
445 | layer = layer->next; |
446 | } |
447 | } |
448 | |
449 | static wms_layer_t *wms_get_requestable_layers(wms_t *wms) { |
450 | printf("\nSearching for usable layers\n"); |
451 | |
452 | wms_layer_t *r_layer = NULL, **c_layer = &r_layer; |
453 | |
454 | wms_layer_t *layer = wms->cap->layer; |
455 | while(layer) { |
456 | wms_llbbox_t *llbbox = &layer->llbbox; |
457 | if(llbbox && !llbbox->valid) llbbox = NULL; |
458 | |
459 | wms_get_child_layers(layer->children, 1, |
460 | layer->epsg4326, llbbox, c_layer); |
461 | |
462 | layer = layer->next; |
463 | } |
464 | |
465 | return r_layer; |
466 | } |
467 | |
468 | static gboolean wms_server_dialog(appdata_t *appdata, wms_t *wms) { |
469 | gboolean ok = FALSE; |
470 | |
471 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("WMS Setup"), |
472 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
473 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
474 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
475 | NULL); |
476 | |
477 | #ifdef USE_HILDON |
478 | /* making the dialog a little wider makes it less "crowded" */ |
479 | gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 50); |
480 | #else |
481 | gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 50); |
482 | #endif |
483 | |
484 | GtkWidget *label; |
485 | GtkWidget *table = gtk_table_new(2, 2, FALSE); // x, y |
486 | |
487 | label = gtk_label_new(_("Server:")); |
488 | gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f); |
489 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, 0,0,0,0); |
490 | GtkWidget *server_entry = gtk_entry_new(); |
491 | HILDON_ENTRY_NO_AUTOCAP(server_entry); |
492 | gtk_entry_set_text(GTK_ENTRY(server_entry), wms->server); |
493 | gtk_table_attach_defaults(GTK_TABLE(table), server_entry, 1, 2, 0, 1); |
494 | |
495 | label = gtk_label_new(_("Path:")); |
496 | gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f); |
497 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2, 0,0,0,0); |
498 | GtkWidget *path_entry = gtk_entry_new(); |
499 | HILDON_ENTRY_NO_AUTOCAP(path_entry); |
500 | gtk_entry_set_text(GTK_ENTRY(path_entry), wms->path); |
501 | gtk_table_attach_defaults(GTK_TABLE(table), path_entry, 1, 2, 1, 2); |
502 | |
503 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table); |
504 | gtk_widget_show_all(dialog); |
505 | |
506 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) { |
507 | wms->server = g_strdup(gtk_entry_get_text(GTK_ENTRY(server_entry))); |
508 | wms->path = g_strdup(gtk_entry_get_text(GTK_ENTRY(path_entry))); |
509 | ok = TRUE; |
510 | } |
511 | |
512 | gtk_widget_destroy(dialog); |
513 | |
514 | return ok; |
515 | } |
516 | |
517 | enum { |
518 | LAYER_COL_TITLE = 0, |
519 | LAYER_COL_SELECTED, |
520 | LAYER_COL_FITS, |
521 | LAYER_COL_DATA, |
522 | LAYER_NUM_COLS |
523 | }; |
524 | |
525 | static void |
526 | layer_toggled(GtkCellRendererToggle *cell, const gchar *path_str, |
527 | GtkListStore *store) { |
528 | GtkTreePath *path; |
529 | GtkTreeIter iter; |
530 | |
531 | path = gtk_tree_path_new_from_string(path_str); |
532 | gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, path); |
533 | |
534 | /* get current enabled flag */ |
535 | gboolean enabled; |
536 | gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, |
537 | LAYER_COL_SELECTED, &enabled, -1); |
538 | |
539 | /* change it and store it */ |
540 | enabled = !enabled; |
541 | gtk_list_store_set(store, &iter, LAYER_COL_SELECTED, enabled, -1); |
542 | |
543 | /* and store it in the layer itself */ |
544 | wms_layer_t *layer = NULL; |
545 | gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, LAYER_COL_DATA, &layer, -1); |
546 | layer->selected = enabled; |
547 | |
548 | /* walk the entire store to get all values */ |
549 | if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) { |
550 | gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, |
551 | LAYER_COL_SELECTED, &enabled, -1); |
552 | |
553 | while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter) && |
554 | !enabled) |
555 | gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, |
556 | LAYER_COL_SELECTED, &enabled, -1); |
557 | } |
558 | |
559 | GtkWidget *dialog = g_object_get_data(G_OBJECT(store), "dialog"); |
560 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), |
561 | GTK_RESPONSE_ACCEPT, enabled); |
562 | |
563 | gtk_tree_path_free(path); |
564 | } |
565 | |
566 | static GtkWidget *wms_layer_widget(appdata_t *appdata, wms_layer_t *layer, |
567 | GtkWidget *dialog) { |
568 | GtkWidget *view = gtk_tree_view_new(); |
569 | |
570 | /* build the store */ |
571 | GtkListStore *store = gtk_list_store_new(LAYER_NUM_COLS, |
572 | G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_POINTER); |
573 | |
574 | g_object_set_data(G_OBJECT(store), "dialog", dialog); |
575 | |
576 | /* --- "selected" column --- */ |
577 | GtkCellRenderer *renderer = gtk_cell_renderer_toggle_new(); |
578 | g_signal_connect(renderer, "toggled", G_CALLBACK(layer_toggled), store); |
579 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
580 | -1, _(""), renderer, |
581 | "active", LAYER_COL_SELECTED, |
582 | "activatable", LAYER_COL_FITS, |
583 | NULL); |
584 | |
585 | /* --- "Title" column --- */ |
586 | renderer = gtk_cell_renderer_text_new(); |
587 | g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL ); |
588 | GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( |
589 | _("Title"), renderer, |
590 | "text", LAYER_COL_TITLE, |
591 | NULL); |
592 | |
593 | gtk_tree_view_column_set_expand(column, TRUE); |
594 | gtk_tree_view_insert_column(GTK_TREE_VIEW(view), column, -1); |
595 | |
596 | gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store)); |
597 | |
598 | GtkTreeIter iter; |
599 | while(layer) { |
600 | gboolean fits = wms_llbbox_fits(appdata->project, &layer->llbbox); |
601 | |
602 | /* Append a row and fill in some data */ |
603 | gtk_list_store_append(store, &iter); |
604 | gtk_list_store_set(store, &iter, |
605 | LAYER_COL_SELECTED, FALSE, |
606 | LAYER_COL_TITLE, layer->title, |
607 | LAYER_COL_FITS, fits, |
608 | LAYER_COL_DATA, layer, |
609 | -1); |
610 | layer = layer->next; |
611 | } |
612 | |
613 | g_object_unref(store); |
614 | |
615 | /* put it into a scrolled window */ |
616 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
617 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
618 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
619 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), |
620 | GTK_SHADOW_ETCHED_IN); |
621 | gtk_container_add(GTK_CONTAINER(scrolled_window), view); |
622 | |
623 | return scrolled_window; |
624 | } |
625 | |
626 | |
627 | static gboolean wms_layer_dialog(appdata_t *appdata, wms_layer_t *layer) { |
628 | gboolean ok = FALSE; |
629 | |
630 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("WMS layer selection"), |
631 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
632 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
633 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
634 | NULL); |
635 | |
636 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), |
637 | GTK_RESPONSE_ACCEPT, FALSE); |
638 | |
639 | #ifdef USE_HILDON |
640 | /* making the dialog a little wider makes it less "crowded" */ |
641 | gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 300); |
642 | #else |
643 | gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 200); |
644 | #endif |
645 | |
646 | /* layer list */ |
647 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
648 | wms_layer_widget(appdata, layer, dialog)); |
649 | |
650 | |
651 | gtk_widget_show_all(dialog); |
652 | |
653 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) |
654 | ok = TRUE; |
655 | |
656 | gtk_widget_destroy(dialog); |
657 | |
658 | return ok; |
659 | } |
660 | |
661 | static gboolean wms_one_layer_is_usable(project_t *project, |
662 | wms_layer_t *layer) { |
663 | gboolean ok = FALSE; |
664 | |
665 | while(layer) { |
666 | if(layer->name && layer->epsg4326 && layer->llbbox.valid) |
667 | ok = TRUE; |
668 | |
669 | printf("----- Layer \"%s\" -----\n", layer->title); |
670 | printf("Name: %s\n", layer->name); |
671 | printf("epsg4326: %s\n", layer->epsg4326?"yes":"no"); |
672 | if(layer->llbbox.valid) { |
673 | printf("llbbox: %f/%f %f/%f\n", |
674 | layer->llbbox.min.lat, layer->llbbox.min.lon, |
675 | layer->llbbox.max.lat, layer->llbbox.max.lon); |
676 | |
677 | printf("llbbox fits project: %s\n", |
678 | wms_llbbox_fits(project, &layer->llbbox)?"yes":"no"); |
679 | } else |
680 | printf("llbbox: none/invalid\n"); |
681 | |
682 | layer = layer->next; |
683 | } |
684 | |
685 | return ok; |
686 | } |
687 | |
688 | void wms_import(appdata_t *appdata) { |
689 | if(!appdata->project) { |
690 | errorf(GTK_WIDGET(appdata->window), |
691 | _("Need an open project to derive WMS coordinates")); |
692 | return; |
693 | } |
694 | |
695 | /* this cancels any wms adjustment in progress */ |
696 | if(appdata->map->action.type == MAP_ACTION_BG_ADJUST) |
697 | map_action_cancel(appdata); |
698 | |
699 | wms_t *wms = g_new0(wms_t,1); |
700 | wms->server = g_strdup(appdata->project->wms_server); |
701 | wms->path = g_strdup(appdata->project->wms_path); |
702 | |
703 | /* reset any background adjustments in the project ... */ |
704 | if((appdata->project->wms_offset.x != 0)|| |
705 | (appdata->project->wms_offset.y != 0)) { |
706 | |
707 | appdata->project->wms_offset.x = 0; |
708 | appdata->project->wms_offset.y = 0; |
709 | appdata->project->dirty = TRUE; |
710 | } |
711 | |
712 | /* ... as well as in the map */ |
713 | appdata->map->bg.offset.x = 0; |
714 | appdata->map->bg.offset.y = 0; |
715 | |
716 | /* get server from dialog */ |
717 | if(!wms_server_dialog(appdata, wms)) { |
718 | wms_free(wms); |
719 | return; |
720 | } |
721 | |
722 | /* ------------- copy values back into project ---------------- */ |
723 | if(strcmp(appdata->project->wms_server, wms->server) != 0) { |
724 | g_free(appdata->project->wms_server); |
725 | appdata->project->wms_server = g_strdup(wms->server); |
726 | appdata->project->dirty = TRUE; |
727 | } |
728 | |
729 | if(strcmp(appdata->project->wms_path, wms->path) != 0) { |
730 | g_free(appdata->project->wms_path); |
731 | appdata->project->wms_path = g_strdup(wms->path); |
732 | appdata->project->dirty = TRUE; |
733 | } |
734 | |
735 | /* ----------- request capabilities -------------- */ |
736 | gboolean path_contains_qm = (strchr(wms->path, '?') != NULL); |
737 | gboolean path_ends_with_special = |
738 | (wms->path[strlen(wms->path)-1] == '?') || |
739 | (wms->path[strlen(wms->path)-1] == '&'); |
740 | |
741 | /* if there's already a question mark, then add further */ |
742 | /* parameters using the &, else use the ? */ |
743 | char *append_char = path_ends_with_special?"":(path_contains_qm?"&":"?"); |
744 | |
745 | char *url = g_strdup_printf("%s%s" |
746 | "%sSERVICE=wms" |
747 | "&WMTVER=1.1.1" |
748 | "&REQUEST=GetCapabilities", |
749 | wms->server, wms->path, append_char); |
750 | |
751 | char *cap = net_io_download_mem(GTK_WIDGET(appdata->window), url); |
752 | g_free(url); |
753 | |
754 | /* ----------- parse capabilities -------------- */ |
755 | |
756 | if(!cap) { |
757 | errorf(GTK_WIDGET(appdata->window), |
758 | _("WMS download failed:\n\n" |
759 | "GetCapabilities failed")); |
760 | } else { |
761 | xmlDoc *doc = NULL; |
762 | |
763 | LIBXML_TEST_VERSION; |
764 | |
765 | /* parse the file and get the DOM */ |
766 | if((doc = xmlReadMemory(cap, strlen(cap), NULL, NULL, 0)) == NULL) { |
767 | xmlErrorPtr errP = xmlGetLastError(); |
768 | errorf(GTK_WIDGET(appdata->window), |
769 | _("WMS download failed:\n\n" |
770 | "XML error while parsing capabilities:\n" |
771 | "%s"), errP->message); |
772 | } else { |
773 | printf("ok, parse doc tree\n"); |
774 | |
775 | wms_cap_parse_doc(wms, doc); |
776 | } |
777 | |
778 | g_free(cap); |
779 | } |
780 | |
781 | /* ------------ basic checks ------------- */ |
782 | |
783 | if(!wms->cap || !wms->service || !wms->cap->layer || |
784 | !wms->cap->request || !wms->cap->request->getmap) { |
785 | errorf(GTK_WIDGET(appdata->window), _("Incomplete/unexpected reply!")); |
786 | wms_free(wms); |
787 | return; |
788 | } |
789 | |
790 | if(!wms->cap->request->getmap->format) { |
791 | errorf(GTK_WIDGET(appdata->window), _("No supported image format found.")); |
792 | wms_free(wms); |
793 | return; |
794 | } |
795 | |
796 | /* ---------- evaluate layers ----------- */ |
797 | |
798 | wms_layer_t *layer = wms_get_requestable_layers(wms); |
799 | gboolean at_least_one_ok = wms_one_layer_is_usable(appdata->project, layer); |
800 | |
801 | if(!at_least_one_ok) { |
802 | errorf(GTK_WIDGET(appdata->window), |
803 | _("Server provides no data in the required format!\n" |
804 | "(epsg4326 and LatLonBoundingBox are mandatory for osm2go)")); |
805 | wms_layer_free(layer); |
806 | wms_free(wms); |
807 | return; |
808 | } |
809 | |
810 | if(!wms_layer_dialog(appdata, layer)) { |
811 | wms_layer_free(layer); |
812 | wms_free(wms); |
813 | return; |
814 | } |
815 | |
816 | /* --------- build getmap request ----------- */ |
817 | |
818 | /* get required image size */ |
819 | wms_setup_extent(appdata->project, wms); |
820 | |
821 | /* start building url */ |
822 | url = g_strdup_printf("%s%s" |
823 | "%sSERVICE=wms" |
824 | "&WMTVER=1.1.1" |
825 | "&REQUEST=GetMap" |
826 | "&LAYERS=", |
827 | wms->server, wms->path, append_char); |
828 | |
829 | /* append layers */ |
830 | char *old; |
831 | wms_layer_t *t = layer; |
832 | gint cnt = 0; |
833 | while(t) { |
834 | if(t->selected) { |
835 | old = url; |
836 | url = g_strconcat(url, (!cnt)?"":",", t->name, NULL); |
837 | g_free(old); |
838 | cnt++; |
839 | } |
840 | t = t->next; |
841 | } |
842 | wms_layer_free(layer); |
843 | |
844 | /* append styles entry */ |
845 | old = url; |
846 | url = g_strconcat(url, "&STYLES=", NULL); |
847 | g_free(old); |
848 | |
849 | while(--cnt) { |
850 | old = url; |
851 | url = g_strconcat(url, ",", NULL); |
852 | g_free(old); |
853 | } |
854 | |
855 | /* and append rest */ |
856 | char minlon[G_ASCII_DTOSTR_BUF_SIZE], minlat[G_ASCII_DTOSTR_BUF_SIZE]; |
857 | char maxlon[G_ASCII_DTOSTR_BUF_SIZE], maxlat[G_ASCII_DTOSTR_BUF_SIZE]; |
858 | |
859 | g_ascii_dtostr(minlon, sizeof(minlon), appdata->project->min.lon); |
860 | g_ascii_dtostr(minlat, sizeof(minlat), appdata->project->min.lat); |
861 | g_ascii_dtostr(maxlon, sizeof(maxlon), appdata->project->max.lon); |
862 | g_ascii_dtostr(maxlat, sizeof(maxlat), appdata->project->max.lat); |
863 | |
864 | gint format = 0; |
865 | while(!(wms->cap->request->getmap->format & (1<<format))) |
866 | format++; |
867 | |
868 | const char *formats[] = { "image/jpg", "image/jpeg", |
869 | "image/png", "image/gif" }; |
870 | |
871 | old = url; |
872 | url = g_strdup_printf("%s&SRS=EPSG:4326&BBOX=%s,%s,%s,%s" |
873 | "&WIDTH=%d&HEIGHT=%d&FORMAT=%s&reaspect=false", url, |
874 | minlon, minlat, maxlon, maxlat, wms->width, |
875 | wms->height, formats[format]); |
876 | g_free(old); |
877 | |
878 | const char *exts[] = { "jpg", "jpg", "png", "gif" }; |
879 | char *filename = g_strdup_printf("%s/wms.%s", appdata->project->path, |
880 | exts[format]); |
881 | |
882 | |
883 | /* remove any existing image before */ |
884 | wms_remove(appdata); |
885 | |
886 | if(!net_io_download_file(GTK_WIDGET(appdata->window), url, filename)) { |
887 | g_free(filename); |
888 | g_free(url); |
889 | wms_free(wms); |
890 | return; |
891 | } |
892 | |
893 | /* there should be a matching file on disk now */ |
894 | map_set_bg_image(appdata->map, filename); |
895 | |
896 | g_free(filename); |
897 | g_free(url); |
898 | |
899 | /* --------- free wms structure -----------------*/ |
900 | wms_free(wms); |
901 | |
902 | gtk_widget_set_sensitive(appdata->menu_item_wms_clear, TRUE); |
903 | gtk_widget_set_sensitive(appdata->menu_item_wms_adjust, TRUE); |
904 | } |
905 | |
906 | /* try to load an existing image into map */ |
907 | void wms_load(appdata_t *appdata) { |
908 | const char *exts[] = { "png", "gif", "jpg", "" }; |
909 | int i=0; |
910 | |
911 | while(exts[i][0]) { |
912 | char *filename = g_strdup_printf("%s/wms.%s", appdata->project->path, |
913 | exts[i]); |
914 | |
915 | if(g_file_test(filename, G_FILE_TEST_EXISTS)) { |
916 | appdata->map->bg.offset.x = appdata->project->wms_offset.x; |
917 | appdata->map->bg.offset.y = appdata->project->wms_offset.y; |
918 | |
919 | map_set_bg_image(appdata->map, filename); |
920 | |
921 | /* restore image to saved position */ |
922 | gint x = appdata->osm->bounds->min.x + appdata->map->bg.offset.x; |
923 | gint y = appdata->osm->bounds->min.y + appdata->map->bg.offset.y; |
924 | canvas_image_move(appdata->map->bg.item, x, y, |
925 | appdata->map->bg.scale.x, appdata->map->bg.scale.y); |
926 | |
927 | g_free(filename); |
928 | |
929 | gtk_widget_set_sensitive(appdata->menu_item_wms_clear, TRUE); |
930 | gtk_widget_set_sensitive(appdata->menu_item_wms_adjust, TRUE); |
931 | |
932 | return; |
933 | } |
934 | g_free(filename); |
935 | i++; |
936 | } |
937 | } |
938 | |
939 | void wms_remove(appdata_t *appdata) { |
940 | const char *exts[] = { "png", "gif", "jpg", "" }; |
941 | int i=0; |
942 | |
943 | /* this cancels any wms adjustment in progress */ |
944 | if(appdata->map->action.type == MAP_ACTION_BG_ADJUST) |
945 | map_action_cancel(appdata); |
946 | |
947 | gtk_widget_set_sensitive(appdata->menu_item_wms_clear, FALSE); |
948 | gtk_widget_set_sensitive(appdata->menu_item_wms_adjust, FALSE); |
949 | |
950 | map_remove_bg_image(appdata->map); |
951 | |
952 | while(exts[i][0]) { |
953 | char *filename = g_strdup_printf("%s/wms.%s", appdata->project->path, |
954 | exts[i]); |
955 | |
956 | if(g_file_test(filename, G_FILE_TEST_EXISTS)) |
957 | g_remove(filename); |
958 | |
959 | g_free(filename); |
960 | i++; |
961 | } |
962 | } |
963 |