Skip to content

Commit 448ba94

Browse files
committed
Update value function for extracting greenery data
New value function uses a step function instead which is 1 if green is the dominant channel of a pixel, 0 otherwise. The value function looks at the proportion of green-dominant pixels in a bounding box surrounding a point and returns that as the greenery value of the point.
1 parent e4d7044 commit 448ba94

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

update_ways_metadata.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,27 @@ def process_gmaps_satellite(cursor) -> None:
9999
:param cursor: database cursor object
100100
"""
101101

102-
def greenery_value(img, x, y):
102+
# noinspection PyUnusedLocal
103+
def greenery_value_relative(img, x, y):
103104
box = (max(0, x - 10), max(0, y - 10), min(256, x + 10), min(256, y + 10))
104105
neighborhood = img.crop(box).resize((1, 1))
105106
r, g, b = neighborhood.getpixel((0, 0))
106107
return min(1., float(max(g - max(r, b), 0)) / 200)
107108

109+
def greenery_value_absolute(img, x, y):
110+
box = (max(0, x - 10), max(0, y - 10), min(256, x + 10), min(256, y + 10))
111+
neighborhood = img.crop(box)
112+
# Get values in each channel as byte array
113+
r = np.array(neighborhood.getdata(0))
114+
g = np.array(neighborhood.getdata(1))
115+
b = np.array(neighborhood.getdata(2))
116+
# Get proportion of pixels where green is dominant channel
117+
g_delta = np.clip(np.minimum(g-r, g-b), 0, 1).mean()
118+
return g_delta
119+
108120
logger.info('Gathering Gmaps satellite data.')
109121
url = 'http://mt1.google.com/vt/lyrs=s&x=${x}&y=${y}&z=${z}'
110-
gmaps_provider = CachedTiledDataProvider(url, greenery_value, zoom=15, convert_args={'mode': 'RGB'})
122+
gmaps_provider = CachedTiledDataProvider(url, greenery_value_absolute, zoom=15, convert_args={'mode': 'RGB'})
111123

112124
ways_metadata = extract_ways_metadata(cursor, gmaps_provider)
113125
upsert_ways_metadata(cursor, 'greenery', ways_metadata)

0 commit comments

Comments
 (0)