码迷,mamicode.com
首页 > 编程语言 > 详细

python——(pykml)

时间:2020-03-27 13:03:46      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:initial   document   item   use   lis   elf   atl   auth   pre   

#!/usr/bin/env python
# encoding: utf-8
‘‘‘
Module Description

Created on Jul 22, 2019
@author: user
@change: Jul 22, 2019 user: initialization
‘‘‘
from lxml import etree #KML节点输出为字符串
from pykml import parser
from pykml.factory import KML_ElementMaker as KML
from pykml.factory import ATOM_ElementMaker as ATOM
from pykml.factory import GX_ElementMaker as GX

class operation_kml():
def __init__(self):
pass

def create_kml(self, coordnation, kml_line_style_info , line_folder_info):
‘‘‘
@summary: create a factory object that can create elements in the KML namespace
‘‘‘

kml_document = KML.Document()

#style content
for s_d in kml_line_style_info:
each_style = KML.Style(
KML.LineStyle(
KML.color(s_d["color"]),
KML.width(s_d["width"]),
)
)
kml_document.append(each_style)

#point content

#line content

for f_data in line_folder_info:
each_folder = KML.Folder(
KML.name(f_i["name"]),
KML.Placemark(
KML.name(f_i["Placemark"]["name"]),
KML.description(f_i["Placemark"]["description"]),
KML.styleUrl(f_i["Placemark"]["styleUrl"]),
KML.LineString(
KML.coordinates()
)
)
)


kml_document.append(f_object)

fld = KML.KML(kml_document)
#write to kml file
content = etree.tostring(fld, pretty_print=True)
with open(‘./gen.kml‘, ‘w‘) as fp:
fp.write(‘<?xml version = "1.0" encoding = "UTF-8"?>‘)
fp.write(content)

def parse_kml(self, kml_file):
‘‘‘
@summary: parse single kml
@param kml_file: the kml file
@return: return gps data
‘‘‘
line_list = []
with open(kml_file) as f:
doc = parser.parse(f).getroot()

# find all the gps data
try:
latlng_list = str(doc.Placemark.LineString.coordinates).replace(‘ ‘, "\n").split("\n")
except AttributeError, e:
try:
latlng_list = str(doc.Document.Placemark.LineString.coordinates).replace(‘ ‘, "\n").split("\n")
except AttributeError, e:
latlng_list = str(doc.Folder.Placemark.LineString.coordinates).replace(‘ ‘, "\n").split("\n")
except Exception, e:
return line_list
except Exception, e:
return line_list

for item in latlng_list:
latlng = item.split(‘,‘)

# check the gps data is ok
if len(latlng) == 3:
# remove the height and exchange the lat and lng
# change the str to float
tmp_latlng = [float(latlng[:2][::-1][0]), float(latlng[:2][::-1][1])]
line_list.append(tmp_latlng)

return line_list


if __name__ == ‘__main__‘:
p_k = operation_kml()
coordnation = [[-1.549013392526688, 52.23895359430604, 0], [-1.54900158540925, 52.23894827935942, 0],
[-1.548989778291812, 52.2389429644128, 0], [-1.548977971174375, 52.23893764946618, 0],
[-1.548966164056937, 52.23893233451956, 0],
[-1.548954356939499, 52.23892701957294, 0], [-1.548942549822061, 52.23892170462633, 0],
[-1.548930742704624, 52.23891638967971, 0], [-1.548918935587186, 52.23891107473309, 0],
[-1.548907128469748, 52.23890575978647, 0], [-1.548895321352311, 52.23890044483985, 0]]
kml_line_style_info = [{"id": "line_g", "color": "ff30ffb5", "width": 2},
{"id": "line_p", "color": "ff5a00ff", "width": 2},
{"id": "error_line_p", "color": "ffff4cb2", "width": 2}
]

folder_info = [{"name": "type_info",
"Folder": {"name": "0.5",
"Folder": {"name": "error_0",
"Placemark": {"name": "", "description": "", "styleUrl":"",
"LineString": {"coordinates": ["-83.2386594234,42.6417507166,0", "-83.2386595741,42.641746077,0", "-83.2386597247,42.6417414376,0"]}}}}},
{"name": "color_error", "Folder": [{"name": "0.5"}, {}]}

]

p_k.create_kml(coordnation, kml_line_style_info, folder_info)

python——(pykml)

标签:initial   document   item   use   lis   elf   atl   auth   pre   

原文地址:https://www.cnblogs.com/ting152/p/12580537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!