-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataux_1_poi.py
214 lines (157 loc) · 6.33 KB
/
dataux_1_poi.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
"""01_DATAUX_Lecture_1_POI.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1VMLWDA5B57uv5B7BwrdNUKRKjy4psT2v
"""
!pip install osmnx &> /dev/null
!pip install matplotlib==3.1.3 &> /dev/null
!pip uninstall numpy
# Commented out IPython magic to ensure Python compatibility.
import os, sys
import osmnx as ox
import networkx as nx
import numpy as np
import pandas as pd
import geopandas as gpd
from PIL import Image
from io import BytesIO
import requests
from tqdm import tqdm
import time
import matplotlib.pyplot as plt
import cv2
import matplotlib.pyplot as plt
import numpy as np
# %matplotlib inline
import logging
logger = logging.getLogger()
# logger.setLevel(logging.DEBUG)
"""### 1.Road Networks Data"""
# Commented out IPython magic to ensure Python compatibility.
# %matplotlib inline
ox.config(log_console=True)
ox.__version__
# download/model a street network for some city then visualize it
G = ox.graph_from_place("南沙区, 广州, 中国", network_type='drive')
# Plot the streets
fig, ax = ox.plot_graph(G);
"""### 2.Building footprint
#### <font color=#A52A2A >osmnx.geometries module
Download geospatial entities’ geometries and attributes from OpenStreetMap.<br>
Retrieve <font color=#A52A2A ><b>points of interest, building footprints, or any other objects</b></font> from OSM, including their geometries and attribute data, and construct a GeoDataFrame of them.
https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.geometries.geometries_from_place
#### <font color=#A52A2A >5 methods:
* osmnx.geometries.geometries_from_address(address, tags, dist=1000)
* osmnx.geometries.geometries_from_bbox(north, south, east, west, tags)
* osmnx.geometries.geometries_from_place(query, tags, which_result=None, buffer_dist=None)
* osmnx.geometries.geometries_from_point(center_point, tags, dist=1000)
* osmnx.geometries.geometries_from_polygon(polygon, tags)
#### <font color=#A52A2A >tags
* <b>tags = {"building": True}</b> would return all building footprints in the area<br>
* <b>tags = {"amenity":True, "landuse":["retail","commercial"], "highway":"bus_stop"}</b> would return all amenities, landuse=retail, landuse=commercial, and highway=bus_stop.
"""
buildings_inside_area = ox.geometries_from_place("南沙区, 广州, 中国", tags={"building": True})
buildings_inside_area.to_csv('buildings.csv')
# print(buildings_inside_area["amenity"].unique())
# print(buildings_inside_area["building"].unique())
# print(buildings_inside_area["leisure"].unique())
buildings_inside_area.plot(figsize=(20, 20))
"""### 3. Points of interest (POI)
#### Points-of-interest
#### OpenStreetMap POI features
https://wiki.openstreetmap.org/wiki/Map_features
<font color=#A52A2A ><b># Amenity </b></font> <br>
Used to map facilities used by visitors and residents. For example: toilets, telephones, banks, pharmacies, cafes, parking and schools. See the page Amenities for an introduction on its usage.
* Sustenance
* Education
* Transportation
* Financial
* Healthcare
* Entertainment, Arts & Culture
* Public Service
* Facilities
* Waste Management
* Others
"""
# get the restaurants POI in the Bronx
restaurants_POIs_around = ox.geometries_from_place("南沙区, 广州, 中国", tags={"amenity":"restaurant"})
print(len(restaurants_POIs_around))
restaurants_POIs_around.to_csv('restaurant.csv')
restaurants_POIs_around.plot(figsize=(20, 20),color="red", markersize=20)
# get the amenities POI in the Bronx
POIs_around = ox.geometries_from_place("南沙区, 广州, 中国", tags={"amenity": True})
print(len(POIs_around))
POIs_around.to_csv('pois.csv')
POIs_around.plot(figsize=(20, 20))
POIs_around
"""# draw the boundary of Bronx
area_name = "南沙区, 广州, 中国"
area, edges = ox.geocode_to_gdf(area_name)
area.plot()
"""
# Fetch OSM street network from the given location
roads = ox.graph_from_place(area_name, network_type="drive")
nodes, edges = ox.graph_to_gdfs(roads)
edges.plot()
nodes.head()
edges.head()
# 叠合所有的分层图像
fig, ax = plt.subplots(figsize=(20, 20))
# Plot the footprint
area.plot(ax=ax, facecolor="black", zorder=0)
# Plot street edges
edges.plot(ax=ax,color="white",linewidth=3, zorder=5)
# Plot buildings
buildings_inside_area.plot(ax=ax, facecolor="khaki")
# Plot restaurants
restaurants_POIs_around.plot(ax=ax, color="red", markersize=10)
# landscape
leisure = ox.geometries_from_place(area_name, tags={"leisure":True})
leisure.head()
# Check all values for the column "leisure"
leisure["leisure"].value_counts()
parks = leisure[leisure["leisure"].isin(["park", "sports_centre", "sport", "stadium"])]
parks.plot()
# overlap all the layers
fig, ax = plt.subplots(figsize=(20, 20), dpi=300)
# turn off the axis
ax.axis("off")
# Plot the footprint
area.plot(ax=ax, color="black",linewidth=3,zorder=0)
# Plot street edges
edges.plot(ax=ax, color="white",alpha=0.6,linewidth=1, zorder=5)
# Plot buildings
buildings_inside_area.plot(ax=ax, facecolor="white", zorder = 3)
# Plot POIs
POIs_around.plot(ax=ax, color="dimgrey", markersize=10, zorder = 4)
# Plot parks
parks.plot(ax=ax,color="GhostWhite", alpha=0.5, zorder = 1)
plt.savefig("output_files.png")
# 讲上述内容简写为一个方便调用的函数
def make_figure_ground(place_name):
filepath = "output_files/{}_figure_grounds.png".format(place_name)
# 边界轮廓
bounding_geometry = ox.geocode_to_gdf(place_name)
# 绿地轮廓
leisure = ox.geometries_from_place(place_name, tags={"leisure":["park", "sports_centre", "sport", "stadium"]})
# 道路轮廓
school_graph = ox.graph_from_place(place_name, network_type="drive")
nodes, edges = ox.graph_to_gdfs(school_graph)
# 建筑轮廓
buildings_inside_area = ox.geometries_from_place(place_name, tags={"building": True})
# 叠合所有的分层图像
fig, ax = plt.subplots(figsize=(8, 8), dpi=100)
# 关掉坐标轴与刻度
ax.axis("off")
# Plot the footprint
bounding_geometry.plot(ax=ax, facecolor="black", zorder=0)
# Plot street edges
edges.plot(ax=ax, linewidth=3, zorder=5)
# Plot buildings
buildings_inside_area.plot(ax=ax, facecolor="khaki", zorder = 3)
# Plot parks
leisure.plot(ax=ax,color="green", alpha=0.5, zorder = 1)
plt.savefig(filepath)
make_figure_ground("海珠区, 广州, 中国")
#make_figure_ground("Bronx County, New York, US")