-
Notifications
You must be signed in to change notification settings - Fork 121
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
tmap_options(check.and.fix = TRUE) doesn't fix invalid geometries unless sf::sf_use_s2(FALSE) is used #606
Comments
Have you tried to set |
Doesn't look like my version of sf (1.0-2) has that > sf::st_use_s2(FALSE)
Error: 'st_use_s2' is not an exported object from 'namespace:sf' |
Sorry - I meant |
That seems to help, it changes it from an error to a warning: > sf::sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> tm_shape(invalid_geometry) +
+ tm_fill()
Warning message:
The shape invalid_geometry is invalid. See sf::st_is_valid |
This is mostly beyond tmap, because The fact that |
Looks like someone already raised this issue over in the sf repo - r-spatial/sf#1732. Can I suggest tmap falling back to |
Thanks for the suggestions. The first suggestion will be a no, because I don't want to include extra dependencies ( @Nowosad what is your opinion? |
My current suggestion would be to update the warning or move to the error message (e.g., There is also another thing to consider related to s2 - how to plot lines in tmap when |
My 2 cents here, discovering this issue. |
Hi Martijn or Jakub I am currently transferring a thesis to publication, my thesis used tmap for all spatial data analysed which as submitted late 2021. So here I am recreating some of the maps and I am getting the same error, I realise tmap will not be supported or updated. I have tried the above fixes with no change in the error message. I think it is in relation to the readtopo dimension but any suggestions you have will be greatly appreciated. CODE library(sf) #Breeding populations locations #obtain bathymetry data for map bathywedge2<-rasterToContour(bathywedge) tmap::tm_shape(bathywedge2)+tm_iso()+ Error in cls[2, ] : incorrect number of dimensions |
Did you try Planning to submit the last 3.x version this week, so if possible/needed I can fix this. |
Yes I have tried sf::sf_use_s2(FALSE) see below minimal working example. When I break it down it is the readtopo code that is the issue. library(tmap) bathywedge <- readtopo("etopo2", xylim = extent(130, 180, -60, -25)) bathywedge2<-rasterToContour(bathywedge) tmap::tm_shape(bathywedge2)+tm_iso()+ |
@pennybeaver I still cannot reproduce the code. Could you maybe save the |
[https://res-h3.public.cdn.office.net/assets/mail/file-icon/png/zip_16x16.png]Tmap data files.RData.zip<https://universitytasmania-my.sharepoint.com/:u:/g/personal/penny_beaver_utas_edu_au/ERzHtIwGpC1Frs2vUkAX6fYBdSWJXigRblhXzwv1LJS5Mw>
HI Jakub
Attached is the zip file.
Thanks
Penny
…________________________________
From: Jakub Nowosad ***@***.***>
Sent: Tuesday, 5 September 2023 8:09 PM
To: r-tmap/tmap ***@***.***>
Cc: Penny Beaver ***@***.***>; Mention ***@***.***>
Subject: Re: [r-tmap/tmap] Error in co[, 1] : incorrect number of dimensions (#606)
@pennybeaver<https://github.com/pennybeaver> I still cannot reproduce the code. Could you maybe save the bathywedge2 object to a file (e.g., using saveRDS, compress it to a zip file, and then attach it here?
—
Reply to this email directly, view it on GitHub<#606 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AFIRKD7E5F7PY7GXDAKKJIDXY326TANCNFSM5FZG4R5A>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
This email is confidential, and is for the intended recipient only. Access, disclosure, copying, distribution, or reliance on any of it by anyone outside the intended recipient organisation is prohibited and may be a criminal offence. Please delete if obtained in error and email confirmation to the sender. The views expressed in this email are not necessarily the views of the University of Tasmania, unless clearly intended otherwise.
|
zip file attached :) |
@pennybeaver are you using the CRAN version of tmap or the GitHub one? The second one seems to be working ( # remotes::install_github("r-tmap/tmap")
library(tmap)
library(tmaptools)
data(World)
tmap_mode("plot")
#> tmap mode set to plotting
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.6.4, PROJ 9.1.1; sf_use_s2() is TRUE
sf::sf_use_s2(FALSE)
#> Spherical geometry (s2) switched off
load("Tmap data files.RData")
tmap::tm_shape(bathywedge2) + tm_iso() +
tmap::tm_shape(World) +
tm_polygons(col = "black") +
tm_text(
"name",
bg.color = "black",
bg.alpha = 0.5,
remove.overlap = T,
size.lowerbound = T,
scale = 1,
size = 0.7,
colorNULL = TRUE
) +
tm_grid(col = "gray80", alpha = 0.3) +
tm_xlab("Longitude", size = 0.8) + tm_ylab("Latitude", size = 0.8) |
I have reinstalled twice once on an external cloud RStudio site (organisational site) and at the same time on the pc hard drive. Both had the error message originally but not the hard drive after I reinstalled tmap using the below code. remotes::install_github("r-tmap/tmap") Thank you very much for your help, it is appreciated. |
This is still an issue, the error is just slightly different now: library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
invalid_geometry = readRDS("invalid_geometry.rds.zip")
st_is_valid(invalid_geometry, reason = TRUE)
#> [1] "Edge 9 crosses edge 26" "Edge 0 crosses edge 2"
#> [3] "Edge 18 crosses edge 20" "Edge 0 crosses edge 15"
#> [5] "Edge 3 crosses edge 5"
library(remotes)
install_github("r-tmap/tmaptools")
#> Skipping install of 'tmaptools' from a github remote, the SHA1 (0c8b0b1c) has not changed since last install.
#> Use `force = TRUE` to force installation
install_github("r-tmap/tmap")
#> Skipping install of 'tmap' from a github remote, the SHA1 (40106020) has not changed since last install.
#> Use `force = TRUE` to force installation
library(tmap)
#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
#> which was just loaded, will retire in October 2023.
#> Please refer to R-spatial evolution reports for details, especially
#> https://r-spatial.org/r/2023/05/15/evolution4.html.
#> It may be desirable to make the sf package available;
#> package maintainers should consider adding sf to Suggests:.
#> The sp package is now running under evolution status 2
#> (status 2 uses the sf package in place of rgdal)
#>
#> Attaching package: 'tmap'
#> The following object is masked from 'package:datasets':
#>
#> rivers
tmap_options(check.and.fix = TRUE)
tm_shape(invalid_geometry) +
tm_fill()
#> Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented, : Loop 0 is not valid: Edge 9 crosses edge 26 |
Hi @neon-ninja -- see https://www.branchtwigleaf.com/shinyapps/make-valid-geom/. In general -- there is no one way to always fix the geometry issues, and many different geometry issues are possible. Try running |
Yes, |
Thanks for your perspective, @neon-ninja. @mtennekes would it be possible to do something like this in tmap, when |
Great idea! |
FYI On 22 January 2024 this is still an issue .. neither |
Implemented using @Nowosad's suggestion. Code review welcome https://github.com/r-tmap/tmap/blob/master/R/check_fix.R library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.3, PROJ 9.2.0; sf_use_s2() is TRUE
temp = tempfile()
tdir = tempdir()
download.file("https://github.com/user-attachments/files/16969923/geoBoundariesCGAZ_ADM2_invalid_polys.rds.zip", temp)
unzip(temp, exdir = tdir)
x = readRDS(file.path(tdir, "geoBoundariesCGAZ_ADM2_invalid_polys.rds"))
sf::sf_use_s2()
#> [1] TRUE
tm_shape(x) +
tm_polygons() +
tm_check_fix()
#> The shape object "x" is invalid. Trying to fix it...
#> Shape x has been fixed with s2 = FALSE. If the map doesn't look correct, please run sf::sf_use_s2(FALSE) before running the tmap code again. sf::sf_use_s2(FALSE)
#> Spherical geometry (s2) switched off
tm_shape(x) +
tm_polygons() +
tm_check_fix() Created on 2024-09-11 with reprex v2.1.0 |
This works thank you very much @mtennekes |
@mtennekes Hi!!
|
Which shapefile exactly? And what the output and where there any messages/warnings/errors? |
I just sent it to you by email .. it is too heavy to be uploaded here |
580 MB is indeed a bit large :-) Unfortunately this is beyond tmap: also plotting with sf doesn't work well. Probably the spatial object is not well defined. What I did: library(sf)
x = readRDS("~/Downloads/Countries.RDS")
isv = sf::st_is_valid(x)
table(isv) # 3 were FALSE
ids = which(!isv)
x2 = x[ids,]
rm(x)
gc()
sf::sf_use_s2(FALSE)
plot(x2)
sf::st_is_valid(sf::st_make_valid(x2))
sf::sf_use_s2(TRUE)
plot(x2)
sf::st_is_valid(sf::st_make_valid(x2)) Both draw a map with the horizontal lines, probably due to a 180/-180 longitude crossover. Also making it valid doesn't fix this artefact. x3 = sf::st_make_valid(x2)
sf::st_is_valid(x3) # returns all TRUE
plot(x3) The returned plot is: Note that sf draws a map per variable, in this case the first 9. |
Hi @mtennekes thank you for looking into it .. |
@anjelinejeline The GADM website sometimes has shapefiles with this issue. I'd recommend you try https://geoboundaries.org/ for a shapefile of the same (country/location) area and see if that works. |
@nickbearman I initially wanted to use geoboundaries but I am not comfortable with its classification .. for instance ADM2 in Italy does not correspond to the right one instead it corresponds to ADM1 as correctly reported in GADM. |
Also check out the |
@mtennekes unfortunately it does not contain adm2 at the global level |
https://www.naturalearthdata.com/ might also be worth a look |
Hi,
After updating to the latest version of tmap, some code that was written for an earlier version of tmap no longer works. The issue appears to be caused by invalid geometries. The error is thrown even with
tmap_options(check.and.fix = TRUE)
. For the purposes of a reprex, I've filtered the invalid geometries out of the larger dataset. Here's a reprex:With invalid_geometry.rds.zip in your working directory:
Created on 2021-10-12 by the reprex package (v2.0.1)
I also note that
st_make_valid
doesn't seem to help:seems to fix it
The text was updated successfully, but these errors were encountered: