Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set CRS for latitude longitude point data

Tags:

r

proj

r-sf

r-sp

I have some points with latitude and longitude. I would like to set CRS to calculate the distance. I tried to set CRS but it shows error " error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘proj4string’ for signature ‘"CRS"’"

long <- c(133.2982, 132.6715,133.2375,133.3048,133.2594,133.2165)
lat <- c(35.5716,35.3551,35.5504,35.5707,35.5680,35.5708)
lonlat <-data.frame(cbind(long,lat))

pj <- sp::CRS("+proj=longlat +datum=WGS84")

fetch_locs = sp::SpatialPoints(lonlat[,1:2], 
                           sp::CRS(proj4string(pj)))
like image 746
marie Avatar asked Oct 31 '25 21:10

marie


1 Answers

First a few coments:

  • Currently use of proj4string is discouraged, and EPSG codes are preferred.
  • The "+datum=" declaration in a proj4string is no longer supported, Use "+ellips" instead.
  • And you might choose to switch to the newer sf package.

So:

Here is how you can create a SpatialPoints object with the older sp package:

library(sp)
long <- c(133.2982, 132.6715,133.2375,133.3048,133.2594,133.2165)
lat <- c(35.5716,35.3551,35.5504,35.5707,35.5680,35.5708)
lonlat <-data.frame(cbind(long,lat))

lonlat_sp = SpatialPoints(coords=lonlat, proj4string=CRS("+proj=longlat +ellips=WGS84"))

And here is how it's done using the sf package

library(sf)
Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1
lonlat_sf = st_as_sf(lonlat, coords=c("long", "lat"), crs="EPSG:4326")
like image 176
Micha Avatar answered Nov 03 '25 12:11

Micha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!