python——(pykml)

Posted 肉松蛋卷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python——(pykml)相关的知识,希望对你有一定的参考价值。

#!/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(‘ ‘, " ").split(" ")
except AttributeError, e:
try:
latlng_list = str(doc.Document.Placemark.LineString.coordinates).replace(‘ ‘, " ").split(" ")
except AttributeError, e:
latlng_list = str(doc.Folder.Placemark.LineString.coordinates).replace(‘ ‘, " ").split(" ")
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)的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python