ATFutures / geoplumber

Serve geographic data from R and consume with scalable front end.
https://atfutures.github.io/geoplumber/
59 stars 7 forks source link

Create max augement to set number of features to show on map with sensible default #25

Closed Robinlovelace closed 4 years ago

Robinlovelace commented 6 years ago

Reproducible example below. The idea is that the view function (existing or created) will check the number of features (or maybe vertices with sf::st_coordinates()) in the current bounding box before sending anything. If there are more than max in there there function will sample from the points. When you zoom-in more features will be shown. That would be awesome. It's what we do in CyIPT.

Robinlovelace commented 6 years ago
# reproducible example
library(stplanr)
ac <- read_stats19_ac() # 214 mb compressed data
names(ac)
ac = ac[!is.na(ac$Latitude), ]
ac_sf = sf::st_as_sf(ac, coords = c("Longitude", "Latitude"))

pryr::object_size(ac_sf) # 600 MB
# now we've generated a large spatial dataset let's see how long it takes to visualize

system.time({
  plot(ac_sf$geometry) # note: blocks your R session for ages: unhelpful, we need an alternative!
})

system.time({
  plot(ac_sf$geometry[sample(nrow(ac_sf), size = 1e4)]) # way faster but also clunky
})

# now interactively
mapview::mapview(ac_sf) # freezes your R session

# with leaflet WIP...
library(leaflet)

# with geoplumber ...
Robinlovelace commented 6 years ago

Here's the beginnings of making the max arg work:

# Below is part of stats19 endpoint:
library(stplanr)
ac <- read_stats19_ac() # 214 mb compressed data
names(ac)
ac = ac[!is.na(ac$Latitude), ]
ac_sf = sf::st_as_sf(ac, coords = c("Longitude", "Latitude"), crs = 4326)
b = spData::lnd

ac_lnd = ac_sf[b, ]
plot(sf::st_geometry(ac_lnd[sample(nrow(ac_lnd), 1000), ]))
ac_sf_mini = ac_sf[1:1000, ]
#' @get /api/uol
uol_geojson <- function(res){
  res$body <- ac_sf_mini
  res
}
layik commented 6 years ago

Poor "plumber", cannot convert sf object into GeoJSON on its own. Two things: 1) code below uses geojsonio not geojsonsf as it fails to parse, 2)code is NOT reprex'able @Robinlovelace.

I wonder if @SymbollixAU would like to have a look at (1) or perhaps open an issue so we can continue using geojsonsf blazing fast package. Error is Error in rcpp_geojson_to_sf(geojson, expand_geometries) : Not compatible with STRSXP: [type=list].

# Below is part of stats19 endpoint:
library(stplanr)
ac <- read_stats19_ac() # 214 mb compressed data
names(ac)
ac = ac[!is.na(ac$Latitude), ]
ac_sf = sf::st_as_sf(ac, coords = c("Longitude", "Latitude"), crs = 4326)
b = spData::lnd

ac_lnd = ac_sf[b, ]
plot(sf::st_geometry(ac_lnd[sample(nrow(ac_lnd), 1000), ]))
ac_sf_mini = ac_sf[1:1000, ]
ac_sf_mini <- geojsonio::geojson_json(ac_sf_mini)

#' @get /api/uol
uol_geojson <- function(res){
  res$headers$`Content-type` <- "application/json" # not necessary.
  res$body <- ac_sf_mini
  res
}
Robinlovelace commented 6 years ago

Heads-up @symbolixAU - @layik the tag didn't work 1st time around. Maybe open-up an issue if one would be appreciated 😄

layik commented 6 years ago

Done https://github.com/SymbolixAU/geojsonsf/issues/29

SymbolixAU commented 6 years ago

closed in https://github.com/SymbolixAU/geojsonsf/issues/29#issuecomment-422572949

you need to use sf_geojson() to go from sf to geojson

layik commented 6 years ago

Thanks @SymbolixAU

Correct code then:

# Below is part of stats19 endpoint:
library(stplanr)
ac <- read_stats19_ac() # 214 mb compressed data
names(ac)
ac = ac[!is.na(ac$Latitude), ]
ac_sf = sf::st_as_sf(ac, coords = c("Longitude", "Latitude"), crs = 4326)
b = spData::lnd

ac_lnd = ac_sf[b, ]
plot(sf::st_geometry(ac_lnd[sample(nrow(ac_lnd), 1000), ]))
ac_sf_mini = ac_sf[1:1000, ]
ac_sf_mini <- geojsonsf::sf_geojson(ac_sf_mini)

#' @get /api/uol
uol_geojson <- function(res){
  res$headers$`Content-type` <- "application/json" # not necessary.
  res$body <- ac_sf_mini
  res
}
layik commented 6 years ago

Other tickets just jump the priority queue over this one.

layik commented 4 years ago

title not clear so closing but happy to reopen again.