1.构建外部轮廓
聚合得到以下轮廓A1
接下来是简化步骤
接下来是要素转线
接下来是要素折点转点操作
接下来添加xy坐标
如下代码可以实现将包含长度编号和方位角、起始点XY坐标的的轮廓线段生成shp文件
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
from shapely.geometry import Polygon,box,LineString
import math
from osgeo import ogr
from matplotlib.font_manager import FontProperties
import shapely
# 读取轮廓线转折点之后的shp文件
point_shp = r'C:\Users\m1959\Desktop\buildings2_all_data\4buildings2_outline\buildings2_Agg_FeatureVertic.shp'
point_gdf = gpd.read_file(point_shp)
# 提取点的几何对象
points = [point for point in point_gdf.geometry]
# 计算连续点之间的距离和方位角
distances = [points[i].distance(points[i+1]) for i in range(len(points)-1)]
# 计算连续点之间的水平夹角
angles = []
for i in range(len(points)-1):
# 计算目标方向线相对于指北方向线的夹角(顺时针方向为正)
angle = math.degrees(math.atan2(points[i+1].x - points[i].x, points[i+1].y - points[i].y))
# 将角度限制在0-360度之间
angle = (angle + 360) % 360
angles.append(angle)
# 创建多个线段并添加属性
lines = []
for i in range(len(points) - 1):
line = LineString([points[i], points[i + 1]]) # 创建线段
line_number = i + 1 # 线段的序号
line_length = line.length # 线段的长度
start_x = points[i].x # 起始点的 x 坐标
start_y = points[i].y # 起始点的 y 坐标
end_x = points[i+1].x # 终点的 x 坐标
end_y = points[i+1].y # 终点的 y 坐标
line_angle = angles[i] # 线段的方位角
line_with_attrs = {'geometry': line, 'line_number': line_number, 'length': line_length, 'angle': line_angle, 'start_x': start_x, 'start_y': start_y, 'end_x': end_x, 'end_y': end_y}
lines.append(line_with_attrs)
# 创建新的 GeoDataFrame 来保存线要素和属性
lines_gdf = gpd.GeoDataFrame(lines, crs=point_gdf.crs)
# 保存为 shp 文件
lines_gdf.to_file(r'C:\Users\m1959\Desktop\buildings2_all_data\10outline_alldata\output_lines.shp')
# 可视化
fig, ax = plt.subplots(figsize=(6, 6))
point_gdf.plot(ax=ax, color='red', markersize=5, alpha=0.7)
lines_gdf.plot(ax=ax, color='blue', label='Lines')
plt.title('Sequentially connected lines with attributes')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()
得到以下属性