0

我在 python 中编写了一个简单的脚本,它使用 simplekml 包将 CSV 文件转换为 KML 文件。它根据我的数据值之一为我的点着色。

我的问题是:我正在尝试使用尽可能小的图标,因为我的地标彼此非常接近。这在样式 ID 为“icon-1739-7CB342”时效果很好,但在样式 ID 为“2”时失败(Google 我的地图呈现下降)</p>

结果是 simplekml 生成的默认 KML 无法用小点渲染,因为我无法控制 ID。

我的代码:

import simplekml
import pandas
import sys

infile = sys.argv[1]
outfile = infile.split(".")[0]+".kml"


def genkml():
    kml = simplekml.Kml()
    ok = simplekml.Style()
    ok.iconstyle.color = 'ff42b37c'  # Green
    ok.iconstyle.icon.href = 'https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png'
    ok.iconstyle.scale = 1

    rc = simplekml.Style()
    rc.iconstyle.color = 'ff00eaff'  # Yellow
    rc.iconstyle.icon.href = 'https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png'
    rc.iconstyle.scale = 1

    seco = simplekml.Style()
    seco.iconstyle.color = 'ff0051e6'  # Red
    seco.iconstyle.icon.href = 'https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png'
    seco.iconstyle.scale = 1
    
    df = pandas.read_csv(infile)
    cuenta = 1
    for lon, lat, desc in zip(df["longitude"], df["latitude"], df["desc"]):
        nogal = kml.newpoint()
        nogal.name = "Nogal"+"-"+str(cuenta)
        nogal.description = desc.upper()
        nogal.coords = [(lon, lat)]
        if "OK" in nogal.description:
            nogal.style = ok
        if "RC" in nogal.description:
            nogal.style = rc
        if "SECO" in nogal.description:
            nogal.style = seco
        cuenta += 1
    kml.save(outfile)
    print(kml.kml())

if __name__ == "__main__":
    genkml()

我的测试输入文件:

type,date time,latitude,longitude,accuracy(m),altitude(m),geoid_height(m),speed(m/s),bearing(deg),sat_used,sat_inview,name,desc
W,2021-05-30 21:08:27,28.66231833,-100.83812667,1,257.122,-23.722,0.000,,28,33,,Q31 ok
W,2021-05-30 21:11:56,28.66243667,-100.83811000,1,256.922,-23.722,0.000,,26,35,,O32 ok

生成的 KML(原始):

<?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">
    <Document id="1">
        <Style id="2">
            <IconStyle id="3">
                <color>ff42b37c</color>
                <colorMode>normal</colorMode>
                <scale>1</scale>
                <heading>0</heading>
                <Icon id="4">
                    <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Placemark id="12">
            <name>Nogal-1</name>
            <description>Q31 OK</description>
            <styleUrl>#2</styleUrl>
            <Point id="11">
                <coordinates>-100.83812667,28.66231833,0.0</coordinates>
            </Point>
        </Placemark>
        <Placemark id="14">
            <name>Nogal-2</name>
            <description>Q32 OK</description>
            <styleUrl>#2</styleUrl>
            <Point id="13">
                <coordinates>-100.83811,28.66243667,0.0</coordinates>
            </Point>
        </Placemark>
    </Document>
</kml>

修改后的 KML,具有手动添加的样式 (ID = “icon-1739-7CB342”),对于 Nogal-2 呈现为一个小点,对于 Nogal-1 呈现为一个水滴(请注意,样式之间的唯一区别是 ID ):

<?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">
    <Document id="1">
        <Style id="2">
            <IconStyle>
                <color>ff42b37c</color>
                <scale>1</scale>
                <Icon>
                    <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="icon-1739-7CB342">
            <IconStyle>
                <color>ff42b37c</color>
                <scale>1</scale>
                <Icon>
                    <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Placemark id="12">
            <name>Nogal-1</name>
            <description>Q31 OK</description>
            <styleUrl>#2</styleUrl>
            <Point id="11">
                <coordinates>-100.83812667,28.66231833,0.0</coordinates>
            </Point>
        </Placemark>
        <Placemark id="14">
            <name>Nogal-2</name>
            <description>Q32 OK</description>
            <styleUrl>#icon-1739-7CB342</styleUrl>
            <Point id="13">
                <coordinates>-100.83811,28.66243667,0.0</coordinates>
            </Point>
        </Placemark>
    </Document>
</kml>

由 Google 我的地图渲染的图像。我想使用 Nogal-2 的图标,而不必手动添加 icon-1739-7CB342 样式

4

1 回答 1

0

尝试将您的样式 ID 更改为以字母开头。根据 XML 规范(因此在 KML 中),ID 属性不能以数字开头。如果这不能解决问题,请告诉我们,我可以仔细查看您的 KML。

于 2021-05-31T16:37:44.153 回答