Spaces:
Runtime error
Runtime error
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Aug 2 12:38:31 2022 | |
@author: syed | |
""" | |
import regex_spatial | |
from utils import geoutil | |
import geopandas as gpd | |
import pandas as pd | |
import re | |
from shapely.geometry import Polygon,mapping | |
import numpy as np | |
from shapely.geometry import Polygon, MultiPoint, LineString, Point | |
from shapely.geometry.base import geom_factory | |
from shapely.geos import lgeos | |
from geocoder import geo_level1 | |
def get_between_coordinates(coordinates1, coordinates2, centroid1, centroid2): | |
# 创建多边形对象 | |
poly1 = Polygon(coordinates1) | |
poly2 = Polygon(coordinates2) | |
# 计算中心点连线 | |
center_line = LineString([centroid1, centroid2]) | |
def max_perpendicular_distance(poly, line): | |
max_dist = 0 | |
farthest_points = None | |
for point in poly.exterior.coords: | |
p = Point(point) | |
# 计算点到中心线的垂直距离 | |
dist = p.distance(line) | |
if dist > max_dist: | |
max_dist = dist | |
farthest_points = p | |
return max_dist * 2 # 计算直径(双倍最大垂直距离) | |
# 计算两个区域的最大垂线距离 | |
diameter1 = max_perpendicular_distance(poly1, center_line) | |
diameter2 = max_perpendicular_distance(poly2, center_line) | |
# 计算平均直径 R | |
R = (diameter1 + diameter2) / 2 | |
# 计算圆心(两个中心点的中点) | |
midpoint = ((centroid1[0] + centroid2[0]) / 2, (centroid1[1] + centroid2[1]) / 2) | |
# 生成圆形区域 | |
circle = Point(midpoint).buffer(R / 2, resolution=100) | |
# 获取圆的坐标 | |
circle_coords = list(circle.exterior.coords) | |
return [circle_coords], midpoint | |
def get_angular_coordinates(coordinates, angle, distance): | |
""" | |
计算基于输入地点坐标、方向角度和距离的目标点,并返回以该点为圆心的等面积圆形坐标集。 | |
:param coordinates: 地点的边界坐标列表 | |
:param angle: 方向角度(以正北为 0 度,顺时针方向) | |
:param distance: 目标点距离(单位:公里) | |
:return: 以目标点为圆心,等面积半径的圆形坐标集 | |
""" | |
# 计算输入地点的几何中心 | |
poly = Polygon(coordinates) | |
poly_center = poly.centroid | |
lat, lon = poly_center.y, poly_center.x # 纬度在前,经度在后 | |
# 计算地球上的 1 度经纬度距离(近似值) | |
lat_km = 111.32 # 1 度纬度 ≈ 111.32 km | |
lon_km = 111.32 * np.cos(np.radians(lat)) # 1 度经度 ≈ 111.32 × cos(纬度) | |
# 计算输入区域的面积(近似 km²,需进一步优化投影转换) | |
area_degrees = poly.area # 原始经纬度面积 | |
area_km2 = area_degrees * (lat_km * lon_km) # 近似转换为 km² | |
# 计算等面积圆的半径 r(单位 km) | |
r_km = np.sqrt(area_km2 / np.pi) # 使得 πr² ≈ area_km2 | |
# 计算方向向量(基于输入角度) | |
angle_rad = np.radians(angle) # 转换为弧度 | |
d_lat = (np.sin(angle_rad) * distance) / lat_km | |
d_lon = (np.cos(angle_rad) * distance) / lon_km | |
# 计算目标点 | |
target_point = (lon + d_lon, lat + d_lat) | |
# 计算以目标点为圆心,半径 r_km 的圆形坐标集 | |
circle_points = [] | |
for theta in np.linspace(0, 360, num=100): # 生成 100 个点构成圆形 | |
theta_rad = np.radians(theta) | |
d_lat = (np.sin(theta_rad) * r_km) / lat_km | |
d_lon = (np.cos(theta_rad) * r_km) / lon_km | |
circle_points.append((target_point[0] + d_lon, target_point[1] + d_lat)) | |
return circle_points | |