Python使用shapefile库读取Shp文件详解

巷口听风
巷口听风
发布时间:2026-10-13 | 2人浏览过
原创

本文详细介绍如何在Python中利用shapefile库读取Shp格式文件。通过具体代码示例,演示如何获取几何形状、记录信息及属性字段,包括遍历要素点坐标和解析记录数据,为GIS数据处理提供基础参考。

具体实现代码如下所示:

import shapefile
sf = shapefile.Reader("E:\\1.2\\cs\\DX_CSL.shp")
shapes = sf.shapes()
print shapes[1].parts
print len(shapes) #79条记录
#print len(list(sf.iterShapes())) #79条记录
#for name in dir(shapes[3]): #不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表
 #       if not name.startswith('__'):
 #           print  name


print sf.numRecords
recds = sf.records()
for i in range(sf.numRecords):
    rcd = sf.record(i)
    #sp = rcd.shape 没有shape属性
    #print sp.points
#recds.shape
#读取记录
print sf.shapeRecord(1).shape.shapeType
print sf.shapeRecord(1).record
print sf.fields


print ''          
for shp in range(len(shapes)):
    shap = shapes[shp]
    print shap.points
    print shap.shapeType
    print len(shap.points)
    for i in range(len(shap.points)):
        print shap.points[i]
        for x in range(len(shap.points[i])):
            print shap.points[i][x]

以上关于Python读取Shp文件的实例详解内容,旨在为读者提供实用的代码参考。如果您觉得本文对您有帮助,欢迎分享给更多需要的朋友,并持续关注我们以获取更多技术分享。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表