[Python] How to plot data from a .kml file using matplotlib on python 3.7 and Windows 10"?

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I will first give a little bit of context to my problem.

    I have obtained a .kml file of the territorial seas around the world on this site, and I would like to display it not on Google Earth but on a matplotlib.pyplot plot (with a cartopy map if possible too). The .kml file looks like this:

    <?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">
    <NetworkLink>
    <name>Territorial Seas (12NM) v3</name>
    <description><![CDATA[Flanders Marine Institute (2019). Maritime Boundaries Geodatabase: Territorial Seas (12NM), version 3. Available online at <a href="http://www.marineregions.org">http://www.marineregions.org</a> https://doi.org/10.14284/387. Consulted on YYYY-MM-DD.]]></description>
    <Link>
    <href>http://geo.vliz.be/geoserver/gwc/service/kml/MarineRegions:eez_12nm.png.kml</href>
    </Link>
    </NetworkLink>
    </kml>


    For that I saws on this other StackOverflow question that using fastkml to read the file was possible.

    So this is the test.py code I am trying to run (it comes from the usage guide):

    from fastkml import kml

    filename = "C:\\Users\\dumasal\\Documents\\GOOGLE_EARTH\\MarineRegions-eez_12nm.kml"
    with open(filename, 'rt', encoding="utf-8") as myfile:
    doc=myfile.read()
    print(doc)

    # Create the KML object to store the parsed result
    k = kml.KML()

    # Read in the KML string
    k.from_string(doc)
    print('k = ', k)

    ### Next we perform some simple sanity checks ###

    # Check that the number of features is correct
    # This corresponds to the single ``Document``
    features = list(k.features())
    print(len(features))

    # Check that we can access the features as a generator
    # (The two Placemarks of the Document)
    print(features[0].features())
    f2 = list(features[0].features())
    print(len(f2))

    # Check specifics of the first Placemark in the Document
    print(f2[0])
    print(f2[0].description)
    print(f2[0].name)

    # Check specifics of the second Placemark in the Document
    print(f2[1].name)
    f2[1].name = "ANOTHER NAME"

    # Verify that we can print back out the KML object as a string
    print(k.to_string(prettyprint=True))


    When I ran it I got the error: ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration..

    I looked the error up on google and found this GitHub page where they were saying that the "from_string()" function only takes bytes so the beginning of my code could be changed to:

    from fastkml import kml

    filename = "C:\\Users\\dumasal\\Documents\\GOOGLE_EARTH\\MarineRegions-eez_12nm.kml"
    with open(filename, 'r') as myfile:
    doc=myfile.read().encode('UTF-8')
    print(doc)

    # Create the KML object to store the parsed result
    k = kml.KML()

    # Read in the KML string
    k.from_string(doc)
    print('k = ', k)

    ### Next we perform some simple sanity checks ###

    # Check that the number of features is correct
    # This corresponds to the single ``Document``
    features = list(k.features())
    print(len(features))


    And strangely enough the ValueError stopped appearing. However now I get the error:

    print(features[0].features())
    IndexError: list index out of range


    this is because my variables features = [], and I don't know why.

    So could you either explain to me why the features variable is empty, or a more direct method to plot a .kml file with python and matplotlib?

    Thank you very much!

    Continue reading...

Compartilhe esta Página