assign CRS to a SpatialPonitsDataFrame object

not sure if here are some users specialized in Geospatial data analysis.

I want to assign a coordination system to a SpatialPointDataFrame object, 'f'

I used code like project(f) <- CRS("+init=epsg:2285") in the past which worked well. It requires to load library (raster)

However, when I run similar code again today, I got the error below:
"Error in if (is.na(get("has_proj_def.dat", envir = .RGDAL_CACHE))) { :
argument is of length zero"

not sure if the function has changed since the last time I used it.

Thank you for whoever may help

Depending on your use case you may consider moving from {sp} package data format to {sf} data format. This would make your workflow easier (and if you absolutely positively require the sp format it is not hard to convert it back).

From your question it is not immediately obvious if you need to "just" assign a CRS (for example because the information was lost somewhere in translation) or reproject from one CRS to another (from one in degrees to a feety one). Both are possible.

library(sf) # just because...

new_f <- st_as_sf(f) # convert from sp to sf package format

# tells object new f that it is in EPSG 2285, without changing any value
new_f <- st_set_crs(new_f, 2285) 

# projects new f from existing CRS (e.g. 4326) to EPSG:2285
# coordinate values are changed
new_f <- st_transform(new_f, 2285)

# saves new_f in the old {sp} format
old_f <- as(new_f, "Spatial")
1 Like

Hi Jlacko,
Thank you so much for your response.

  • Before I saw your answer, I found this post on stack overflow which suggests re-install 'sp' package that works for me.

  • I then tried 'sf' package and your example code which does give me the same results as well :slight_smile:

The reason I used 'sp' because I have the book 'Applied Spatial Data Analysis with R' by Bivand et al. and I learned it from there a while ago. And I agree with you that some methods and relevant workflow when working with 'sp' is not so convenient. I understand that book is a bit old and I am actually about to start reading 'Geocoomputation with R' by Lovelace, Nowosad and Muenchow and I notice 'sf' is used.

If you have any other good learning resource or any shiny app project using 'sf' can recommend, it would be much appreciated.

Once a while I have to analyze some remote sensing data for ground or building deformation monitoring.

Glad to be of service!

Roger Bivand is a towering figure in spatial R, but his book starts showing its age. The statistical parts are rock solid, but the data manipulation parts feel somewhat dated. The package world is changing at a breakneck speed.

I am certain that you will find the Lovelace & al book an interesting read.

An interesting read on spatial data ecosystem - touching the sp / sf issue - has been published. Written by Mr. Bivand himself, none the less.

Well worth the read by users both old and new: https://link.springer.com/article/10.1007/s10109-020-00336-0

Hi Jlacko
Thank you for sharing. It is indeed a nice read and also a useful tutorial.

1 Like