通过Python将多组经纬度坐标生成多个矢量面文件
时间: 2023-08-05 12:06:11 浏览: 154
要通过Python将多组经纬度坐标生成多个矢量面文件,您可以在上述代码的基础上进行修改。下面是一个示例代码,可以将多组经纬度坐标转换为多个矢量面并保存为不同的Shapefile文件:
```python
from osgeo import ogr
# 多组经纬度坐标
coordinates_sets = [
[(lon1, lat1), (lon2, lat2), (lon3, lat3)],
[(lon4, lat4), (lon5, lat5), (lon6, lat6)],
# ...添加更多的坐标组...
]
# 遍历每组经纬度坐标
for i, coordinates in enumerate(coordinates_sets):
# 创建Shapefile数据源
driver = ogr.GetDriverByName('ESRI Shapefile')
data_source = driver.CreateDataSource(f'output_{i}.shp')
# 创建图层
layer = data_source.CreateLayer('polygon', geom_type=ogr.wkbPolygon)
# 创建字段
field_defn = ogr.FieldDefn('id', ogr.OFTInteger)
layer.CreateField(field_defn)
# 创建多边形
ring = ogr.Geometry(ogr.wkbLinearRing)
# 添加经纬度坐标点
for lon, lat in coordinates:
ring.AddPoint(lon, lat)
# 创建多边形对象
polygon = ogr.Geometry(ogr.wkbPolygon)
polygon.AddGeometry(ring)
# 创建要素
feature_defn = layer.GetLayerDefn()
feature = ogr.Feature(feature_defn)
feature.SetGeometry(polygon)
feature.SetField('id', i+1)
# 将要素添加到图层
layer.CreateFeature(feature)
# 清理资源
feature = None
data_source = None
```
请将代码中的经纬度坐标替换为您实际的坐标。此示例代码将遍历`coordinates_sets`列表中的每组坐标,并为每组坐标创建一个单独的Shapefile文件。文件名将使用索引编号进行命名,例如"output_0.shp","output_1.shp"等。
请注意,此示例代码假设每组坐标都是一个列表,并且列表中的每个元素都是一个经纬度对。您可以根据实际情况进行修改和调整。
确保已安装GDAL库,可以使用`pip install gdal`命令进行安装。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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/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://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)