使用pyKML解析KML文档 [英] Using pyKML to parse KML Document

查看:873
本文介绍了使用pyKML解析KML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pyKML模块从给定的KML文件中提取坐标.

I'm using the pyKML module for extracting coordinates from a given KML file.

我的Python代码如下:

My Python code is as follows:

from pykml import parser
fileobject = parser.fromstring(open('MapSource.kml', 'r').read())
root = parser.parse(fileobject).getroot()
print(xml.Document.Placemark.Point.coordinates)

但是,在运行它时,出现以下错误:

However, on running this, I get the following error:

ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

在寻找解决方案时,我遇到了这个解决方案 http ://twigstechtips.blogspot.in/2013/06/python-lxml-strings-with-encoding.html 从我尝试过的位置(我不确定这是正确的方法):

Looking for solutions, I came across this solution http://twigstechtips.blogspot.in/2013/06/python-lxml-strings-with-encoding.html from where I've tried this (which I'm not sure is the correct method):

from pykml import parser
from lxml import etree
from os import path
kml_file = open('MapSource.kml', 'r')
parser = etree.XMLParser(recover=True)
xml = etree.fromstring(kml_file, parser)
print(xml.Document.Placemark.Point.coordinates)

这给了我ValueError: can only parse strings.我解析KML并获取该结构处的坐标的正确方法是什么?

This gives me ValueError: can only parse strings. What is the correct way for me to parse the KML and get the coordinates at that structure?

推荐答案

在上面的示例中,root = parser.parse(fileobject).getroot()在文件内容上调用parse()作为从上一行的fromstring()函数返回的字符串.

In above example, root = parser.parse(fileobject).getroot() is calling parse() on file contents as a string returned from fromstring() function from the previous line.

有两种方法可以使用pyKML解析KML文件:

There are two methods to parse a KML file using pyKML:

1:使用parse.parse()解析文件.

1: Using parse.parse() to parse the file.

from pykml import parser
with open('MapSource.kml', 'r') as f:
  root = parser.parse(f).getroot()
print(root.Document.Placemark.Point.coordinates)

2:使用parse.parsestring()解析字符串内容.

2: Using parse.parsestring() to parse the string contents.

from pykml import parser
root = parser.fromstring(open('MapSource.kml', 'rb').read())
print(root.Document.Placemark.Point.coordinates)

如果KML文件的第一行采用非UTF8编码,并且XML序言标头为XML,并且尝试以二进制格式读取"r"作为文本而"rb"的文件,则方法#2可能会失败.

Method #2 can fail if the KML file has an XML prolog header as the first line with non-UTF8 encoding and try to read the file with 'r' as text vs 'rb' for binary format.

如果在KML文档中未正确指定编码,则注释解析可能会失败.由于名称和说明中使用国际和图形字符,因此在下面的示例中使用了ISO-8859-1编码.省略编码或使用"UTF-8".会使其成为无效的XML文件.

Note parsing can fail if the encoding is incorrectly specified in the KML document. ISO-8859-1 encoding is used in example below because of the international and graphic characters in the name and description. Omitting the encoding or using ""UTF-8" would make it an invalid XML file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
  <Placemark>
    <name>Río Grande</name> 
    <description>
      Location: 18° 22′ 49″ N, 65° 49′ 53″ W
    </description>
    ...

这篇关于使用pyKML解析KML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