Parent Directory | Revision Log
Track handling redone
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 | enum { |
469 | WMS_SERVER_COL_NAME = 0, |
470 | WMS_SERVER_COL_DATA, |
471 | WMS_SERVER_NUM_COLS |
472 | }; |
473 | |
474 | typedef struct { |
475 | appdata_t *appdata; |
476 | wms_t *wms; |
477 | GtkWidget *dialog, *list; |
478 | GtkListStore *store; |
479 | GtkWidget *server_label, *path_label; |
480 | } wms_server_context_t; |
481 | |
482 | static wms_server_t *get_selection(wms_server_context_t *context) { |
483 | GtkTreeSelection *selection; |
484 | GtkTreeModel *model; |
485 | GtkTreeIter iter; |
486 | |
487 | selection = list_get_selection(context->list); |
488 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
489 | wms_server_t *wms_server; |
490 | gtk_tree_model_get(model, &iter, WMS_SERVER_COL_DATA, &wms_server, -1); |
491 | return(wms_server); |
492 | } |
493 | |
494 | return NULL; |
495 | } |
496 | |
497 | static void wms_server_selected(wms_server_context_t *context, |
498 | wms_server_t *selected) { |
499 | |
500 | if(!selected && context->wms->server && context->wms->path) { |
501 | /* if the projects settings match a list entry, then select this */ |
502 | |
503 | GtkTreeSelection *selection = list_get_selection(context->list); |
504 | |
505 | /* walk the entire store to get all values */ |
506 | wms_server_t *server = NULL; |
507 | GtkTreeIter iter; |
508 | |
509 | gboolean valid = |
510 | gtk_tree_model_get_iter_first(GTK_TREE_MODEL(context->store), &iter); |
511 | |
512 | while(valid && !selected) { |
513 | gtk_tree_model_get(GTK_TREE_MODEL(context->store), &iter, |
514 | WMS_SERVER_COL_DATA, &server, -1); |
515 | g_assert(server); |
516 | |
517 | if((strcmp(server->server, context->wms->server) == 0) && |
518 | (strcmp(server->path, context->wms->path) == 0)) { |
519 | gtk_tree_selection_select_iter(selection, &iter); |
520 | selected = server; |
521 | } |
522 | |
523 | valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(context->store), &iter); |
524 | } |
525 | } |
526 | |
527 | list_button_enable(context->list, LIST_BUTTON_REMOVE, selected != NULL); |
528 | list_button_enable(context->list, LIST_BUTTON_EDIT, selected != NULL); |
529 | |
530 | /* user can click ok if a entry is selected or if both fields are */ |
531 | /* otherwise valid */ |
532 | if(selected) { |
533 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
534 | GTK_RESPONSE_ACCEPT, TRUE); |
535 | |
536 | gtk_label_set_text(GTK_LABEL(context->server_label), selected->server); |
537 | gtk_label_set_text(GTK_LABEL(context->path_label), selected->path); |
538 | } else { |
539 | gtk_dialog_set_response_sensitive(GTK_DIALOG(context->dialog), |
540 | GTK_RESPONSE_ACCEPT, context->wms->server && context->wms->path); |
541 | |
542 | if(context->wms->server) |
543 | gtk_label_set_text(GTK_LABEL(context->server_label), |
544 | context->wms->server); |
545 | if(context->wms->path) |
546 | gtk_label_set_text(GTK_LABEL(context->path_label), |
547 | context->wms->path); |
548 | } |
549 | } |
550 | |
551 | static gboolean |
552 | wms_server_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, |
553 | GtkTreePath *path, gboolean path_currently_selected, |
554 | gpointer userdata) { |
555 | wms_server_context_t *context = (wms_server_context_t*)userdata; |
556 | GtkTreeIter iter; |
557 | |
558 | if(gtk_tree_model_get_iter(model, &iter, path)) { |
559 | wms_server_t *wms_server = NULL; |
560 | |
561 | g_assert(gtk_tree_path_get_depth(path) == 1); |
562 | gtk_tree_model_get(model, &iter, WMS_SERVER_COL_DATA, &wms_server, -1); |
563 | wms_server_selected(context, wms_server); |
564 | } |
565 | |
566 | return TRUE; /* allow selection state to change */ |
567 | } |
568 | |
569 | static void on_server_remove(GtkWidget *but, wms_server_context_t *context) { |
570 | GtkTreeSelection *selection; |
571 | GtkTreeModel *model; |
572 | GtkTreeIter iter; |
573 | |
574 | selection = list_get_selection(context->list); |
575 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
576 | wms_server_t *server = NULL; |
577 | gtk_tree_model_get(model, &iter, WMS_SERVER_COL_DATA, &server, -1); |
578 | |
579 | g_assert(server); |
580 | |
581 | /* de-chain */ |
582 | printf("de-chaining server %s\n", server->name); |
583 | wms_server_t **prev = &context->appdata->settings->wms_server; |
584 | while(*prev != server) prev = &((*prev)->next); |
585 | *prev = server->next; |
586 | |
587 | /* free tag itself */ |
588 | wms_server_free(server); |
589 | |
590 | /* and remove from store */ |
591 | gtk_list_store_remove(GTK_LIST_STORE(model), &iter); |
592 | } |
593 | |
594 | wms_server_selected(context, NULL); |
595 | } |
596 | |
597 | static void callback_modified_name(GtkWidget *widget, gpointer data) { |
598 | wms_server_context_t *context = (wms_server_context_t*)data; |
599 | |
600 | char *name = (char*)gtk_entry_get_text(GTK_ENTRY(widget)); |
601 | |
602 | /* name must not contain some special chars */ |
603 | gboolean ok = TRUE; |
604 | |
605 | /* search all entries except the last (which is the one we are editing) */ |
606 | wms_server_t *server = context->appdata->settings->wms_server; |
607 | while(server && server->next) { |
608 | if(strcasecmp(server->name, name) == 0) ok = FALSE; |
609 | server = server->next; |
610 | } |
611 | |
612 | GtkWidget *toplevel = gtk_widget_get_toplevel(widget); |
613 | /* toplevel is a dialog only of dialog has been realized */ |
614 | if(GTK_IS_DIALOG(toplevel)) |
615 | gtk_dialog_set_response_sensitive( |
616 | GTK_DIALOG(gtk_widget_get_toplevel(widget)), |
617 | GTK_RESPONSE_ACCEPT, ok); |
618 | } |
619 | |
620 | /* edit url and path of a given wms server entry */ |
621 | gboolean wms_server_edit(wms_server_context_t *context, gboolean edit_name, |
622 | wms_server_t *wms_server) { |
623 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Edit WMS Server"), |
624 | GTK_WINDOW(context->dialog), GTK_DIALOG_MODAL, |
625 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
626 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
627 | NULL); |
628 | |
629 | #ifdef USE_HILDON |
630 | gtk_window_set_default_size(GTK_WINDOW(dialog), 500, 50); |
631 | #else |
632 | gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 50); |
633 | #endif |
634 | |
635 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), |
636 | GTK_RESPONSE_ACCEPT); |
637 | |
638 | GtkWidget *label, *name, *server, *path; |
639 | GtkWidget *table = gtk_table_new(2, 3, FALSE); |
640 | |
641 | gtk_table_attach(GTK_TABLE(table), |
642 | label = gtk_label_new(_("Name:")), 0, 1, 0, 1, |
643 | GTK_FILL, 0, 0, 0); |
644 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); |
645 | gtk_table_attach_defaults(GTK_TABLE(table), |
646 | name = gtk_entry_new(), 1, 2, 0, 1); |
647 | gtk_entry_set_activates_default(GTK_ENTRY(name), TRUE); |
648 | HILDON_ENTRY_NO_AUTOCAP(name); |
649 | gtk_widget_set_sensitive(GTK_WIDGET(name), edit_name); |
650 | g_signal_connect(G_OBJECT(name), "changed", |
651 | G_CALLBACK(callback_modified_name), context); |
652 | |
653 | gtk_table_attach(GTK_TABLE(table), |
654 | label = gtk_label_new(_("Server:")), 0, 1, 1, 2, |
655 | GTK_FILL, 0, 0, 0); |
656 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); |
657 | gtk_table_attach_defaults(GTK_TABLE(table), |
658 | server = gtk_entry_new(), 1, 2, 1, 2); |
659 | gtk_entry_set_activates_default(GTK_ENTRY(server), TRUE); |
660 | HILDON_ENTRY_NO_AUTOCAP(server); |
661 | |
662 | gtk_table_attach(GTK_TABLE(table), |
663 | label = gtk_label_new(_("Path:")), 0, 1, 2, 3, |
664 | GTK_FILL, 0, 0, 0); |
665 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); |
666 | gtk_table_attach_defaults(GTK_TABLE(table), |
667 | path = gtk_entry_new(), 1, 2, 2, 3); |
668 | gtk_entry_set_activates_default(GTK_ENTRY(path), TRUE); |
669 | HILDON_ENTRY_NO_AUTOCAP(path); |
670 | |
671 | gtk_entry_set_text(GTK_ENTRY(name), wms_server->name); |
672 | gtk_entry_set_text(GTK_ENTRY(server), wms_server->server); |
673 | gtk_entry_set_text(GTK_ENTRY(path), wms_server->path); |
674 | |
675 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table); |
676 | |
677 | gtk_widget_show_all(dialog); |
678 | |
679 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) { |
680 | if(edit_name) { |
681 | GtkTreeSelection *selection; |
682 | GtkTreeModel *model; |
683 | GtkTreeIter iter; |
684 | selection = list_get_selection(context->list); |
685 | gtk_tree_selection_get_selected(selection, &model, &iter); |
686 | gtk_list_store_set(context->store, &iter, |
687 | WMS_SERVER_COL_NAME, wms_server->name, |
688 | -1); |
689 | |
690 | g_free(wms_server->name); |
691 | wms_server->name = strdup((char*)gtk_entry_get_text(GTK_ENTRY(name))); |
692 | } |
693 | |
694 | g_free(wms_server->server); |
695 | wms_server->server = strdup((char*)gtk_entry_get_text(GTK_ENTRY(server))); |
696 | g_free(wms_server->path); |
697 | wms_server->path = strdup((char*)gtk_entry_get_text(GTK_ENTRY(path))); |
698 | printf("setting %s/%s\n", wms_server->server, wms_server->path); |
699 | |
700 | /* set texts below */ |
701 | gtk_label_set_text(GTK_LABEL(context->server_label), wms_server->server); |
702 | gtk_label_set_text(GTK_LABEL(context->path_label), wms_server->path); |
703 | |
704 | gtk_widget_destroy(dialog); |
705 | |
706 | return TRUE; |
707 | } |
708 | |
709 | gtk_widget_destroy(dialog); |
710 | return FALSE; |
711 | } |
712 | |
713 | /* user clicked "edit..." button in the wms server list */ |
714 | static void on_server_edit(GtkWidget *but, wms_server_context_t *context) { |
715 | GtkTreeSelection *selection; |
716 | GtkTreeModel *model; |
717 | GtkTreeIter iter; |
718 | |
719 | selection = list_get_selection(context->list); |
720 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
721 | wms_server_t *server = NULL; |
722 | gtk_tree_model_get(model, &iter, WMS_SERVER_COL_DATA, &server, -1); |
723 | g_assert(server); |
724 | |
725 | wms_server_edit(context, FALSE, server); |
726 | } |
727 | } |
728 | |
729 | /* user clicked "add..." button in the wms server list */ |
730 | static void on_server_add(GtkWidget *but, wms_server_context_t *context) { |
731 | |
732 | /* attach a new server item to the chain */ |
733 | wms_server_t **prev = &context->appdata->settings->wms_server; |
734 | while(*prev) prev = &(*prev)->next; |
735 | |
736 | *prev = g_new0(wms_server_t, 1); |
737 | (*prev)->name = g_strdup("<service name>"); |
738 | (*prev)->server = g_strdup("<server url>"); |
739 | (*prev)->path = g_strdup("<path in server>"); |
740 | |
741 | GtkTreeModel *model = list_get_model(context->list); |
742 | |
743 | GtkTreeIter iter; |
744 | gtk_list_store_append(GTK_LIST_STORE(model), &iter); |
745 | gtk_list_store_set(GTK_LIST_STORE(model), &iter, |
746 | WMS_SERVER_COL_NAME, (*prev)->name, |
747 | WMS_SERVER_COL_DATA, *prev, |
748 | -1); |
749 | |
750 | GtkTreeSelection *selection = list_get_selection(context->list); |
751 | gtk_tree_selection_select_iter(selection, &iter); |
752 | |
753 | if(!wms_server_edit(context, TRUE, *prev)) { |
754 | /* user has cancelled request. remove newly added item */ |
755 | printf("user clicked cancel\n"); |
756 | |
757 | wms_server_free(*prev); |
758 | *prev = NULL; |
759 | |
760 | gtk_list_store_remove(GTK_LIST_STORE(model), &iter); |
761 | } else |
762 | /* update name from edit result */ |
763 | gtk_list_store_set(GTK_LIST_STORE(model), &iter, |
764 | WMS_SERVER_COL_NAME, (*prev)->name, |
765 | -1); |
766 | |
767 | wms_server_selected(context, *prev); |
768 | } |
769 | |
770 | /* widget to select a wms server from a list */ |
771 | static GtkWidget *wms_server_widget(wms_server_context_t *context) { |
772 | |
773 | context->list = list_new(LIST_HILDON_WITHOUT_HEADERS); |
774 | |
775 | list_set_selection_function(context->list, wms_server_selection_func, |
776 | context); |
777 | |
778 | list_set_columns(context->list, |
779 | _("Name"), WMS_SERVER_COL_NAME, LIST_FLAG_ELLIPSIZE, |
780 | NULL); |
781 | |
782 | /* build and fill the store */ |
783 | context->store = gtk_list_store_new(WMS_SERVER_NUM_COLS, |
784 | G_TYPE_STRING, G_TYPE_POINTER); |
785 | |
786 | list_set_store(context->list, context->store); |
787 | |
788 | GtkTreeIter iter; |
789 | wms_server_t *wms_server = context->appdata->settings->wms_server; |
790 | while(wms_server) { |
791 | /* Append a row and fill in some data */ |
792 | gtk_list_store_append(context->store, &iter); |
793 | gtk_list_store_set(context->store, &iter, |
794 | WMS_SERVER_COL_NAME, wms_server->name, |
795 | WMS_SERVER_COL_DATA, wms_server, |
796 | -1); |
797 | |
798 | wms_server = wms_server->next; |
799 | } |
800 | |
801 | g_object_unref(context->store); |
802 | |
803 | list_set_static_buttons(context->list, G_CALLBACK(on_server_add), |
804 | G_CALLBACK(on_server_edit), G_CALLBACK(on_server_remove), context); |
805 | |
806 | return context->list; |
807 | } |
808 | |
809 | static gboolean wms_server_dialog(appdata_t *appdata, wms_t *wms) { |
810 | gboolean ok = FALSE; |
811 | |
812 | wms_server_context_t *context = g_new0(wms_server_context_t, 1); |
813 | context->appdata = appdata; |
814 | context->wms = wms; |
815 | |
816 | context->dialog = gtk_dialog_new_with_buttons(_("WMS Server Selection"), |
817 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
818 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
819 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
820 | NULL); |
821 | |
822 | #ifdef USE_HILDON |
823 | gtk_window_set_default_size(GTK_WINDOW(context->dialog), 500, 300); |
824 | #else |
825 | gtk_window_set_default_size(GTK_WINDOW(context->dialog), 400, 200); |
826 | #endif |
827 | |
828 | /* server selection box */ |
829 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
830 | wms_server_widget(context)); |
831 | |
832 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
833 | gtk_hseparator_new(), FALSE, FALSE, 0); |
834 | |
835 | GtkWidget *label; |
836 | GtkWidget *table = gtk_table_new(2, 2, FALSE); // x, y |
837 | gtk_table_set_col_spacing(GTK_TABLE(table), 0, 10); |
838 | |
839 | label = gtk_label_new(_("Server:")); |
840 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); |
841 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0,0,0); |
842 | context->server_label = gtk_label_new(""); |
843 | gtk_label_set_ellipsize(GTK_LABEL(context->server_label), |
844 | PANGO_ELLIPSIZE_MIDDLE); |
845 | gtk_misc_set_alignment(GTK_MISC(context->server_label), 0.f, 0.5f); |
846 | gtk_table_attach_defaults(GTK_TABLE(table), context->server_label, |
847 | 1, 2, 0, 1); |
848 | |
849 | label = gtk_label_new(_("Path:")); |
850 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); |
851 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2, GTK_FILL, 0,0,0); |
852 | context->path_label = gtk_label_new(""); |
853 | gtk_label_set_ellipsize(GTK_LABEL(context->path_label), |
854 | PANGO_ELLIPSIZE_MIDDLE); |
855 | gtk_misc_set_alignment(GTK_MISC(context->path_label), 0.f, 0.5f); |
856 | gtk_table_attach_defaults(GTK_TABLE(table), context->path_label, |
857 | 1, 2, 1, 2); |
858 | |
859 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
860 | table, FALSE, FALSE, 0); |
861 | |
862 | |
863 | wms_server_selected(context, NULL); |
864 | |
865 | gtk_widget_show_all(context->dialog); |
866 | |
867 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(context->dialog))) { |
868 | wms_server_t *server = get_selection(context); |
869 | if(server) { |
870 | /* fetch parameters from selected entry */ |
871 | printf("WMS: using %s\n", server->name); |
872 | if(wms->server) g_free(wms->server); |
873 | wms->server = g_strdup(server->server); |
874 | if(wms->path) g_free(wms->path); |
875 | wms->path = g_strdup(server->path); |
876 | ok = TRUE; |
877 | } else { |
878 | if(wms->server && wms->path) |
879 | ok = TRUE; |
880 | |
881 | } |
882 | } |
883 | |
884 | gtk_widget_destroy(context->dialog); |
885 | |
886 | g_free(context); |
887 | return ok; |
888 | } |
889 | |
890 | enum { |
891 | LAYER_COL_TITLE = 0, |
892 | LAYER_COL_SELECTED, |
893 | LAYER_COL_FITS, |
894 | LAYER_COL_DATA, |
895 | LAYER_NUM_COLS |
896 | }; |
897 | |
898 | static void |
899 | layer_toggled(GtkCellRendererToggle *cell, const gchar *path_str, |
900 | GtkListStore *store) { |
901 | GtkTreePath *path; |
902 | GtkTreeIter iter; |
903 | |
904 | path = gtk_tree_path_new_from_string(path_str); |
905 | gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, path); |
906 | |
907 | /* get current enabled flag */ |
908 | gboolean enabled; |
909 | gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, |
910 | LAYER_COL_SELECTED, &enabled, -1); |
911 | |
912 | /* change it and store it */ |
913 | enabled = !enabled; |
914 | gtk_list_store_set(store, &iter, LAYER_COL_SELECTED, enabled, -1); |
915 | |
916 | /* and store it in the layer itself */ |
917 | wms_layer_t *layer = NULL; |
918 | gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, LAYER_COL_DATA, &layer, -1); |
919 | layer->selected = enabled; |
920 | |
921 | /* walk the entire store to get all values */ |
922 | if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) { |
923 | gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, |
924 | LAYER_COL_SELECTED, &enabled, -1); |
925 | |
926 | while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter) && |
927 | !enabled) |
928 | gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, |
929 | LAYER_COL_SELECTED, &enabled, -1); |
930 | } |
931 | |
932 | GtkWidget *dialog = g_object_get_data(G_OBJECT(store), "dialog"); |
933 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), |
934 | GTK_RESPONSE_ACCEPT, enabled); |
935 | |
936 | gtk_tree_path_free(path); |
937 | } |
938 | |
939 | static GtkWidget *wms_layer_widget(appdata_t *appdata, wms_layer_t *layer, |
940 | GtkWidget *dialog) { |
941 | GtkWidget *view = gtk_tree_view_new(); |
942 | |
943 | /* build the store */ |
944 | GtkListStore *store = gtk_list_store_new(LAYER_NUM_COLS, |
945 | G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_POINTER); |
946 | |
947 | g_object_set_data(G_OBJECT(store), "dialog", dialog); |
948 | |
949 | /* --- "selected" column --- */ |
950 | GtkCellRenderer *renderer = gtk_cell_renderer_toggle_new(); |
951 | g_signal_connect(renderer, "toggled", G_CALLBACK(layer_toggled), store); |
952 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
953 | -1, _(""), renderer, |
954 | "active", LAYER_COL_SELECTED, |
955 | "activatable", LAYER_COL_FITS, |
956 | NULL); |
957 | |
958 | /* --- "Title" column --- */ |
959 | renderer = gtk_cell_renderer_text_new(); |
960 | g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL ); |
961 | GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( |
962 | _("Title"), renderer, |
963 | "text", LAYER_COL_TITLE, |
964 | NULL); |
965 | |
966 | gtk_tree_view_column_set_expand(column, TRUE); |
967 | gtk_tree_view_insert_column(GTK_TREE_VIEW(view), column, -1); |
968 | |
969 | gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store)); |
970 | |
971 | GtkTreeIter iter; |
972 | while(layer) { |
973 | gboolean fits = wms_llbbox_fits(appdata->project, &layer->llbbox); |
974 | |
975 | /* Append a row and fill in some data */ |
976 | gtk_list_store_append(store, &iter); |
977 | gtk_list_store_set(store, &iter, |
978 | LAYER_COL_SELECTED, FALSE, |
979 | LAYER_COL_TITLE, layer->title, |
980 | LAYER_COL_FITS, fits, |
981 | LAYER_COL_DATA, layer, |
982 | -1); |
983 | layer = layer->next; |
984 | } |
985 | |
986 | g_object_unref(store); |
987 | |
988 | /* put it into a scrolled window */ |
989 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
990 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
991 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
992 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), |
993 | GTK_SHADOW_ETCHED_IN); |
994 | gtk_container_add(GTK_CONTAINER(scrolled_window), view); |
995 | |
996 | return scrolled_window; |
997 | } |
998 | |
999 | |
1000 | static gboolean wms_layer_dialog(appdata_t *appdata, wms_layer_t *layer) { |
1001 | gboolean ok = FALSE; |
1002 | |
1003 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("WMS layer selection"), |
1004 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, |
1005 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
1006 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
1007 | NULL); |
1008 | |
1009 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), |
1010 | GTK_RESPONSE_ACCEPT, FALSE); |
1011 | |
1012 | #ifdef USE_HILDON |
1013 | /* making the dialog a little wider makes it less "crowded" */ |
1014 | gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 300); |
1015 | #else |
1016 | gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 200); |
1017 | #endif |
1018 | |
1019 | /* layer list */ |
1020 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
1021 | wms_layer_widget(appdata, layer, dialog)); |
1022 | |
1023 | |
1024 | gtk_widget_show_all(dialog); |
1025 | |
1026 | if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) |
1027 | ok = TRUE; |
1028 | |
1029 | gtk_widget_destroy(dialog); |
1030 | |
1031 | return ok; |
1032 | } |
1033 | |
1034 | static gboolean wms_one_layer_is_usable(project_t *project, |
1035 | wms_layer_t *layer) { |
1036 | gboolean ok = FALSE; |
1037 | |
1038 | while(layer) { |
1039 | if(layer->name && layer->epsg4326 && layer->llbbox.valid) |
1040 | ok = TRUE; |
1041 | |
1042 | printf("----- Layer \"%s\" -----\n", layer->title); |
1043 | printf("Name: %s\n", layer->name); |
1044 | printf("epsg4326: %s\n", layer->epsg4326?"yes":"no"); |
1045 | if(layer->llbbox.valid) { |
1046 | printf("llbbox: %f/%f %f/%f\n", |
1047 | layer->llbbox.min.lat, layer->llbbox.min.lon, |
1048 | layer->llbbox.max.lat, layer->llbbox.max.lon); |
1049 | |
1050 | printf("llbbox fits project: %s\n", |
1051 | wms_llbbox_fits(project, &layer->llbbox)?"yes":"no"); |
1052 | } else |
1053 | printf("llbbox: none/invalid\n"); |
1054 | |
1055 | layer = layer->next; |
1056 | } |
1057 | |
1058 | return ok; |
1059 | } |
1060 | |
1061 | void wms_import(appdata_t *appdata) { |
1062 | if(!appdata->project) { |
1063 | errorf(GTK_WIDGET(appdata->window), |
1064 | _("Need an open project to derive WMS coordinates")); |
1065 | return; |
1066 | } |
1067 | |
1068 | /* this cancels any wms adjustment in progress */ |
1069 | if(appdata->map->action.type == MAP_ACTION_BG_ADJUST) |
1070 | map_action_cancel(appdata); |
1071 | |
1072 | wms_t *wms = g_new0(wms_t,1); |
1073 | wms->server = g_strdup(appdata->project->wms_server); |
1074 | wms->path = g_strdup(appdata->project->wms_path); |
1075 | |
1076 | /* reset any background adjustments in the project ... */ |
1077 | if((appdata->project->wms_offset.x != 0)|| |
1078 | (appdata->project->wms_offset.y != 0)) { |
1079 | |
1080 | appdata->project->wms_offset.x = 0; |
1081 | appdata->project->wms_offset.y = 0; |
1082 | appdata->project->dirty = TRUE; |
1083 | } |
1084 | |
1085 | /* ... as well as in the map */ |
1086 | appdata->map->bg.offset.x = 0; |
1087 | appdata->map->bg.offset.y = 0; |
1088 | |
1089 | /* get server from dialog */ |
1090 | if(!wms_server_dialog(appdata, wms)) { |
1091 | wms_free(wms); |
1092 | return; |
1093 | } |
1094 | |
1095 | /* ------------- copy values back into project ---------------- */ |
1096 | if(!appdata->project->wms_server || |
1097 | strcmp(appdata->project->wms_server, wms->server) != 0) { |
1098 | if(appdata->project->wms_server) |
1099 | g_free(appdata->project->wms_server); |
1100 | appdata->project->wms_server = g_strdup(wms->server); |
1101 | appdata->project->dirty = TRUE; |
1102 | } |
1103 | |
1104 | if(!appdata->project->wms_path || |
1105 | strcmp(appdata->project->wms_path, wms->path) != 0) { |
1106 | if(appdata->project->wms_path) |
1107 | g_free(appdata->project->wms_path); |
1108 | appdata->project->wms_path = g_strdup(wms->path); |
1109 | appdata->project->dirty = TRUE; |
1110 | } |
1111 | |
1112 | /* ----------- request capabilities -------------- */ |
1113 | gboolean path_contains_qm = (strchr(wms->path, '?') != NULL); |
1114 | gboolean path_ends_with_special = |
1115 | (wms->path[strlen(wms->path)-1] == '?') || |
1116 | (wms->path[strlen(wms->path)-1] == '&'); |
1117 | |
1118 | /* if there's already a question mark, then add further */ |
1119 | /* parameters using the &, else use the ? */ |
1120 | char *append_char = path_ends_with_special?"":(path_contains_qm?"&":"?"); |
1121 | |
1122 | char *url = g_strdup_printf("%s%s" |
1123 | "%sSERVICE=wms" |
1124 | // "&WMTVER=1.1.1" |
1125 | "&VERSION=1.1.1" |
1126 | "&REQUEST=GetCapabilities", |
1127 | wms->server, wms->path, append_char); |
1128 | |
1129 | char *cap = NULL; |
1130 | net_io_download_mem(GTK_WIDGET(appdata->window), url, &cap); |
1131 | g_free(url); |
1132 | |
1133 | /* ----------- parse capabilities -------------- */ |
1134 | |
1135 | if(!cap) { |
1136 | errorf(GTK_WIDGET(appdata->window), |
1137 | _("WMS download failed:\n\n" |
1138 | "GetCapabilities failed")); |
1139 | } else { |
1140 | xmlDoc *doc = NULL; |
1141 | |
1142 | LIBXML_TEST_VERSION; |
1143 | |
1144 | /* parse the file and get the DOM */ |
1145 | if((doc = xmlReadMemory(cap, strlen(cap), NULL, NULL, 0)) == NULL) { |
1146 | xmlErrorPtr errP = xmlGetLastError(); |
1147 | errorf(GTK_WIDGET(appdata->window), |
1148 | _("WMS download failed:\n\n" |
1149 | "XML error while parsing capabilities:\n" |
1150 | "%s"), errP->message); |
1151 | } else { |
1152 | printf("ok, parse doc tree\n"); |
1153 | |
1154 | wms_cap_parse_doc(wms, doc); |
1155 | } |
1156 | |
1157 | g_free(cap); |
1158 | } |
1159 | |
1160 | /* ------------ basic checks ------------- */ |
1161 | |
1162 | if(!wms->cap || !wms->service || !wms->cap->layer || |
1163 | !wms->cap->request || !wms->cap->request->getmap) { |
1164 | errorf(GTK_WIDGET(appdata->window), _("Incomplete/unexpected reply!")); |
1165 | wms_free(wms); |
1166 | return; |
1167 | } |
1168 | |
1169 | if(!wms->cap->request->getmap->format) { |
1170 | errorf(GTK_WIDGET(appdata->window), _("No supported image format found.")); |
1171 | wms_free(wms); |
1172 | return; |
1173 | } |
1174 | |
1175 | /* ---------- evaluate layers ----------- */ |
1176 | |
1177 | wms_layer_t *layer = wms_get_requestable_layers(wms); |
1178 | gboolean at_least_one_ok = wms_one_layer_is_usable(appdata->project, layer); |
1179 | |
1180 | if(!at_least_one_ok) { |
1181 | errorf(GTK_WIDGET(appdata->window), |
1182 | _("Server provides no data in the required format!\n" |
1183 | "(epsg4326 and LatLonBoundingBox are mandatory for osm2go)")); |
1184 | wms_layer_free(layer); |
1185 | wms_free(wms); |
1186 | return; |
1187 | } |
1188 | |
1189 | if(!wms_layer_dialog(appdata, layer)) { |
1190 | wms_layer_free(layer); |
1191 | wms_free(wms); |
1192 | return; |
1193 | } |
1194 | |
1195 | /* --------- build getmap request ----------- */ |
1196 | |
1197 | /* get required image size */ |
1198 | wms_setup_extent(appdata->project, wms); |
1199 | |
1200 | /* start building url */ |
1201 | url = g_strdup_printf("%s%s" |
1202 | "%sSERVICE=wms" |
1203 | // "&WMTVER=1.1.1" |
1204 | "&VERSION=1.1.1" |
1205 | "&REQUEST=GetMap" |
1206 | "&LAYERS=", |
1207 | wms->server, wms->path, append_char); |
1208 | |
1209 | /* append layers */ |
1210 | char *old; |
1211 | wms_layer_t *t = layer; |
1212 | gint cnt = 0; |
1213 | while(t) { |
1214 | if(t->selected) { |
1215 | old = url; |
1216 | url = g_strconcat(url, (!cnt)?"":",", t->name, NULL); |
1217 | g_free(old); |
1218 | cnt++; |
1219 | } |
1220 | t = t->next; |
1221 | } |
1222 | wms_layer_free(layer); |
1223 | |
1224 | /* append styles entry */ |
1225 | old = url; |
1226 | url = g_strconcat(url, "&STYLES=", NULL); |
1227 | g_free(old); |
1228 | |
1229 | while(--cnt) { |
1230 | old = url; |
1231 | url = g_strconcat(url, ",", NULL); |
1232 | g_free(old); |
1233 | } |
1234 | |
1235 | /* and append rest */ |
1236 | char minlon[G_ASCII_DTOSTR_BUF_SIZE], minlat[G_ASCII_DTOSTR_BUF_SIZE]; |
1237 | char maxlon[G_ASCII_DTOSTR_BUF_SIZE], maxlat[G_ASCII_DTOSTR_BUF_SIZE]; |
1238 | |
1239 | /* build strings of min and max lat and lon to be used in url */ |
1240 | g_ascii_formatd(minlon, sizeof(minlon), LL_FORMAT, |
1241 | appdata->project->min.lon); |
1242 | g_ascii_formatd(minlat, sizeof(minlat), LL_FORMAT, |
1243 | appdata->project->min.lat); |
1244 | g_ascii_formatd(maxlon, sizeof(maxlon), LL_FORMAT, |
1245 | appdata->project->max.lon); |
1246 | g_ascii_formatd(maxlat, sizeof(maxlat), LL_FORMAT, |
1247 | appdata->project->max.lat); |
1248 | |
1249 | /* find preferred supported video format */ |
1250 | gint format = 0; |
1251 | while(!(wms->cap->request->getmap->format & (1<<format))) |
1252 | format++; |
1253 | |
1254 | const char *formats[] = { "image/jpg", "image/jpeg", |
1255 | "image/png", "image/gif" }; |
1256 | |
1257 | /* build complete url */ |
1258 | old = url; |
1259 | url = g_strdup_printf("%s&SRS=EPSG:4326&BBOX=%s,%s,%s,%s" |
1260 | "&WIDTH=%d&HEIGHT=%d&FORMAT=%s" |
1261 | "&reaspect=false", url, |
1262 | minlon, minlat, maxlon, maxlat, wms->width, |
1263 | wms->height, formats[format]); |
1264 | g_free(old); |
1265 | |
1266 | const char *exts[] = { "jpg", "jpg", "png", "gif" }; |
1267 | char *filename = g_strdup_printf("%s/wms.%s", appdata->project->path, |
1268 | exts[format]); |
1269 | |
1270 | |
1271 | /* remove any existing image before */ |
1272 | wms_remove(appdata); |
1273 | |
1274 | if(!net_io_download_file(GTK_WIDGET(appdata->window), url, filename)) { |
1275 | g_free(filename); |
1276 | g_free(url); |
1277 | wms_free(wms); |
1278 | return; |
1279 | } |
1280 | |
1281 | /* there should be a matching file on disk now */ |
1282 | map_set_bg_image(appdata->map, filename); |
1283 | |
1284 | g_free(filename); |
1285 | g_free(url); |
1286 | |
1287 | /* --------- free wms structure -----------------*/ |
1288 | wms_free(wms); |
1289 | |
1290 | gtk_widget_set_sensitive(appdata->menu_item_wms_clear, TRUE); |
1291 | gtk_widget_set_sensitive(appdata->menu_item_wms_adjust, TRUE); |
1292 | } |
1293 | |
1294 | /* try to load an existing image into map */ |
1295 | void wms_load(appdata_t *appdata) { |
1296 | const char *exts[] = { "png", "gif", "jpg", "" }; |
1297 | int i=0; |
1298 | |
1299 | while(exts[i][0]) { |
1300 | char *filename = g_strdup_printf("%s/wms.%s", appdata->project->path, |
1301 | exts[i]); |
1302 | |
1303 | if(g_file_test(filename, G_FILE_TEST_EXISTS)) { |
1304 | appdata->map->bg.offset.x = appdata->project->wms_offset.x; |
1305 | appdata->map->bg.offset.y = appdata->project->wms_offset.y; |
1306 | |
1307 | map_set_bg_image(appdata->map, filename); |
1308 | |
1309 | /* restore image to saved position */ |
1310 | gint x = appdata->osm->bounds->min.x + appdata->map->bg.offset.x; |
1311 | gint y = appdata->osm->bounds->min.y + appdata->map->bg.offset.y; |
1312 | canvas_image_move(appdata->map->bg.item, x, y, |
1313 | appdata->map->bg.scale.x, appdata->map->bg.scale.y); |
1314 | |
1315 | g_free(filename); |
1316 | |
1317 | gtk_widget_set_sensitive(appdata->menu_item_wms_clear, TRUE); |
1318 | gtk_widget_set_sensitive(appdata->menu_item_wms_adjust, TRUE); |
1319 | |
1320 | return; |
1321 | } |
1322 | g_free(filename); |
1323 | i++; |
1324 | } |
1325 | } |
1326 | |
1327 | void wms_remove(appdata_t *appdata) { |
1328 | const char *exts[] = { "png", "gif", "jpg", "" }; |
1329 | int i=0; |
1330 | |
1331 | /* this cancels any wms adjustment in progress */ |
1332 | if(appdata->map->action.type == MAP_ACTION_BG_ADJUST) |
1333 | map_action_cancel(appdata); |
1334 | |
1335 | gtk_widget_set_sensitive(appdata->menu_item_wms_clear, FALSE); |
1336 | gtk_widget_set_sensitive(appdata->menu_item_wms_adjust, FALSE); |
1337 | |
1338 | map_remove_bg_image(appdata->map); |
1339 | |
1340 | while(exts[i][0]) { |
1341 | char *filename = g_strdup_printf("%s/wms.%s", appdata->project->path, |
1342 | exts[i]); |
1343 | |
1344 | if(g_file_test(filename, G_FILE_TEST_EXISTS)) |
1345 | g_remove(filename); |
1346 | |
1347 | g_free(filename); |
1348 | i++; |
1349 | } |
1350 | } |
1351 | |
1352 | struct server_preset_s { |
1353 | char *name, *server, *path; |
1354 | } default_servers[] = { |
1355 | { "NASA landsat", "http://onearth.jpl.nasa.gov", "/wms.cgi" }, |
1356 | /* add more servers here ... */ |
1357 | { NULL, NULL, NULL } |
1358 | }; |
1359 | |
1360 | wms_server_t *wms_server_get_default(void) { |
1361 | wms_server_t *server = NULL, **cur = &server; |
1362 | struct server_preset_s *preset = default_servers; |
1363 | |
1364 | while(preset->name) { |
1365 | *cur = g_new0(wms_server_t, 1); |
1366 | (*cur)->name = g_strdup(preset->name); |
1367 | (*cur)->server = g_strdup(preset->server); |
1368 | (*cur)->path = g_strdup(preset->path); |
1369 | cur = &(*cur)->next; |
1370 | preset++; |
1371 | } |
1372 | |
1373 | return server; |
1374 | } |
1375 | |
1376 | void wms_server_free(wms_server_t *wms_server) { |
1377 | if(wms_server->name) g_free(wms_server->name); |
1378 | if(wms_server->server) g_free(wms_server->server); |
1379 | if(wms_server->path) g_free(wms_server->path); |
1380 | g_free(wms_server); |
1381 | } |
1382 | |
1383 | void wms_servers_free(wms_server_t *wms_server) { |
1384 | while(wms_server) { |
1385 | wms_server_t *next = wms_server->next; |
1386 | wms_server_free(wms_server); |
1387 | wms_server = next; |
1388 | } |
1389 | } |