sf icon indicating copy to clipboard operation
sf copied to clipboard

Turn some messages into warnings?

Open barryrowlingson opened this issue 4 years ago • 2 comments

A few things in sf produce messages which seem to be output via message, via functions such as:

https://github.com/r-spatial/sf/blob/245f1b3bb5b8ea1d5ba18dedbd654f9016bb47a6/R/geom-measures.R#L111

for example, "dist is assumed to be in decimal degrees (arc_degrees)." when doing a buffer:

> nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet=TRUE)
> bnc = st_buffer(nc, .01)
dist is assumed to be in decimal degrees (arc_degrees).
Warning message:
In st_buffer.sfc(st_geometry(x), dist, nQuadSegs, endCapStyle = endCapStyle,  :
  st_buffer does not correctly buffer longitude/latitude data

which displays that as a message and also produces a warning.

Also, "although coordinates are longitude/latitude, st_intersects assumes that they are planar" appears as a message if:

> z = st_intersects(nc, nc)
although coordinates are longitude/latitude, st_intersects assumes that they are planar

These are in some sense warning the user that they've made an assumption that might not be true, so I'd argue they should be warnings. If they were generated as "warning()" output then they would appear with other warnings in the order of generation, making it easier to debug complex scripts when the origin of an "st_buffer does not correctly buffer longitude/latitude data" might not be obvious. Users can also trap, silence, or ignore warnings via the usual R methods. Neither st_buffer nor st_intersects have a "quiet" option (like st_read) to silence these outputs.

barryrowlingson avatar Oct 26 '20 12:10 barryrowlingson

Users also have the option to suppress messages, and I've seen packages that use sf using suppressMessages() around those calls to sf that generate messages. But they can't make a message into an error, as you can with warnings. I think your example above is "only" a message as depending on GEOS (PostGIS, QGIS, geopandas) does it like this, but without any notification AFAIK. We can elevate this into a warning once spherical geometry (s2) becomes the default but has been switched off on purpose.

edzer avatar Nov 12 '20 21:11 edzer

A related request is to emit these messages only once per operation. For example, the following call to summarize emits although coordinates are longitude/latitude, st_union assumes that they are planar 22 times:

nc %>%
  group_by(first_letter = substr(NAME, 1, 1)) %>%
  summarize(do_union = TRUE)

dbaston avatar Dec 08 '20 00:12 dbaston

reprex:

library(sf)
# Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.1; sf_use_s2() is TRUE
library(dplyr)

# Attaching package: ‘dplyr’

# The following objects are masked from ‘package:stats’:

#     filter, lag

# The following objects are masked from ‘package:base’:

#     intersect, setdiff, setequal, union

demo(nc, ask=FALSE,echo=FALSE)
sf_use_s2(FALSE)
# Spherical geometry (s2) switched off
nc %>%
  group_by(first_letter = substr(NAME, 1, 1)) %>%
  summarize(do_union=TRUE)
# although coordinates are longitude/latitude, st_union assumes that they are
# planar
# Simple feature collection with 22 features and 1 field
# Geometry type: MULTIPOLYGON
# Dimension:     XY
# Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
# Geodetic CRS:  NAD27
# # A tibble: 22 × 2
#    first_letter                                                             geom
#    <chr>                                                      <MULTIPOLYGON [°]>
#  1 A            (((-80.27512 35.19311, -80.26109 35.20404, -80.24759 35.20452, …
#  2 B            (((-78.63027 34.0102, -78.58778 34.03061, -78.56343 34.05894, -…
#  3 C            (((-79.00642 34.36627, -79.00224 34.38804, -78.97536 34.39917, …
#  4 D            (((-77.93931 34.71941, -77.98915 34.71709, -78.01736 34.72893, …
#  5 E            (((-77.67122 35.67027, -77.73315 35.73955, -77.75749 35.7981, -…
#  6 F            (((-78.26685 35.84838, -78.30841 35.89345, -78.32954 35.88785, …
#  7 G            (((-81.31943 35.2605, -81.35235 35.2751, -81.3648 35.31037, -81…
#  8 H            (((-79.45536 35.03736, -79.35005 35.1408, -79.23529 35.20321, -…
#  9 I            (((-80.72652 35.50757, -80.77623 35.50681, -80.95535 35.50912, …
# 10 J            (((-77.05746 34.81505, -77.07314 34.81544, -77.10069 34.7975, -…
# # … with 12 more rows

edzer avatar Mar 26 '23 20:03 edzer