前提
pythonでGISデータ処理を行えるGeoPandasを検証しています(google colaboratoryの環境下)。
下記のQGISの操作をGeoPandasを用いて行いたいと考えています。
・最寄りの対象地物までの距離算出および対象地物の指定属性抽出(点と線)
>QGISでの操作:ベクタ解析>最寄りハブの距離を使用(計測単位メートル)
参考として、下記のURLに実装したい機能のスクリプトが載っていたのですが、スクリプトをほぼそのまま記述してもitertoolsでエラーが起きてしまいます。
URL:https://qatop.pythonwood.com/gis/ask/15562081/
まだpythonもGeoPandasも触り始めたばかりで知識は乏しいです。
エラー解決方法や別の方法を教えていただけると幸甚でございます。
よろしくお願いいたします。
実現したいこと
pythonでGISデータ(ポイントとライン)の最近傍探索
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last) <ipython-input-15-2cb0121b196f> in <module>() 38 columns=['Place', 'geometry']) 39 ---> 40 c = ckdnearest(gpd1, gpd2) <ipython-input-15-2cb0121b196f> in ckdnearest(gdfA, gdfB, gdfB_cols) 18 B = [np.array(geom.coords) for geom in gdfB.geometry.to_list()] 19 B_ix = tuple(itertools.chain.from_iterable( ---> 20 (itertools.repeat(i, x) for i, x in enumerate(list(map(len, B)))))) 21 B = np.concatenate(B) 22 ckd_tree = cKDTree(B) TypeError: 'module' object is not callable
該当のソースコード
python
1!apt install gdal-bin python-gdal python3-gdal 2# Install rtree - Geopandas requirment 3!apt install python3-rtree 4# Install Geopandas 5!#pip install git+git://github.com/geopandas/geopandas.git 6# Install Folium for Geographic data visualization 7# !pip install folium 8!pip install plotly-express 9!pip install --upgrade plotly 10!pip install matplotlib-scalebar 11# Use EE in Python 12!pip install pygeos #geopandasよりも先 13!conda install pygeos #geopandasよりも先 14!pip install geemap 15!pip install ipygee 16!pip install geopandas 17 18from google.colab import drive 19drive.mount('/content/drive') 20 21 22import numpy as np 23import os 24from shapely.geometry import Point 25import matplotlib 26import matplotlib.pyplot as plt 27import folium 28#import plotly_express as px 29from datetime import datetime 30import geemap 31from ipygee import* 32 33 34import itertools 35from operator import itemgetter 36 37import geopandas as gpd 38import numpy as np 39import pandas as pd 40 41from scipy.spatial import cKDTree 42from shapely.geometry import Point, LineString 43 44 45#点と線の2つのジオデータフレームから最近傍探索を求める関数 46def ckdnearest(gdfA, gdfB, gdfB_cols=['Place']): 47 A = np.concatenate( 48 [np.array(geom.coords) for geom in gdfA.geometry.to_list()]) 49 B = [np.array(geom.coords) for geom in gdfB.geometry.to_list()] 50 B_ix = tuple(itertools.chain.from_iterable( 51 [itertools.repeat(i, x) for i, x in enumerate(list(map(len, B)))])) 52 B = np.concatenate(B) 53 ckd_tree = cKDTree(B) 54 dist, idx = ckd_tree.query(A, k=1) 55 idx = itemgetter(*idx)(B_ix) 56 gdf = pd.concat( 57 [gdfA, gdfB.loc[idx, gdfB_cols].reset_index(drop=True), 58 pd.Series(dist, name='dist')], axis=1) 59 return gdf 60 61gpd1 = gpd.GeoDataFrame([['John', 1, Point(1, 1)], 62 ['Smith', 1, Point(2, 2)], 63 ['Soap', 1, Point(0, 2)]], 64 columns=['Name', 'ID', 'geometry']) 65gpd2 = gpd.GeoDataFrame([['Work', LineString([Point(100, 0), Point(100, 1)])], 66 ['Shops', LineString([Point(101, 0), Point(101, 1), Point(102, 3)])], 67 ['Home', LineString([Point(101, 0), Point(102, 1)])]], 68 columns=['Place', 'geometry']) 69 70c = ckdnearest(gpd1, gpd2)
試したこと
上述
補足情報(FW/ツールのバージョンなど)
python3

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/07/26 06:11