andybega / forecaster2

Coup forecasts
https://www.predictiveheuristics.com/forecasts
MIT License
3 stars 0 forks source link

Reduce spatial data size #11

Open andybega opened 4 years ago

andybega commented 4 years ago

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:

andybega commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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?