На этом сайте я получил KML-файл территориальных морей мира и хотел бы отобразите его не в Google Earth, а на графике matplotlib.pyplot (если возможно, с картографической картой). Файл .kml выглядит следующим образом:
Код: Выделить всё
Territorial Seas (12NM) v3
http://geo.vliz.be/geoserver/gwc/service/kml/MarineRegions:eez_12nm.png.kml
Итак, это тест . py, который я пытаюсь запустить (он взят из руководства по использованию):
Код: Выделить всё
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))
Я поискал ошибку в Google и нашел эту страницу GitHub, где говорилось, что функция "from_string()" занимает только байты, поэтому начало моего кода можно изменить на:
Код: Выделить всё
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))
Код: Выделить всё
print(features[0].features())
IndexError: list index out of range
Так что не могли бы вы объяснить почему переменная Features пуста, или более прямой метод построения файла .kml с помощью python и matplotlib?
Большое спасибо!
Подробнее здесь: https://stackoverflow.com/questions/714 ... windows-10