I have two shapefiles, points and linestring, as shown below. In my R (version 4.2 on Windows 10), I am trying to snap all the points to the nearest line and getting error.
library(maptools)
library(rgdal)
library(sp)
library(sf)
# setwd(path_to_dir)
points <- read_sf("points.shp")
line <- read_sf("line.shp")
# Both files are in the same UTM projection CRS
# Drop ZM
points$geometry <- st_zm(points$geometry)
# Snap
pts_snap <- snapPointsToLines(points, line, maxDist=50, withAttrs = TRUE, idField="IDField")
I get the following error:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘is.projected’ for signature ‘"sf"’
I found a relevant question here, and tried steps suggested there to fix the error, but no luck.
class(points)
# [1] "sf" "tbl_df" "tbl" "data.frame"
class(line)
# [1] "sf" "tbl_df" "tbl" "data.frame"
# Transform to WGS 84
points <- st_transform(points,4326)
line <- st_transform(line,4326)
shape_proj1 <-st_transform(points, CRS("+proj=gnom +lat_0=90 +lon_0=-50"))
shape_proj2 <-st_transform(line, CRS("+proj=gnom +lat_0=90 +lon_0=-50"))
# and tried Snapping again, but the same error
Can anyone help me to fix the error?
Sample scenario:
Edit 1:
Here is ST_CRS(layer)
info.
> sf::st_crs(points)
Coordinate Reference System:
User input: ETRS89 / UTM zone 32N
wkt:
PROJCRS["ETRS89 / UTM zone 32N",
BASEGEOGCRS["ETRS89",
DATUM["European Terrestrial Reference System 1989",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4258]],
CONVERSION["UTM zone 32N",
METHOD["Transverse Mercator",
ID["EPSG",9807]],
PARAMETER["Latitude of natural origin",0,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8801]],
PARAMETER["Longitude of natural origin",9,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8802]],
PARAMETER["Scale factor at natural origin",0.9996,
SCALEUNIT["unity",1],
ID["EPSG",8805]],
PARAMETER["False easting",500000,
LENGTHUNIT["metre",1],
ID["EPSG",8806]],
PARAMETER["False northing",0,
LENGTHUNIT["metre",1],
ID["EPSG",8807]]],
CS[Cartesian,2],
AXIS["(E)",east,
ORDER[1],
LENGTHUNIT["metre",1]],
AXIS["(N)",north,
ORDER[2],
LENGTHUNIT["metre",1]],
USAGE[......