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

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

Refactored pakfile generation code into libgosm.cpp
Separated elemstyle loading from pakfile generation for future implementation of changing elemstyles without recompiling pakfile
gosmore.pak and master.pak can now be found in $HOME/.gosmore or $RES_DIR as well as current folder
gosmore can now take a pakfile as the first argument to load an arbitrary pakfile instead of gosmore.pak 'Usage: gosmore [rebuild [bbox]] pakfile'.
NOTE: arbitrary pakfiles are *not* current supported for rebuild, only for loading

  • Property svn:executable set to *
File size: 19.9 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};
314
315inline int Layer (wayType *w) { return w->bits >> 29; }
316
317inline int Latitude (double lat)
318{ /* Mercator projection onto a square means we have to clip
319     everything beyond N85.05 and S85.05 */
320  return lat > 85.051128779 ? 2147483647 : lat < -85.051128779 ? -2147483647 :
321    lrint (log (tan (M_PI / 4 + lat * M_PI / 360)) / M_PI * 2147483648.0);
322}
323
324inline int Longitude (double lon)
325{
326  return lrint (lon / 180 * 2147483648.0);
327}
328
329inline double LatInverse (int lat)
330{
331  return (atan (exp (lat / 2147483648.0 * M_PI)) - M_PI / 4) / M_PI * 360;
332}
333
334inline double LonInverse (int lon)
335{
336  return lon / 2147483648.0 * 180;
337}
338
339/*---------- Global variables -----------*/
340#define searchCnt 40
341extern wayType *gosmSway[searchCnt];
342extern int *hashTable, bucketsMin1, pakHead;
343extern char *gosmData, *gosmSstr[searchCnt];
344extern ndType *ndBase;
345extern styleStruct *style;
346
347inline wayType *Way (ndType *nd) { return (wayType *)(nd->wayPtr+gosmData); }
348
349inline int StyleNr (wayType *w) { return w->bits & ((2 << STYLE_BITS) - 1); }
350
351inline styleStruct *Style (wayType *w) { return &style[StyleNr (w)]; }
352
353unsigned inline ZEnc (int lon, int lat)
354{ // Input as bits : lon15,lon14,...lon0 and lat15,lat14,...,lat0
355  int t = (lon << 16) | lat;
356  t = (t & 0xff0000ff) | ((t & 0x00ff0000) >> 8) | ((t & 0x0000ff00) << 8);
357  t = (t & 0xf00ff00f) | ((t & 0x0f000f00) >> 4) | ((t & 0x00f000f0) << 4);
358  t = (t & 0xc3c3c3c3) | ((t & 0x30303030) >> 2) | ((t & 0x0c0c0c0c) << 2);
359  return (t & 0x99999999) | ((t & 0x44444444) >> 1) | ((t & 0x22222222) << 1);
360} // Output as bits : lon15,lat15,lon14,lat14,...,lon0,lat0
361
362inline int Hash (int lon, int lat, int lowz = 0)
363{ /* All the normal tiles (that make up a super tile) are mapped to sequential
364     buckets thereby improving caching and reducing the number of disk tracks
365     required to render / route through a super tile sized area.
366     The map to sequential buckets is a 2-D Hilbert curve. */
367  if (lowz) {
368    lon >>= 7;
369    lat >>= 7;
370  }
371 
372  int t = ZEnc (lon >> TILEBITS, ((unsigned) lat) >> TILEBITS);
373  int s = ((((unsigned)t & 0xaaaaaaaa) >> 1) | ((t & 0x55555555) << 1)) ^ ~t;
374  // s=ZEnc(lon,lat)^ZEnc(lat,lon), so it can be used to swap lat and lon.
375  #define SUPERTILEBITS (TILEBITS + 8)
376  for (int lead = 1 << (SUPERTILEBITS * 2 - TILEBITS * 2); lead; lead >>= 2) {
377    if (!(t & lead)) t ^= ((t & (lead << 1)) ? s : ~s) & (lead - 1);
378  }
379
380  return (((((t & 0xaaaaaaaa) >> 1) ^ t) + (lon >> SUPERTILEBITS) * 0x00d20381
381    + (lat >> SUPERTILEBITS) * 0x75d087d9) &
382    (lowz ? bucketsMin1 >> 7 : bucketsMin1)) + (lowz ? bucketsMin1 + 1 : 0);
383}
384
385int TagCmp (char *a, char *b); 
386
387struct OsmItr { // Iterate over all the objects in a square
388  ndType *nd[1]; /* Readonly. Either can be 'from' or 'to', but you */
389  /* can be guaranteed that nodes will be in hs[0] */
390 
391  int slat, slon, left, right, top, bottom, tsize; /* Private */
392  ndType *end;
393 
394  OsmItr (int l, int t, int r, int b)
395  {
396    tsize = r - l > 10000000 ? TILESIZE << 7 : TILESIZE;
397    left = l & (~(tsize - 1));
398    right = (r + tsize - 1) & (~(tsize-1));
399    top = t & (~(tsize - 1));
400    bottom = (b + tsize - 1) & (~(tsize-1));
401   
402    slat = top;
403    slon = left - tsize;
404    nd[0] = end = NULL;
405  }
406};
407
408int Next (OsmItr &itr); /* Friend of osmItr */
409
410struct routeNodeType {
411  ndType *nd;
412  routeNodeType *shortest;
413  int best, heapIdx, dir, remain; // Dir is 0 or 1
414};
415
416extern routeNodeType *route, *shortest, **routeHeap;
417extern int routeHeapSize, tlat, tlon, flat, flon, rlat, rlon;
418
419void Route (int recalculate, int plon, int plat, int Vehicle, int fast);
420
421int JunctionType (ndType *nd);
422
423void GosmSearch (int clon, int clat, char *key);
424
425int GosmInit (void *d, long size);
426
427// *** EVERYTHING AFTER THIS POINT IS NOT IN THE WINDOWS BUILDS ***
428
429#ifndef _WIN32
430
431// struct to hold mappings between elemstyles.xml and stylesrec
432// these are needed when the osm file is converted to a pak file
433typedef struct {
434  char style_k[80];
435  char style_v[80];
436  int ruleNr;
437  int defaultRestrict;
438} elemstyleMapping;
439
440// reads the elemstyles.xml file into srec, with the mapping between
441// srec and elemstyles.xml stored in map. StyleCnt representing the
442// location of the first elemstyle. Returns the final styleCnt.
443int readElemstyles(const char *elemstylesfname, const char *iconsfname, 
444                   styleStruct *srec, elemstyleMapping *map, 
445                   int styleCnt);
446
447// creates a new pakfile from an osmdata file read from standard in
448int rebuildpak(const char* pakfile, const char* elemstylefile, 
449               const char* iconscsvfile, const char* masterpakfile, 
450               const int bbox[4]);
451
452#endif // #ifndef _WIN32
453
454#endif
Note: See TracBrowser for help on using the repository browser.