如何将地理信息坐标转化成shp文件

在之前的博客中已经讲述了该如何将dota形式的像素级标签转化成地理信息标签,感兴趣的朋友请移步:如何将DOTA数据集的标注坐标转换为地理坐标-CSDN博客
 

为了方便查看目标的真实地理位置,我们可以将地理信息标签转化成点标签的shp文件,有了shp文件后可以导入进地理信息软件中进行查看。

代码

'''
This code is used to transfer geo to point shapely file
'''

import geopandas as gpd
from shapely.geometry import Point
import pandas as pd
import os

files = ['']  # 文件前缀列表,可根据实际情况调整
geo_files = [
    r'dota_geo_v1.txt',
    r'dota_geo_v2.txt'
    # 这里添加更多文件路径
]
output = r"E:\研究生\project\data\dota_ours"  # 输出基础路径
output_shp_base = os.path.join(output, "anno")  # 将多个文件的数据合并到一个目录中

if not os.path.exists(output_shp_base):
    os.makedirs(output_shp_base)

data_points = []  # 初始化数据点列表

for geo_file in geo_files:  # 遍历每个文件
    with open(geo_file, 'r', encoding="utf-8") as t:
        for line in t:
            parts = line.split()
            coords = [(float(parts[i]), float(parts[i + 1])) for i in range(0, 8, 2)]
            dota_type = parts[-2]  # 数据标签类型
            center_x = sum(x for x, y in coords) / 4
            center_y = sum(y for x, y in coords) / 4
            data_points.append({'geometry': Point(center_x, center_y), 'dota_type': dota_type})

# 将数据转换为GeoDataFrame
gdf_points = gpd.GeoDataFrame(data_points, crs="EPSG:4326")

# 根据类型分组,并为每种类型保存一个shp文件
for dota_type, group in gdf_points.groupby('dota_type'):
    # 创建输出文件路径
    output_shp_points = os.path.join(output_shp_base, f"Combined_{dota_type}_point.shp")
    # 保存为点Shapefile
    group.drop('dota_type', axis=1).to_file(output_shp_points)
    print(f"Point Shapefile for {dota_type} saved to {output_shp_points}")


 

  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值