Python 中的 KML/KMZ

问题描述 投票:0回答:1

我有一个matlab图形,我想将其转换为kml/kmz,但似乎无法弄清楚,我目前有一个.csv,我使用它将每个点排序为十六进制值,我只是无法将其转换为一个 kml/kmz 代表我的一生。

这就是我到目前为止尝试做的,

    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 文件 https://drive.google.com/file/d/1Fm51NNlxdb5wCYTr8Zg9dZfx9__l6YGt/view?usp=sharing

python matlab kml kmz
1个回答
0
投票

这里发生了几个问题。

  • 首先KML中的颜色值没有写在表格中 aabbggrr。 RGB 值需要反转。
  • 第二每一行创建一个新的图标样式定义 而不是重复使用共享样式,使 KML 文件至少两次 一样大
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)])
        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.")
© www.soinside.com 2019 - 2024. All rights reserved.