用shapefile.Reader读取.shp,将.shp转化为numpy数组
时间: 2023-11-22 12:54:13 浏览: 233
可以使用Python的`shapefile`和`numpy`库来实现读取.shp文件并将其转化为numpy数组的操作。具体步骤如下:
1. 安装`shapefile`和`numpy`库
```
pip install pyshp
pip install numpy
```
2. 导入库
```python
import shapefile
import numpy as np
```
3. 读取.shp文件
```python
reader = shapefile.Reader("path/to/shapefile.shp")
```
4. 获取shapefile的所有记录
```python
records = reader.records()
```
5. 获取shapefile的所有几何对象
```python
shapes = reader.shapes()
```
6. 将几何对象转化为numpy数组
```python
arrays = []
for shape in shapes:
points = np.array(shape.points)
parts = shape.parts
if len(parts) == 1:
arrays.append(points)
else:
for i in range(len(parts)):
if i == len(parts)-1:
arrays.append(points[parts[i]:])
else:
arrays.append(points[parts[i]:parts[i+1]])
```
这里将几何对象中的所有点坐标都存储在了`arrays`列表中,每个元素都是一个numpy数组,表示一个多边形的点坐标。如果shapefile中包含多个多边形,则`arrays`列表中会有多个元素。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)