首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用lmxl或pykml从GE kml中提取LineString元素颜色?

如何使用lmxl或pykml从GE kml中提取LineString元素颜色?
EN

Stack Overflow用户
提问于 2021-05-16 14:18:25
回答 1查看 213关注 0票数 1

下面的GE kml文件不会直接在html代码中进行元素颜色标识。如何在csv文件中使用python扩展名及其坐标提取它们。

我已经得到了名称和坐标,但是元素的颜色还不能。提取这些信息是不可能的吗?

我使用了我在这里得到的几个代码,但元素的颜色仍然是个谜。其中之一是:

代码语言:javascript
复制
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文件,其中包含以下字段: name;坐标;color或具有相同字段的简单表。

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-17 19:45:14

我对pykml并不熟悉,但你可以这样得到你想要的:

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67557693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档