Google Maps changing placemark icon solely based on styleUrl string syntax

1k views Asked by At

I've written a simple script in python that converts a CSV file to a KML file using the simplekml package. It colors my points based on one of my data values.

My problem is this: I’m trying to use the smallest icon possible because my placemarks are very close to each other. This works well when the style ID is “icon-1739-7CB342” but fails (Google My Maps renders a drop) when the style ID is “2”

The result is that the default KML generated by simplekml cannot be rendered with the small point as I cannot control the IDs.

My code:

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()

My test input file:

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

The resulting KML (original):

<?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>

The modified KML, with a manually added style (ID = “icon-1739-7CB342”) that renders as a small dot for Nogal-2 and a drop for Nogal-1 (please note that the only difference between the styles is the 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>

The rendered image by Google My Maps. I would like to use Nogal-2's icon without having to manually add the icon-1739-7CB342 style

1

There are 1 answers

3
Christiaan Adams On

Try changing your style ID(s) to start with a letter. Per the XML spec (and therefore in KML), ID attributes cannot start with a number. If that doesn't fix it, let us know and I can take a closer look at your KML.