1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2011 Navit Team
|
4 |
*
|
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.
|
8 |
*
|
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.
|
13 |
*
|
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.
|
18 |
*/
|
19 |
#include <stdio.h>
|
20 |
#include <string.h>
|
21 |
#include "maptool.h"
|
22 |
#include "attr.h"
|
23 |
|
24 |
struct relations {
|
25 |
GHashTable *member_hash[3];
|
26 |
};
|
27 |
|
28 |
struct relations_func {
|
29 |
void (*func)(void *func_priv, void *relation_priv, struct item_bin *member, void *member_priv);
|
30 |
void *func_priv;
|
31 |
};
|
32 |
|
33 |
struct relations_member {
|
34 |
osmid memberid;
|
35 |
void *relation_priv,*member_priv;
|
36 |
struct relations_func *func;
|
37 |
};
|
38 |
|
39 |
static guint
|
40 |
relations_member_hash(gconstpointer key)
|
41 |
{
|
42 |
const struct relations_member *memb=key;
|
43 |
return (memb->memberid >> 32)^(memb->memberid & 0xffffffff);
|
44 |
}
|
45 |
|
46 |
static gboolean
|
47 |
relations_member_equal(gconstpointer a, gconstpointer b)
|
48 |
{
|
49 |
const struct relations_member *memba=a;
|
50 |
const struct relations_member *membb=b;
|
51 |
return (memba->memberid == membb->memberid);
|
52 |
}
|
53 |
|
54 |
struct relations *
|
55 |
relations_new(void)
|
56 |
{
|
57 |
struct relations *ret=g_new(struct relations, 1);
|
58 |
int i;
|
59 |
|
60 |
for (i = 0 ; i < 3 ; i++)
|
61 |
ret->member_hash[i]=g_hash_table_new_full(relations_member_hash, relations_member_equal, NULL, NULL);
|
62 |
return ret;
|
63 |
}
|
64 |
|
65 |
struct relations_func *
|
66 |
relations_func_new(void (*func)(void *func_priv, void *relation_priv, struct item_bin *member, void *member_priv), void *func_priv)
|
67 |
{
|
68 |
struct relations_func *relations_func=g_new(struct relations_func, 1);
|
69 |
relations_func->func=func;
|
70 |
relations_func->func_priv=func_priv;
|
71 |
return relations_func;
|
72 |
}
|
73 |
|
74 |
void
|
75 |
relations_add_func(struct relations *rel, struct relations_func *func, void *relation_priv, void *member_priv, int type, osmid id)
|
76 |
{
|
77 |
GHashTable *member_hash=rel->member_hash[type-1];
|
78 |
struct relations_member *memb=g_new(struct relations_member, 1);
|
79 |
|
80 |
memb->memberid=id;
|
81 |
memb->relation_priv=relation_priv;
|
82 |
memb->member_priv=member_priv;
|
83 |
memb->func=func;
|
84 |
g_hash_table_insert(member_hash, memb, g_list_append(g_hash_table_lookup(member_hash, memb), memb));
|
85 |
}
|
86 |
|
87 |
void
|
88 |
relations_process(struct relations *rel, FILE *nodes, FILE *ways, FILE *relations)
|
89 |
{
|
90 |
char buffer[128];
|
91 |
struct item_bin *ib=(struct item_bin *)buffer;
|
92 |
long long *id;
|
93 |
struct coord *c=(struct coord *)(ib+1),cn={0,0};
|
94 |
struct node_item *ni;
|
95 |
GList *l;
|
96 |
|
97 |
if (nodes) {
|
98 |
item_bin_init(ib, type_point_unkn);
|
99 |
item_bin_add_coord(ib, &cn, 1);
|
100 |
item_bin_add_attr_longlong(ib, attr_osm_nodeid, 0);
|
101 |
id=item_bin_get_attr(ib, attr_osm_nodeid, NULL);
|
102 |
while ((ni=read_node_item(nodes))) {
|
103 |
*id=ni->id;
|
104 |
*c=ni->c;
|
105 |
l=g_hash_table_lookup(rel->member_hash[0], id);
|
106 |
while (l) {
|
107 |
struct relations_member *memb=l->data;
|
108 |
memb->func->func(memb->func->func_priv, memb->relation_priv, ib, memb->member_priv);
|
109 |
l=g_list_next(l);
|
110 |
}
|
111 |
}
|
112 |
}
|
113 |
if (ways) {
|
114 |
while ((ib=read_item(ways))) {
|
115 |
id=item_bin_get_attr(ib, attr_osm_wayid, NULL);
|
116 |
if (id) {
|
117 |
l=g_hash_table_lookup(rel->member_hash[1], id);
|
118 |
while (l) {
|
119 |
struct relations_member *memb=l->data;
|
120 |
memb->func->func(memb->func->func_priv, memb->relation_priv, ib, memb->member_priv);
|
121 |
l=g_list_next(l);
|
122 |
}
|
123 |
}
|
124 |
}
|
125 |
}
|
126 |
}
|