-
Notifications
You must be signed in to change notification settings - Fork 5
/
Example.py
110 lines (77 loc) · 2.97 KB
/
Example.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
98
99
100
101
102
103
104
105
106
107
108
109
110
from matplotlib import pyplot as plt
import my_graph_helpers as mgh
"""
This example file demonstrates how to import a shapefile, finding and
plotting the shortest distance of roads necessary to acheive universal road
access for all parcels.
The shortest distance of roads algorithm is based on probablistic greedy search
so different runs will give slightly different answers.
"""
def new_import(filename, name=None, byblock=True, threshold=1):
""" imports the file, plots the original map, and returns
a list of blocks from the original map.
"""
if name is None:
name = filename
original = mgh.import_and_setup(filename,
threshold=threshold,
byblock=byblock,
name=name)
blocklist = original.connected_components()
print("This map has {} block(s). \n".format(len(blocklist)))
plt.figure()
# plot the full original map
for b in blocklist:
# defines original geometery as a side effect,
b.plot_roads(master=b, new_plot=False, update=True)
blocklist.sort(key=lambda b: len(b.interior_parcels), reverse=True)
return blocklist
def run_once(blocklist):
"""Given a list of blocks, builds roads to connect all interior parcels and
plots all blocks in the same figure.
"""
map_roads = 0
plt.figure()
for original in blocklist:
original.define_roads()
original.define_interior_parcels()
if len(original.interior_parcels) > 0:
block = original.copy()
# define interior parcels in the block based on existing roads
block.define_interior_parcels()
# finds roads to connect all interior parcels for a given block
block_roads = mgh.build_all_roads(block, wholepath=True)
map_roads = map_roads + block_roads
else:
block = original.copy()
block.plot_roads(master=original, new_plot=False)
return map_roads
if __name__ == "__main__":
# SINGLE SMALL BLOCK
# filename = "data/epworth_demo"
# name = "ep single"
# byblock = True
# threshold = 0.5
# MANY SMALL BLOCKS, epworth
# some of the blocks here require a threshold of 0.5
# filename = "data/epworth_before"
# name = "ep many"
# byblock = True
# threshold = 0.5
# MANY SMALL BLOCKS, Phule Nagar
# some of the blocks here require a threshold of 0.5
filename = "data/phule_nagar_v6"
name = "phule"
byblock = False
threshold = 0.5
# ONE LARGE BLOCK
# filename = "data/capetown"
# name = "cape"
# byblock = False
# threshold = 1
blocklist = new_import(filename, name, byblock=byblock,
threshold=threshold)
g = blocklist[0]
ep_geojson = g.myedges_geoJSON()
map_roads = run_once(blocklist)
plt.show()