python——(pykml)

#!/usr/bin/env python# encoding: utf-8‘‘‘Module DescriptionCreated on Jul 22, 2019@author: user@change: Jul 22, 2019 user: initialization‘‘‘from lxml import etree  #将KML节点输出为字符串from pykml import parserfrom pykml.factory import KML_ElementMaker as KMLfrom pykml.factory import ATOM_ElementMaker as ATOMfrom pykml.factory import GX_ElementMaker as GXclass 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_listexcept Exception, e:return line_listfor 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_listif __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":