Skip to content

Commit 7a85d9d

Browse files
committed
Generalizes post-processing handlers across zoom levels, closes #51
1 parent 6867993 commit 7a85d9d

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

robosat/features/parking.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@
55

66
import shapely.geometry
77

8+
from robosat.tiles import meters_per_pixel
89
from robosat.features.core import denoise, grow, contours, simplify, featurize, parents_in_hierarchy
910

1011

1112
class ParkingHandler:
12-
kernel_size_denoise = 20
13-
kernel_size_grow = 20
13+
kernel_size_denoise_m = 10
14+
kernel_size_grow_m = 10
1415
simplify_threshold = 0.01
1516

1617
def __init__(self):
1718
self.features = []
1819

1920
def apply(self, tile, mask):
20-
if tile.z != 18:
21-
raise NotImplementedError("Parking lot post-processing thresholds are tuned for z18")
21+
resolution = meters_per_pixel(tile, mask.shape[0])
22+
23+
kernel_size_denoise_px = round(self.kernel_size_denoise_m / resolution)
24+
kernel_size_grow_px = round(self.kernel_size_grow_m / resolution)
25+
26+
assert kernel_size_denoise_px >= 1, "denoising kernel is at least a single pixel in size"
27+
assert kernel_size_grow_px >= 1, "growing kernel is at least a single pixel in size"
2228

2329
# The post-processing pipeline removes noise and fills in smaller holes. We then
2430
# extract contours, simplify them and transform tile pixels into coordinates.

robosat/tiles.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
See: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
99
"""
1010

11+
import math
1112
import csv
1213
import io
1314
import os
@@ -16,6 +17,29 @@
1617
import mercantile
1718

1819

20+
def meters_per_pixel(tile, size):
21+
"""Returns the resolution for a tile.
22+
23+
Args:
24+
tile: the mercantile.tile to calculate the resolution for.
25+
size: the size in pixels for this tile (e.g. 256).
26+
27+
Returns:
28+
The resolution in meters per pixel.
29+
"""
30+
31+
# https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale
32+
33+
z = tile.z
34+
lat = mercantile.ul(tile).lat
35+
36+
radius = 6378137
37+
mpp = radius * 2 * math.pi / size
38+
resolution = mpp * math.cos(math.radians(lat)) / (2 ** z)
39+
40+
return resolution
41+
42+
1943
def pixel_to_location(tile, dx, dy):
2044
"""Converts a pixel in a tile to a coordinate.
2145

robosat/tools/features.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def main(args):
4646
image = np.array(Image.open(path).convert("P"), dtype=np.uint8)
4747
mask = (image == index).astype(np.uint8)
4848

49+
h, w = mask.shape[:2]
50+
assert h == w
51+
4952
handler.apply(tile, mask)
5053

5154
handler.save(args.out)

0 commit comments

Comments
 (0)