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. Commented Apr 7, 2023 at 9:20
  • @user35594 doesnt my answer work?
    – Bera
    Commented Apr 14, 2023 at 10:04

3 Answers 3

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).

0

Since geopandas 1.0 a new, faster, engine became the default to read/write files: pyogrio.

It also has a function to get basic information about a file without reading all data: pyogrio.read_info.

The information returned is the following (note the "geometry_type" field):

{
    "layer_name": "<layer name>",
    "crs": "<crs>",
    "fields": <ndarray of field names>,
    "dtypes": <ndarray of field dtypes>,
    "encoding": "<encoding>",
    "fid_column": "<fid column name or "">",
    "geometry_name": "<geometry column name or "">",
    "geometry_type": "<geometry type>",
    "features": <feature count or -1>,
    "total_bounds": <tuple with total bounds or None>,
    "driver": "<driver>",
    "capabilities": "<dict of driver capabilities>"
    "dataset_metadata": "<dict of dataset metadata or None>"
}

Sample code:

import pyogrio

geometry_type = pyogrio.read_info("file.shp")["geometry_type"]

Your Answer

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

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