รูปทรงเรขาคณิต Shapely และความสัมพันธ์เชิงพื้นที่

การทำงานกับข้อมูลเชิงพื้นที่ใน Python

Dani Arribas-Bel

Geographic Data Science Lab (University of Liverpool)

ค่าเรขาคณิตแบบสเกลาร์

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)
การทำงานกับข้อมูลเชิงพื้นที่ใน Python

ค่าเรขาคณิตแบบสเกลาร์

brussels = cities.loc[170, 'geometry']
print(brussels)
POINT (4.33137074969045 50.83526293533032)
type(brussels)
shapely.geometry.point.Point
การทำงานกับข้อมูลเชิงพื้นที่ใน Python

แพ็กเกจ Python Shapely

type(brussels)
shapely.geometry.point.Point

Shapely

  • แพ็กเกจ Python สำหรับจัดการและวิเคราะห์วัตถุเรขาคณิต
  • มีออบเจกต์ Point, LineString และ Polygon
  • GeoSeries (คอลัมน์ 'geometry' ของ GeoDataFrame) ประกอบด้วย shapely objects
การทำงานกับข้อมูลเชิงพื้นที่ใน Python

ออบเจกต์เรขาคณิต

การเข้าถึงจาก 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()

การสร้างด้วยตนเอง:

from shapely.geometry import Point
p = Point(1, 2)
print(p)
POINT (1 2)
การทำงานกับข้อมูลเชิงพื้นที่ใน Python

เมธอดเชิงพื้นที่

พื้นที่ ของรูปทรงเรขาคณิต:

belgium.area
3.8299974609075753

ระยะห่าง ระหว่างรูปทรงเรขาคณิต 2 รูป:

brussels.distance(paris)
2.8049127723186214

และอื่น ๆ อีกมาก! (เช่น centroid, simplify, ...)

การทำงานกับข้อมูลเชิงพื้นที่ใน Python

ความสัมพันธ์เชิงพื้นที่

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

การทำงานกับข้อมูลเชิงพื้นที่ใน Python

ความสัมพันธ์เชิงพื้นที่

belgium.contains(brussels)
True
france.contains(brussels)
False
brussels.within(belgium)
True
belgium.touches(france)
True
line.intersects(france)
True
line.intersects(uk)
False
การทำงานกับข้อมูลเชิงพื้นที่ใน Python

มาฝึกกันเถอะ!

การทำงานกับข้อมูลเชิงพื้นที่ใน Python

Preparing Video For Download...