程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Use Python and osmnx library to obtain foreign POI data

編輯:Python

Because of Baidu or Gaode API Only domestic POI, Google API Need to climb over the wall and charge , So I have been wondering why I whore abroad for nothing POI Data headache . Recently, I finally found a way , Use OSMnx, To be based on OSM The map provides POI data .

In the process of learning , I found that no one actually wrote a related article introduction , There is no way but to touch the stone to cross the river , Learn little by little , This function is most proposed in github issue Mentioned in . For the initial function design 、 Content presentation and other issues are discussed , It's still interesting . At the same time, I also thank these open source contributors , Thank you very much !

List of articles

  • Basic introduction
  • obtain POI Methods
  • actual combat
    • Pass around the point POI
    • Get the city's POI

Basic introduction

OSMnx It is from the planning department of Nanjing University Geoff Boeing Written by professor OpenStreetMap Of python Expansion pack .

Refer to the official website :https://geoffboeing.com/

OSMnx Basic functions of : Download street network 、 download POI、 Download the urban road network structure 、 Visualization, etc. .
There are many basic tutorials online , But I recommend the introduction written by the author in the following link

https://geoffboeing.com/2016/11/osmnx-python-street-networks/

For this article , We just need to know , How to obtain the administrative location boundary can , Because get POI To use .

Get the administrative location boundary :osmnx.geocode_to_gdf(place)
This paper takes (‘Berkeley, California’) As place

import osmnx as ox
place = 'Berkeley, California'
city = ox.geocode_to_gdf(place)
ax = ox.project_gdf(city).plot()
_ = ax.axis('off') # Eliminate the chart box

obtain POI Methods

OSMnx Provide access to POI Methods , And originally in github issue The names in are different , The current module is called osmnx.geometries module. The modules are very regular , given 5 A way to get data , Namely :

  1. Get the data within a certain range of the given address Center

osmnx.geometries_from_address(address, tags, dist=1000)

  1. Get the data in a longitude and latitude matrix

geometries.geometries_from_bbox(north, south, east, west, tags)

  1. Get the data within a certain range of a given address

osmnx.geometries.geometries_from_place(query, tags, which_result=None, buffer_dist=None)

  1. Obtain data within a certain range around a given point

osmnx.geometries.geometries_from_point(center_point, tags, dist=1000)

  1. Get the data of the given address

osmnx.geometries.geometries_from_polygon(polygon, tags)

this 5 Methods , You can choose according to your needs . The specific content can be seen Official documents , The following main uses 4 and 5 Method .
Need to be right tags emphasize . What you want to get is through tags To define , Please refer to relevant materials for details . Here is only given to get all POI.

tags = {"amenity": True} # Represents all POI

actual combat

Here, in order to compare different methods , Use 4 and 5 Method for demonstration .

Method 4: The longitude and latitude of a given point and the search range , Get point centric , Point to the data within the range
Method 5: Given city name , Get city wide data


Pass around the point POI

Get longitude -122.3133443 ; dimension 37.9285095, Near this point 10km Of POI Distribution .

# longitude -122.3133443 dimension 37.9285095
p1 = ox.geometries.geometries_from_point((37.9285095, -122.3133443),
tags={'amenity': True},
dist=10000)

Got p1 It's a DataFrame,index Consists of three :node,way,relation.

We will p1 Visualize the content of

ds = p1.loc['node']['geometry']
ds2 = p1.loc['way']['geometry']
ds3 = p1.loc['relation']['geometry']
fig = plt.figure(figsize = (15,10))
ax = plt.subplot()
ds.plot(edgecolor = 'b', alpha = 0.5, ax = ax)
ds2.plot(edgecolor = 'k',alpha = 0.5, ax = ax)
ds3.plot(edgecolor = 'r',alpha = 0.5, ax = ax)
plt.show()

way and relation Is the polygon in the figure , For getting POI It doesn't make much sense , We just need node

Get the city's POI

ox.geometries_from_polygon(place) , Among them place Need to be shapely.geometry.Polygon or shapely.geometry.MultiPolygon data type , Therefore, it is necessary to obtain the city boundary function mentioned at the beginning geocode_to_gdf, By first obtaining the boundary of the specified city , Then pass the boundary into geometries_from_polygon

place = 'Berkeley, California'
city = ox.geocode_to_gdf(place) # Get city boundaries
p2 = ox.geometries_from_polygon(city['geometry'].all(), tags={"amenity": True} )

Visualization of all contents

ds = p2.loc['node']['geometry']
ds2 = p2.loc['way']['geometry']
ds3 = p2.loc['relation']['geometry']
fig = plt.figure(figsize = (15,10))
ax = plt.subplot()
ds.plot(edgecolor = 'b', alpha = 0.5, ax = ax)
ds2.plot(edgecolor = 'k',alpha = 0.5, ax = ax)
ds3.plot(edgecolor = 'r',alpha = 0.5, ax = ax)
plt.show()


POI visualization

Finally, store data , Can be p1 All content is stored as csv, Convenience is right POI Classify and analyze , Can also be POI Click Save as shp file , Convenient in gis Software operation .

p2.to_csv(r'..\all_poi.csv')
p2.loc['node']['geometry'].to_file(r'..\node.shp')

gis In


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved