Skip to content

Commit

Permalink
Backend: Formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Jun 16, 2021
1 parent c4f11c3 commit 9a9a76f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 93 deletions.
9 changes: 4 additions & 5 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,7 @@ def add_api_endpoints(app):
from backend.api.system.image_upload import SystemImageUploadRestAPI

# Mapillary API endpoint
from backend.api.mapillary.mapillary import (
MapillaryTasksAPI,
SequencesAsGPX
)
from backend.api.mapillary.mapillary import MapillaryTasksAPI, SequencesAsGPX

# Projects REST endpoint
api.add_resource(ProjectsAllAPI, format_url("projects/"), methods=["GET"])
Expand Down Expand Up @@ -563,7 +560,9 @@ def add_api_endpoints(app):
)

# Mapillary Projects as sequences
api.add_resource(SequencesAsGPX, format_url("projects/<int:project_id>/sequences-as-gpx"))
api.add_resource(
SequencesAsGPX, format_url("projects/<int:project_id>/sequences-as-gpx")
)

# Annotations REST endoints
api.add_resource(
Expand Down
8 changes: 2 additions & 6 deletions backend/api/mapillary/mapillary.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def get(self):
else:
raise NotFound
except NotFound:
return {
"Error": "No Mapillary Sequences found with parameters"
}, 404
return {"Error": "No Mapillary Sequences found with parameters"}, 404
except Exception as e:
error_msg = f"Mapillary GET - unhandled error: {str(e)}"
current_app.logger.critical(error_msg)
Expand Down Expand Up @@ -136,9 +134,7 @@ def get(self, project_id):

return Response(gpx, mimetype="text/xml", status=200)
except NotFound:
return {
"Error": "No Mapillary Sequences found with parameters"
}, 404
return {"Error": "No Mapillary Sequences found with parameters"}, 404
except Exception as e:
error_msg = f"Mapillary GET - unhandled error: {str(e)}"
current_app.logger.critical(error_msg)
Expand Down
7 changes: 5 additions & 2 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ class EnvironmentConfig:
TASK_AUTOUNLOCK_AFTER = os.getenv("TM_TASK_AUTOUNLOCK_AFTER", "2h")

# Image sources
MAPILLARY_API = {'base': 'https://a.mapillary.com/v3/', 'clientId': os.getenv('MAPILLARY_API_KEY', None)}
MAPILLARY_TIME_CORRELATION = os.getenv('MAPILLARY_TIME_CORRELATION', 60)
MAPILLARY_API = {
"base": "https://a.mapillary.com/v3/",
"clientId": os.getenv("MAPILLARY_API_KEY", None),
}
MAPILLARY_TIME_CORRELATION = os.getenv("MAPILLARY_TIME_CORRELATION", 60)

# Configuration for sending emails
SMTP_SETTINGS = {
Expand Down
123 changes: 43 additions & 80 deletions backend/services/mapillary_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def __repr__(self):
)

@staticmethod
def tile_latlon_bounds(
x_coordinate: int, y_coordinate: int, zoom: int
) -> "BBox":
def tile_latlon_bounds(x_coordinate: int, y_coordinate: int, zoom: int) -> "BBox":
"""Convert a tile to some rounded bounds"""
lon1 = round(Tile.x_to_lon(x_coordinate, zoom), 6)
lon2 = round(Tile.x_to_lon(x_coordinate + 1, zoom), 6)
Expand Down Expand Up @@ -98,8 +96,14 @@ def convert(self, coordinates: List[List[int]]) -> List[List[float]]:
for coords in coordinates:
x_coor = coords[0] / self.extent
y_coor = coords[1] / self.extent
y_coor = self.tile_bounds.minLat + (self.tile_bounds.maxLat - self.tile_bounds.minLat) * y_coor
x_coor = self.tile_bounds.minLon + (self.tile_bounds.maxLon - self.tile_bounds.minLon) * x_coor
y_coor = (
self.tile_bounds.minLat
+ (self.tile_bounds.maxLat - self.tile_bounds.minLat) * y_coor
)
x_coor = (
self.tile_bounds.minLon
+ (self.tile_bounds.maxLon - self.tile_bounds.minLon) * x_coor
)
converted_coordinates.append([x_coor, y_coor])
return converted_coordinates

Expand All @@ -112,9 +116,7 @@ def x_to_lon(x_coordinate: int, zoom: int) -> float:
def y_to_lat(y_coordinate: int, zoom: int) -> float:
"""Convert a y/z int from a tile to a lat"""
temp = math.pi - 2 * math.pi * y_coordinate / 2 ** zoom
return (
180 / math.pi * math.atan((math.exp(temp) - math.exp(-temp)) / 2)
)
return 180 / math.pi * math.atan((math.exp(temp) - math.exp(-temp)) / 2)

def set_extent(self, extent: int):
self.extent = extent
Expand All @@ -132,9 +134,7 @@ def compute_cutoff_keys(self, tile_list):
not in tile_list
):
keys.add((f["properties"]["key"], Bounds.WEST))
self.download_additional = (
self.download_additional | Bounds.WEST
)
self.download_additional = self.download_additional | Bounds.WEST
if (
any(
coord[0] >= self.tile_bounds.maxLon
Expand All @@ -144,9 +144,7 @@ def compute_cutoff_keys(self, tile_list):
not in tile_list
):
keys.add((f["properties"]["key"], Bounds.EAST))
self.download_additional = (
self.download_additional | Bounds.EAST
)
self.download_additional = self.download_additional | Bounds.EAST
if (
any(
coord[1] <= self.tile_bounds.minLat
Expand All @@ -156,9 +154,7 @@ def compute_cutoff_keys(self, tile_list):
not in tile_list
):
keys.add((f["properties"]["key"], Bounds.SOUTH))
self.download_additional = (
self.download_additional | Bounds.SOUTH
)
self.download_additional = self.download_additional | Bounds.SOUTH
if (
any(
coord[1] >= self.tile_bounds.maxLat
Expand All @@ -168,9 +164,7 @@ def compute_cutoff_keys(self, tile_list):
not in tile_list
):
keys.add((f["properties"]["key"], Bounds.NORTH))
self.download_additional = (
self.download_additional | Bounds.NORTH
)
self.download_additional = self.download_additional | Bounds.NORTH
self.cutoff_keys = keys

def set_geometry(self, geom):
Expand Down Expand Up @@ -220,8 +214,7 @@ def lat_lon_to_tile(lat: float, lon: float, zoom: int) -> "Tile":
1
- (
math.log(
math.tan(math.radians(lat))
+ 1 / math.cos(math.radians(lat))
math.tan(math.radians(lat)) + 1 / math.cos(math.radians(lat))
)
/ math.pi
)
Expand All @@ -238,27 +231,34 @@ def geom_in_bounds(feature, tile, bounding_box):
if feature["geometry"]["type"] == "LineString":
converted_coordinates = tile.convert(coordinates)
for c in converted_coordinates:
if c[0] <= bounding_box.maxLon and c[0] >= bounding_box.minLon and c[1] <= bounding_box.maxLat and c[1] >= bounding_box.minLat:
if (
c[0] <= bounding_box.maxLon
and c[0] >= bounding_box.minLon
and c[1] <= bounding_box.maxLat
and c[1] >= bounding_box.minLat
):
return True
elif feature["geometry"]["type"] == "MultiLineString":
for coords in coordinates:
converted_coordinates.append(tile.convert(coords))
for coords in converted_coordinates:
for c in coords:
if c[0] <= bounding_box.maxLon and c[0] >= bounding_box.minLon and c[1] <= bounding_box.maxLat and c[1] >= bounding_box.minLat:
if (
c[0] <= bounding_box.maxLon
and c[0] >= bounding_box.minLon
and c[1] <= bounding_box.maxLat
and c[1] >= bounding_box.minLat
):
return True
return False


def bbox_to_tiles(
bbox: BBox, zoom: int, client_id: Optional[str] = ""
) -> Generator[Tile, None, None]:
"""Convert a bbox to a series of tiles"""
tile_lower_left: Tile = Tile.lat_lon_to_tile(
bbox.minLat, bbox.minLon, zoom
)
tile_upper_right: Tile = Tile.lat_lon_to_tile(
bbox.maxLat, bbox.maxLon, zoom
)
tile_lower_left: Tile = Tile.lat_lon_to_tile(bbox.minLat, bbox.minLon, zoom)
tile_upper_right: Tile = Tile.lat_lon_to_tile(bbox.maxLat, bbox.maxLon, zoom)
if (
tile_lower_left.x_coordinate > tile_upper_right.x_coordinate
or tile_lower_left.y_coordinate < tile_upper_right.y_coordinate
Expand Down Expand Up @@ -288,9 +288,7 @@ def deduplicate_data(geometry):
dup_shapes.append(shape(feature["geometry"]))
geom = linemerge(dup_shapes).simplify(0.00005)
if geom.type == "LineString":
feature["geometry"]["coordinates"] = [
[g[0], g[1]] for g in geom.coords
]
feature["geometry"]["coordinates"] = [[g[0], g[1]] for g in geom.coords]
elif geom.type == "MultiLineString":
coordinates = []
for line in geom.geoms:
Expand All @@ -304,9 +302,7 @@ def deduplicate_data(geometry):

def compute_tasks(geom):
""" Generate tasks for a project based off of geometry """
MAPILLARY_TIME_CORRELATION = current_app.config[
"MAPILLARY_TIME_CORRELATION"
]
MAPILLARY_TIME_CORRELATION = current_app.config["MAPILLARY_TIME_CORRELATION"]
project_tasks = []
for feature in geom:
t1 = datetime.datetime.fromtimestamp(
Expand Down Expand Up @@ -361,9 +357,7 @@ def compute_tasks(geom):


def fetch_additional_tiles(project_tiles, client_id):
project_tile_coordinates = [
p_t.get_tile_coordinates() for p_t in project_tiles
]
project_tile_coordinates = [p_t.get_tile_coordinates() for p_t in project_tiles]
additional_tiles = set()
for t in project_tiles:
if t.download_additional:
Expand All @@ -376,13 +370,7 @@ def fetch_additional_tiles(project_tiles, client_id):
t.zoom,
client_id=client_id,
),
tuple(
[
k[0]
for k in t.cutoff_keys
if k[1] == Bounds.SOUTH
]
),
tuple([k[0] for k in t.cutoff_keys if k[1] == Bounds.SOUTH]),
)
)
if Bounds.NORTH in t.download_additional:
Expand All @@ -394,13 +382,7 @@ def fetch_additional_tiles(project_tiles, client_id):
t.zoom,
client_id=client_id,
),
tuple(
[
k[0]
for k in t.cutoff_keys
if k[1] == Bounds.NORTH
]
),
tuple([k[0] for k in t.cutoff_keys if k[1] == Bounds.NORTH]),
)
)
if Bounds.WEST in t.download_additional:
Expand All @@ -412,13 +394,7 @@ def fetch_additional_tiles(project_tiles, client_id):
t.zoom,
client_id=client_id,
),
tuple(
[
k[0]
for k in t.cutoff_keys
if k[1] == Bounds.WEST
]
),
tuple([k[0] for k in t.cutoff_keys if k[1] == Bounds.WEST]),
)
)
if Bounds.EAST in t.download_additional:
Expand All @@ -430,13 +406,7 @@ def fetch_additional_tiles(project_tiles, client_id):
t.zoom,
client_id=client_id,
),
tuple(
[
k[0]
for k in t.cutoff_keys
if k[1] == Bounds.EAST
]
),
tuple([k[0] for k in t.cutoff_keys if k[1] == Bounds.EAST]),
)
)

