0

I am trying to make the projection of a set of coordinates identical to a shape from wrld_simpl. But I keep getting the error below. How to solve that?

d=read.csv()
it= wrld_simpl[wrld_simpl$NAME == "Italy", ] 
d=na.omit(d)
d1=d[,c("lon","lat")]
coordinates(d1)=~lon+lat
d1=spTransform(d1,CRSobj="+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 
+no_defs") 

Error in if (is.projected(obj)) { : valor ausente donde TRUE/FALSE es necesario Además: Warning message: In spTransform(xSP, CRSobj, ...) : NULL source CRS comment, falling back to PROJ string

Spacedman
69.2k6 gold badges84 silver badges125 bronze badges
asked Sep 3, 2023 at 18:38
1

1 Answer 1

1

There's a few problems here: d1 at the error point has an NA (missing) CRS so any attempt to reproject it with spTransform will fail.

On my system it fails with a different error message:

> d1=spTransform(d1,CRSobj="+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs") 
Error in st_transform.sfc(st_geometry(x), crs, ...) : 
 cannot transform sfc object with missing crs

which makes me think you have an old version of sp.

To assign a CRS to an sp object you can use proj4string(d1) <- like:

proj4string(d1) <- "+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs"

this is telling sp what coordinate system the numbers are. Since they look like lat-long WGS84 coordinates, I've used the proj4string you've got from the world map. At this point you should be able to plot them both with no more transformation needed:

> plot(it)
> plot(d1, add=TRUE)

enter image description here

If I plot the whole of the points and add the world it looks like everything lines up:

> plot(d1)
> plot(wrld_simpl, add=TRUE)

enter image description here

The other problem is that the sp package is deprecated and you should look at using the sf package and its data structures instead.

answered Sep 4, 2023 at 11:07
1
  • Thank you @spacedman! It seems I will have to stop delaying that shift to sf in my scripts. Commented Sep 5, 2023 at 12:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.