Parent Directory | Revision Log
ID hashing for fast ID resolving
1 | harbaum | 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 | #ifndef OSM_H | ||
21 | #define OSM_H | ||
22 | |||
23 | #include <math.h> | ||
24 | |||
25 | #define OSM_FLAG_DIRTY (1<<0) | ||
26 | #define OSM_FLAG_DELETED (1<<1) | ||
27 | #define OSM_FLAG_NEW (1<<2) | ||
28 | #define OSM_FLAG_HIDDEN (1<<3) | ||
29 | |||
30 | typedef gulong item_id_t; | ||
31 | |||
32 | #define ID_ILLEGAL ((item_id_t)0) | ||
33 | |||
34 | /* icon stuff is required since nodes may held a icon reference */ | ||
35 | struct icon_s; | ||
36 | |||
37 | typedef struct bounds_s { | ||
38 | pos_t ll_min, ll_max; | ||
39 | lpos_t min, max; | ||
40 | lpos_t center; | ||
41 | float scale; | ||
42 | } bounds_t; | ||
43 | |||
44 | typedef struct user_s { | ||
45 | char *name; | ||
46 | struct user_s *next; | ||
47 | } user_t; | ||
48 | |||
49 | typedef struct tag_s { | ||
50 | char *key, *value; | ||
51 | struct tag_s *next; | ||
52 | } tag_t; | ||
53 | |||
54 | typedef struct node_s { | ||
55 | item_id_t id; | ||
56 | pos_t pos; | ||
57 | lpos_t lpos; | ||
58 | user_t *user; | ||
59 | gboolean visible; | ||
60 | time_t time; | ||
61 | tag_t *tag; | ||
62 | int ways; | ||
63 | int flags; | ||
64 | float zoom_max; | ||
65 | |||
66 | /* icon */ | ||
67 | GdkPixbuf *icon_buf; | ||
68 | |||
69 | /* a link to the visual representation on screen */ | ||
70 | struct map_item_chain_s *map_item_chain; | ||
71 | |||
72 | struct node_s *next; | ||
73 | } node_t; | ||
74 | |||
75 | typedef struct node_chain { | ||
76 | node_t *node; | ||
77 | struct node_chain *next; | ||
78 | } node_chain_t; | ||
79 | |||
80 | #define OSM_DRAW_FLAG_AREA (1<<0) | ||
81 | #define OSM_DRAW_FLAG_BG (1<<1) | ||
82 | |||
83 | typedef struct way_s { | ||
84 | item_id_t id; | ||
85 | user_t *user; | ||
86 | gboolean visible; | ||
87 | time_t time; | ||
88 | tag_t *tag; | ||
89 | node_chain_t *node_chain; | ||
90 | int flags; | ||
91 | |||
92 | /* visual representation from elemstyle */ | ||
93 | struct { | ||
94 | guint flags; | ||
95 | gulong color; | ||
96 | gint width; | ||
97 | float zoom_max; | ||
98 | achadwick | 13 | gboolean dashed; |
99 | harbaum | 1 | |
100 | union { | ||
101 | struct { | ||
102 | gulong color; | ||
103 | gint width; | ||
104 | } bg; | ||
105 | |||
106 | struct { | ||
107 | gulong color; | ||
108 | } area; | ||
109 | }; | ||
110 | } draw; | ||
111 | |||
112 | /* a link to the visual representation on screen */ | ||
113 | struct map_item_chain_s *map_item_chain; | ||
114 | |||
115 | struct way_s *next; | ||
116 | } way_t; | ||
117 | |||
118 | typedef struct way_chain { | ||
119 | way_t *way; | ||
120 | struct way_chain *next; | ||
121 | } way_chain_t; | ||
122 | |||
123 | typedef struct relation_s { | ||
124 | item_id_t id; | ||
125 | user_t *user; | ||
126 | gboolean visible; | ||
127 | time_t time; | ||
128 | tag_t *tag; | ||
129 | struct member_s *member; | ||
130 | int flags; | ||
131 | |||
132 | struct relation_s *next; | ||
133 | } relation_t; | ||
134 | |||
135 | typedef struct relation_chain_s { | ||
136 | relation_t *relation; | ||
137 | struct relation_chain_s *next; | ||
138 | } relation_chain_t; | ||
139 | |||
140 | harbaum | 42 | /* two of these hash tables are used, one for nodes and one for ways */ |
141 | /* currently relations aren't used often enough to justify the use */ | ||
142 | /* of a hash table */ | ||
143 | |||
144 | /* the current hash table uses 16 bits. each table thus is */ | ||
145 | /* 256 kbytes (2^16 * sizeof(void*)) in size */ | ||
146 | #define ID2HASH(a) ((unsigned short)(a) ^ (unsigned short)((a)>>16)) | ||
147 | |||
148 | typedef struct hash_item_s { | ||
149 | union { | ||
150 | node_t *node; | ||
151 | way_t *way; | ||
152 | relation_t *relation; | ||
153 | } data; | ||
154 | |||
155 | struct hash_item_s *next; | ||
156 | } hash_item_t; | ||
157 | |||
158 | typedef struct { | ||
159 | hash_item_t *hash[65536]; | ||
160 | } hash_table_t; | ||
161 | |||
162 | harbaum | 1 | typedef enum { |
163 | ILLEGAL=0, NODE, WAY, RELATION, NODE_ID, WAY_ID, RELATION_ID | ||
164 | } type_t; | ||
165 | |||
166 | typedef struct member_s { | ||
167 | type_t type; | ||
168 | char *role; | ||
169 | |||
170 | union { | ||
171 | node_t *node; | ||
172 | way_t *way; | ||
173 | relation_t *relation; | ||
174 | item_id_t id; | ||
175 | }; | ||
176 | |||
177 | struct member_s *next; | ||
178 | } member_t; | ||
179 | |||
180 | typedef struct osm_s { | ||
181 | bounds_t *bounds; // original bounds as they appear in the file | ||
182 | user_t *user; | ||
183 | harbaum | 42 | |
184 | harbaum | 1 | node_t *node; |
185 | harbaum | 42 | hash_table_t *node_hash; |
186 | |||
187 | harbaum | 1 | way_t *way; |
188 | harbaum | 42 | hash_table_t *way_hash; |
189 | |||
190 | // hashing relations doesn't yet make much sense as relations are quite rare | ||
191 | harbaum | 1 | relation_t *relation; |
192 | harbaum | 42 | |
193 | harbaum | 1 | } osm_t; |
194 | |||
195 | #include <libxml/parser.h> | ||
196 | #include <libxml/tree.h> | ||
197 | |||
198 | osm_t *osm_parse(char *filename); | ||
199 | gboolean osm_sanity_check(GtkWidget *parent, osm_t *osm); | ||
200 | tag_t *osm_parse_osm_tag(osm_t *osm, xmlDocPtr doc, xmlNode *a_node); | ||
201 | node_chain_t *osm_parse_osm_way_nd(osm_t *osm, xmlDocPtr doc, xmlNode *a_node); | ||
202 | member_t *osm_parse_osm_relation_member(osm_t *osm, xmlDocPtr doc, xmlNode *a_node); | ||
203 | void osm_dump(osm_t *osm); | ||
204 | void osm_free(struct icon_s **icon, osm_t *osm); | ||
205 | |||
206 | char *osm_node_get_value(node_t *node, char *key); | ||
207 | gboolean osm_node_has_tag(node_t *node); | ||
208 | |||
209 | void osm_node_dump(node_t *node); | ||
210 | |||
211 | void osm_way_free(way_t *way); | ||
212 | void osm_way_dump(way_t *way); | ||
213 | char *osm_way_get_value(way_t *way, char *key); | ||
214 | gboolean osm_node_has_value(node_t *node, char *str); | ||
215 | gboolean osm_way_has_value(way_t *way, char *str); | ||
216 | void osm_way_append_node(way_t *way, node_t *node); | ||
217 | |||
218 | gboolean osm_node_in_way(way_t *way, node_t *node); | ||
219 | |||
220 | void osm_node_chain_free(node_chain_t *node_chain); | ||
221 | int osm_node_chain_length(node_chain_t *node_chain); | ||
222 | void osm_node_free(struct icon_s **icon, node_t *node); | ||
223 | |||
224 | void osm_members_free(member_t *member); | ||
225 | void osm_member_free(member_t *member); | ||
226 | |||
227 | void osm_tag_free(tag_t *tag); | ||
228 | void osm_tags_free(tag_t *tag); | ||
229 | char *osm_tag_get_by_key(tag_t *tag, char *key); | ||
230 | gboolean osm_is_creator_tag(tag_t *tag); | ||
231 | gboolean osm_tag_key_and_value_present(tag_t *haystack, tag_t *tag); | ||
232 | gboolean osm_tag_key_other_value_present(tag_t *haystack, tag_t *tag); | ||
233 | |||
234 | char *osm_generate_xml_node(osm_t *osm, node_t *node); | ||
235 | char *osm_generate_xml_way(osm_t *osm, way_t *way); | ||
236 | char *osm_generate_xml_relation(osm_t *osm, relation_t *relation); | ||
237 | |||
238 | node_t *osm_get_node_by_id(osm_t *osm, item_id_t id); | ||
239 | way_t *osm_get_way_by_id(osm_t *osm, item_id_t id); | ||
240 | relation_t *osm_get_relation_by_id(osm_t *osm, item_id_t id); | ||
241 | |||
242 | guint osm_way_number_of_nodes(way_t *way); | ||
243 | relation_chain_t *osm_node_to_relation(osm_t *osm, node_t *node); | ||
244 | relation_chain_t *osm_way_to_relation(osm_t *osm, way_t *way); | ||
245 | way_chain_t *osm_node_to_way(osm_t *osm, node_t *node); | ||
246 | |||
247 | /* ----------- edit functions ----------- */ | ||
248 | node_t *osm_node_new(osm_t *osm, gint x, gint y); | ||
249 | void osm_node_attach(osm_t *osm, node_t *node); | ||
250 | way_chain_t *osm_node_delete(osm_t *osm, struct icon_s **icon, node_t *node, | ||
251 | gboolean permanently, gboolean affect_ways); | ||
252 | void osm_way_delete(osm_t *osm, struct icon_s **icon, way_t *way, | ||
253 | gboolean permanently); | ||
254 | |||
255 | way_t *osm_way_new(void); | ||
256 | void osm_way_attach(osm_t *osm, way_t *way); | ||
257 | |||
258 | gboolean osm_position_within_bounds(osm_t *osm, gint x, gint y); | ||
259 | item_id_t osm_new_way_id(osm_t *osm); | ||
260 | gboolean osm_way_ends_with_node(way_t *way, node_t *node); | ||
261 | |||
262 | void osm_way_revert(way_t *way); | ||
263 | |||
264 | void osm_node_remove_from_relation(osm_t *osm, node_t *node); | ||
265 | void osm_way_remove_from_relation(osm_t *osm, way_t *way); | ||
266 | |||
267 | node_t *osm_way_get_last_node(way_t *way); | ||
268 | node_t *osm_way_get_first_node(way_t *way); | ||
269 | void osm_way_rotate(way_t *way, gint offset); | ||
270 | |||
271 | tag_t *osm_tags_copy(tag_t *tag, gboolean update_creator); | ||
272 | |||
273 | #endif /* OSM_H */ | ||
274 | achadwick | 13 | |
275 | // vim:et:ts=8:sw=2:sts=2:ai |