在之前的博客中已经讲述了该如何将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}")