Filter NotNull Geometries in an ArcGIS SpatialDataFrame
When working with Pandas DataFrames it is fairly common to filter out records with null values using the
notnull Pandas function. This however, does not work with the geometry column created when loading data into a SpatialDataFrame. It still has a value, albeit an invalid value, but Pandas still sees something there. Hence, it is not technically null.
Fortunately, there is a way to filter out, and only keep rows with valid geometries using the
is_empty property of the geometry. Here is a code sample illustrating how to accomplish this.
from arcgis.features import SpatialDataFrame as SDF store_fc_path = r".\data.gdb\store_locations" # load the data from the feature class into a SpatialDataFrame sdf = SDF.from_featureclass(store_fc_path) # keep only geometries, which are not empty sdf = sdf[~sdf['SHAPE'].is_empty]
The trick, which took me a decent amount of Googling to recall how to do, is using the
~ to negate the expression. The expression above means filter based on rows whose geometry is not empty. Without the tilde, the new data frame will only keep rows with empty geometries, so if you are wanting to locate errors, this also proves a very useful method as well.
Hopefully this makes you life easier, saves you some time, and keeps you from having to dig around and Google as much as I did to find this!