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

Last change on this file since 17731 was 17731, checked in by nic, 10 years ago

Search results now include distance, direction, icons and, if they are
different, the names. Selected search results are now highlighted.

It was achieved by drastically changing the layout and will break WinCE and
will need more work.

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