sf icon indicating copy to clipboard operation
sf copied to clipboard

st_graticule() produces hundreds of tiny line segments when break falls onto bbox boundary

Open clauswilke opened this issue 5 years ago • 1 comments

Originally reported for ggplot2 here: https://github.com/tidyverse/ggplot2/issues/3938 When an axis break falls exactly onto a bounding box boundary, st_graticule() produces hundreds of tiny line segments. While ggplot2 should probably be tolerant to this, the result is strange nonetheless, and it would be good if it could be fixed.

# works fine
bbox <- c(-18.4, -12.4, -9.6, -3.6)
sf::st_graticule(
  bbox,
  crs = NULL,
  lat = NULL,
  lon = NULL,
  datum = sf::st_crs(4326),
  ndiscr = 100
)
#> Simple feature collection with 10 features and 10 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: -18.4 ymin: -12.4 xmax: -9.6 ymax: -3.6
#> CRS:            NA
#>    degree type degree_label                       geometry x_start y_start
#> 2     -18    E          -18 LINESTRING (-18 -12.4, -18 ...   -18.0   -12.4
#> 3     -16    E          -16 LINESTRING (-16 -12.4, -16 ...   -16.0   -12.4
#> 4     -14    E          -14 LINESTRING (-14 -12.4, -14 ...   -14.0   -12.4
#> 5     -12    E          -12 LINESTRING (-12 -12.4, -12 ...   -12.0   -12.4
#> 6     -10    E          -10 LINESTRING (-10 -12.4, -10 ...   -10.0   -12.4
#> 9     -12    N          -12 LINESTRING (-18.4 -12, -18....   -18.4   -12.0
#> 10    -10    N          -10 LINESTRING (-18.4 -10, -18....   -18.4   -10.0
#> 11     -8    N           -8 LINESTRING (-18.4 -8, -18.3...   -18.4    -8.0
#> 12     -6    N           -6 LINESTRING (-18.4 -6, -18.3...   -18.4    -6.0
#> 13     -4    N           -4 LINESTRING (-18.4 -4, -18.3...   -18.4    -4.0
#>    x_end y_end angle_start angle_end plot12
#> 2  -18.0  -3.6          90        90   TRUE
#> 3  -16.0  -3.6          90        90   TRUE
#> 4  -14.0  -3.6          90        90   TRUE
#> 5  -12.0  -3.6          90        90   TRUE
#> 6  -10.0  -3.6          90        90   TRUE
#> 9   -9.6 -12.0           0         0   TRUE
#> 10  -9.6 -10.0           0         0   TRUE
#> 11  -9.6  -8.0           0         0   TRUE
#> 12  -9.6  -6.0           0         0   TRUE
#> 13  -9.6  -4.0           0         0   TRUE

# creates hundreds of graticule lines
bbox <- c(-18, -12, -10, -4)
sf::st_graticule(
  bbox,
  crs = NULL,
  lat = NULL,
  lon = NULL,
  datum = sf::st_crs(4326),
  ndiscr = 100
)
#> Simple feature collection with 506 features and 10 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: -18 ymin: -12 xmax: -10 ymax: -4
#> CRS:            NA
#> First 10 features:
#>     degree type degree_label                       geometry x_start   y_start
#> 1      -18    E          -18 LINESTRING (-18 -12, -18 -1...     -18 -12.00000
#> 1.1    -18    E          -18 LINESTRING (-18 -11.91919, ...     -18 -11.91919
#> 1.2    -18    E          -18 LINESTRING (-18 -11.83838, ...     -18 -11.83838
#> 1.3    -18    E          -18 LINESTRING (-18 -11.75758, ...     -18 -11.75758
#> 1.4    -18    E          -18 LINESTRING (-18 -11.68, -18...     -18 -11.68000
#> 1.5    -18    E          -18 LINESTRING (-18 -11.67677, ...     -18 -11.67677
#> 1.6    -18    E          -18 LINESTRING (-18 -11.59596, ...     -18 -11.59596
#> 1.7    -18    E          -18 LINESTRING (-18 -11.51515, ...     -18 -11.51515
#> 1.8    -18    E          -18 LINESTRING (-18 -11.43434, ...     -18 -11.43434
#> 1.9    -18    E          -18 LINESTRING (-18 -11.36, -18...     -18 -11.36000
#>     x_end     y_end angle_start angle_end plot12
#> 1     -18 -11.91919          90        90   TRUE
#> 1.1   -18 -11.83838          90        90  FALSE
#> 1.2   -18 -11.75758          90        90  FALSE
#> 1.3   -18 -11.68000          90        90  FALSE
#> 1.4   -18 -11.67677          90        90  FALSE
#> 1.5   -18 -11.59596          90        90  FALSE
#> 1.6   -18 -11.51515          90        90  FALSE
#> 1.7   -18 -11.43434          90        90  FALSE
#> 1.8   -18 -11.36000          90        90  FALSE
#> 1.9   -18 -11.35354          90        90  FALSE

