tmap icon indicating copy to clipboard operation
tmap copied to clipboard

extensions: spatial object classes

Open mtennekes opened this issue 1 year ago • 3 comments

So far, tmap v4 can handle sf objects, stars and SpatRaster objects natively. At the moment SpatVector are cast to sf objects (can be improved later).

A difference with tmap3, is that it should be possible for developers to write methods for their own spatial classes. I don't expect that this would happen a lot, but in essence tmap4 should work with any spatial object class.

To test the extendability, I wrote a pretty rudimentary but working code for sfnetworks. Trigger: https://github.com/luukvdmeer/sfnetworks/discussions/126#discussioncomment-6614048

library(sfnetworks)
library(tmap)
sfn <- as_sfnetwork(roxel)

tm_shape(sfn) +
    tm_lines(col = "type", lwd = 2) +
    tm_dots(size = .3)
#> [1] "tmapGetShapeMeta1"
#> [1] "tmapSubsetShp"
#> [1] "tmapGetShapeMeta2"
#> [1] "tmapShape"

Created on 2023-08-04 with reprex v2.0.2

Soem background: tmapShape.<x> is the main method to process spatial objects. In tmap, the geometry is separated from the data:

  • geometry: stored in an object called shapeTM, which contains:
    • shp: for sf objects an sfc object, for stars an empty raster
    • tmapID: id values, one for each spatial feature (so 1,...n)
    • bbox

*. data: a data.table with two additional columns:

  • tmapID__: the id that can be joined to the geometry tmapID
  • sel__: a logical that determines if this feature is plotted (only relevant if filter from tm_shape is used)

Currently, there are three preprocessing functions prior to tmapShape: tmapGetShapeMeta1, tmapSubsetShp, tmapGetShapeMeta2

The aim is that large spatial objects are processed as fast as possible. For information is found in the code comments in https://github.com/r-tmap/tmap/blob/v4/demo/tutorials/sfnetworks.R

mtennekes avatar Aug 04 '23 06:08 mtennekes

The fact that works out-of-the box, as far as I can tell, is super impressive!

Robinlovelace avatar Aug 04 '23 08:08 Robinlovelace

Check out https://github.com/r-tmap/tmap.sfnetworks

Still a lot of code-cleaning to do, but at least it's working

mtennekes avatar Aug 13 '23 21:08 mtennekes

Ace.

Robinlovelace avatar Aug 13 '23 21:08 Robinlovelace