ateucher / rmapshaper

An R wrapper for the mapshaper javascript library
http://andyteucher.ca/rmapshaper/
Other
200 stars 13 forks source link

no_repair = FALSE not working #126

Closed csmontt closed 1 year ago

csmontt commented 1 year ago

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

ateucher commented 1 year ago

@csmontt can you please provide a reproducible example?

csmontt commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@ateucher that works, thanks!

ateucher commented 1 year ago

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