Syntax for creating a new, empty GeoDataFrame

Question:

I have a GeoDataFrame (let’s call it Destinations) that was made from a point shapefile using GeoPandas. For every feature (correct me if the terminology is wrong) in Destinations, I need to find the nearest node on a graph and save that node to another GeoDataFrame (let’s call it Destination_nodes, it will be used later for shortest-path analysis). Presumably, this requires creating a new, blank Destination_nodes GeoDataFrame and appending nodes to it as I loop through Destinations. But what is the syntax for creating a new empty GeoDataFrame?

Asked By: Borillar0821

||

Answers:

For a GeoDataFrame you need to say which column will be the geometry, i.e. contain features as you say. So you could simply specify the columns of your empty dataframe at creation, without specifying any data:

>>> dest = geopandas.GeoDataFrame(columns=['id', 'distance', 'feature'], geometry='feature')
>>> dest
Empty GeoDataFrame
Columns: [id, distance, feature]
Index: []
>>> dest.geometry
GeoSeries([], Name: feature, dtype: geometry)
Answered By: Cimbali

Unfortunately, I can’t comment on the @Cimbali’s answer, but would like to complete it. To avoid UserWarning when appending or concatenating newly created empty GeoDataFrame, you need to add crs parameter, for example:

dest = geopandas.GeoDataFrame(columns=['id', 'distance', 'feature'], geometry='feature', crs='EPSG:4326')
Answered By: Ksenia