2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
27 #include "transform.h"
34 struct item_range item_range_all = { type_none, type_last };
36 struct default_flags {
41 struct item busy_item;
43 struct default_flags default_flags2[]={
44 {type_street_nopass, AF_PBH},
45 {type_street_0, AF_ALL},
46 {type_street_1_city, AF_ALL},
47 {type_street_2_city, AF_ALL},
48 {type_street_3_city, AF_ALL},
49 {type_street_4_city, AF_ALL},
50 {type_highway_city, AF_MOTORIZED_FAST},
51 {type_street_1_land, AF_ALL},
52 {type_street_2_land, AF_ALL},
53 {type_street_3_land, AF_ALL},
54 {type_street_4_land, AF_ALL},
55 {type_street_n_lanes, AF_MOTORIZED_FAST},
56 {type_highway_land, AF_MOTORIZED_FAST},
57 {type_ramp, AF_MOTORIZED_FAST},
58 {type_roundabout, AF_ALL},
60 {type_cycleway, AF_PBH},
61 {type_track_paved, AF_ALL},
62 {type_track_gravelled, AF_ALL},
63 {type_track_unpaved, AF_ALL},
64 {type_track_ground, AF_ALL},
65 {type_track_grass, AF_ALL},
66 {type_footway, AF_PBH},
67 {type_living_street, AF_ALL},
68 {type_street_service, AF_ALL},
69 {type_street_parking_lane, AF_ALL},
70 {type_bridleway, AF_PBH},
73 {type_street_pedestrian, AF_PBH},
78 struct item_name item_names[]={
79 #define ITEM2(x,y) ITEM(y)
80 #define ITEM(x) { type_##x, #x },
86 static GHashTable *default_flags_hash;
88 static GHashTable *item_hash;
91 item_create_hash(void)
94 item_hash=g_hash_table_new(g_str_hash, g_str_equal);
95 for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) {
96 g_hash_table_insert(item_hash, item_names[i].name, GINT_TO_POINTER(item_names[i].item));
101 item_destroy_hash(void)
103 g_hash_table_destroy(item_hash);
108 item_get_default_flags(enum item_type type)
110 if (!default_flags_hash) {
112 default_flags_hash=g_hash_table_new(NULL, NULL);
113 for (i = 0 ; i < sizeof(default_flags2)/sizeof(struct default_flags); i++) {
114 g_hash_table_insert(default_flags_hash, (void *)(long)default_flags2[i].type, &default_flags2[i].flags);
117 return g_hash_table_lookup(default_flags_hash, (void *)(long)type);
123 if (default_flags_hash)
124 g_hash_table_destroy(default_flags_hash);
128 item_coord_rewind(struct item *it)
130 it->meth->item_coord_rewind(it->priv_data);
134 item_coord_get(struct item *it, struct coord *c, int count)
136 return it->meth->item_coord_get(it->priv_data, c, count);
140 item_coord_set(struct item *it, struct coord *c, int count, enum change_mode mode)
142 if (!it->meth->item_coord_set)
144 return it->meth->item_coord_set(it->priv_data, c, count, mode);
148 item_coord_get_within_selection(struct item *it, struct coord *c, int count, struct map_selection *sel)
150 int i,ret=it->meth->item_coord_get(it->priv_data, c, count);
152 struct map_selection *curr;
153 if (ret <= 0 || !sel)
157 for (i = 1 ; i < ret ; i++) {
169 struct coord_rect *sr=&curr->u.c_rect;
170 if (r.lu.x <= sr->rl.x && r.rl.x >= sr->lu.x &&
171 r.lu.y >= sr->rl.y && r.rl.y <= sr->lu.y)
179 item_coord_get_pro(struct item *it, struct coord *c, int count, enum projection to)
181 int ret=item_coord_get(it, c, count);
183 enum projection from=map_projection(it->map);
185 for (i = 0 ; i < count ; i++)
186 transform_from_to(c+i, from, c+i, to);
191 item_coord_is_node(struct item *it)
193 if (it->meth->item_coord_is_node)
194 return it->meth->item_coord_is_node(it->priv_data);
199 item_attr_rewind(struct item *it)
201 it->meth->item_attr_rewind(it->priv_data);
205 item_attr_get(struct item *it, enum attr_type attr_type, struct attr *attr)
207 return it->meth->item_attr_get(it->priv_data, attr_type, attr);
211 item_attr_set(struct item *it, struct attr *attr, enum change_mode mode)
213 if (!it->meth->item_attr_set)
215 return it->meth->item_attr_set(it->priv_data, attr, mode);
218 * @brief Set map item type.
220 * @param it reference to the item.
221 * @param type New type for the item. Setting it to type_none is expected to delete item from the map.
222 * @return Non-zero if this action is supported by the map and type is set successfully, 0 on error.
225 item_type_set(struct item *it, enum item_type type)
227 if (!it->meth->item_type_set)
229 return it->meth->item_type_set(it->priv_data, type);
232 struct item * item_new(char *type, int zoom)
236 it = g_new0(struct item, 1);
238 /* FIXME evaluate arguments */
244 item_from_name(const char *name)
249 return GPOINTER_TO_INT(g_hash_table_lookup(item_hash, name));
251 for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) {
252 if (! strcmp(item_names[i].name, name))
253 return item_names[i].item;
259 item_to_name(enum item_type item)
263 for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) {
264 if (item_names[i].item == item)
265 return item_names[i].name;
275 item_hash_hash(gconstpointer key)
277 const struct item *itm=key;
278 gconstpointer hashkey=(gconstpointer)GINT_TO_POINTER(itm->id_hi^itm->id_lo^(GPOINTER_TO_INT(itm->map)));
279 return g_direct_hash(hashkey);
283 item_hash_equal(gconstpointer a, gconstpointer b)
285 const struct item *itm_a=a;
286 const struct item *itm_b=b;
287 if (item_is_equal(*itm_a, *itm_b))
293 item_id_hash(const void *key)
295 const struct item_id *id=key;
296 return id->id_hi^id->id_lo;
300 item_id_equal(const void *a, const void *b)
302 const struct item_id *id_a=a;
303 const struct item_id *id_b=b;
304 return (id_a->id_hi == id_b->id_hi && id_a->id_lo == id_b->id_lo);
312 struct item_hash *ret=g_new(struct item_hash, 1);
314 ret->h=g_hash_table_new_full(item_hash_hash, item_hash_equal, g_free, NULL);
319 item_hash_insert(struct item_hash *h, struct item *item, void *val)
321 struct item *hitem=g_new(struct item, 1);
323 dbg(2,"inserting (0x%x,0x%x) into %p\n", item->id_hi, item->id_lo, h->h);
324 g_hash_table_insert(h->h, hitem, val);
328 item_hash_remove(struct item_hash *h, struct item *item)
332 dbg(2,"removing (0x%x,0x%x) from %p\n", item->id_hi, item->id_lo, h->h);
333 ret=g_hash_table_remove(h->h, item);
334 dbg(2,"ret=%d\n", ret);
340 item_hash_lookup(struct item_hash *h, struct item *item)
342 return g_hash_table_lookup(h->h, item);
347 item_hash_destroy(struct item_hash *h)
349 g_hash_table_destroy(h->h);
354 item_range_intersects_range(struct item_range *range1, struct item_range *range2)
356 if (range1->max < range2->min)
358 if (range1->min > range2->max)
363 item_range_contains_item(struct item_range *range, enum item_type type)
365 if (type >= range->min && type <= range->max)
371 item_dump_attr(struct item *item, struct map *map, FILE *out)
374 fprintf(out,"type=%s", item_to_name(item->type));
375 while (item_attr_get(item, attr_any, &attr))
376 fprintf(out," %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1));
380 item_dump_filedesc(struct item *item, struct map *map, FILE *out)
383 int i,count,max=16384;
384 struct coord *ca=g_alloca(sizeof(struct coord)*max);
386 count=item_coord_get(item, ca, item->type < type_line ? 1: max);
387 if (item->type < type_line)
388 fprintf(out,"mg:0x%x 0x%x ", ca[0].x, ca[0].y);
389 item_dump_attr(item, map, out);
391 if (item->type >= type_line)
392 for (i = 0 ; i < count ; i++)
393 fprintf(out,"mg:0x%x 0x%x\n", ca[i].x, ca[i].y);