python 从大量经纬度列表中,计算出分布比较均匀的经纬度列表

输入 经纬度列表
返回分散比较均匀的 经纬度列表

def generate_scattered_coordinates(coordinates, num_clusters):
    # 计算两个经纬度之间的距离
    def calculate_distance(coord1, coord2):
        return distance(coord1, coord2).km

    # 使用k-means算法对经纬度进行聚类
    kmeans = KMeans(n_clusters=num_clusters)
    clustered_coords = kmeans.fit_predict(coordinates)

    # 获取每个簇的中心点
    cluster_centers = kmeans.cluster_centers_

    # 对每个簇的中心点进行排序,按照到其他点的平均距离从小到大排列
    sorted_centers = sorted(cluster_centers,
                            key=lambda x: np.mean([calculate_distance(x, coord) for coord in coordinates]))

    # 返回排序后的簇中心点
    return sorted_centers


#原始列表这里只写几个
coordinates =[(35.048659, 118.360698), (35.076599, 118.336026)]
#输出的个数
num_scattered_coordinates = 25

# 生成分散的经纬度列表
scattered_coordinates = generate_scattered_coordinates(coordinates, num_scattered_coordinates)

# 输出结果
result = []
for coord in scattered_coordinates:
    result.append([round(float(coord[0]),6),round(float(coord[1]),6)])
print(result)
  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于经纬度和高度以及大量约束条件下的无人机航路规划A星算法Python实现的示例代码: ```python import math # 地球半径,单位:米 EARTH_RADIUS = 6371000 # 目标点经纬度坐标 GOAL_LATITUDE = 31.23 GOAL_LONGITUDE = 121.47 # 起点经纬度坐标 START_LATITUDE = 31.22 START_LONGITUDE = 121.47 # 航路规划的障碍物点经纬度坐标 OBSTACLES = [(31.22, 121.48), (31.23, 121.46), (31.24, 121.47)] # 无人机飞行高度,单位:米 FLIGHT_HEIGHT = 100 # 无人机飞行速度,单位:米/秒 FLIGHT_SPEED = 20 # 无人机飞行时每个航路点之间的最小距离,单位:米 MINIMUM_DISTANCE = 10 class Node: def __init__(self, latitude, longitude, height, parent=None): self.latitude = latitude self.longitude = longitude self.height = height self.parent = parent self.g = 0 self.h = 0 self.f = 0 def __eq__(self, other): return self.latitude == other.latitude and self.longitude == other.longitude and self.height == other.height def __hash__(self): return hash((self.latitude, self.longitude, self.height)) def get_distance(node1, node2): """ 计算两个节点之间的距离 :param node1: 节点1 :param node2: 节点2 :return: 距离,单位:米 """ lat1 = math.radians(node1.latitude) lat2 = math.radians(node2.latitude) delta_lat = math.radians(node2.latitude - node1.latitude) delta_long = math.radians(node2.longitude - node1.longitude) a = math.sin(delta_lat / 2) * math.sin(delta_lat / 2) + math.cos(lat1) * math.cos(lat2) * math.sin(delta_long / 2) * math.sin(delta_long / 2) c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) distance = EARTH_RADIUS * c return distance def calculate_h(node): """ 计算节点到目标点的估价函数 :param node: 节点 :return: 估价函数值 """ goal_node = Node(GOAL_LATITUDE, GOAL_LONGITUDE, FLIGHT_HEIGHT) distance = get_distance(node, goal_node) return distance def get_neighbors(node): """ 获取节点的相邻节点 :param node: 节点 :return: 相邻节点列表 """ neighbors = [] for i in range(-1, 2): for j in range(-1, 2): if i == 0 and j == 0: continue latitude = node.latitude + (i * MINIMUM_DISTANCE) / EARTH_RADIUS * (180 / math.pi) longitude = node.longitude + (j * MINIMUM_DISTANCE) / EARTH_RADIUS * (180 / math.pi) / math.cos(node.latitude * math.pi / 180) height = node.height neighbor_node = Node(latitude, longitude, height) # 判断相邻节点是否在障碍物上 obstacle_flag = False for obstacle in OBSTACLES: distance = get_distance(neighbor_node, Node(obstacle[0], obstacle[1], height)) if distance < MINIMUM_DISTANCE: obstacle_flag = True break if obstacle_flag: continue # 判断相邻节点是否超了地球表面 if neighbor_node.latitude > 90 or neighbor_node.latitude < -90 or neighbor_node.longitude > 180 or neighbor_node.longitude < -180: continue # 判断相邻节点是否与父节点重合 if node.parent is not None and node.parent == neighbor_node: continue neighbors.append(neighbor_node) return neighbors def a_star(start_node, goal_node): """ A星算法 :param start_node: 起点节点 :param goal_node: 目标节点 :return: 路径列表 """ open_list = [start_node] closed_list = set() while open_list: current_node = min(open_list, key=lambda node: node.f) open_list.remove(current_node) closed_list.add(current_node) if current_node == goal_node: path = [] while current_node is not None: path.append(current_node) current_node = current_node.parent return path[::-1] for neighbor_node in get_neighbors(current_node): if neighbor_node in closed_list: continue tentative_g = current_node.g + get_distance(current_node, neighbor_node) / FLIGHT_SPEED if neighbor_node not in open_list: open_list.append(neighbor_node) elif tentative_g >= neighbor_node.g: continue neighbor_node.parent = current_node neighbor_node.g = tentative_g neighbor_node.h = calculate_h(neighbor_node) neighbor_node.f = neighbor_node.g + neighbor_node.h return None if __name__ == '__main__': start_node = Node(START_LATITUDE, START_LONGITUDE, FLIGHT_HEIGHT) goal_node = Node(GOAL_LATITUDE, GOAL_LONGITUDE, FLIGHT_HEIGHT) path = a_star(start_node, goal_node) if path is not None: for node in path: print(f"({node.latitude}, {node.longitude}, {node.height})") else: print("无法找到路径") ``` 该代码实现了一个A星算法,用于在地球表面基于经纬度和高度以及大量约束条件下规划无人机的航路。其,`Node`类表示一个节点,保存了节点的经纬度和高度信息,以及与该节点相关的一些算法参数。`get_distance`函数用于计算两个节点之间的距离。`calculate_h`函数用于计算一个节点到目标节点的估价函数。`get_neighbors`函数用于获取一个节点的相邻节点。`a_star`函数实现了A星算法,在起点和目标点之间寻找一条最优路径,并返回路径列表。最后,我们可以通过调用`a_star`函数来获取无人机的航路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值