1

I am trying to read the following file with GeoPandas:

import geopandas as gp
gp.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'

df = gp.read_file('http://watchthemed.net/index.php/json/layer/9', driver='KML')

This returns one geometry:

gp.read_file('http://watchthemed.net/index.php/json/layer/9', driver='KML').shape

>> 1,3

Which is a multilinestring of length 1.

print(gp.read_file('http://watchthemed.net/index.php/json/layer/9', 
       driver='KML').geometry.loc[0])

>> MULTILINESTRING Z ((11.526082 33.171136 0, 11.531247 33.196851 0, 
11.537371 33.213983 0, 11.566443 33.298784 0, 11.585108 33.364755 0, 12 
33.916667 0, 12.218734 34.175 0, 12.40997642454452 34.37477266876127 0, 
23.583333 34.333333 0, 24.1667 34 0, 25.150691 31.669654 0))

However, if you visit the link, you can see that the file has many lines. How can I get the rest of them?

3

GeoPandas (OGR actually) considers that Document tags in KML file are different layers. When you try to get the KML content, you actually get the first layer. So you need for loop for iterating over layers.

import requests
import fiona
import geopandas as gp

gp.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'

r = requests.get("http://watchthemed.net/index.php/json/layer/9")

# convert the page content to bytes
f =  fiona.BytesCollection(bytes(r.content))

# empty GeoDataFrame
df = gp.GeoDataFrame()

# iterate over layers
for layer in fiona.listlayers(f.path):
    s = gp.read_file(f.path, driver='KML', layer=layer)
    df = df.append(s, ignore_index=True)


df.head()
# OUTPUT
# Name  Description geometry
# 0     0           (LINESTRING Z (11.526082 33.171136 0, 11.53124...
# 1     0           (LINESTRING Z (19 40.417 0, 19 36.5 0, 21.88 3...
# 2     0           (LINESTRING Z (25.929422 39.641178 0, 25.92758...
# 3     0           (LINESTRING Z (26.303632 38.751889 0, 26.26639...
# 4     0           (LINESTRING Z (25.928848 39.641112 0, 25.92824...

df.plot()

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.