import geopandas as gpd
import matplotlib.pyplot as plt
gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'
polys = gpd.read_file('PKS_suuralue.kml', driver='KML')
/Users/lchen/anaconda3/envs/geo_env/lib/python3.8/site-packages/geopandas/geodataframe.py:422: RuntimeWarning: Sequential read of iterator was interrupted. Resetting iterator. This can negatively impact the performance. for feature in features_lst:
polys.plot()
<AxesSubplot:>
import pandas as pd
data = pd.read_csv('addresses.txt', sep=';')
from geopandas.tools import geocode
geo = geocode(data['addr'])
join = geo.join(data)
join.head()
geometry | address | id | addr | |
---|---|---|---|---|
0 | POINT (24.91441 60.16320) | Itämerenkatu 14, 00180 Helsinki, Finland | 1000 | Itämerenkatu 14, 00101 Helsinki, Finland |
1 | POINT (24.93041 60.16860) | Kampinkuja 1, 00100 Helsinki, Finland | 1001 | Kampinkuja 1, 00100 Helsinki, Finland |
2 | POINT (24.94141 60.16999) | Kaivokatu 8, 00100 Helsinki, Finland | 1002 | Kaivokatu 8, 00101 Helsinki, Finland |
3 | POINT (24.97441 60.19530) | Hermannin rantatie, Helsinki, Finland | 1003 | Hermannin rantatie 1, 00580 Helsinki, Finland |
4 | POINT (24.92154 60.15670) | Tyynenmerenkatu 9, 00220 Helsinki, Finland | 1005 | Tyynenmerenkatu 9, 00220 Helsinki, Finland |
southern = polys[polys['Name']=='Eteläinen']
southern.reset_index(drop=True, inplace=True)
southern.head()
Name | Description | geometry | |
---|---|---|---|
0 | Eteläinen | POLYGON Z ((24.78277 60.09997 0.00000, 24.8197... |
fig, ax = plt.subplots()
polys.plot(ax=ax, facecolor='gray');
southern.plot(ax=ax, facecolor='red');
join.plot(ax=ax, color='blue', markersize=5);
# makes some of the spatial queries running faster
import shapely.speedups
shapely.speedups.enable()
pip_mask = join.within(southern.loc[0, 'geometry'])
pip_data = join[pip_mask]
pip_data.head()
geometry | address | id | addr | |
---|---|---|---|---|
0 | POINT (24.91441 60.16320) | Itämerenkatu 14, 00180 Helsinki, Finland | 1000 | Itämerenkatu 14, 00101 Helsinki, Finland |
1 | POINT (24.93041 60.16860) | Kampinkuja 1, 00100 Helsinki, Finland | 1001 | Kampinkuja 1, 00100 Helsinki, Finland |
2 | POINT (24.94141 60.16999) | Kaivokatu 8, 00100 Helsinki, Finland | 1002 | Kaivokatu 8, 00101 Helsinki, Finland |
4 | POINT (24.92154 60.15670) | Tyynenmerenkatu 9, 00220 Helsinki, Finland | 1005 | Tyynenmerenkatu 9, 00220 Helsinki, Finland |
10 | POINT (24.94241 60.17190) | Rautatientori 1, 00170 Helsinki, Finland | 1011 | Rautatientori 1, 00100 Helsinki, Finland |
fig, ax = plt.subplots()
polys.plot(ax=ax, facecolor='gray');
southern.plot(ax=ax, facecolor='red');
pip_data.plot(ax=ax, color='gold', markersize=2);
unary_union = join.unary_union
type(unary_union)
shapely.geometry.multipoint.MultiPoint
polys['centroid'] = polys.centroid
<ipython-input-60-6015d0ace0b9>:1: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. polys['centroid'] = polys.centroid
from shapely.ops import nearest_points
nearest = join['geometry'] == nearest_points(polys['geometry'][0], unary_union)[1]
join[nearest]['id']
0 1000 Name: id, dtype: int64
polys['geometry'][0]
join.head()
geometry | address | id | addr | |
---|---|---|---|---|
0 | POINT (24.91441 60.16320) | Itämerenkatu 14, 00180 Helsinki, Finland | 1000 | Itämerenkatu 14, 00101 Helsinki, Finland |
1 | POINT (24.93041 60.16860) | Kampinkuja 1, 00100 Helsinki, Finland | 1001 | Kampinkuja 1, 00100 Helsinki, Finland |
2 | POINT (24.94141 60.16999) | Kaivokatu 8, 00100 Helsinki, Finland | 1002 | Kaivokatu 8, 00101 Helsinki, Finland |
3 | POINT (24.97441 60.19530) | Hermannin rantatie, Helsinki, Finland | 1003 | Hermannin rantatie 1, 00580 Helsinki, Finland |
4 | POINT (24.92154 60.15670) | Tyynenmerenkatu 9, 00220 Helsinki, Finland | 1005 | Tyynenmerenkatu 9, 00220 Helsinki, Finland |