KML/KMZ in python

191 Views Asked by At

I have a matlab figure that I want to convert to a kml/kmz and cant seem to figure it out, I currently have a .csv where I use I sorted each point to a hex value, I just can't convert it to a kml/kmz for the life of me.

This is what ive tried to do so far,

    import pandas as pd
    import simplekml

    def create_kml(data):
        kml = simplekml.Kml()

        for _, row in data.iterrows():
            latitude, longitude, altitude, rsrp, color_hex = row
            point = kml.newpoint(name=f'RSRP: {rsrp}', coords=[(longitude, latitude, altitude)])
            point.style.iconstyle.color = simplekml.Color.changealphaint(255, color_hex)
            point.style.iconstyle.scale = 1.0

        return kml

    def write_kmz(kml, output_file):
        kml.savekmz(output_file)

    if __name__ == "__main__":
        # Load data from CSV
        csv_file = "C:/Users/djtil/Desktop/EE598/all_data.csv"
        data = pd.read_csv(csv_file)

        # Output KMZ file
        output_file = "output.kmz"

        # Create KML object
        kml = create_kml(data)

        # Write to KMZ file
        write_kmz(kml, output_file)

        print(f"KMZ file '{output_file}' created successfully.")

MatLab Figure

CSV File https://drive.google.com/file/d/1Fm51NNlxdb5wCYTr8Zg9dZfx9__l6YGt/view?usp=sharing

1

There are 1 best solutions below

0
On

Three issues are happening here:

  1. Color values in KML are not being written in the form aabbggrr. The RGB values need to be reversed to be in the correct order.
  2. Each row is creating a new icon style definition rather than reuse a shared style making the KML file at least twice as large
  3. Each point is being created as clamp to ground and need to set altitude mode = "absolute" to use the height
import pandas as pd
import simplekml

styles = {}

def create_kml(data):
    kml = simplekml.Kml()    
    for _, row in data.iterrows():
        latitude, longitude, altitude, rsrp, color_hex = row
        style = styles.get(color_hex)
        if style is None:
            style = simplekml.Style()
            style.iconstyle.color = "ff" + color_hex[::-1]
            style.iconstyle.scale = 1.0
            styles[color_hex] = style
        point = kml.newpoint(description=f'RSRP: {rsrp}',
                            coords=[(longitude, latitude, altitude)],
                            altitudemode=simplekml.AltitudeMode.absolute)
        point.style = style
    return kml

def write_kmz(kml, output_file):
    kml.savekmz(output_file)

if __name__ == "__main__":
    # Load data from CSV
    csv_file = "all_data.csv"
    data = pd.read_csv(csv_file)

    # Output KMZ file
    output_file = "output.kmz"

    # Create KML object
    kml = create_kml(data)

    # Write to KMZ file
    write_kmz(kml, output_file)

    print(f"KMZ file '{output_file}' created successfully.")

Note setting the name in placemark clutters the map so set the name to the description.

Here's the data displayed in Google Earth Pro which matches the matlab image with green and orange colors at top and reds at the bottom.

Screenshot of KML in Google Earth Pro