# works fine, breaks are internal only
sf::st_graticule(
  bbox,
  crs = NULL,
  lat = c(-10, -8, -6),
  lon = c(-16, -14, -12),
  datum = sf::st_crs(4326),
  ndiscr = 100
)
#> Simple feature collection with 6 features and 10 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: -18 ymin: -12 xmax: -10 ymax: -4
#> CRS:            NA
#>   degree type degree_label                       geometry x_start y_start x_end
#> 1    -16    E          -16 LINESTRING (-16 -12, -16 -1...     -16     -12   -16
#> 2    -14    E          -14 LINESTRING (-14 -12, -14 -1...     -14     -12   -14
#> 3    -12    E          -12 LINESTRING (-12 -12, -12 -1...     -12     -12   -12
#> 4    -10    N          -10 LINESTRING (-18 -10, -17.91...     -18     -10   -10
#> 5     -8    N           -8 LINESTRING (-18 -8, -17.919...     -18      -8   -10
#> 6     -6    N           -6 LINESTRING (-18 -6, -17.919...     -18      -6   -10
#>   y_end angle_start angle_end plot12
#> 1    -4          90        90   TRUE
#> 2    -4          90        90   TRUE
#> 3    -4          90        90   TRUE
#> 4   -10           0         0   TRUE
#> 5    -8           0         0   TRUE
#> 6    -6           0         0   TRUE

# problematic again, the breaks are right on the bounding box boundary
sf::st_graticule(
  bbox,
  crs = NULL,
  lat = c(-12, -4),
  lon = c(-18, -10),
  datum = sf::st_crs(4326),
  ndiscr = 100
)
#> Simple feature collection with 492 features and 10 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: -18 ymin: -12 xmax: -10 ymax: -4
#> CRS:            NA
#> First 10 features:
#>     degree type degree_label                       geometry x_start   y_start
#> 1      -18    E          -18 LINESTRING (-18 -12, -18 -1...     -18 -12.00000
#> 1.1    -18    E          -18 LINESTRING (-18 -11.91919, ...     -18 -11.91919
#> 1.2    -18    E          -18 LINESTRING (-18 -11.83838, ...     -18 -11.83838
#> 1.3    -18    E          -18 LINESTRING (-18 -11.75758, ...     -18 -11.75758
#> 1.4    -18    E          -18 LINESTRING (-18 -11.68, -18...     -18 -11.68000
#> 1.5    -18    E          -18 LINESTRING (-18 -11.67677, ...     -18 -11.67677
#> 1.6    -18    E          -18 LINESTRING (-18 -11.59596, ...     -18 -11.59596
#> 1.7    -18    E          -18 LINESTRING (-18 -11.51515, ...     -18 -11.51515
#> 1.8    -18    E          -18 LINESTRING (-18 -11.43434, ...     -18 -11.43434
#> 1.9    -18    E          -18 LINESTRING (-18 -11.36, -18...     -18 -11.36000
#>     x_end     y_end angle_start angle_end plot12
#> 1     -18 -11.91919          90        90   TRUE
#> 1.1   -18 -11.83838          90        90  FALSE
#> 1.2   -18 -11.75758          90        90  FALSE
#> 1.3   -18 -11.68000          90        90  FALSE
#> 1.4   -18 -11.67677          90        90  FALSE
#> 1.5   -18 -11.59596          90        90  FALSE
#> 1.6   -18 -11.51515          90        90  FALSE
#> 1.7   -18 -11.43434          90        90  FALSE
#> 1.8   -18 -11.36000          90        90  FALSE
#> 1.9   -18 -11.35354          90        90  FALSE

Created on 2020-04-11 by the reprex package (v0.3.0)

clauswilke avatar Apr 12 '20 04:04 clauswilke

Upon further reflection, ggplot2 is actually fully tolerant to these graticules (https://github.com/tidyverse/ggplot2/issues/3938#issuecomment-612563396), so fixing this issue is maybe not as urgent as it initially seemed to be. Nevertheless, if it can be fixed that'd be nice.

clauswilke avatar Apr 12 '20 04:04 clauswilke