Giter VIP home page Giter VIP logo

Comments (3)

theroggy avatar theroggy commented on June 11, 2024 1

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.

theroggy avatar theroggy commented on June 11, 2024 1

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 use gdf.set_index(['fid'], inplace=True) or gdf = 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.

digital-idiot avatar digital-idiot commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.