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

Reduce spatial data size #11

Open
andybega opened this issue May 27, 2020 · 3 comments
Open

Reduce spatial data size #11

andybega opened this issue May 27, 2020 · 3 comments

Comments

@andybega
Copy link
Owner

The index page (index.html) is quite big. I started out at almost 6MB when loaded in a browser, and through playing around with st_simplify() for the two map objects I can get it down to around 4.53 MB when loaded. This still doesn't work with the Twitter card validator (see #10), and reducing the number of points further at this point I think will detract too much visibly.

Another option might be to reduce the precision of the coordinates.

Right now index.html stores the coordinates like "-0.456945916095746", i.e. with 15 digits. That's probably way more than needed. (These are lag/long coordinates, so range from -180 - 180 and -90 to 90.)

So try this instead: reduce number of digits in the coordinates (and probabilities, for that matter); and possible maybe try to revert the point reduction (st_simplify()) back to the earlier values.

In rleaflet, for sf, the code to convert sf to GeoJSON straight pulls from the sf coordinates, see https://github.com/rstudio/leaflet/blob/master/R/normalize-sf.R.

sf has a set of precision functions, but these only come into play when writing out data (see https://r-spatial.github.io/sf/reference/st_precision.html). rleafet's straight access to the sf coordinates thus circumvents this. Maybe either:

  • try to override rleaflet::sf_coords()
  • write from sf to GeoJSON; manipulate the coordinates somehow, and then add it as a GeoJSON layer in leaflet.
@andybega
Copy link
Owner Author

rleaflet::leaflet() contains the map data added with rleaflet::addPolygons(). It creates a html widget using htmlwidgets::createWidget().

There, widget_data() is used to convert the data to JSON, using jsonlite::toJSON().

@andybega
Copy link
Owner Author

Wups, actually htmlwidgets uses a wrapper for jsonlite::toJSON(). Turns out the arguments that get passed to this can be controlled by setting some options (https://github.com/ramnathv/htmlwidgets/pull/103; don't want to auto link spam). E.g.:

options(htmlwidgets.TOJSON_ARGS = list(pretty = TRUE, digits = 6))

@andybega
Copy link
Owner Author

  • use 4 digits
  • changed st_simplify() to use 7000 as tolerance (5000 before)
  • removed whitespace from html popup labels

This gets it down to ~3.2MB in browser and 2MB for the file system size. Good enough for twitter card validation.

Another thing is that the html labels are repeated for each addPolygon() layer. Maybe just use one invisible, clickable layer on top for the popups?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant