source: subversion/applications/rendering/gosmore/libgosm.h @ 10060

Last change on this file since 10060 was 10060, checked in by nic, 11 years ago

Move data access routines to new module.

  • Property svn:executable set to *
File size: 18.5 KB
Line 
1#ifndef LIBGOSM_H
2#define LIBGOSM_H
3
4#include <math.h>
5#include <stdlib.h>
6#include <string.h>
7#include <ctype.h>
8#include <stdio.h>
9#ifdef _WIN32_WCE
10#include <windows.h>
11#define lrint(x) int ((x) < 0 ? (x) - 0.5 : (x) + 0.5)
12typedef int intptr_t;
13#define strncasecmp _strnicmp
14#define stricmp _stricmp
15#endif
16#ifndef _WIN32
17#include <inttypes.h>
18#define stricmp strcasecmp
19typedef long long __int64;
20#endif
21#ifndef M_PI
22#define M_PI 3.14159265358979323846 // Not in math ??
23#endif
24
25#ifndef TRUE
26#define TRUE 1
27#define FALSE 0
28#endif
29
30#ifndef INT_MIN
31#define INT_MIN 0x80000000 // -2147483648
32#endif
33
34#define Sqr(x) ((x)*(x))
35
36#define TILEBITS (18)
37#define TILESIZE (1<<TILEBITS)
38
39#define RESTRICTIONS M (access) M (motorcar) M (bicycle) M (foot) M (goods) \
40  M (hgv) M (horse) M (motorcycle) M (psv) M (motorboat) M (boat) \
41  M (oneway) M (roundabout)
42
43#define M(field) field ## R,
44enum { STYLE_BITS = 8, RESTRICTIONS layerBit1,layerBit2,layerBit3 };
45#undef M
46
47// Below is a list of the tags that the user may add. It should also include
48// any tags that gosmore.cpp may test for directly, e.g.
49// StyleNr(w) == highway_traffic_signals .
50// See http://etricceline.de/osm/Europe/En/tags.htm for the most popular tags
51// The fields are k, v, short name and the additional tags.
52#define STYLES \
53 s (highway, residential,     "residential"     , "") \
54 s (highway, unclassified,    "unclassified"    , "") \
55 s (highway, tertiary,        "tertiary"        , "") \
56 s (highway, secondary,       "secondary"       , "") \
57 s (highway, primary,         "primary"         , "") \
58 s (highway, trunk,           "trunk"           , "") \
59 s (highway, footway,         "footway"         , "") \
60 s (highway, service,         "service"         , "") \
61 s (highway, track,           "track"           , "") \
62 s (highway, cycleway,        "cycleway"        , "") \
63 s (highway, pedestrian,      "pedestrian"      , "") \
64 s (highway, steps,           "steps"           , "") \
65 s (highway, bridleway,       "bridleway"       , "") \
66 s (railway, rail,            "railway"         , "") \
67 s (railway, station,         "railway station" , "") \
68 s (highway, mini_roundabout, "mini roundabout" , "") \
69 s (highway, traffic_signals, "traffic signals" , "") \
70 s (highway, bus_stop,        "bus stop"        , "") \
71 s (amenity, parking,         "parking"         , "") \
72 s (amenity, fuel,            "fuel"            , "") \
73 s (amenity, school,          "school"          , "") \
74 s (place,   village,         "village"         , "") \
75 s (shop,    supermarket,     "supermarket"     , "") \
76 s (religion, christian,      "church"          , \
77               "  <tag k='amenity' v='place_of_worship' />\n") \
78 s (religion, jewish,         "synagogue"       , \
79               "  <tag k='amenity' v='place_of_worship' />\n") \
80 s (religion, muslim,         "mosque"          , \
81               "  <tag k='amenity' v='place_of_worship' />\n") \
82 s (amenity, pub,             "pub"             , "") \
83 s (amenity, restaurant,      "restaurant"      , "") \
84 s (power,   tower,           "power tower"     , "") \
85 s (waterway, stream,         "stream"          , "") \
86 s (amenity, grave_yard,      "grave yard"      , "") \
87 s (amenity, crematorium,     "crematorium"     , "") \
88 s (amenity, shelter,         "shelter"         , "") \
89 s (tourism, picnic_site,     "picnic site"     , "") \
90 s (leisure, common,          "common area"     , "") \
91 s (amenity, park_bench,      "park bench"      , "") \
92 s (tourism, viewpoint,       "viewpoint"       , "") \
93 s (tourism, artwork,         "artwork"         , "") \
94 s (tourism, museum,          "museum"          , "") \
95 s (tourism, theme_park,      "theme park"      , "") \
96 s (tourism, zoo,             "zoo"             , "") \
97 s (leisure, playground,      "playground"      , "") \
98 s (leisure, park,            "park"            , "") \
99 s (leisure, nature_reserve,  "nature reserve"  , "") \
100 s (leisure, miniature_golf,  "miniature golf"  , "") \
101 s (leisure, golf_course,     "golf course"     , "") \
102 s (leisure, sports_centre,   "sports centre"   , "") \
103 s (leisure, stadium,         "stadium"         , "") \
104 s (leisure, pitch,           "pitch"           , "") \
105 s (leisure, track,           "track"           , "") \
106 s (sport,   athletics,       "athletics"       , "") \
107 s (sport,   10pin,           "10 pin"          , "") \
108 s (sport,   boules,          "boules"          , "") \
109 s (sport,   bowls,           "bowls"           , "") \
110 s (sport,   baseball,        "baseball"        , "") \
111 s (sport,   basketball,      "basketball"      , "") \
112 s (sport,   cricket,         "cricket"         , "") \
113 s (sport,   cricket_nets,    "cricket nets"    , "") \
114 s (sport,   croquet,         "croquet"         , "") \
115 s (sport,   dog_racing,      "dog racing"      , "") \
116 s (sport,   equestrian,      "equestrian"      , "") \
117 s (sport,   football,        "football"        , "") \
118 s (sport,   soccer,          "soccer"          , "") \
119 s (sport,   climbing,        "climbing"        , "") \
120 s (sport,   gymnastics,      "gymnastics"      , "") \
121 s (sport,   hockey,          "hockey"          , "") \
122 s (sport,   horse_racing,    "horse racing"    , "") \
123 s (sport,   motor,           "motor sport"     , "") \
124 s (sport,   pelota,          "pelota"          , "") \
125 s (sport,   rugby,           "rugby"           , "") \
126 s (sport,   australian_football, "australian football" , "") \
127 s (sport,   skating,         "skating"         , "") \
128 s (sport,   skateboard,      "skateboard"      , "") \
129 s (sport,   handball,        "handball"        , "") \
130 s (sport,   table_tennis,    "table tennis"    , "") \
131 s (sport,   tennis,          "tennis"          , "") \
132 s (sport,   racquet,         "racquet"         , "") \
133 s (sport,   badminton,       "badminton"       , "") \
134 s (sport,   paintball,       "paintball"       , "") \
135 s (sport,   shooting,        "shooting"        , "") \
136 s (sport,   volleyball,      "volleyball"      , "") \
137 s (sport,   beachvolleyball, "beach volleyball" , "") \
138 s (sport,   archery,         "archery"         , "") \
139 s (sport,   skiing,          "skiing"          , "") \
140 s (sport,   rowing,          "rowing"          , "") \
141 s (sport,   sailing,         "sailing"         , "") \
142 s (sport,   diving,          "diving"          , "") \
143 s (sport,   swimming,        "swimming"        , "") \
144 s (leisure, swimming_pool,   "swimming pool"   , "") \
145 s (leisure, water_park,      "water park"      , "") \
146 s (leisure, marina,          "marina"          , "") \
147 s (leisure, slipway,         "slipway"         , "") \
148 s (leisure, fishing,         "fishing"         , "") \
149 s (shop,    bakery,          "bakery"          , "") \
150 s (shop,    butcher,         "butcher"         , "") \
151 s (shop,    florist,         "florist"         , "") \
152 s (shop,    groceries,       "groceries"       , "") \
153 s (shop,    clothes,         "clothing shop"   , "") \
154 s (shop,    shoes,           "shoe shop"       , "") \
155 s (shop,    jewelry,         "jewelry store"   , "") \
156 s (shop,    books,           "bookshop"        , "") \
157 s (shop,    newsagent,       "newsagent"       , "") \
158 s (shop,    furniture,       "furniture store" , "") \
159 s (shop,    hifi,            "Hi-Fi store"     , "") \
160 s (shop,    electronics,     "electronics store" , "") \
161 s (shop,    computer,        "computer shop"   , "") \
162 s (shop,    video,           "video rental"    , "") \
163 s (shop,    toys,            "toy shop"        , "") \
164 s (shop,    motorcycle,      "motorcycle"      , "") \
165 s (shop,    car_repair,      "car repair"      , "") \
166 s (shop,    doityourself,    "doityourself"    , "") \
167 s (shop,    garden_centre,   "garden centre"   , "") \
168 s (shop,    outdoor,         "outdoor"         , "") \
169 s (shop,    bicycle,         "bicycle shop"    , "") \
170 s (shop,    dry_cleaning,    "dry cleaning"    , "") \
171 s (shop,    laundry,         "laundry"         , "") \
172 s (shop,    hairdresser,     "hairdresser"     , "") \
173 s (shop,    travel_agency,   "travel_agency"   , "") \
174 s (shop,    convenience,     "convenience"     , "") \
175 s (shop,    mall,            "mall"            , "") \
176 s (shop,    department_store, "department store" , "") \
177 s (amenity, biergarten,      "biergarten"      , "") \
178 s (amenity, nightclub,       "nightclub"       , "") \
179 s (amenity, bar,             "bar"             , "") \
180 s (amenity, cafe,            "cafe"            , "") \
181 s (amenity, fast_food,       "fast food"       , "") \
182 s (amenity, ice_cream,       "icecream"        , "") \
183 s (amenity, bicycle_rental,  "bicycle rental"  , "") \
184 s (amenity, car_rental,      "car rental"      , "") \
185 s (amenity, car_sharing,     "car sharing"     , "") \
186 s (amenity, car_wash,        "car wash"        , "") \
187 s (amenity, taxi,            "taxi"            , "") \
188 s (amenity, telephone,       "telephone"       , "") \
189 s (amenity, post_office,     "post office"     , "") \
190 s (amenity, post_box,        "post box"        , "") \
191 s (tourism, information,     "tourist info"    , "") \
192 s (amenity, toilets,         "toilets"         , "") \
193 s (amenity, recycling,       "recycling"       , "") \
194 s (amenity, fire_station,    "fire station"    , "") \
195 s (amenity, police,          "police"          , "") \
196 s (amenity, courthouse,      "courthouse"      , "") \
197 s (amenity, prison,          "prison"          , "") \
198 s (amenity, public_building, "public building" , "") \
199 s (amenity, townhall,        "townhall"        , "") \
200 s (amenity, cinema,          "cinema"          , "") \
201 s (amenity, arts_centre,     "arts centre"     , "") \
202 s (amenity, theatre,         "theatre"         , "") \
203 s (tourism, hotel,           "hotel"           , "") \
204 s (tourism, motel,           "motel"           , "") \
205 s (tourism, guest_house,     "guest house"     , "") \
206 s (tourism, hostel,          "hostel"          , "") \
207 s (tourism, chalet,          "chalet"          , "") \
208 s (tourism, camp_site,       "camp site"       , "") \
209 s (tourism, caravan_site,    "caravan site"    , "") \
210 s (amenity, pharmacy,        "pharmacy"        , "") \
211 s (amenity, dentist,         "dentist"         , "") \
212 s (amenity, hospital,        "hospital"        , "") \
213 s (amenity, bank,            "bank"            , "") \
214 s (amenity, bureau_de_change, "bureau de change" , "") \
215 s (amenity, atm,             "atm"            , "") \
216 s (amenity, drinking_water,  "drinking water"  , "") \
217 s (amenity, fountain,        "fountain"        , "") \
218 s (natural, spring,          "spring"          , "") \
219 s (amenity, university,      "university"      , "") \
220 s (amenity, college,         "college"         , "") \
221 s (amenity, kindergarten,    "kindergarten"    , "") \
222 s (highway, living_street,   "living street"   , "") \
223 s (highway, motorway,        "motorway"        , "") \
224 s (highway, motorway_link,   "mway link"       , "") \
225 s (highway, trunk_link,      "trunk link"      , "") \
226 s (highway, primary_link,    "primary_link"    , "") \
227 s (man_made, beacon,         "beacon"          , "" ) \
228 s (man_made, survey_point,   "survey point"    , "" ) \
229 s (man_made, tower,          "tower"           , "" ) \
230 s (man_made, water_tower,    "water tower"     , "" ) \
231 s (man_made, gasometer,      "gasometer"       , "" ) \
232 s (man_made, reservoir_covered, "covered reservoir", "" ) \
233 s (man_made, lighthouse,     "lighthouse"      , "" ) \
234 s (man_made, windmill,       "windmill"        , "" ) \
235 s (man_made, pier,           "pier"            , "" ) \
236 s (man_made, pipeline,       "pipeline"        , "" ) \
237 s (man_made, wastewater_plant, "wastewater plant" , "" ) \
238 s (man_made, crane,          "crane"           , "" ) \
239 s (building, yes,            "building"        , "") \
240 s (landuse, forest,          "forest"          , "") \
241 s (landuse, residential,     "residential area", "") \
242 s (landuse, industrial,      "industrial area" , "") \
243 s (landuse, retail,          "retail area"     , "") \
244 s (landuse, commercial,      "commercial area" , "") \
245 s (landuse, construction,    "construction area" , "") \
246 s (landuse, reservoir,       "reservoir"       , "") \
247 s (natural, water,           "lake / dam"      , "") \
248 s (landuse, basin,           "basin"           , "") \
249 s (landuse, landfill,        "landfill"        , "") \
250 s (landuse, quarry,          "quarry"          , "") \
251 s (landuse, cemetery,        "cemetery"        , "") \
252 s (landuse, allotments,      "allotments"      , "") \
253 s (landuse, farm,            "farmland"        , "") \
254 s (landuse, farmyard,        "farmyard"        , "") \
255 s (landuse, military,        "military area"   , "") \
256 s (religion, bahai,          "bahai"           , \
257               "  <tag k='amenity' v='place_of_worship' />\n") \
258 s (religion, buddhist,       "buddhist"        , \
259               "  <tag k='amenity' v='place_of_worship' />\n") \
260 s (religion, hindu,          "hindu"           , \
261               "  <tag k='amenity' v='place_of_worship' />\n") \
262 s (religion, jain,           "jainism"         , \
263               "  <tag k='amenity' v='place_of_worship' />\n") \
264 s (religion, sikh,           "sikhism"         , \
265               "  <tag k='amenity' v='place_of_worship' />\n") \
266 s (religion, shinto,         "shinto"          , \
267               "  <tag k='amenity' v='place_of_worship' />\n") \
268 s (religion, taoist,         "taoism"          , \
269               "  <tag k='amenity' v='place_of_worship' />\n") \
270 /* relations must be last and restriction_no_right_turn must be first */ \
271 s (restriction, no_right_turn, ""              , "") \
272 s (restriction, no_left_turn, ""               , "") \
273 s (restriction, no_u_turn, ""                  , "") \
274 s (restriction, no_straight_on, ""             , "") \
275 s (restriction, only_right_turn, ""            , "") \
276 s (restriction, only_left_turn, ""             , "") \
277 s (restriction, only_straight_on, ""           , "") \
278 /* restriction_only_straight_on must be the last restriction */
279
280#define XXXFSDFSF \
281 s (sport,   orienteering,    "orienteering"    , "") \
282 s (sport,   gym,             "gym"             , "") \
283 /* sport=golf isn't a golf course, so what is it ? */ \
284
285#define s(k,v,shortname,extraTags) k ## _ ## v,
286enum { STYLES firstElemStyle }; // highway_residential, ...
287#undef s
288
289struct styleStruct {
290  int  x[16], lineWidth, lineRWidth, lineColour, lineColourBg, dashed;
291  int  scaleMax, areaColour, dummy /* pad to 8 for 64 bit compatibility */;
292  double aveSpeed[layerBit1], invSpeed[layerBit1];
293};
294
295struct ndType {
296  int wayPtr, lat, lon, other[2];
297};
298
299struct wayType {
300  int bits;
301  int clat, clon, dlat, dlon; /* Centre coordinates and (half)diameter */
302};
303
304inline int Layer (wayType *w) { return w->bits >> 29; }
305
306inline int Latitude (double lat)
307{ /* Mercator projection onto a square means we have to clip
308     everything beyond N85.05 and S85.05 */
309  return lat > 85.051128779 ? 2147483647 : lat < -85.051128779 ? -2147483647 :
310    lrint (log (tan (M_PI / 4 + lat * M_PI / 360)) / M_PI * 2147483648.0);
311}
312
313inline int Longitude (double lon)
314{
315  return lrint (lon / 180 * 2147483648.0);
316}
317
318inline double LatInverse (int lat)
319{
320  return (atan (exp (lat / 2147483648.0 * M_PI)) - M_PI / 4) / M_PI * 360;
321}
322
323inline double LonInverse (int lon)
324{
325  return lon / 2147483648.0 * 180;
326}
327
328/*---------- Global variables -----------*/
329#define searchCnt 40
330extern wayType *gosmSway[searchCnt];
331extern int *hashTable, bucketsMin1, pakHead;
332extern char *gosmData, *gosmSstr[searchCnt];
333extern ndType *ndBase;
334extern styleStruct *style;
335
336inline wayType *Way (ndType *nd) { return (wayType *)(nd->wayPtr+gosmData); }
337
338inline int StyleNr (wayType *w) { return w->bits & ((2 << STYLE_BITS) - 1); }
339
340inline styleStruct *Style (wayType *w) { return &style[StyleNr (w)]; }
341
342unsigned inline ZEnc (int lon, int lat)
343{ // Input as bits : lon15,lon14,...lon0 and lat15,lat14,...,lat0
344  int t = (lon << 16) | lat;
345  t = (t & 0xff0000ff) | ((t & 0x00ff0000) >> 8) | ((t & 0x0000ff00) << 8);
346  t = (t & 0xf00ff00f) | ((t & 0x0f000f00) >> 4) | ((t & 0x00f000f0) << 4);
347  t = (t & 0xc3c3c3c3) | ((t & 0x30303030) >> 2) | ((t & 0x0c0c0c0c) << 2);
348  return (t & 0x99999999) | ((t & 0x44444444) >> 1) | ((t & 0x22222222) << 1);
349} // Output as bits : lon15,lat15,lon14,lat14,...,lon0,lat0
350
351inline int Hash (int lon, int lat, int lowz = 0)
352{ /* All the normal tiles (that make up a super tile) are mapped to sequential
353     buckets thereby improving caching and reducing the number of disk tracks
354     required to render / route through a super tile sized area.
355     The map to sequential buckets is a 2-D Hilbert curve. */
356  if (lowz) {
357    lon >>= 7;
358    lat >>= 7;
359  }
360 
361  int t = ZEnc (lon >> TILEBITS, ((unsigned) lat) >> TILEBITS);
362  int s = ((((unsigned)t & 0xaaaaaaaa) >> 1) | ((t & 0x55555555) << 1)) ^ ~t;
363  // s=ZEnc(lon,lat)^ZEnc(lat,lon), so it can be used to swap lat and lon.
364  #define SUPERTILEBITS (TILEBITS + 8)
365  for (int lead = 1 << (SUPERTILEBITS * 2 - TILEBITS * 2); lead; lead >>= 2) {
366    if (!(t & lead)) t ^= ((t & (lead << 1)) ? s : ~s) & (lead - 1);
367  }
368
369  return (((((t & 0xaaaaaaaa) >> 1) ^ t) + (lon >> SUPERTILEBITS) * 0x00d20381
370    + (lat >> SUPERTILEBITS) * 0x75d087d9) &
371    (lowz ? bucketsMin1 >> 7 : bucketsMin1)) + (lowz ? bucketsMin1 + 1 : 0);
372}
373
374int TagCmp (char *a, char *b); 
375
376struct OsmItr { // Iterate over all the objects in a square
377  ndType *nd[1]; /* Readonly. Either can be 'from' or 'to', but you */
378  /* can be guaranteed that nodes will be in hs[0] */
379 
380  int slat, slon, left, right, top, bottom, tsize; /* Private */
381  ndType *end;
382 
383  OsmItr (int l, int t, int r, int b)
384  {
385    tsize = r - l > 10000000 ? TILESIZE << 7 : TILESIZE;
386    left = l & (~(tsize - 1));
387    right = (r + tsize - 1) & (~(tsize-1));
388    top = t & (~(tsize - 1));
389    bottom = (b + tsize - 1) & (~(tsize-1));
390   
391    slat = top;
392    slon = left - tsize;
393    nd[0] = end = NULL;
394  }
395};
396
397int Next (OsmItr &itr); /* Friend of osmItr */
398
399struct routeNodeType {
400  ndType *nd;
401  routeNodeType *shortest;
402  int best, heapIdx, dir, remain; // Dir is 0 or 1
403};
404
405extern routeNodeType *route, *shortest, **routeHeap;
406extern int dhashSize, routeHeapSize, tlat, tlon, flat, flon, rlat, rlon;
407
408void Route (int recalculate, int plon, int plat, int Vehicle, int fast);
409
410int JunctionType (ndType *nd);
411
412void GosmSearch (int clon, int clat, char *key);
413
414int GosmInit (void *d, long size);
415
416#endif
Note: See TracBrowser for help on using the repository browser.