Repository navigation
Description
I'm running into an error when I try to "erase" one shape file from another using the st_erase function that I can't seem to resolve using information from
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)"
library(sf)
library(lwgeom)
ag2015_sf <- st_read("data/2015AgBaseline.shp") %>%
st_transform(st_crs("+proj=utm +ellps=GRS80 +datum=WGS84")) %>%
st_set_precision(1000000) %>%
st_make_valid() # class sf and data.frame
ag1980_sf <- st_read("data/alum.shp") %>%
st_transform(st_crs("+proj=utm +ellps=GRS80 +datum=WGS84")) %>%
st_set_precision(1000000) %>%
st_make_valid()
# a helper function that erases all y from x
st_erase = function(x, y)
st_difference(st_geometry(x), st_union(st_combine(st_geometry(y))))
ag_erase <- st_erase(ag1980_sf, ag2015_sf)
#ag_erase <- st_erase(ag1980_sf, st_buffer(ag2015_sf, 0))
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:
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
Matrix products: default
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lwgeom_0.2-1 rgeos_0.5-2 ggspatial_1.0.3 forcats_0.4.0 stringr_1.4.0
[6] dplyr_0.8.3 purrr_0.3.3 readr_1.3.1 tidyr_1.0.0 tibble_2.1.3
[11] ggplot2_3.2.1 tidyverse_1.3.0 sf_0.8-1 PBSmapping_2.72.1 rgdal_1.4-8
[16] gpclib_1.5-5 maptools_0.9-9 raster_3.0-12 sp_1.4-0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.3 lubridate_1.7.4 lattice_0.20-38 class_7.3-15 assertthat_0.2.1
[6] zeallot_0.1.0 R6_2.4.1 cellranger_1.1.0 backports_1.1.5 reprex_0.3.0
[11] e1071_1.7-3 httr_1.4.1 pillar_1.4.3 rlang_0.4.2 lazyeval_0.2.2
[16] readxl_1.3.1 rstudioapi_0.10 foreign_0.8-71 munsell_0.5.0 broom_0.5.3
[21] compiler_3.6.1 modelr_0.1.5 xfun_0.12 pkgconfig_2.0.3 tidyselect_0.2.5
[26] codetools_0.2-16 fansi_0.4.1 crayon_1.3.4 dbplyr_1.4.2 withr_2.1.2
[31] grid_3.6.1 nlme_3.1-140 jsonlite_1.6 gtable_0.3.0 lifecycle_0.1.0
[36] DBI_1.1.0 magrittr_1.5 units_0.6-5 scales_1.1.0 KernSmooth_2.23-15
[41] cli_2.0.1 stringi_1.4.5 fs_1.3.1 xml2_1.2.2 vctrs_0.2.1
[46] generics_0.0.2 tools_3.6.1 glue_1.3.1 hms_0.5.3 colorspace_1.4-1
[51] classInt_0.4-2 rvest_0.3.5 knitr_1.27 haven_2.2.0
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?
When I was running the following code
ag_difference <- st_difference(ag1980_sf, ag2015_sf)
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!
Activity
etiennebr commentedon Feb 26, 2020
Using a 0m buffer is a good track. Try to buffer both geometries before computing the difference...
Let me know if it works!
nakoafarrant commentedon Feb 26, 2020
@etiennebr Your fix worked! Thanks for the help!
It could also be worth noting for future viewers that I was running into the issue on a Windows 10 system. I moved to my Mac laptop and tried the original non-buffered lines that were throwing the error and it worked on the first try using the lines below. However, the suggested fix seems like a safer bet across all cases.
My laptop has the basic specifications

And my session info from my Mac run is below in case anyone is curious
etiennebr commentedon Feb 26, 2020
Awesome! What are the linked GEOS versions, are they the same? I have: