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

Last change on this file since 16481 was 13894, checked in by daviddean, 11 years ago

Added maxspeed= support for ways to gosmore. Currently assumes all maxspeed tags are in kph regardless of any suffix. Major changes to pak file format, recompile of pak files definitely required.

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