shapefile文件(shp)读取

本文介绍两种解析GIS Shapefile数据的方法:pyshp和geopandas。pyshp库支持读写Shapefile文件,包括点、线和多边形数据;geopandas则以pandas模式封装shp文件,便于地理空间数据分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


shapefile是GIS中一种数据类型,在ArcGIS中被称为要素类(Feature Classes),主要包括点(point)、线(polyline)和多边形(polygon)。

解析geopandas文件的方式很多,本文介绍两个 pyshp和geopandas。


1.pyshp(Python Shapefile Library)

是一个Python库,用于在Python脚本中对ArcGIS中的Shapefile文件(.shp,.shx,.dbf等格式)进行读写操作。

安装:
pip install pyshp
解析:
Reader类, 对shapefile文件读取;Editor类,对shapefile文件编辑;Writer类,对shapefile文件写操作

每个文件包含 "几何数据"(Geometry)和"属性数据"(Attribute Record) ,两个文件数据一一对应。

import shapefile
tpath = 'D:\\work2017\\china_train\\china-shp\\train\\rai_4m.shp'
sf = shapefile.Reader(tpath)
shapes = sf.shapes()
每个几何对象包含有4个属性:数据类型(shapeType),代表该"几何数据"对象的数据类型(点,shapeType=1,线,shapeType=3,多边形,shapeType=5);数据范围(bbox),只针对多点数据,代表该"几何数据"对象的边界范围;数据块(parts),只针对线或者多边形,代表该"几何数据"对象各个块的第一个点的索引;点集(points),代表该"几何数据"对象的所有点坐标。 "属性数据"即每个"几何数据"对象在属性表中的对应项。

属性数据就是一个二维数据 

recds = sf.records()
for i in recds:
    print i[9]

解析后一行属性的结果:

<type 'list'>: [2.4675391, 9.0042706, 2, 3, '30000', '232723', '\xc4\xae\xba\xd3\xcf\xd8', '23', '27', '23', 52.933796, 122.69707]


2. geopandas

它是以pandas的模式对 shp文件的封装,使用起来更方便。

安装:conda install -c conda-forge geopandas

依赖库需要c++环境,如果还是用 pip安装,可能会安装不成功。

解析与展示:

 %matplotlib  inline
import shapely, geopandas, fiona
import seaborn as sns
from fiona.crs import from_epsg,from_string

tpath = 'D:\\work2017\\china_train\\china-shp\\train\\rai_4m.shp'
shp_df = geopandas.GeoDataFrame.from_file(tpath,encoding = 'gb18030')
shp_df.head()
shp_df.plot()

解析后数据:


展示图形:

参考:

1.Using geopandas on Windows
http://geoffboeing.com/2014/09/using-geopandas-windows/


2.shapefile+matplotlib
https://stackoverflow.com/questions/15968762/shapefile-and-matplotlib-plot-polygon-collection-of-shapefile-coordinates


3.用Python做地图投影
http://www.jianshu.com/p/241546a36a93

### 使用 Node.js 和 shapefile 模块读取 shp 文件 在 Node.js 中,`shapefile` 库是一个强大的工具,用于读取和处理 Shapefile 文件。以下是如何使用 `shapefile` 模块读取 `.shp` 文件的示例代码及说明。 #### 安装依赖 首先需要安装 `@geospatial-io/shapefile` 库,这是一个现代的 Shapefile 处理库。可以通过 npm 进行安装: ```bash npm install @geospatial-io/shapefile ``` #### 示例代码 以下是一个完整的示例代码,展示如何使用 `shapefile` 模块读取 `.shp` 文件并解析其内容: ```javascript const shapefile = require('@geospatial-io/shapefile'); // 定义 Shapefile 文件路径 const filePath = './path/to/your/file.shp'; // 异步读取 Shapefile 文件 async function readShapefile() { try { const source = await shapefile.open(filePath); const reader = source.read(); // 遍历每个记录 for (let i = 0; true; i++) { const { done, value } = await reader.next(); if (done) break; console.log(`Record ${i + 1}:`); console.log('Geometry:', value.geometry); // 几何数据 console.log('Properties:', value.properties); // 属性数据 } await source.close(); // 关闭文件 } catch (error) { console.error('Error reading shapefile:', error.message); } } readShapefile(); ``` #### 代码解释 1. **模块引入**:通过 `require` 引入 `@geospatial-io/shapefile` 模块[^1]。 2. **文件路径**:指定要读取的 `.shp` 文件路径。 3. **异步操作**:使用 `async/await` 处理异步任务,确保文件读取和解析过程流畅。 4. **遍历记录**:通过 `source.read()` 方法获取迭代器,并逐个读取文件中的记录。 5. **几何与属性数据**:每个记录包含 `geometry`(几何数据)和 `properties`(属性数据),分别表示空间信息和附加字段[^1]。 #### 注意事项 - 确保提供的 `.shp` 文件与其关联的 `.dbf` 和 `.shx` 文件位于同一目录下,因为这些文件是相互依赖的。 - 如果需要处理坐标转换,可以结合 `gcoord` 或 `proj4` 等库实现[^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值