r-tmap / r-tmap/tmap

Improve crs stuff

Open
#954 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Help wanted
Dominant language
R
Stars
907
Forks
123
PR merge metrics
No merged PRs in 30d

Description

Triggered by #457

Historically in tmap, projected spatial objects were preferred, not just for plotting, but also for calculating distances and areas. (With the important message to use a proper CRS for the map area). Nowadays, calculation of distances and areas is easy with s2. Not sure about the accuracy, but at least good enough and at least better than an unsuitable CRS, and therefore safer to use.

We need to decide where to do the CRS transformation in the process: before step 3 (transformation, e.g. cartograms, centroids, etc), or before step 4 (plotting). In the current v4 implementation, it takes place before step 3, but if the crs is specified as option, it is subsequently applied in step 4 (needed for the view mode). If the keep this approach, then we need to think about how to specify the crs and bbox for step 4 only.

# this is good: crs used for geometric distortion in step 3, and also for step 4 (plotting)
tm_shape(World, crs = "+proj=robin") +
tm_cartogram(size = "pop_est")
# this is not so good, because there may be step-3 layer transformations,
# for which an orthogonal projection is not good for global use cases
tm_shape(World, crs = "+proj=ortho +lat_0=0 +lon_0=0") +
tm_polygons() +
tm_style("natural")

# better to use this crs only in step 4:
tm_shape(World) +
tm_polygons() +
tm_style("natural") +
tm_options( crs = "+proj=ortho +lat_0=0 +lon_0=0")

But this is not very user-friendly I guess. Should be add another element for this purpose @Nowosad @nickbearman and others?

E.g.

tm_crs("+proj=ortho +lat_0=0 +lon_0=0")

Which sets the crs for the whole plot, so users don't have to figure other which shape is the main shape.
If we do it this way, a follow-up: what to do with the bounding box? tm_bbox ? Or a combined element tm_plot(crs = ..., bbox = ...)

Also I/we need to improve the spatial manipulation. Probably we can make use of s2 much more. Also tmaptools::bbis still used a lot in tmap, but it is outdated so needs an update.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue does not name implementation files or tests. Start by tracing the current v4 CRS flow through transformation step 3 and plotting step 4, then inspect the remaining uses of tmaptools::bb and opportunities for s2. Done means agreeing on the CRS and bounding-box API, documenting when each applies, and covering the chosen behavior with tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.