Hình học Shapely và quan hệ không gian

Làm việc với Dữ liệu Không gian địa lý trong Python

Dani Arribas-Bel

Geographic Data Science Lab (University of Liverpool)

Giá trị hình học vô hướng

cities = geopandas.read_file("ne_110m_populated_places.shp")
cities.head()
           name                                      geometry
0  Vatican City   POINT (12.45338654497177 41.90328217996012)
1    San Marino     POINT (12.44177015780014 43.936095834768)
2         Vaduz   POINT (9.516669472907267 47.13372377429357)
3       Lobamba  POINT (31.19999710971274 -26.46666746135247)
4    Luxembourg   POINT (6.130002806227083 49.61166037912108)
brussels = cities.loc[170, 'geometry']
print(brussels)
POINT (4.33137074969045 50.83526293533032)
Làm việc với Dữ liệu Không gian địa lý trong Python

Giá trị hình học vô hướng

brussels = cities.loc[170, 'geometry']
print(brussels)
POINT (4.33137074969045 50.83526293533032)
type(brussels)
shapely.geometry.point.Point
Làm việc với Dữ liệu Không gian địa lý trong Python

Gói Python Shapely

type(brussels)
shapely.geometry.point.Point

Shapely

  • Gói Python để thao tác và phân tích đối tượng hình học
  • Cung cấp các đối tượng Point, LineString, Polygon
  • GeoSeries (cột 'geometry' của GeoDataFrame) gồm các đối tượng shapely
Làm việc với Dữ liệu Không gian địa lý trong Python

Đối tượng hình học

Truy cập từ GeoDataFrame:

brussels = cities.loc[170, 'geometry']
paris = cities.loc[235, 'geometry']
belgium = countries.loc[countries['name'] == 'Belgium', 'geometry'].squeeze()
france = countries.loc[countries['name'] == 'France', 'geometry'].squeeze()
uk = countries.loc[countries['name'] == 'United Kingdom', 'geometry'].squeeze()

Tạo thủ công:

from shapely.geometry import Point
p = Point(1, 2)
print(p)
POINT (1 2)
Làm việc với Dữ liệu Không gian địa lý trong Python

Phương thức không gian

Diện tích của một hình học:

belgium.area
3.8299974609075753

Khoảng cách giữa 2 hình học:

brussels.distance(paris)
2.8049127723186214

Còn nhiều nữa! (ví dụ: centroid, simplify, ...)

Làm việc với Dữ liệu Không gian địa lý trong Python

Quan hệ không gian

geopandas.GeoSeries([belgium, france, uk, paris,  brussels, line]).plot()

Làm việc với Dữ liệu Không gian địa lý trong Python

Quan hệ không gian

belgium.contains(brussels)
True
france.contains(brussels)
False
brussels.within(belgium)
True
belgium.touches(france)
True
line.intersects(france)
True
line.intersects(uk)
False
Làm việc với Dữ liệu Không gian địa lý trong Python

Ayo berlatih!

Làm việc với Dữ liệu Không gian địa lý trong Python

Preparing Video For Download...