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

no_repair = FALSE not working #126

Closed
csmontt opened this issue Nov 3, 2022 · 5 comments · Fixed by #118
Closed

no_repair = FALSE not working #126

csmontt opened this issue Nov 3, 2022 · 5 comments · Fixed by #118
Labels
Milestone

Comments

@csmontt
Copy link

csmontt commented Nov 3, 2022

When using ms_simplify(), snap is always applied even if snap = FALSE.

@ateucher
Copy link
Owner

ateucher commented Nov 3, 2022

@csmontt can you please provide a reproducible example?

@csmontt csmontt changed the title snap = FALSE not working no_repair = FALSE not working Nov 9, 2022
@csmontt
Copy link
Author

csmontt commented Nov 9, 2022

Sorry @ateucher I was actualy refering to no_repair not snap.

I am trying to get in R the same output I get when using mapshaper.org

The file I am using is https://www.nisra.gov.uk/sites/nisra.gov.uk/files/publications/SOA2011_Esri_Shapefile_0.zip

If I load that file to mapshaper and simplified to 15% using visvalingam / weighted area I get the following:

image

Using R with no_repair = FALSE seems to still repair line intersections.

image

Basically, i dont want R to try to fix the line intersections but it seems to do it anyway.

Reproducible code

library(sf)
library(dplyr)
library(rmapshaper)

NORTHERN_IRELAND = "https://www.nisra.gov.uk/sites/nisra.gov.uk/files/publications/SOA2011_Esri_Shapefile_0.zip"


# Downloads a link to a shapefile and unzips it somewhere on the disk
# Returns the location of the unzipped shapefile directory
download_shp_zip = function(link) {
  dest = tempfile(fileext = ".zip")
  shp = tempfile()
  dir.create(shp)
  
  download.file(link, dest)
  unzip(dest, exdir = shp)
  shp
}

# Reads in shp and converts it to geojson
shp_to_geojson <- function(shp_path, layer = NULL) {
  if (is.null(layer)) {
    shp <- sf::st_read(shp_path)
  } else {
    shp <- sf::st_read(shp_path, layer = layer)
  }
  
  shp %>%
    dplyr::mutate(valid = sf::st_is_valid(geometry)) 
}

# Gets a geojson dataframe from a zipped shapefile URL
download_geojson = function(link) {
  path = download_shp_zip(link)
  shp_to_geojson(path)
}

df_northern_ireland = download_geojson(NORTHERN_IRELAND)

simplified_not_repaired <- ms_simplify(df_northern_ireland, 
                                       keep = 0.15,
                                       no_repair = TRUE,
                                       sys = TRUE
)

sf::st_write(simplified_not_repaired , "./simplified_geojsons/not_repaired.geojson")

@ateucher
Copy link
Owner

ateucher commented Nov 9, 2022

Hi @csmontt - thanks for reporting, and for the good example. I can reproduce this, and it turns out it's not a snapping thing by mapshaper after all. The writing of the geojson file/object is done internally by geojsonio::geojson_write() - which enforces a set precision on the coordinates, which in this case reduces that precision by a significant amount, which has a "snapping" effect.

I am currently working to replace the internal read/write functions with those from the geojsonsf package, in the geojsonsf branch, which fixes it for me.

Can you please try installing from that branch and trying again?

remotes::install_github("ateucher/rmapshaper@geojsonsf")

@csmontt
Copy link
Author

csmontt commented Nov 10, 2022

@ateucher that works, thanks!

@csmontt csmontt closed this as completed Nov 10, 2022
@ateucher ateucher reopened this Nov 10, 2022
@ateucher
Copy link
Owner

Reopening just to keep track of issues that will be closed by #118

@ateucher ateucher added this to the 0.5 milestone Nov 10, 2022
@ateucher ateucher added the bug label Nov 10, 2022
@ateucher ateucher mentioned this issue Nov 10, 2022
@ateucher ateucher linked a pull request Nov 10, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants