Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalizes post-processing handlers across zoom levels, closes #51 #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions robosat/features/parking.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@

import shapely.geometry

from robosat.tiles import meters_per_pixel
from robosat.features.core import denoise, grow, contours, simplify, featurize, parents_in_hierarchy


class ParkingHandler:
kernel_size_denoise = 20
kernel_size_grow = 20
kernel_size_denoise_m = 10
kernel_size_grow_m = 10
simplify_threshold = 0.01

def __init__(self):
self.features = []

def apply(self, tile, mask):
if tile.z != 18:
raise NotImplementedError("Parking lot post-processing thresholds are tuned for z18")
resolution = meters_per_pixel(tile, mask.shape[0])

kernel_size_denoise_px = round(self.kernel_size_denoise_m / resolution)
kernel_size_grow_px = round(self.kernel_size_grow_m / resolution)

assert kernel_size_denoise_px >= 1, "denoising kernel is at least a single pixel in size"
assert kernel_size_grow_px >= 1, "growing kernel is at least a single pixel in size"

# The post-processing pipeline removes noise and fills in smaller holes. We then
# extract contours, simplify them and transform tile pixels into coordinates.
Expand Down
24 changes: 24 additions & 0 deletions robosat/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
See: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
"""

import math
import csv
import io
import os
Expand All @@ -16,6 +17,29 @@
import mercantile


def meters_per_pixel(tile, size):
"""Returns the resolution for a tile.

Args:
tile: the mercantile.tile to calculate the resolution for.
size: the size in pixels for this tile (e.g. 256).

Returns:
The resolution in meters per pixel.
"""

# https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale

z = tile.z
lat = mercantile.ul(tile).lat

radius = 6378137
mpp = radius * 2 * math.pi / size
resolution = mpp * math.cos(math.radians(lat)) / (2 ** z)

return resolution


def pixel_to_location(tile, dx, dy):
"""Converts a pixel in a tile to a coordinate.

Expand Down
3 changes: 3 additions & 0 deletions robosat/tools/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def main(args):
image = np.array(Image.open(path).convert("P"), dtype=np.uint8)
mask = (image == index).astype(np.uint8)

h, w = mask.shape[:2]
assert h == w

handler.apply(tile, mask)

handler.save(args.out)