【问题标题】:PyKML : appending a Placemark to more than one KML documentPyKML:将地标附加到多个 KML 文档
【发布时间】:2015-04-21 14:57:22
【问题描述】:

我正在使用 PyKML 创建几个 KML 文件,并且遇到了一些奇怪的行为,希望有人能解释一下。下面重现了这个问题:

from lxml import etree
from pykml.factory import KML_ElementMaker as KML

doc1 = KML.kml(KML.Document())
doc2 = KML.kml(KML.Document())

p = KML.Placemark()

doc1.Document.append(p)
doc2.Document.append(p)

print etree.tostring(etree.ElementTree(doc1),pretty_print=True)
print etree.tostring(etree.ElementTree(doc2),pretty_print=True)

这是输出:

<kml xmlns:gx="http://www.google.com/kml/ext/2.2"    xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">
  <Document/>
</kml>

<kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark/>
  </Document>
</kml>

地标出现在第二个文档中,但不在第一个文档中。 就好像地标一次只能附加到一个文件。

如果我将最后几行重新排列如下,一切正常。

doc1.Document.append(p)
print etree.tostring(etree.ElementTree(doc1),pretty_print=True)

doc2.Document.append(p)
print etree.tostring(etree.ElementTree(doc2),pretty_print=True)

和输出:

<kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark/>
  </Document>
</kml>

<kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark/>
  </Document>
</kml>

但这需要对我的代码进行重大重组,我希望避免这种情况。

我怀疑我遗漏了有关 PyKML、lxmlelementtree 甚至 Python 工作原理的基本知识。有人可以解释一下这里可能发生的事情吗?

【问题讨论】:

    标签: python lxml kml elementtree pykml


    【解决方案1】:

    (部分答案 - 仍然希望得到解释!)

    如果我这样做:

    from copy import deepcopy
    doc1.Document.append(deepcopy(p))
    doc2.Document.append(deepcopy(p))
    

    一切正常。但是,etree.tostring 对输入对象 doc1doc2 做了什么?就好像它们被某种方式改变了一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多