Comments (3)
The error is a bit cryptic, but the problem is that in one of the rows in the second gdf, the "fid" value is the same as one of the rows in the first gdf. Because in GPKG the "fid" is used as primary key, there is an error if a duplicate "fid" is inserted.
If you e.g. rename the "fid" column to "fid_orig", the error disappears.
import geopandas as gpd
from owslib.wfs import WebFeatureService
wfs = WebFeatureService("https://data.3dbag.nl/api/BAG3D/wfs", version="2.0.0")
response1 = wfs.getfeature(
typename="BAG3D:lod12",
bbox=(204000.00016165, 486000.00045928, 207000.0001644, 489000.00046109),
method="Post",
outputFormat="GML2",
startindex=0,
maxfeatures=500,
)
response2 = wfs.getfeature(
typename="BAG3D:lod12",
bbox=(204000.00016165, 486000.00045928, 207000.0001644, 489000.00046109),
method="Post",
outputFormat="GML2",
startindex=500,
maxfeatures=500,
)
gdf1 = gpd.read_file(response1)
gdf2 = gpd.read_file(response2)
print(all(gdf1.columns == gdf2.columns)) # True: Attributes are identical
print(all(gdf1.dtypes == gdf2.dtypes)) # True: Data types are identical
# In the second gdf, there is a row with an fid that already occurs in the first gdf.
# As the fid column is reused as a primary key in GPKG, error.
gdf1 = gdf1.rename(columns={"fid": "fid_orig"})
gdf2 = gdf2.rename(columns={"fid": "fid_orig"})
gdf1.to_file("/tmp/foo.gpkg", driver="GPKG", layer="Building", mode="w", engine="fiona")
gdf2.to_file(
"/tmp/foo.gpkg", driver="GPKG", layer="Building", mode="a", engine="fiona"
) # Record does not match collection schema
from geopandas.
For completeness sake, regarding the gdf.set_index(['fid'])
, two things to note:
- probably just an oversight, but
gdf.set_index
returns a new dataframe with the index set, so either usegdf.set_index(['fid'], inplace=True)
orgdf = gdf.set_index(['fid'])
- when you actually set the index to the "fid" column, I noticed while testing this that the index will still be used for the "fid" column/primary key as long as the index name is "fid". So you need to additionally set the index name to e.g. None, with
gdf.index.name = None
.
from geopandas.
The error is a bit cryptic, but the problem is that in one of the rows in the second gdf, the "fid" value is the same as one of the rows in the first gdf. Because in GPKG the "fid" is used as primary key, there is an error if a duplicate "fid" is inserted.
If you e.g. rename the "fid" column to "fid_orig", the error disappears.
import geopandas as gpd from owslib.wfs import WebFeatureService wfs = WebFeatureService("https://data.3dbag.nl/api/BAG3D/wfs", version="2.0.0") response1 = wfs.getfeature( typename="BAG3D:lod12", bbox=(204000.00016165, 486000.00045928, 207000.0001644, 489000.00046109), method="Post", outputFormat="GML2", startindex=0, maxfeatures=500, ) response2 = wfs.getfeature( typename="BAG3D:lod12", bbox=(204000.00016165, 486000.00045928, 207000.0001644, 489000.00046109), method="Post", outputFormat="GML2", startindex=500, maxfeatures=500, ) gdf1 = gpd.read_file(response1) gdf2 = gpd.read_file(response2) print(all(gdf1.columns == gdf2.columns)) # True: Attributes are identical print(all(gdf1.dtypes == gdf2.dtypes)) # True: Data types are identical # In the second gdf, there is a row with an fid that already occurs in the first gdf. # As the fid column is reused as a primary key in GPKG, error. gdf1 = gdf1.rename(columns={"fid": "fid_orig"}) gdf2 = gdf2.rename(columns={"fid": "fid_orig"}) gdf1.to_file("/tmp/foo.gpkg", driver="GPKG", layer="Building", mode="w", engine="fiona") gdf2.to_file( "/tmp/foo.gpkg", driver="GPKG", layer="Building", mode="a", engine="fiona" ) # Record does not match collection schema
Thanks, that is indeed the actual problem. I knew fid
is treated as primary key of the sqlite
database, that is the reason I set it as index. But given the error message it did not occur to me that duplicate primary key is the problem.
from geopandas.
Related Issues (20)
- BUG: Is it possible to use GeoPandas on Windows 7 ?
- BUG: plotting_with_geoplot example is broken HOT 1
- ENH: Add a Voronoi Diagram algorithm HOT 1
- BUG: The dissolve feature generates a new geometry HOT 4
- REGR: incorrect order of left sjoin with within predicate HOT 1
- BUG: wrong foxpro DBF file read HOT 7
- Fiona 1.10a2 issues HOT 2
- ENH: support writing + filtered reading from bbox columns in GeoParquet HOT 4
- ENH: support reading and writing the geoarrow-based encodings of GeoParquet
- API: Series alignment for non binary op methods HOT 5
- PERF: optimize `==` and `!=` for GeoSeries (`GeometryArray.__eq__`)
- numpy dependency missing & numpy 2.0 support status? HOT 3
- ENH: inplace=True for .dissolve and .explode? HOT 4
- BUG: `pyogrio` doesn't like io.BytesIO? HOT 10
- BUG: Issue with webpage code interp HOT 1
- ENH: sjoin() should allow to return the distance when using the dwithin predicate, just like sjoin_nearest() HOT 3
- BUG: set_precision() doesn't work HOT 1
- ENH: CRS from GeoDataFrame not passed on to individual geometry objects HOT 3
- BUG: Unable to open files with fiona where filepath contains a # character HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geopandas.