GeoPandasで都市データのプロット
GeoPandasのdatasetsには、naturalearth_lowresの他に主要都市のデータnaturalearth_citiesがあります。これを使い世界地図に都市もプロットしてみましょう。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import geopandas as gpd
import matplotlib.pyplot as plt
# 世界地図のデータ
world = gpd.datasets.get_path('naturalearth_lowres')
# 主要都市のデータ
cities = gpd.datasets.get_path('naturalearth_cities')
wdf = gpd.read_file(world)
cdf = gpd.read_file(cities)
print(wdf.head())
print()
print(wdf.shape)
print()
print(cdf.head())
print()
print(cdf.shape)
結果
       pop_est      continent                      name iso_a3  gdp_md_est                                           geometry
0     889953.0        Oceania                      Fiji    FJI        5496  MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
1   58005463.0         Africa                  Tanzania    TZA       63177  POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2     603253.0         Africa                 W. Sahara    ESH         907  POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
3   37589262.0  North America                    Canada    CAN     1736425  MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4  328239523.0  North America  United States of America    USA    21433226  MULTIPOLYGON (((-122.84000 49.00000, -120.0000...
(177, 6)
           name                    geometry
0  Vatican City   POINT (12.45339 41.90328)
1    San Marino   POINT (12.44177 43.93610)
2         Vaduz    POINT (9.51667 47.13372)
3       Lobamba  POINT (31.20000 -26.46667)
4    Luxembourg    POINT (6.13000 49.61166)
(243, 2)
naturalearth_citiesは主要都市の名前と位置というシンプルなデータです。
これを世界地図に乗せていきます。
世界地図のGeoDataFrameオブジェクトを変数にして、axに指定することで両方プロットすることが出来ます。
# ベースとなる世界地図
bg = wdf.plot(column='continent', figsize=(18, 8))
# 主要都市をマッピング
cdf.plot(ax=bg, marker='o', color='firebrick')
# アノテーションで主要都市の名前をマッピング
for i, (city, point) in cdf.iterrows():
    # pointオブジェクトから緯度経度を抽出
    x1, y1, x2, y2 = point.bounds
    bg.annotate(city, xy=(x1, y1))
plt.show()
geomatryデータからshapely.geometry.point.Pointオブジェクトを抜き出し、
bounds変数には(minx, miny, maxx, maxy)が入っているので、xy座標に指定します。
世界地図に都市名も表示出来ました。
shapely.Point
https://shapely.readthedocs.io/en/stable/reference/shapely.Point.html

 
 
0 件のコメント:
コメントを投稿