0

Using GeoPandas, how can I check the geometry type of a shapefile (i.e. Point, LineString, Polygon etc).

I can use the following to check the geometry type of each feature but this is unnecessary, I would just like to know if the shapefile is a LineString or not:

import geopandas as gpd
df = gpd.read_file('path/to/shapefile')
df.geom_type

0      LineString
1      LineString
2      LineString
3      LineString
4      LineString
             ...
423    LineString
424    LineString
425    LineString

Length: 426, dtype: object
2
  • Theoretically and practically, a GeoDataFrame can contain multiple type of geometry (Example). The best option look like using fiona as in BERA's answer. Apr 7 at 9:20
  • @user35594 doesnt my answer work?
    – BERA
    Apr 14 at 10:04

2 Answers 2

2

Fiona.open seems to be fast (geopandas depend on fiona):

import fiona, geopandas, time

#A line shapefile, total size 118 MB
shape = r"/home/bera/Desktop/GIStest/ml_riks.shp"

#Fiona open
start = time.process_time()
c = fiona.open(shape)
if c.schema["geometry"]=='LineString':
    print("Line")
print(time.process_time()-start)
#Line
#0.0016448840000009568

#Geopandas checking geometry type of first feature
start = time.process_time()
df = geopandas.read_file(shape)
if df.iloc[0].geometry.type=='LineString':
    print("Line 2")
print(time.process_time()-start)
#Line 2
#5.306254288999995

#Geopandas checking if all features are linestrings
start = time.process_time()
df = geopandas.read_file(shape)
if df.geom_type.str.contains("LineString").all():
    print("Line 3")
print(time.process_time()-start)
#Line 3
#5.525147246000017
0

If you are using GeoPandas and are going to load the shapefile anyway, you can check immediately after reading it by doing df.geom_type.unique(). It's good to check in case there are mix of LineString and MultiLineString (or Polygon/MultiPolygon or Point/MultiPoint).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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