64 lines
2.7 KiB
Python
64 lines
2.7 KiB
Python
from xml.dom import minidom
|
|
import numpy as np
|
|
import transformer
|
|
import pandas as pd
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
def prettify(elem):
|
|
"""Return a pretty-printed XML string for the Element.
|
|
"""
|
|
rough_string = ET.tostring(
|
|
elem, encoding="unicode", method="xml", xml_declaration=True
|
|
)
|
|
reparsed = minidom.parseString(rough_string)
|
|
return reparsed.toprettyxml(indent="\t", encoding="utf-8")
|
|
|
|
|
|
def main():
|
|
df = pd.read_excel("杆塔成果表(左侧)-复测.xls")
|
|
needed_df = df.iloc[:85, :]
|
|
trans = transformer.Projector(transformer.EPSG_Xian_1980_114E, transformer.EPSG_WGS_84)
|
|
y_array = needed_df["北坐标"].to_numpy()
|
|
x_array = needed_df["东坐标"].to_numpy()
|
|
yx_array = np.array([y_array, x_array]).transpose()
|
|
wgs_84_point = np.array([trans.transform(y, x) for y, x in yx_array])
|
|
name_lat_lon_df = pd.DataFrame(
|
|
{"桩号": needed_df["杆塔"], "纬度": wgs_84_point[:, 0], "经度": wgs_84_point[:, 1]}
|
|
)
|
|
ET.register_namespace("", "http://www.opengis.net/kml/2.2")
|
|
ns = {
|
|
"NS": r"http://www.opengis.net/kml/2.2",
|
|
}
|
|
xml_root = ET.Element("Document")
|
|
ET.SubElement(xml_root, "name").text = "施工图路径"
|
|
point_folder = ET.SubElement(xml_root, "Folder")
|
|
ET.SubElement(point_folder, "name").text = "杆塔位置"
|
|
line_coordination_list = []
|
|
for _, row in name_lat_lon_df.iterrows():
|
|
name = row["桩号"]
|
|
lon = row["经度"]
|
|
lat = row["纬度"]
|
|
ele_placemark = ET.SubElement(point_folder, "Placemark")
|
|
ele_name = ET.SubElement(ele_placemark, "name")
|
|
ele_name.text = name
|
|
ele_point = ET.SubElement(ele_placemark, "Point")
|
|
point_coordinates = ET.SubElement(ele_point, "coordinates")
|
|
point_coordinates.text = "{lon},{lat},0".format(lon=lon, lat=lat)
|
|
line_coordination_list.append([lat, lon, 0])
|
|
line_folder = ET.SubElement(xml_root, "Folder")
|
|
ET.SubElement(line_folder, "name").text = "线路路径"
|
|
line_placemark = ET.SubElement(line_folder, "Placemark")
|
|
ET.SubElement(line_placemark, "name").text = "线路路径"
|
|
line_style = ET.SubElement(ET.SubElement(line_placemark, "Style"), "LineStyle")
|
|
ET.SubElement(line_style, "color").text = "ff0000ff"
|
|
ET.SubElement(line_style, "width").text = "2"
|
|
line_string = ET.SubElement(line_placemark, "LineString")
|
|
line_coordinates = ET.SubElement(line_string, "coordinates")
|
|
line_coordinates.text = " ".join(
|
|
"{lon},{lat},{elevation}".format(lon=foo[1], lat=foo[0], elevation=foo[2])
|
|
for foo in line_coordination_list
|
|
)
|
|
with open("周口.kml", "w", encoding="utf-8") as f:
|
|
f.write(prettify(xml_root).decode("utf-8"))
|