marie
marie

Reputation: 385

set CRS for latitude longitude point data

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)))

Upvotes: 4

Views: 3234

Answers (1)

Micha
Micha

Reputation: 493

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")

Upvotes: 8

Related Questions