矢栅处理(杂):geopandas常用函数小结

汇总一下常用的几行geopandas代码便于复用

geopandas一般用于处理数据量不大的文件,文件大的空间操作较慢、另外,理论上esri shapefile的文件大小上限为2Gb

1
2
3
import pandas as pd
import geopandas as gpd
from geopandas import sjoin

创建,读取or写出

1
2
3
4
5
6
7
8
9
10
# 读
shapefilepath = "..shp"
gdf = gpd.read_file(shapefilepath, encoding = 'utf-8')
gdf = gpd.read_file(shapefilepath, encoding = 'gbk')
# 通过df并添加集合创建
df = pd.read_csv(filepath)
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.lng, df.lat), crs="EPSG:4326")
gdf = gpd.GeoDataFrame(df, geometry=geometries, crs="EPSG:4326")
# 写
res0.to_file(outshapefile, encoding = 'utf-8')

空间连接-ref, predicate可选参数

  • intersects、contains、within、touches、crosses、overlaps
  • how: left、right、inner,连接方式含义
  • 如果pandas的版本较旧,predicate参数名称为op
1
2
3
4
res = sjoin(left_df=gdf_points, right_df=gdf_polygon, predicate='within', how='inner')
res = sjoin(left_df=gdf_polygon, right_df=gdf_points, predicate='contains', how='inner')
res = sjoin(left_df=gdf_polygon1, right_df=gdf_polygon2, predicate='intersects', how='inner')
res = sjoin(left_df=gdf_polygon1, right_df=gdf_polygon2, op='intersects', how='inner')

除了官方文档,还可以参考一下:

https://www.cnblogs.com/feffery/p/12909284.html:缓冲区、叠加分析、融合与拆分、clip等