given an sf point geometry column, return COMID, reachcode, and measure for each.

get_flowline_index(
  flines,
  points,
  search_radius = NULL,
  precision = NA,
  max_matches = 1
)

Arguments

flines

sf data.frame of type LINESTRING or MULTILINESTRING including COMID, REACHCODE, ToMeas, and FromMeas. Can be "download_nhdplusv2" and remote nhdplusv2 data will be downloaded for the bounding box surround the submitted points. NOTE: The download option may not work for large areas, use with caution.

points

sf or sfc of type POINT in analysis projection. NOTE: flines will be projected to the projection of the points layer.

search_radius

units distance for the nearest neighbor search to extend in analysis projection. If missing or NULL, and points are in a lon lat projection, a default of 0.01 degree is used, otherwise 200 m is used. Conversion to the linear unit used by the provided crs of points is attempted. See RANN nn2 documentation for more details.

precision

numeric the resolution of measure precision in the output in meters.

max_matches

numeric the maximum number of matches to return if multiple are found in search_radius

Value

data.frame with five columns, id, COMID, REACHCODE, REACH_meas, and offset. id is the row or list element in the point input.

Details

Note 1: Inputs are cast into LINESTRINGS. Because of this, the measure output of inputs that are true multipart lines may be in error.

Note 2: This algorithm finds the nearest node in the input flowlines to identify which flowline the point should belong to. As a second pass, it can calculate the measure to greater precision than the nearest flowline geometry node.

Note 3: Offset is returned in units consistent with the projection of the input points.

Note 4: See `dfMaxLength` input to sf::st_segmentize() for details of handling of precision parameter.

Note 5: "from" is downstream -- 0 is the outlet "to" is upstream -- 100 is the inlet

Examples

# \donttest{

source(system.file("extdata", "sample_flines.R", package = "nhdplusTools"))

point <- sf::st_sfc(sf::st_point(c(-76.87479, 39.48233)),
                    crs = 4326)

get_flowline_index(sample_flines, point)
#> Warning: converting to LINESTRING, this may be slow, check results
#>   id    COMID      REACHCODE REACH_meas       offset
#> 1  1 11689978 02060003001472     51.502 0.0003010502

point <- sf::st_transform(point, 5070)

get_flowline_index(sample_flines, point,
                   search_radius = units::set_units(200, "m"))
#> Warning: crs of lines and points don't match. attempting st_transform of lines
#> Warning: converting to LINESTRING, this may be slow, check results
#>   id    COMID      REACHCODE REACH_meas   offset
#> 1  1 11689978 02060003001472    51.8751 25.88438

get_flowline_index("download_nhdplusv2", point)
#> Spherical geometry (s2) switched off
#> although coordinates are longitude/latitude, st_intersects assumes that they
#> are planar
#> Spherical geometry (s2) switched on
#>   id    COMID      REACHCODE REACH_meas   offset
#> 1  1 11689978 02060003001472    51.8761 25.85641

get_flowline_index(sample_flines, point, precision = 30)
#> Warning: crs of lines and points don't match. attempting st_transform of lines
#> Warning: converting to LINESTRING, this may be slow, check results
#>   id    COMID      REACHCODE REACH_meas   offset
#> 1  1 11689978 02060003001472    52.7758 4.302007

get_flowline_index(sample_flines,
                   sf::st_sfc(list(sf::st_point(c(-76.86934, 39.49328)),
                                   sf::st_point(c(-76.91711, 39.40884)),
                                   sf::st_point(c(-76.88081, 39.36354))),
                              crs = 4326),
                   search_radius = units::set_units(0.2, "degrees"),
                   max_matches = 10)
#> Warning: converting to LINESTRING, this may be slow, check results
#>    id    COMID      REACHCODE REACH_meas       offset
#> 1   1 11688298 02060003000579     0.0000 6.026811e-05
#> 2   2 11688808 02060003000519    53.5874 5.641410e-04
#> 3   3 11688980 02060003000253    75.3779 3.102970e-04
#> 4   1 11689926 02060003001467   100.0000 6.026811e-05
#> 5   3 11688950 02060003000254    18.5358 1.131181e-03
#> 6   1 11689928 02060003001468     0.0000 2.026592e-03
#> 7   1 11689978 02060003001472   100.0000 2.026592e-03
#> 8   1 11690530 02060003000585     0.0000 2.853201e-03
#> 9   1 11690490 02060003000580     0.0000 2.853201e-03
#> 10  3 11688948 02060003000516     0.0000 3.207544e-03
#> 11  2 11690110 02060003001493   100.0000 7.424781e-03
#> 12  1 11690532 02060003000256     0.0000 4.513088e-03
#> 13  2 11688822 02060003000518    39.5280 7.677891e-03
#> 14  1 11688296 02060003000584     0.0000 6.017732e-03
#> 15  1 11688338 02060003000581     0.0000 6.017732e-03
#> 16  2 11688742 02060003000521     0.0000 8.549572e-03
#> 17  3 11689022 02060003000252    54.2321 6.961051e-03
#> 18  2 11688778 02060003000520     0.0000 8.549572e-03
#> 19  1 11687550 02060003000585    18.5588 6.832206e-03
#> 20  2 11690112 02060003001494   100.0000 9.547712e-03
#> 21  3 11688990 02060003000515     1.9144 8.652186e-03
#> 22  2 11690122 02060003001495   100.0000 1.026162e-02
#> 23  3 11689016 02060003000294    18.9609 1.162680e-02
#> 24  2 11688868 02060003000517    27.7564 1.694256e-02
#> 25  3 11689072 02060003000251   100.0000 1.366486e-02
#> 26  2 11690124 02060003001496   100.0000 1.953370e-02
#> 27  3 11690130 02060003001499     0.0000 1.638423e-02
#> 28  2 11690128 02060003001498   100.0000 2.099143e-02
#> 29  3 11689696 02060003000387     0.4731 1.872134e-02
#> 30  3 11689006 02060003000295     0.0000 1.876644e-02

# }