This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
forked from satejsoman/prclz
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmoketest_planar_graph.py
111 lines (86 loc) · 4.32 KB
/
smoketest_planar_graph.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
111
import argparse
import os
import sys
import time
from typing import Iterable
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from shapely.geometry import (LineString, MultiLineString, MultiPolygon, Point,
Polygon)
from shapely.ops import cascaded_union
from shapely.wkt import loads
from prclz.topology import Edge, Node, PlanarGraph
from prclz.utils import (edge_list_from_linestrings, load_geopandas_files,
prepare_parcels)
# DEFINE GLOBAL PATHS
ROOT = "../"
DATA_PATH = os.path.join(ROOT, "data")
BLOCK_PATH = os.path.join(DATA_PATH, "blocks")
BLDGS_PATH = os.path.join(DATA_PATH, "buildings")
PARCELS_PATH = os.path.join(DATA_PATH, "parcels")
LINES_PATH = os.path.join(DATA_PATH, "lines")
# some test params
# region = "Africa"
# gadm_code = "SLE"
# gadm = "SLE.2.2.5_1"
# region = "Africa"
# gadm_code = "DJI"
# gadm = "DJI.3.1_1"
# example_block = 'DJI.3.1_1_26'
if __name__ == "__main__":
#########################################
waterway_edges = edge_list_from_linestrings(example_lines[example_lines['waterway'].notna()])
natural_edges = edge_list_from_linestrings(example_lines[example_lines['natural'].notna()])
highway_edges = edge_list_from_linestrings(example_lines[example_lines['highway'].notna()])
for u, v, edge_data in example_graph.edges(data=True):
edge_tuple = (u,v)
if edge_tuple in waterway_edges:
edge_data['weight'] = 999
edge_data['edge_type'] = "waterway"
elif edge_tuple in natural_edges:
edge_data['weight'] = 999
edge_data['edge_type'] = "natural"
elif edge_tuple in highway_edges:
edge_data['weight'] = 0
edge_data['edge_type'] = "highway"
else:
edge_data['edge_type'] = "parcel"
#########################################
# (1) Just load our data for one GADM
bldgs, blocks, parcels, lines = load_geopandas_files(region, gadm_code, gadm)
# (2) Now build the parcel graph and prep the buildings
graph_parcels = prepare_parcels(bldgs, blocks, parcels)
# (3) We can grab a graph, and just add the corresponding building Nodes
example_graph = graph_parcels[graph_parcels['block_id']==example_block]['planar_graph'].item()
example_buildings = graph_parcels[graph_parcels['block_id']==example_block]['buildings'].item()
print("\nGraph pre-adding building nodes:\n", example_graph, "\n")
total_blgds = len(example_buildings)
for i, bldg_node in enumerate(example_buildings):
bldg_node.terminal = True
example_graph.add_node_to_closest_edge(bldg_node)
print("through {} of {} buildings".format(i, total_blgds))
print("Graph post-adding building nodes:\n", example_graph)
# (4) Now we take out example graph and update the weights on those edges
example_lines = lines[lines['block_id']==example_block]
#update_graph_with_edge_type(example_graph, example_lines)
#steiner = example_graph.steiner_tree_approx()
#example_graph.plot_reblock()
# Graph snippet
ax = parcels[parcels['block_id']==block_id].plot(color='blue', alpha=0.5)
lines[(lines['block_id']==block_id)&lines['waterway'].notna()].plot(ax=ax, color='blue')
lines[(lines['block_id']==block_id)&lines['highway'].notna()].plot(ax=ax, color='black')
lines[(lines['block_id']==block_id)&lines['natural'].notna()].plot(ax=ax, color='green')
fig, axes = plt.subplots(nrows=1, ncols=2)
# Plot the lines
lines[(lines['block_id']==block_id)&lines['waterway'].notna()].plot(ax=axes[0], color='blue')
lines[(lines['block_id']==block_id)&lines['highway'].notna()].plot(ax=axes[1], color='black')
lines[(lines['block_id']==block_id)&lines['natural'].notna()].plot(ax=axes[0], color='green')
# Plot the resulting block
#blocks[blocks['block_id']==block_id].plot(ax=axes[1], color='black')
# Plot the resulting parcel
parcels[parcels['block_id']==block_id].plot(ax=axes[1], color='black')
waterway_edges = edge_list_from_linestrings(lines[(lines['block_id']==block_id)&lines['waterway'].notna()])
natural_edges = edge_list_from_linestrings(lines[(lines['block_id']==block_id)&lines['natural'].notna()])
highway_edges = edge_list_from_linestrings(lines[(lines['block_id']==block_id)&lines['highway'].notna()])