-
Notifications
You must be signed in to change notification settings - Fork 293
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
TopologyException in set intersection functions #603
Comments
|
@tim-salabim @timelyportfolio this happens a lot, and is a point where a user would like to fire up mapview to check, and then mapedit to repair the broken geometry. Any ideas? I could load these geometries in mapview, but had a hard time finding the particular point the error message points to. Can this be done easily, or can we make it easy? |
|
This seems somehow related to discussions in #600 regarding precision. It appears that the p2 object contains valid geometries, but "barely". In fact: readRDS("D:/p2.rds") %>% st_sfc() %>% st_is_valid()
[1] TRUE, but : > readRDS("D:/p2.rds") %>% st_sfc() %>% st_set_precision(10000000) %>% st_is_valid()
[1] FALSE
Warning message:
In evalq((function (..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE, :
Too few points in geometry component at or near point 1602556.2014496 4270010.0747427Looking at Now, if I understand correctly the st_set_precision help page, routines in GEOS and lwgeom always receive "rounded coordinates", even if you do not specify manually a precision. I am guessing here therefore that the "automatic" rounding is sufficient to invalidate your geometries, thus leading to the error in A workaround on this appears to be to call p1 <- readRDS("D:/p1.rds") %>% st_sfc()
p2 <- readRDS("D:/p2.rds") %>% st_sfc() %>% st_set_precision(1000000) %>% lwgeom::st_make_valid()
p3 <- sf::st_difference(p1, p2)
p3
Geometry set for 1 feature
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: 1586298 ymin: 4277065 xmax: 1588823 ymax: 4278701
epsg (SRID): NA
proj4string: NA
MULTIPOLYGON (((1588730.25642744 4277695.439676..., which at least does not throw an error (although it is difficult for me to know if it does what you wish). PS: @edzer : I hope I am not completely off-track, here. In case, feel free to delete this. |
|
@edzer I am on holidays until early Jan so bare with me. I will have a look at this as soon as I'm back. Just as a thought, would it be possible to translate the error message to geometry ids somehow and provide them as part of the error message? Would make finding the culprit a little easier at least. |
|
@lbusett great that you solved this! You are nearly on track; by default, precision is set to 0, which means do nothing to the coordinates (see here). So no: GEOS/GDAL/liblwgeom all receive the raw coordinates. I remember discussing with @rsbivand a year ago that it might be a good idea to do something else, i.e. set a non-zer default precision, but that didn't lead to action. @tim-salabim sure no hurries; yeah, fishing the problematic coordinates out of the GEOS emitted error will be tricky, and is on my plate. I'll promise to not do that this year. |
|
Brilliant - yeah that fixes the problem @lbusett. Thank you very much everyone. |
|
I'm running into a similar error when I try to "erase" one shape file from another using the st_erase function that I can't seem to resolve. The data is found online here http://planning.hawaii.gov/gis/download-gis-data/. Once you follow that link, click on the first drop down labeled "Agriculture and Farming" and download the two shape files: "Agricultural Land Use Baseline (2015)" and "Agricultural Land Use Maps (ALUM)" Running this code results in the error "Error in CPL_geos_op2(op, st_geometry(x), st_geometry(y)) : Evaluation error: TopologyException: Input geom 1 is invalid: Self-intersection at or near point 2947191.1423960002 2626263.8412870001 at 2947191.1423960002 2626263.8412870001." I also tried running the above code with the final commented out line instead that uses an st_buffer of 0 based on suggestions I saw on other forums, but the same error was thrown Here is the session info: I'm not sure if I have to some how cast the points from one of the geometries into a multipolygon or something? What I noticed is that the geometries between the two data sets seem slightly different ag1980 uses list(c(points)) and ag2015 uses list(list(c(points)). Not sure if this will make the difference. Alternatively, is this maybe an issue with the size of the files? I received a different error about bad_alloc which seems to be related to memory allocation for the processing or something? Thanks for any advice! |
Hi,
I've encountered two
sfg(MULTIPOLYGON) objects which throw a TopologyException error when I try to use set intersection functions on them (e.g.sf::st_difference). I've tried usinglwgeom::st_make_validto fix the data (thoughsf::st_is_validreturnsTRUEfor both objects), buffering them by a distance of zero, and modifying the precision of the calculations (usingsf::st_set_precisionafter converting them to ansfcobject).If anyone has a moment, I would really appreciate it if you could take a look at this. I've attached the files in RDS format (polygon_data.zip). Below is some example code that imports the objects and generates the error.
Also, here is my session information (using the latest version of sf on GitHub).
Apologies if I've posted this in the wrong place, and please let me know the correct forum to post this.
The text was updated successfully, but these errors were encountered: