本文主要是介绍基于kml线路生成路线规划文档,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景
在野外工程采集中,一线工作人员为了记录所在方位与坐标,一般会对相关点做标记,最终导出成一个kml格式的文件,该文件记录了一系列工程点和路线,很多工程报告需要根据这些线路来对周边环境进行描述,如高压线路的铺设,野外测绘等,比较严谨的方案是结合最新的影像地图以及其它实测数据和规划数据,由相关人员进行撰写,最近在研究自动化的过程,手里的数据比较少,因此仅仅实现一个思路。
读取kml
目的是将kml读取成可以解析的坐标
# 递归提取LineString点坐标
def extract_point_coordinates(element):"""递归函数,用于提取LineString的坐标。"""points = []if element.tag.endswith('LineString'):coords_text = element.find('kml:coordinates', ns).textcoords_pairs = coords_text.strip().split(' ')points = [(float(lat), float(lon)) for lon, lat, _ in (coords_pair.split(',') for coords_pair in coords_pairs)]return points# 对于MultiGeometry或Placemark,递归搜索其中的LineStringfor child in element:points += extract_point_coordinates(child)return points
查找相关POI
一个测量点也可以被看作一个拐点,即线路走到这里会发生改变,因此需要找到附近的地物点及方向,以便加以描述
#寻找周边地物
def searcg_nearby_places(lon,lat,tk):"""通过经纬度和地物类型搜索周边地物"""base_url = 'http://api.tianditu.gov.cn/geocoder'params = {"postStr":json.dumps({'lon':lon,'lat':lat,'ver':1}),"type":"geocode","tk":tk}response = requests.get(base_url,params=params)if response.status_code == 200:return response.json()else:return None
通过poi能拿到对应点附近的地物信息,如经纬度,完整描述,所在城市、县区、最近点、最近点方位、到最近点距离等,但是还需要一个最终要的信息,就是路线的走线,及经过该点以后往哪个方向走。
走向可以用该点和下一个点的经纬度进行计算
#计算方向
def calculate_direction(point,next_point):"""计算两点之间的方向"""lat1,lon1 = pointlat2,lon2 = next_pointdLon = lon2 - lon1y = math.sin(dLon) * math.cos(lat2)x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dLon)brng = math.atan2(y, x)brng = math.degrees(brng)brng = (brng + 360) % 360if brng > 337.5 or brng <= 22.5:return '正北'elif brng > 22.5 and brng <= 67.5:return '东北'elif brng > 67.5 and brng <= 112.5:return '正东'elif brng > 112.5 and brng <= 157.5:return '东南'elif brng > 157.5 and brng <= 202.5:return '正南'elif brng > 202.5 and brng <= 247.5:return '西南'elif brng > 247.5 and brng <= 292.5:return '正西'else:return '西北'
创建语句字典
需要对每个途径点创建语句描述:常见的语句描述有:
- through_dict = [“途经”,“经过”,“行至”]
- arrive_dict=[‘到达{}{}侧后,开始转向{}走线,’,‘到达{}{}区域,转向{}方向,继续走线,’]
- (‘最终走出{}。\n线路进入{}后向{}走线’)
- ‘{}{}{}侧,继续向{}方向走线,’
- 对于每个途径点,先判断是否已经不在该市区,如果不在,则描述离开该地区,转向其他地区
根据poi信息依次读取每个途径点,生成相关的语句描述
for i in range(len(results)-1) :if i == 0:direction = results[i]['走向']str+= "向{}出线,".format(direction)pos = results[i]next_pos = results[i+1]county = pos['所在县区']next_county = next_pos['所在县区']current_city = countydescription = pos['描述']next_description = next_pos['描述']direction = pos['走向']next_direction = next_pos['走向']address_position = pos['相对于最近点方向']next_address_position = next_pos['相对于最近点方向']city = pos['所在城市']next_city = next_pos['所在城市']road = pos['最近道路']next_road = next_pos['最近道路']# road_direction = pos['相对于最近道路方向']# next_road_direction = next_pos['相对于最近道路方向']detail_desp = handler.extract_between(county,address_position,description)#考虑线路是否离开本地区if county != next_county:str+=('最终走出{}。\n线路进入{}后向{}走线。'.format(city+county,next_city+next_county,next_direction))#如果地区一样,则考虑道路# elif road == next_road and road_direction != next_road_direction:# str+=('向{}跨越{}后向{}走线,'.format(direction,road,next_direction)) #最后考虑途径点elif current_city == county:if direction == next_direction:str+=('{}{}{}侧,继续向{}方向走线,'.format(through_dict[i%3],detail_desp,address_position,direction))else:str+=(arrive_dict[i%2].format(detail_desp,address_position,direction))elif current_city != county:str+=('{}{}{}侧,向{}方向走线,'.format(through_dict[i%3],detail_desp,address_position,direction))
str+= "最终接入******构架。"
生成word文档
#创建文档对象
doc = Document()
paragraphs = str.split('\n')for para in paragraphs:paragraph = doc.add_paragraph()paragraph.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY # 设置两端对齐paragraph.paragraph_format.first_line_indent = Pt(24) # 设置首行缩进paragraph.paragraph_format.line_spacing_rule = WD_LINE_SPACING.SINGLE # 设置单倍行距run = paragraph.add_run(' ' + para)run.font.size = Pt(12) # 设置字体大小为小四run.font.name = '宋体' # 设置字体为宋体
doc.save('result.docx')
最终生成了一篇三千多字文档,内容部分如下:
线路进入惠州市惠东县后向正东走线。到达多祝镇林氏祠堂东南区域,转向正东方向,继续走线,到达多祝镇大坪西北侧后,开始转向东南走线,到达多祝镇上横坑塘东南区域,转向正南方向,继续走线,到达多祝镇9棵松西北侧后,开始转向正北走线,到达多祝镇9棵松西北区域,转向正东方向,继续走线,行至多祝镇竹盐岗西北侧,继续向东南方向走线,到达多祝镇谷离山西南区域,转向东南方向,继续走线,经过多祝镇合掌山东南侧,继续向正东方向走线,最终走出惠州市惠东县。
这篇关于基于kml线路生成路线规划文档的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!