jbaileyh / geogrid

Turning geospatial polygons into regular or hexagonal grids. For other similar functionality see the tilemaps package https://github.com/kaerosen/tilemaps
Other
393 stars 31 forks source link

sf support by objects conversion #25

Closed Nowosad closed 6 years ago

Nowosad commented 6 years ago

@jbaileyh, I've added an sf support for the geogrid package by creating new methods and objects conversion. It is (probably) the easiest way for allowing sf objects without rewriting the whole code.

An example is attached below:

library(geogrid)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.4, proj.4 4.9.3
library(spData)
library(tmap)
us_states = st_transform(us_states, 2163)

hex_cells = calculate_grid(us_states, grid_type = "hexagonal", seed = 25, learning_rate = 0.03)
us_states_hex = st_as_sf(assign_polygons(us_states, hex_cells))

reg_cells = calculate_grid(us_states, grid_type = "regular", seed = 25, learning_rate = 0.03)
us_states_reg = st_as_sf(assign_polygons(us_states, reg_cells))

m1 = tm_shape(us_states) + tm_polygons()
m2 = tm_shape(us_states_hex) + tm_polygons()
m3 = tm_shape(us_states_reg) + tm_polygons()

tmap_arrange(m1, m2, m3)

Created on 2018-04-29 by the reprex package (v0.2.0).

Related to https://github.com/jbaileyh/geogrid/issues/8.

jbaileyh commented 6 years ago

@Nowosad fantastic - thanks for the contribution. I'm aware that I really should have started supporting sf out of the box so this is really helpful. Next step - using them in HTML widgets.