I am aware that similar questions have been asked before, but I do not understand the answers; I am completely new to spatial analysis. I have a single point and a set of polygons that look as below. I would like to calculate the distance from the centroids of the polygons to the point, but the polygons are in lambert canonical and the point is in latitude longitude.
This code is reproducible
library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
#> Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(tidygeocoder)
library(tidyverse)
address<-tibble(street="935 Ramsey Lake Road",
city="Sudbury", province="ON", postalcode="P3E 2C6", country="Canada")
laurentian<-geocode(.tbl=address, street="street", city="city", state="province", postalcode="postalcode", country="country")
#> Passing 1 address to the Nominatim single address geocoder
#> Query completed in: 1 seconds
#> Passing 1 address to the Nominatim single address geocoder
#> Query completed in: 1 seconds
laurentian<-st_as_sf(laurentian,coords=c('lat', 'long'))
poll<-structure(list(OBJECTID = 63404L, ED_ID = 103, PD_NUMBER = 413,
PD_LABEL = "PD 413", ED_NAME_EN = "Sudbury", ED_NAME_FR = "Sudbury",
SHAPE_Leng = 452.723045332, SHAPE_Area = 12088.4481776, Year = "2018",
geometry = structure(list(structure(c(1229668.80703722, 5795765.97362501
), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
"sfc"), precision = 0, bbox = structure(c(xmin = 1229668.80703722,
ymin = 5795765.97362501, xmax = 1229668.80703722, ymax = 5795765.97362501
), class = "bbox"), crs = structure(list(input = "EO_Lambert_Conformal_Conic",
wkt = "PROJCRS[\"EO_Lambert_Conformal_Conic\",\n BASEGEOGCRS[\"NAD83\",\n DATUM[\"North American Datum 1983\",\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]],\n ID[\"EPSG\",6269]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"Degree\",0.0174532925199433]]],\n CONVERSION[\"unnamed\",\n METHOD[\"Lambert Conic Conformal (2SP)\",\n ID[\"EPSG\",9802]],\n PARAMETER[\"Latitude of false origin\",0,\n ANGLEUNIT[\"Degree\",0.0174532925199433],\n ID[\"EPSG\",8821]],\n PARAMETER[\"Longitude of false origin\",-84,\n ANGLEUNIT[\"Degree\",0.0174532925199433],\n ID[\"EPSG\",8822]],\n PARAMETER[\"Latitude of 1st standard parallel\",44.5,\n ANGLEUNIT[\"Degree\",0.0174532925199433],\n ID[\"EPSG\",8823]],\n PARAMETER[\"Latitude of 2nd standard parallel\",54.5,\n ANGLEUNIT[\"Degree\",0.0174532925199433],\n ID[\"EPSG\",8824]],\n PARAMETER[\"Easting at false origin\",1000000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8826]],\n PARAMETER[\"Northing at false origin\",0,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8827]]],\n CS[Cartesian,2],\n AXIS[\"(E)\",east,\n ORDER[1],\n LENGTHUNIT[\"metre\",1,\n ID[\"EPSG\",9001]]],\n AXIS[\"(N)\",north,\n ORDER[2],\n LENGTHUNIT[\"metre\",1,\n ID[\"EPSG\",9001]]]]"), class = "crs"), n_empty = 0L)), row.names = c(NA,
-1L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(c(OBJECTID = NA_integer_,
ED_ID = NA_integer_, PD_NUMBER = NA_integer_, PD_LABEL = NA_integer_,
ED_NAME_EN = NA_integer_, ED_NAME_FR = NA_integer_, SHAPE_Leng = NA_integer_,
SHAPE_Area = NA_integer_, Year = NA_integer_), class = "factor", levels = c("constant",
"aggregate", "identity")))
poll<-st_centroid(poll)
Created on 2025-04-24 with reprex v2.1.1
Your current laurentian
(the sf
object ) is without coordinate reference system, we can fix this through crs
when calling st_as_sf()
. From there we can transform poll
to the same crs with st_transform(poll, st_crs(laurentian))
:
library(sf)
#> Linking to GEOS 3.13.1, GDAL 3.10.2, PROJ 9.5.1; sf_use_s2() is TRUE
( laurentian <- st_as_sf(laurentian, coords=c('long', 'lat'), crs = "WGS84") )
#> Simple feature collection with 1 feature and 5 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -80.96433 ymin: 46.46364 xmax: -80.96433 ymax: 46.46364
#> Geodetic CRS: WGS 84
#> street city province postalcode country geometry
#> 1 935 Ramsey Lake Road Sudbury ON P3E 2C6 Canada POINT (-80.96433 46.46364)
poll
#> Simple feature collection with 1 feature and 9 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: 1229669 ymin: 5795766 xmax: 1229669 ymax: 5795766
#> Projected CRS: EO_Lambert_Conformal_Conic
#> OBJECTID ED_ID PD_NUMBER PD_LABEL ED_NAME_EN ED_NAME_FR SHAPE_Leng SHAPE_Area Year geometry
#> 1 63404 103 413 PD 413 Sudbury Sudbury 452.723 12088.45 2018 POINT (1229669 5795766)
st_distance(laurentian, st_transform(poll, st_crs(laurentian)))
#> Units: [m]
#> [,1]
#> [1,] 2906.559