You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import re
import random
def generate_valid_latitude():
# 生成有效的纬度坐标 (0°-90°)
latitude_degree = random.randint(0, 90)
latitude_minute = random.randint(0, 59)
latitude_second = random.randint(0, 59)
latitude_direction = random.choice(['N', 'S'])
return f"{latitude_degree}°{latitude_minute}{latitude_second}{latitude_direction}"
def generate_valid_longitude():
# 生成有效的经度坐标 (0°-180°)
longitude_degree = random.randint(0, 180)
longitude_minute = random.randint(0, 59)
longitude_second = random.randint(0, 59)
longitude_direction = random.choice(['E', 'W'])
return f"{longitude_degree}°{longitude_minute}{longitude_second}{longitude_direction}"
def generate_invalid_latitude():
# 生成错误的纬度坐标 (超过90°或负值)
latitude_degree = random.randint(91, 180)
latitude_minute = random.randint(0, 59)
latitude_second = random.randint(0, 59)
latitude_direction = random.choice(['N', 'S'])
return f"{latitude_degree}°{latitude_minute}{latitude_second}{latitude_direction}"
def generate_invalid_longitude():
# 生成错误的经度坐标 (超过180°或负值)
longitude_degree = random.randint(181, 360)
longitude_minute = random.randint(0, 59)
longitude_second = random.randint(0, 59)
longitude_direction = random.choice(['E', 'W'])
return f"{longitude_degree}°{longitude_minute}{longitude_second}{longitude_direction}"
def generate_invalid_coordinate():
# 随机决定生成哪种类型的错误坐标
if random.choice([True, False]):
# 纬度正确,经度错误
return f"{generate_valid_latitude()},{generate_invalid_longitude()}"
else:
# 纬度错误,经度正确
return f"{generate_invalid_latitude()},{generate_valid_longitude()}"
def insert_invalid_coordinates(file_path, num_invalid_coords):
# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as file:
original_content = file.read()
# 正则表达式匹配现有的经纬度坐标
pattern = r'(\d{1,2}°[0-5]?\d[0-5]?\d″[NS]),(\d{1,3}°[0-5]?\d[0-5]?\d″[WE])'
matches = re.findall(pattern, original_content)
# 将现有的坐标转换为字符串列表
valid_coords = [f"{match[0]},{match[1]}" for match in matches]
# 生成并插入错误坐标
invalid_coords = [generate_invalid_coordinate() for _ in range(num_invalid_coords)]
all_coords = valid_coords + invalid_coords
# 打乱所有坐标的顺序
random.shuffle(all_coords)
# 写回文件
with open(file_path, 'w', encoding='utf-8') as file:
file.write("\n".join(all_coords))
# 指定文件路径和要插入的错误坐标数量
file_path = r"E:\_Ufo\000jiegou\TheBattleCar\coordinate.txt"
num_invalid_coords = 1000
# 插入错误坐标
insert_invalid_coordinates(file_path, num_invalid_coords)
print(f"已成功将 {num_invalid_coords} 个错误坐标插入到 {file_path} 文件中。")