Expand Down Expand Up @@ -473,9 +443,7 @@ def getMapillarySequences(parameters: dict):
if "bbox" not in parameters:
raise ValueError("parameters must include a bbox")
if "start_time" not in parameters and "end_time" not in parameters:
raise ValueError(
"parameters must have at least a start or an end time"
)
raise ValueError("parameters must have at least a start or an end time")

start_epoch, end_epoch = None, None
# Kaart org key
Expand Down Expand Up @@ -535,7 +503,8 @@ def getMapillarySequences(parameters: dict):
if (
"organization_key" in f["properties"]
and f["properties"]["organization_key"] in org_keys
and geom_in_bounds(f, tile, bbox)):
and geom_in_bounds(f, tile, bbox)
):
tile_geom.append(f)
if len(tile_geom) > 0:
tile.set_geometry(tile_geom)
Expand Down Expand Up @@ -608,9 +577,7 @@ def getSequencesAsGPX(project_id: int, task_ids_str: str):
root.append(trk)
ET.SubElement(
trk, "name"
).text = (
f"Task for project {project_id}. Do not edit outside of this area!"
)
).text = f"Task for project {project_id}. Do not edit outside of this area!"

# Create trkseg element
trkseg = ET.Element("trkseg")
Expand All @@ -627,14 +594,10 @@ def getSequencesAsGPX(project_id: int, task_ids_str: str):
raise NotFound()

for task in tasks:
key = json.loads(task.extra_properties)["mapillary"][
"sequence_key"
]
key = json.loads(task.extra_properties)["mapillary"]["sequence_key"]
gpx = requests.get(url.format(key), headers=headers).content
root2 = ET.fromstring(gpx)
for trkpt in root2.iter(
"{http://www.topografix.com/GPX/1/1}trkpt"
):
for trkpt in root2.iter("{http://www.topografix.com/GPX/1/1}trkpt"):
ET.SubElement(trkseg, "trkpt", attrib=trkpt.attrib)

sequences_gpx = ET.tostring(root, encoding="utf8")
Expand Down

0 comments on commit 9a9a76f

Please sign in to comment.