0
我環顧了一下,似乎無法找到解決我的問題的方法。我的根本問題是,我需要找到所有KML元素的名稱,其子麪包含具有給定緯度/經度的點。如何在python(lxml)中過濾後訪問kml/xml屬性?
環顧四周我發現使用keytree,shapely和lxml我可以將所有KML元素過濾到有問題的多邊形,然後訪問其父母。但是,當我嘗試訪問父級的屬性時,我總是收到一個空列表。我試過以下內容:
def __init__(self):
root=etree.fromstring(open("Example.kml", "r").read())
kmlns = root.tag.split("}")[0][1:]
polygons=root.findall(".//{%s}Polygon"%kmlns)
p = Point(-128.1605,52.474) #this point exists in one of the polygons
hits = filter(
lambda e: shape(keytree.geometry(e)).contains(p),
polygons)
print hits
hit_parent=hits[0].getparent()
print hit_parent.attrib#this prints {}
我能夠通過pycharm中的調試器找到多邊形所在的行;根據這一點,hits [0]有一個源代碼行屬性,當我在我的KML文檔中找到該行號時,該多邊形確實包含該點。滾動到多邊形的父級,我發現它有屬性(即不是空列表)。我是xml和kml解析的新手;我看錯了地方?下面是從KML多邊形與其父:
<Placemark>
<name>THIS IS THE NAME</name>
<visibility>0</visibility>
<styleUrl>#falseColor184010</styleUrl>
<ExtendedData>
<SchemaData schemaUrl="#S_AL_TA_BC_2_41_eng_SSSSISSSSSSSSSSSSSSSSSSSSS10">
<SimpleData name="ACQTECH">Computed</SimpleData>
<SimpleData name="METACOVER">Partial</SimpleData>
<SimpleData name="CREDATE">20030416</SimpleData>
<SimpleData name="REVDATE">20130504</SimpleData>
<SimpleData name="ACCURACY">-1</SimpleData>
<SimpleData name="PROVIDER">Federal</SimpleData>
<SimpleData name="DATASETNAM">BC</SimpleData>
<SimpleData name="SPECVERS">1.1</SimpleData>
<SimpleData name="NID">7103157bba3511d892e2080020a0f4c9</SimpleData>
<SimpleData name="ALCODE">07876</SimpleData>
<SimpleData name="LANGUAGE1">English</SimpleData>
<SimpleData name="NAME1">NEEKAS 4</SimpleData>
<SimpleData name="LANGUAGE2">French</SimpleData>
<SimpleData name="NAME2">NEEKAS NO 4</SimpleData>
<SimpleData name="LANGUAGE3">No Language</SimpleData>
<SimpleData name="NAME3">NULL</SimpleData>
<SimpleData name="LANGUAGE4">No Language</SimpleData>
<SimpleData name="NAME4">NULL</SimpleData>
<SimpleData name="LANGUAGE5">No Language</SimpleData>
<SimpleData name="NAME5">NULL</SimpleData>
<SimpleData name="JUR1">BC</SimpleData>
<SimpleData name="JUR2"></SimpleData>
<SimpleData name="JUR3"></SimpleData>
<SimpleData name="JUR4"></SimpleData>
<SimpleData name="ALTYPE">Indian Reserve</SimpleData>
<SimpleData name="WEBREF">http://clss.nrcan.gc.ca/map-carte/mapbrowser-navigateurcartographique-eng.php?cancode=07876</SimpleData>
</SchemaData>
</ExtendedData>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-128.1615722,52.47385589999999,0 -128.1618475,52.47338730000003,0 -128.1623126999999,52.47275560000004,0 -128.1622705,52.47253640000001,0 -128.162017,52.47243320000002,0 -128.1619326,52.4722527,0 -128.1618904,52.4721108,0 -128.161827,52.47202060000003,0 -128.1615523,52.47204629999998,0 -128.1613199,52.47211069999996,0 -128.1607705,52.47205899999999,0 -128.1604538,52.47172369999999,0 -128.1600750999999,52.47149440000001,0 -128.1600821,52.47510580000001,0 -128.1615621,52.47510469999996,0 -128.1615294999999,52.474926,0 -128.1615508,52.47452629999999,0 -128.1615298,52.47416529999997,0 -128.1615722,52.47385589999999,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
我想「這是NAME」從多邊形的父。
非常感謝! –