pythonpandasshapefilegeopandasclipping

GeoPandas: Error plotting after clipping shapefile


I am interested in plotting data on the city of Göteborg (in English Gothenburg, Sweden). I am starting with shapefiles of Sweden and then clipping them to concentrate my study in Göteborg.

# Extracting Göteborg boundary
boundary = adm_2.loc[(adm_2["NAME_2"] == "Göteborg")].copy()

boundary
>>
    ID_0    ISO NAME_0  ID_1    NAME_1  ID_2    NAME_2  TYPE_2  ENGTYPE_2   NL_NAME_2   VARNAME_2   geometry
246 222 SWE Sweden  21  Västra Götaland 247 Göteborg    Kommuner    Municipality    None    None    MULTIPOLYGON (((12.09368 57.67838, 12.09466 57...


# Clipping the shapefiles
roads = geopandas.clip(roads_sweden, boundary)
buildings = geopandas.clip(buildings_sweden, boundary)

#roads.shape #(9747, 7)
#roads_sweden.shape #(148403, 7)

#buildings.shape #(1163, 4)
#buildings_sweden.shape #(15253, 4)

If I plot ALL the roads, i.e roads_sweden. I have plot of the rows of all Sweden. However, when trying to plot just the roads in Göteborg I get the error:

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

I can't understand why while they look the same. I even attempted resetting the index but this seems unrelated.

For the plot I'm using:

fig, ax = plt.subplots(figsize=(12, 10), facecolor='white', edgecolor='k')
boundary.plot(ax=ax, facecolor= "white", edgecolor="black", linewidth=.5)
roads.plot(ax=ax)
buildings.plot(ax=ax)

Even when done individualy, i.e. roads.plot(), I get the error. roads_sweden.plot() always works fine.

Here's some preview of the data

roads_sweden.head()

>>
osm_id  name    ref type    oneway  maxspeed    geometry
0   1240    Klensmedsvägen None    unclassified    0   0   LINESTRING (17.99027 59.29686, 17.99182 59.296...
1   1241    Hyvelvägen None    residential 0   0   LINESTRING (17.99273 59.29666, 17.99212 59.295...
2   1242    Spikvägen  None    residential 0   0   LINESTRING (17.99353 59.29640, 17.99299 59.295...
3   1243    Bultvägen  None    residential 0   0   LINESTRING (17.99439 59.29617, 17.99385 59.295...
4   1245    Tångvägen None    residential 0   0   LINESTRING (17.99691 59.29512, 17.99637 59.294...

roads.head()

>>
osm_id  name    ref type    oneway  maxspeed    geometry
0   3846609 None    None    motorway_link   1   0   LINESTRING (12.09100 57.67369, 12.09090 57.673...
1   4040303 E6  None    motorway    0   0   LINESTRING (12.00496 57.84283, 12.00525 57.841...
2   4040436 None    None    motorway_link   1   0   LINESTRING (12.00643 57.79800, 12.00560 57.796...
3   4040439 None    E 20    motorway    1   0   LINESTRING (11.99463 57.71530, 11.99492 57.715...
4   4040441 None    E 20    motorway    1   0   LINESTRING (11.99520 57.71580, 11.99434 57.71536)


DATA SOURCES

administrative areas (see area 2): https://www.diva-gis.org/datadown

roads and buildings: https://mapcruzin.com/free-sweden-arcgis-maps-shapefiles.htm


Solution

  • I traced the issue back to the road with index 125779, which has these coordinates: [(11.8892841, 57.6579022), (11.8892841, 57.6579022)], so it is basically a point. When clipping, this geometry is converted to "LINESTRING EMPTY". Maybe it should instead convert this empty line to the corresponding Point, but anyway I guess a fix should be made in geopandas' plotting to ignore these empty lines instead of raising such an unclear error (feel free to submit an issue https://github.com/geopandas/geopandas/issues).

    In the meantime, you can plot your data by selecting only non-emtpy geometries:

    roads.loc[~roads.geometry.is_empty].plot(ax=ax)