-1

I am quite new to working with spatial dataframes, and have what I thought was a relatively simple task: take a dataframe of 6 points, with x and y columns representing the lat/long positions of those points, and project them so that they can be used in a spatial data frame that I have made.

Here is the way I coded in the 6 points:

d1 <- structure(list(latitude = c(37.427733, 37.565759, 37.580956, 37.429285, 37.424270, 37.502496), longitude = c(-108.011061, -107.814039, -107.676662, -107.677166, -108.898826, -108.586042)))

d2 <- as.data.frame(d1)

d3 <- SpatialPointsDataFrame(c(d2[,c('longitude','latitude')]), data = d2)

And I tried changing/assigning a projection for these (these lat/long data were taken from Google Maps), but I can't seem to make it work. The projection for the data I want to overlay these points on is the following:

+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs

So basically my question is, how can I convert these lat/long into the format for x/y that this projection uses? Here is the extent of the dataset I want to overlay it on for reference, showing that it is clearly not in simple lat/long:

class      : Extent 
xmin       : -1145835 
xmax       : -1011345 
ymin       : 1613205 
ymax       : 1704855 

Thank you all so much in advance!

Cameron
  • 164
  • 14

1 Answers1

2

You need to (re)project your spatial points into the same projection as your other data source. I'm more familiar with the sf package for working with spatial vector information in R, but it looks like you are using the sp packcage.

The first step is to assign your data frame to the correct projection. Latitude and longitude are generally in WGS84, or epsg:4326 so:

library(sp)

d1 <- structure(list(latitude = c(37.427733, 37.565759, 37.580956, 37.429285, 37.424270, 37.502496), longitude = c(-108.011061, -107.814039, -107.676662, -107.677166, -108.898826, -108.586042)))

d2 <- as.data.frame(d1)

d3 <- SpatialPointsDataFrame(c(d2[,c('longitude','latitude')]), data = d2)


proj4string(d3) <- CRS("+init=epsg:4326")

sf::st_bbox(d3)
# xmin       ymin       xmax       ymax 
# -108.89883   37.42427 -107.67666   37.58096 

Looking at the summary, or this extent, you can see that the lat and long are as the original. Now we can reproject using the proj4string you supplied

target_crs = CRS("+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")  # This is your string assigned to an object


d4 = spTransform(d3, target_crs) ## object used to transform your data frame

sf::st_bbox(d4)
# xmin     ymin     xmax     ymax 
# -1127246  1661667 -1018854  1679942 

Your coordinates are now represented in the target space, and should be able to be plotted on top of your background data.

If you are willing to use an alternative package, sf::st_transform() is a little easier to use, and the sf package is generally more user friendly.

Brian Fisher
  • 1,305
  • 7
  • 17
  • Thank you so much! This works great. I think you're right that the sp format threw me off somewhat. Thank you so much for the kind and helpful feedback. – Cameron Dec 05 '20 at 17:00