利用shapely矢量数据求交
时间: 2024-06-09 08:08:21 浏览: 159
武汉市路网矢量数据WGS84
要利用Shapely求矢量数据的交,可以按照以下步骤进行:
1. 导入必要的模块
```python
from shapely.geometry import Polygon
```
2. 创建要进行交集运算的两个多边形对象
```python
poly1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
poly2 = Polygon([(0.5, 0.5), (0.5, 1.5), (1.5, 1.5), (1.5, 0.5)])
```
3. 进行交集运算
```python
intersection = poly1.intersection(poly2)
```
4. 可以查看交集的面积或者绘制交集
```python
print(intersection.area)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.add_patch(PolygonPatch(intersection, alpha=0.5))
plt.show()
```
完整代码如下:
```python
from shapely.geometry import Polygon
from descartes import PolygonPatch
import matplotlib.pyplot as plt
# 创建两个多边形对象
poly1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
poly2 = Polygon([(0.5, 0.5), (0.5, 1.5), (1.5, 1.5), (1.5, 0.5)])
# 进行交集运算
intersection = poly1.intersection(poly2)
# 输出交集面积
print(intersection.area)
# 绘制交集图形
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.add_patch(PolygonPatch(intersection, alpha=0.5))
plt.show()
```
阅读全文