Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makevalid and autocorrect geometries not working properly #9596

Closed
latot opened this issue Apr 2, 2024 · 4 comments
Closed

makevalid and autocorrect geometries not working properly #9596

latot opened this issue Apr 2, 2024 · 4 comments

Comments

@latot
Copy link

latot commented Apr 2, 2024

What is the bug?

Hi, this is pretty weird, ogr2ogr is able to fix geometries, I have a file with an invalid geometry, but depending on how and what we do with it, the file can be fixed, and then it turns it back, some times there is warnings for invalid geometries and other times there is not.

Maybe here is more than issue.

Also... to trigger some warnings we need to convert two times with ogr2ogr + makevalid on the first conversion.

Steps to reproduce the issue

sample.zip

There will be several examples, for different weird aspects, the base is that the sample only has one geometry, and is a invalid one.

No warning on invalid geometries

GDAL tell us if a file has invalid geometries, but seems is not done if we parse this file directly:

pipe@pipe-pc /tmp/tests $ ogr2ogr sample2.shp sample.shp
pipe@pipe-pc /tmp/tests $ 

Something important here, doing things and converting several times and between SHP and GPKG will not trigger any warning.

The geometry is not valid.

Use makevalid still does not shows the error, but after run it shows

pipe@pipe-pc /tmp/tests $ ogr2ogr sample2.shp sample.shp -makevalid
pipe@pipe-pc /tmp/tests $ ogr2ogr sample3.shp sample2.shp
Warning 1: sample2.shp contains polygon(s) with rings with invalid winding order. Autocorrecting them, but that shapefile should be corrected using ogr2ogr for example.

The problem here is that, any conversion from sample2.shp will trigger this warning, and when is read, how GDAL already fixed something we only will see a valid geometry (I tested in R), but when we save it, and load again we still have the invalid geometry warning.

data <- sf::st_read("sample2.shp", quiet = TRUE)
Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  :
  GDAL Message 1: /tmp/tests/sample2.shp contains polygon(s) with rings with invalid winding order. Autocorrecting them, but that shapefile should be corrected using ogr2ogr for example.
sf::st_is_valid(data)
TRUE
sf::st_write(data, "sampleR.shp", quiet = TRUE)
data2 <- sf::st_read("sampleR.shp", quiet = TRUE)
Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  :
  GDAL Message 1: /tmp/tests/sampleR.shp contains polygon(s) with rings with invalid winding order. Autocorrecting them, but that shapefile should be corrected using ogr2ogr for example.

GPKG seems to works?

ogr2ogr sample2.gpkg sample.shp -makevalid
Warning 1: Feature id 0 not preserved
ogr2ogr sample3.gpkg sample2.gpkg

No idea what Warning 1: Feature id 0 not preserved!!

All works right? well no, transform to shp again and fill be bad again! but need to be converted at least one time:

pipe@pipe-pc /tmp/tests $ ogr2ogr sample3.shp sample3.gpkg
pipe@pipe-pc /tmp/tests $ ogr2ogr sample4.shp sample3.shp
Warning 1: sample3.shp contains polygon(s) with rings with invalid winding order. Autocorrecting them, but that shapefile should be corrected using ogr2ogr for example.

Thx!

Versions and provenance

GDAL 3.8.0, released 2023/11/06

Linux 64

Additional context

This issue is not exactly about makevalid fixing geometries, is the weird behaviors in the resultant files when we try to make them valid!

@rouault
Copy link
Member

rouault commented Apr 2, 2024

Warning 1: sample2.shp contains polygon(s) with rings with invalid winding order. Autocorrecting them, but that shapefile should be corrected using ogr2ogr for example.

This was a bug that has been fixed in 3.8.1 (#8767)

No idea what Warning 1: Feature id 0 not preserved!!

The Shapefile driver starts numbering at 0, whereas the GPKG driver normally starts at 1.
The GPKG driver has been made more tolerant in 38c9e51 (master / 3.9.0dev)

@latot
Copy link
Author

latot commented Apr 2, 2024

if all is fine, then we can close this, thx :)

@rouault rouault closed this as completed Apr 2, 2024
@latot
Copy link
Author

latot commented Apr 2, 2024

@rouault Just to know, part of this issue is also the inconsistency of when was the warning print, was that fixed on #8767? Which is different from the warning it self.

@rouault
Copy link
Member

rouault commented Apr 2, 2024

was that fixed on #8767?

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants