-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_2d_from_json.py
97 lines (75 loc) · 3.01 KB
/
plot_2d_from_json.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
from shapely.geometry import Polygon
import matplotlib.pyplot as plt
import json
# find centroid of a polygon
def centroid_coords(poly):
centroid = poly.centroid
x, y = centroid.coords[0]
return x, y
# parallel moving all coords to the center of the graph
def move_coords(coords, diff_xy):
new_coords = []
for j in coords:
x, y = j
new_coords.append([x - diff_xy[0], y - diff_xy[1]])
return new_coords
# function of plotting
def plot_community(community, canvas_size, only_boundary=False, only_buildings=False,
hide_axis=False, hide_spines=False, printing_title=True,
building_color='cornflowerblue', boundary_color='red'):
canvas_poly = Polygon([(0, 0), (canvas_size, 0), (canvas_size, canvas_size), (0, canvas_size)])
canvas_cen_x, canvas_cen_y = centroid_coords(canvas_poly)
# if coords transfer is needed, read data from raw data, else read data from _rp
boundary_coords = community['boundary']
buildings = community['buildings']
_id = community['_id']
# construct boundary polygon
boundary_poly = Polygon(boundary_coords)
boundary_cen_x, boundary_cen_y = centroid_coords(boundary_poly)
diff_xy = [boundary_cen_x-canvas_cen_x, boundary_cen_y-canvas_cen_y]
# plot buildings by 'filling', if needed, transfer the wgs84 coords to webmercator coords
for i in range(len(buildings)):
building_coords = buildings[i]['coords']
# move buildings to the center
building_coords = move_coords(building_coords, diff_xy)
# polygons for buildings
buildings_poly = Polygon(building_coords)
if not only_boundary:
plt.fill(*buildings_poly.exterior.xy, color=building_color)
continue
# move boundary to the center and plot it
boundary_coords = move_coords(boundary_coords, diff_xy)
# polygon for boundary
boundary_poly = Polygon(boundary_coords)
if not only_buildings:
plt.plot(*boundary_poly.exterior.xy, color=boundary_color)
# set axis to 'equal'
plt.axis('equal')
# constrain x, y values
plt.xlim((0, canvas_size))
plt.ylim((0, canvas_size))
# hide axis spines
if hide_spines:
hide_axis = True
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['bottom'].set_visible(False)
plt.gca().spines['left'].set_visible(False)
# hide x, y axis
if hide_axis:
plt.gca().get_xaxis().set_visible(False)
plt.gca().get_yaxis().set_visible(False)
if printing_title:
print(_id)
if __name__ == '__main__':
_id_to_draw = '61ef8a8b32b5d4672152cf77'
canvas_size = 800
plotting = {}
with open('ReCo_json.json', encoding='utf-8') as f:
data = json.load(f)
for example in data:
if example['_id'] == _id_to_draw:
plotting = example
plot_community(plotting, canvas_size, building_color='black', boundary_color='black')
plt.show()