visualize_map.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import datetime
  2. import geo_util
  3. import config
  4. import db
  5. # https://www.latlong.net/Show-Latitude-Longitude.html
  6. _BBOX = [
  7. # (53.114059,-6.448975),
  8. # (53.607418,-6.019135),
  9. (53.244098,-6.394043),
  10. (53.458048,-6.035614),
  11. ]
  12. _POIS = [
  13. ('H', (53.375669,-6.071320)), # Howth
  14. ('D', (53.347949,-6.261520)), # Dublin city centre
  15. ('D', (53.289498,-6.139984)), # Dun Laohaire
  16. ('D', (53.278155,-6.107154)), # Dalkey
  17. ('B', (53.200953,-6.112175)), # Bray
  18. ('G', (53.143331,-6.072006)), # Graystones
  19. ('S', (53.372233,-6.177664)), # St Annes Park
  20. ('S', (53.369417,-6.193714)), # St Annes Park
  21. ('S', (53.367266,-6.172686)), # St Annes Park
  22. ('S', (53.374333,-6.166763)), # St Annes Park
  23. ('S', (53.374896,-6.176205)), # St Annes Park
  24. ('S', (53.372028,-6.185131)), # St Annes Park
  25. ('M', (53.450095,-6.157150)), # Malahide
  26. ('s', (53.585810,-6.142559)), # Skerries
  27. ('s', (53.581734,-6.105652)), # Skerries
  28. ('s', (53.566241,-6.098957)), # Skerries
  29. ]
  30. # x, y resolution
  31. _RESOLUTION = 60
  32. def _get_pos(resolution, loc, corner_nw):
  33. lat, lon = loc
  34. lat_idx = int(resolution * geo_util.ggcd(loc, (corner_nw[0], lon)))
  35. lon_idx = int(2 * resolution * geo_util.ggcd(loc, (lat, corner_nw[1])))
  36. return lat_idx, lon_idx
  37. def _visualize(label, bbox, properties_dict):
  38. ts_cutoff = db.datetime_to_str(datetime.datetime.now() - datetime.timedelta(days=14))
  39. properties = [p for p in properties_dict.values()
  40. if geo_util.inside(p['loc'], bbox) and p['ts'] > ts_cutoff]
  41. properties.sort(key=lambda p: p['score'])
  42. if not properties:
  43. return
  44. corner_sw = bbox[0]
  45. corner_ne = bbox[1]
  46. corner_nw = (corner_ne[0], corner_sw[1])
  47. corner_se = (corner_sw[0], corner_ne[1])
  48. width_m = geo_util.ggcd(corner_sw, corner_se)
  49. height_m = geo_util.ggcd(corner_sw, corner_nw)
  50. resolution = _RESOLUTION / max(width_m, height_m) # pixel per meter
  51. symbols = [] # symbols[y][x]
  52. for y in range(int(resolution * height_m) + 1):
  53. symbols.append([])
  54. for x in range(int(2 * resolution * width_m) + 1):
  55. symbols[-1].append(' ')
  56. for symbol, loc in _POIS:
  57. if geo_util.inside(loc, bbox):
  58. lat_idx, lon_idx = _get_pos(resolution, loc, corner_nw)
  59. symbols[lat_idx][lon_idx] = symbol
  60. for p in properties:
  61. loc = p['loc']
  62. lat_idx, lon_idx = _get_pos(resolution, loc, corner_nw)
  63. symbols[lat_idx][lon_idx] = str(int(10*p['score']))
  64. print()
  65. print(label)
  66. for line in symbols:
  67. print(''.join(line))
  68. def visualize(properties_dict):
  69. for label, bbox in sorted(config.VISUALIZED_BBOXES.items()):
  70. _visualize(label, bbox, properties_dict)