-
Notifications
You must be signed in to change notification settings - Fork 4
/
display_boundary_utils.py
executable file
·67 lines (44 loc) · 2.12 KB
/
display_boundary_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
def thin_geom_sql(zoom_level):
tolerance = get_tolerance(zoom_level, "degrees")
return "ST_Multi(ST_Union(ST_MakeValid(ST_SimplifyVW(geom, {0}))))".format(tolerance,)
# calculates the area tolerance (in metres or degrees squared)
# for input into the Visvalingam-Whyatt vector simplification
def get_tolerance(zoom_level, units="degrees"):
# pixels squared factor
tolerance_square_pixels = 20
# default Google/Bing map tile scales
metres_per_pixel = 156543.03390625 / math.pow(2.0, float(zoom_level + 1))
if units == "metres":
square_metres_per_pixel = math.pow(metres_per_pixel, 2.0)
# tolerance to use
tolerance = square_metres_per_pixel * tolerance_square_pixels
else:
# rough metres to degrees conversation, using spherical WGS84 datum radius (for simplicity and speed)
metres2degrees = (2.0 * math.pi * 6378137.0) / 360.0
degrees_per_pixel = metres_per_pixel / metres2degrees
square_degrees_per_pixel = math.pow(degrees_per_pixel, 2.0)
# tolerance to use
tolerance = square_degrees_per_pixel * tolerance_square_pixels
return tolerance
# maximum number of decimal places for boundary coordinates - improves display performance
def get_decimal_places(zoom_level):
# rough metres to degrees conversation, using spherical WGS84 datum radius for simplicity and speed
metres2degrees = (2.0 * math.pi * 6378137.0) / 360.0
# default Google/Bing map tile scales
metres_per_pixel = 156543.03390625 / math.pow(2.0, float(zoom_level))
# the tolerance for thinning data and limiting decimal places in GeoJSON responses
degrees_per_pixel = metres_per_pixel / metres2degrees
scale_string = "{:10.9f}".format(degrees_per_pixel).split(".")[1]
places = 1
trigger = "0"
# find how many zero decimal places there are. e.g. 0.00001234 = 4 zeros
for c in scale_string:
if c == trigger:
places += 1
else:
trigger = "don't do anything else" # used to cleanly exit the loop
return places
print(thin_geom_sql(15))