Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for creating a new, empty GeoDataFrame

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?

like image 207
Borillar0821 Avatar asked Jun 19 '26 00:06

Borillar0821


2 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)
like image 173
Cimbali Avatar answered Jun 21 '26 13:06

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')
like image 43
Ksenia Avatar answered Jun 21 '26 15:06

Ksenia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!