购物网站设计图,百安居装修报价清单,厦门网红打卡景点有哪些,做网站运营跟专业有关吗Python操作Shapefile文件库推荐 1. PyShp (pyshp)
特点#xff1a;纯Python实现#xff0c;无外部依赖#xff0c;轻量级#xff0c;支持完整的Shapefile格式#xff08;shp、dbf、shx#xff09;读写。适用场景#xff1a;基础读写、简单几何操作、文件格式转换。安装…Python操作Shapefile文件库推荐 1. PyShp (pyshp)
特点纯Python实现无外部依赖轻量级支持完整的Shapefile格式shp、dbf、shx读写。适用场景基础读写、简单几何操作、文件格式转换。安装pip install pyshp示例代码import shapefile
# 读取文件
sf shapefile.Reader(example.shp)
shapes sf.shapes() # 几何对象
records sf.records() # 属性表
# 写入文件
w shapefile.Writer(new_file.shp)
w.field(name, C) # 添加字段
w.point(120, 30) # 添加点几何
w.record(Point1) # 添加属性记录
w.close()引用[1]详细说明了PyShp的读取流程和功能。 2. GeoPandas
特点基于Pandas的扩展提供高级数据操作如空间连接、空间查询支持直接读写Shapefile。优势集成Shapely几何操作、支持空间索引、与Matplotlib无缝结合可视化。依赖需安装fiona读写库、shapely几何操作、pyproj坐标转换。安装pip install geopandas示例代码import geopandas as gpd
# 读取文件
gdf gpd.read_file(example.shp)
# 空间查询如筛选包含某点的要素
from shapely.geometry import Point
point Point(120, 30)
result gdf[gdf.contains(point)]
# 写入文件
gdf.to_file(output.shp)3. Fiona
特点基于GDAL的高性能读写库支持多种地理空间格式包括Shapefile。适用场景复杂格式处理、批量操作、与GDAL工具链集成。安装pip install fiona示例代码import fiona
# 读取文件
with fiona.open(example.shp) as src:for feature in src:geometry feature[geometry] # 几何对象GeoJSON格式properties feature[properties] # 属性表
# 写入文件
schema {geometry: Point, properties: {name: str}}
with fiona.open(output.shp, w, ESRI Shapefile, schema) as dst:dst.write({geometry: {type: Point, coordinates: (120, 30)}, properties: {name: Point1}})4. Shapely辅助库
作用处理几何对象如计算面积、缓冲区分析、空间关系判断。搭配使用常与PyShp或Fiona联合使用。示例from shapely.geometry import Polygon
polygon Polygon([(0, 0), (1, 1), (1, 0)])
print(polygon.area) # 计算面积推荐选择
简单读写优先选择PyShp代码简洁依赖少。数据分析使用GeoPandas支持Pandas操作适合复杂分析。高性能/多格式选择Fiona需处理GDAL依赖。