5

I have scripts to download and extract two shapefiles every 24 hours and create a cleaned DataFrame. I want to append the DataFrame for each one to a specific layer in a gpkg. The only example I can find replaces the contents of the existing layer, but I want to append the new df each day to an existing layer (of two total layers in the pgkg)

This is the snippet I have so far:

df.to_file("myfile.gpkg", layer="layer1", driver="GPKG")
1
  • 2
    try appending using mode='a' i.e df.to_file(etc..., mode='a') stackoverflow.com/a/63491963/737471 (note linked answer shows GPKG does not support append, but fiona source show it does, so you may need a very recent version of fiona).
    – user2856
    Commented Jul 22, 2021 at 22:58

3 Answers 3

5

You can read the geopackage file and append the dataframe and then re-write it:

gdf_base = gpd.read_file("myfile.gpkg", layer='layer1')
gdf_base = gdf_base.append(df,ignore_index=True)
gdf_base .to_file("myfile.gpkg", layer="layer1", driver="GPKG")

If your data is too big to be read every day, then you need to look for Spatialite coding to append your data.

UPDATE!

The append method is going to be depricated. Use concat instead:

gdf_base = gpd.read_file("myfile.gpkg", layer='layer1')
gdf_base = pd.concat([gdf_base,df],ignore_index=True)
gdf_base .to_file("myfile.gpkg", layer="layer1", driver="GPKG")
2

geopandas.GeoDataFrame.to_file can take the keyword argument mode set to 'a' in order to append data to a file:

gdf_base.to_file("myfile.gpkg", layer="layer1", driver="GPKG", mode='a')
1

From what i red from the docs this should be half ccomplished with

df.geometry.append(another_red_df.geometry)

I don't think that the data will be added too. Here you can look further in the docs for joining values by attributes.

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.