如何使用 lmxl 或 pykml 从 GE kml 中提取 LineString 元素颜色?
How to extract a LineString element color from a GE kml by using lmxl or pykml?
下面的GE kml文件没有直接在html代码中进行元素颜色识别。我如何将它们连同名称及其坐标以 python 扩展名提取到 csv 文件中。
我已经得到了名称和坐标,但是元素颜色还没有。无法提取此信息吗?
我使用了这里得到的几个代码,但元素的颜色仍然是个谜。其中之一是:
from pykml import parser
from pykml.factory import nsmap
namespace = {"ns": nsmap[None]}
with open('test.kml') as f:
root = parser.parse(f).getroot()
pms = root.xpath(".//ns:Placemark[.//ns:LineString]", namespaces=namespace)
for pm in pms:
print(pm.name)
print(pm.LineString.coordinates)
预期结果是一个包含以下字段的 csv 文件:名称;坐标;颜色或具有相同字段的简单 table。
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>test.kml</name>
<StyleMap id="inline93460">
<Pair>
<key>normal</key>
<styleUrl>#inline169131</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#inline37027</styleUrl>
</Pair>
</StyleMap>
<Style id="inline8348">
<LineStyle>
<color>ff00ffff</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<StyleMap id="inline7838">
<Pair>
<key>normal</key>
<styleUrl>#inline7066</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#inline7746</styleUrl>
</Pair>
</StyleMap>
<Style id="inline37027">
<LineStyle>
<color>ff00ffff</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Style id="inline7746">
<LineStyle>
<color>ffff0000</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Style id="inline7066">
<LineStyle>
<color>ffff0000</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<StyleMap id="inline55590">
<Pair>
<key>normal</key>
<styleUrl>#inline8348</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#inline5777</styleUrl>
</Pair>
</StyleMap>
<Style id="inline5777">
<LineStyle>
<color>ff00ffff</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Style id="inline169131">
<LineStyle>
<color>ff00ffff</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Folder>
<name>Cidade de Esmeraldas</name>
<open>1</open>
<Folder>
<name>Aldeias do lago</name>
<open>1</open>
<Placemark>
<name>Medida do caminho</name>
<styleUrl>#inline93460</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
-44.18468786850732,-19.7311490448918,0 -44.18476571351906,-19.73196615345114,0 -44.18516928457656,-19.73196209518785,0
</coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Medida do caminho</name>
<styleUrl>#inline7838</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
-44.18116177806758,-19.73104199822188,0 -44.18149124079289,-19.73002254245169,0 -44.18093826107535,-19.73172738045029,0 -44.1809508776952,-19.73186017013899,0 -44.18160756352079,-19.73204395759025,0
</coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Medida do caminho</name>
<styleUrl>#inline55590</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
-44.19110137765401,-19.72300828552742,0 -44.19173625397613,-19.72303182576567,0 -44.19283033803023,-19.72258037041406,0 -44.19316612473877,-19.72323246634359,0
</coordinates>
</LineString>
</Placemark>
</Folder>
</Folder>
</Document>
</kml>
我对pykml不熟悉,但是你可以这样得到你想要的:
from lxml import etree
import pandas as pd
ns = {"xx":"http://www.opengis.net/kml/2.2"}
rows =[]
for su in root.xpath('//xx:Placemark/xx:styleUrl',namespaces=ns):
name = su.xpath('./preceding-sibling::xx:name/text()',namespaces=ns)[0]
coo = su.xpath('./following-sibling::xx:LineString/xx:coordinates/text()',namespaces=ns)[0].strip()
sm = su.text.replace("#","")
sy = doc.xpath(f'//xx:StyleMap[@id="{sm}"]//xx:Pair[./xx:key["normal"]]/xx:styleUrl',namespaces=ns)[0].text.replace("#","")
color = doc.xpath(f'//xx:Style[@id="{sy}"]//xx:LineStyle/xx:color/text()',namespaces=ns)[0]
rows.append([name,coo,color])
columns = ['name','coordinates','color']
df = pd.DataFrame(rows,columns=columns)
df
输出:
name coordinates color
0 Medida do caminho -44.18468786850732,-19.7311490448918,0 -44.184... ff00ffff
1 Medida do caminho -44.18116177806758,-19.73104199822188,0 -44.18... ffff0000
2 Medida do caminho -44.19110137765401,-19.72300828552742,0 -44.19... ff00ffff
下面的GE kml文件没有直接在html代码中进行元素颜色识别。我如何将它们连同名称及其坐标以 python 扩展名提取到 csv 文件中。
我已经得到了名称和坐标,但是元素颜色还没有。无法提取此信息吗?
我使用了这里得到的几个代码,但元素的颜色仍然是个谜。其中之一是:
from pykml import parser
from pykml.factory import nsmap
namespace = {"ns": nsmap[None]}
with open('test.kml') as f:
root = parser.parse(f).getroot()
pms = root.xpath(".//ns:Placemark[.//ns:LineString]", namespaces=namespace)
for pm in pms:
print(pm.name)
print(pm.LineString.coordinates)
预期结果是一个包含以下字段的 csv 文件:名称;坐标;颜色或具有相同字段的简单 table。
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>test.kml</name>
<StyleMap id="inline93460">
<Pair>
<key>normal</key>
<styleUrl>#inline169131</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#inline37027</styleUrl>
</Pair>
</StyleMap>
<Style id="inline8348">
<LineStyle>
<color>ff00ffff</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<StyleMap id="inline7838">
<Pair>
<key>normal</key>
<styleUrl>#inline7066</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#inline7746</styleUrl>
</Pair>
</StyleMap>
<Style id="inline37027">
<LineStyle>
<color>ff00ffff</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Style id="inline7746">
<LineStyle>
<color>ffff0000</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Style id="inline7066">
<LineStyle>
<color>ffff0000</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<StyleMap id="inline55590">
<Pair>
<key>normal</key>
<styleUrl>#inline8348</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#inline5777</styleUrl>
</Pair>
</StyleMap>
<Style id="inline5777">
<LineStyle>
<color>ff00ffff</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Style id="inline169131">
<LineStyle>
<color>ff00ffff</color>
<width>2</width>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Folder>
<name>Cidade de Esmeraldas</name>
<open>1</open>
<Folder>
<name>Aldeias do lago</name>
<open>1</open>
<Placemark>
<name>Medida do caminho</name>
<styleUrl>#inline93460</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
-44.18468786850732,-19.7311490448918,0 -44.18476571351906,-19.73196615345114,0 -44.18516928457656,-19.73196209518785,0
</coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Medida do caminho</name>
<styleUrl>#inline7838</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
-44.18116177806758,-19.73104199822188,0 -44.18149124079289,-19.73002254245169,0 -44.18093826107535,-19.73172738045029,0 -44.1809508776952,-19.73186017013899,0 -44.18160756352079,-19.73204395759025,0
</coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Medida do caminho</name>
<styleUrl>#inline55590</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
-44.19110137765401,-19.72300828552742,0 -44.19173625397613,-19.72303182576567,0 -44.19283033803023,-19.72258037041406,0 -44.19316612473877,-19.72323246634359,0
</coordinates>
</LineString>
</Placemark>
</Folder>
</Folder>
</Document>
</kml>
我对pykml不熟悉,但是你可以这样得到你想要的:
from lxml import etree
import pandas as pd
ns = {"xx":"http://www.opengis.net/kml/2.2"}
rows =[]
for su in root.xpath('//xx:Placemark/xx:styleUrl',namespaces=ns):
name = su.xpath('./preceding-sibling::xx:name/text()',namespaces=ns)[0]
coo = su.xpath('./following-sibling::xx:LineString/xx:coordinates/text()',namespaces=ns)[0].strip()
sm = su.text.replace("#","")
sy = doc.xpath(f'//xx:StyleMap[@id="{sm}"]//xx:Pair[./xx:key["normal"]]/xx:styleUrl',namespaces=ns)[0].text.replace("#","")
color = doc.xpath(f'//xx:Style[@id="{sy}"]//xx:LineStyle/xx:color/text()',namespaces=ns)[0]
rows.append([name,coo,color])
columns = ['name','coordinates','color']
df = pd.DataFrame(rows,columns=columns)
df
输出:
name coordinates color
0 Medida do caminho -44.18468786850732,-19.7311490448918,0 -44.184... ff00ffff
1 Medida do caminho -44.18116177806758,-19.73104199822188,0 -44.18... ffff0000
2 Medida do caminho -44.19110137765401,-19.72300828552742,0 -44.19... ff00ffff