Loading [MathJax]/extensions/tex2jax.js

2024年10月18日金曜日

Pythonで地図空間データを扱う⑤

ベースの地図が出来た所で、他のデータを被せてみます。

国土地理院の 500mメッシュ別将来推計人口データ を使用します。

同じく神奈川県のデータ 500m_mesh_suikei_2018_shape_14.zip をダウンロードします。

ベースの地図データと同じ場所に展開します。

そして同じようにread_fileで読み込みます。

  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 -*-  
  3.   
  4. #  
  5. import geopandas as gpd  
  6. import matplotlib.pyplot as plt  
  7. import japanize_matplotlib  
  8.   
  9. fn = "../data/N03-20240101_14.shp"  
  10. gdf = gpd.read_file(fn)  
  11. fn2 = "../data/500m_mesh_2018_14.shp"  
  12. pdf = gpd.read_file(fn2)  
  13.   
  14. print(gdf.head(10))  
  15. print("")  
  16. print(gdf.shape)  
  17. print("")  
  18. print(pdf.head())  
  19. print("")  
  20. print(pdf.shape)  
(6193, 235)と巨大なデータフレームなので、必要なデータ以外は取り除いてもいいでしょう。
  1. # 必要な列だけ取り出し軽量化します。  
  2. pdf2 = pdf[['SHICODE''PTN_2020''geometry']]  
  3.   
  4. # ベースとなる地図  
  5. bg = gdf.boundary.plot(linewidth=0.5, edgecolor="gray", figsize=(148))  
  6.   
  7. # 人口データのプロット  
  8. pdf2.plot(ax=bg, column="PTN_2020", cmap="jet", legend=True)  
  9.   
  10. # 地域名を表示  
  11. for name, row in citys:  
  12.     if row['N03_001'].count() > 1:  
  13.         x = row["geometry"].centroid.bounds.minx.mean()  
  14.         y = row["geometry"].centroid.bounds.miny.mean()  
  15.         bg.annotate(name, xy=(x, y), color="red")  
  16.     else:  
  17.         x = row["geometry"].centroid.bounds.minx  
  18.         y = row["geometry"].centroid.bounds.miny  
  19.         bg.annotate(name, xy=(x, y), color="red")    
  20.   
  21. plt.title("神奈川県人口マップ")  
  22. plt.show()  
地図データに人口データを重ね合わせることが出来ました。

2024年10月15日火曜日

Pythonで地図空間データを扱う④

日本地図データ

テストデータで概要を掴めたら、外部からshapeファイルを読み込み使用してみます。
 日本に住んでる方は、日本の地域データを活用することが多いでしょうから国土地理院のデータを読み込んでみます。

国土地理院

全国はデータが大きいので今回は神奈川県のデータをダウンロードしました。
データはzipファイルなので、使用する場所に移動して解凍します。

$ unzip N03-20240101_14_GML.zip 

解凍するとshapeファイルなどいくつかのファイルが出来ます。
それらをリンクして使用されるので、個別で移動させたり、ファイル名を変えたりはしないでください。

回答したshapeファイルを前回と同じように読み込みます。

  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 -*-  
  3.   
  4. import geopandas as gpd  
  5. import matplotlib.pyplot as plt  
  6. import japanize_matplotlib  
  7.   
  8. fn = "../data/N03-20240101_14.shp"  
  9. gdf = gpd.read_file(fn)  
  10.   
  11. print(gdf.head())  
  12. print("")  
  13. print(gdf.shape)  
  14. print("")  
結果
N03_001 N03_002 N03_003 N03_004 N03_005 N03_007 geometry
0 神奈川県 None None 横浜市 鶴見区 14101 POLYGON ((139.65294 35.50000, 139.65281 35.50010, 139.65270 35.50015, 139.65221 35.50030, 139.65214 35.50034, 139.65207 35.50038,・・・
1 神奈川県 None None 横浜市 鶴見区 14101 POLYGON ((139.67394 35.46229, 139.67347 35.46329, 139.67303 35.46426, 139.67272 35.46494, 139.67259 35.46523,・・・
2 神奈川県 None None 横浜市 鶴見区 14101 POLYGON ((139.70755 35.47204, 139.70756 35.47208, 139.70719 35.47264, 139.70595 35.47454, 139.70582 35.47457, ・・・
3 神奈川県 None None 横浜市 鶴見区 14101 POLYGON ((139.71140 35.48577, 139.71141 35.48575, 139.71108 35.48560, 139.71114 35.48551, 139.71097 35.48543, 139.71091 35.48553,・・・
4 神奈川県 None None 横浜市 鶴見区 14101 POLYGON ((139.70710 35.47226, 139.70713 35.47228, 139.70715 35.47227, 139.70722 35.47216, 139.70722 35.47215,・・・

1275, 7


地図の表示

次に地図をプロットしてみます。 boundary.plotは境界線のみ描画します。
linewidthやedgecolorで線を調節します。

 
  1. gdf.boundary.plot(linewidth=0.5, edgecolor="gray", figsize=(128))  
  2. plt.title("神奈川県")  
  3. plt.show()  
神奈川県の地区データが表示されました。

2024年10月12日土曜日

ValueError: numpy.dtype size changed, may indicate binary incompatibility. というエラーが出たケース

 以前動いたnumpyのコードからエラーが出るようになりました。

エラー内容

ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject

どうやら他のライブラリと互換性が無い場合に出るエラーの様で、新しくなったnumpyに対応出来ていないライブラリが使われているとのことです。


 pip freeze | grep numpy

numpy==2.0.2


ライブラリが対応するまでnumpyのバージョンを落とすなどの措置が必要です。

numpyが2.0以上なら、それ以前の最新版1.26.4に落とします。


pip uninstall numpy

pip install numpy=1.26.4


一先ず解決出来ました。

Pythonで地図空間データを扱う⑤

ベースの地図が出来た所で、他のデータを被せてみます。 国土地理院の  500mメッシュ別将来推計人口データ  を使用します。 同じく神奈川県のデータ  500m_mesh_suikei_2018_shape_14.zip をダウンロードします。 ベースの地図データと同じ場所に展開...