|
22 | 22 | import time |
23 | 23 | import drawSvg as draw |
24 | 24 | from datetime import datetime |
| 25 | +import math |
25 | 26 | from config.logging_utils import configure_logging |
26 | 27 |
|
27 | 28 | configure_logging() |
@@ -115,22 +116,25 @@ def handle(): |
115 | 116 | return response |
116 | 117 |
|
117 | 118 | dimensions = 700, 700 |
| 119 | + data = preprocessor["ca.mcgill.a11y.image.preprocessor.openstreetmap"] |
| 120 | + lat = data["bounds"]["latitude"] |
| 121 | + lon = data["bounds"]["longitude"] |
| 122 | + coords = getMidpoint(contents) |
118 | 123 |
|
119 | 124 | renderingDescription = ("Tactile rendering of map centered at latitude " + |
120 | | - str(contents["coordinates"]["latitude"]) + |
| 125 | + str(coords["latitude"]) + |
121 | 126 | " and longitude " + |
122 | | - str(contents["coordinates"]["longitude"])) |
| 127 | + str(coords["longitude"])) |
123 | 128 | caption = ("Map centered at latitude " + |
124 | | - str(contents["coordinates"]["latitude"]) + |
| 129 | + str(coords["latitude"]) + |
125 | 130 | " and longitude " + |
126 | | - str(contents["coordinates"]["longitude"])) |
| 131 | + str(coords["longitude"])) |
127 | 132 | # List of minor street types ('footway', 'crossing' and 'steps') |
128 | 133 | # to be filtered out to simplify the resulting rendering |
129 | 134 | remove_streets = ["footway", "crossing", "steps", "elevator"] |
130 | 135 | svg = draw.Drawing(dimensions[0], dimensions[1], |
131 | 136 | origin=(0, -dimensions[1])) |
132 | 137 |
|
133 | | - data = preprocessor["ca.mcgill.a11y.image.preprocessor.openstreetmap"] |
134 | 138 | if "streets" in data: |
135 | 139 | streets = data["streets"] |
136 | 140 | lat = data["bounds"]["latitude"] |
@@ -438,14 +442,12 @@ def getNodeCategoryData(POI): |
438 | 442 | category = POI["cat"] |
439 | 443 | match category: |
440 | 444 | case "crossing": |
441 | | - if POI["crossing"] == "marked": |
442 | | - tag += "Marked crossing, " |
443 | | - elif POI["crossing"] == "unmarked": |
444 | | - tag += "Unmarked crossing, " |
445 | | - elif POI["crossing"] == "traffic_signals": |
446 | | - tag += "Crossing with traffic signal, " |
447 | | - else: |
448 | | - tag += "Crossing, " |
| 445 | + crossing_types = { |
| 446 | + "marked": "Marked crossing, ", |
| 447 | + "unmarked": "Unmarked crossing, ", |
| 448 | + "traffic_signals": "Crossing with traffic signal, " |
| 449 | + } |
| 450 | + tag += crossing_types.get(POI.get("crossing"), "Crossing, ") |
449 | 451 | case "traffic_signals": |
450 | 452 | tag += "Traffic lights present, " |
451 | 453 | case _: |
@@ -491,5 +493,42 @@ def health(): |
491 | 493 | }), 200 |
492 | 494 |
|
493 | 495 |
|
| 496 | +def getMidpoint(contents): |
| 497 | + if "coordinates" in contents: |
| 498 | + logging.debug("Coordinates found in request") |
| 499 | + return {"latitude": contents["coordinates"]["latitude"], |
| 500 | + "longitude": contents["coordinates"]["longitude"]} |
| 501 | + |
| 502 | + logging.debug("Coordinates not found in request. " |
| 503 | + "Calculating midpoint from bounds.") |
| 504 | + data = contents["preprocessors"][ |
| 505 | + "ca.mcgill.a11y.image.preprocessor.openstreetmap"] |
| 506 | + lat = data["bounds"]["latitude"] |
| 507 | + lon = data["bounds"]["longitude"] |
| 508 | + |
| 509 | + # Convert degrees to radians |
| 510 | + lat1, lon1, lat2, lon2 = map(math.radians, |
| 511 | + [lat["min"], lon["min"], |
| 512 | + lat["max"], lon["max"]]) |
| 513 | + |
| 514 | + # Convert to Cartesian coordinates |
| 515 | + x1, y1, z1 = (math.cos(lat1) * math.cos(lon1), |
| 516 | + math.cos(lat1) * math.sin(lon1), math.sin(lat1)) |
| 517 | + x2, y2, z2 = (math.cos(lat2) * math.cos(lon2), |
| 518 | + math.cos(lat2) * math.sin(lon2), math.sin(lat2)) |
| 519 | + |
| 520 | + # Compute the midpoint in Cartesian coordinates |
| 521 | + x_m, y_m, z_m = (x1 + x2) / 2, (y1 + y2) / 2, (z1 + z2) / 2 |
| 522 | + |
| 523 | + # Convert back to latitude and longitude |
| 524 | + lon_m = math.atan2(y_m, x_m) |
| 525 | + hyp = math.sqrt(x_m**2 + y_m**2) |
| 526 | + lat_m = math.atan2(z_m, hyp) |
| 527 | + |
| 528 | + # Convert radians back to degrees |
| 529 | + return {"latitude": round(math.degrees(lat_m), 6), |
| 530 | + "longitude": round(math.degrees(lon_m), 6)} |
| 531 | + |
| 532 | + |
494 | 533 | if __name__ == "__main__": |
495 | 534 | app.run(host="0.0.0.0", port=80, debug=True) |
0 commit comments