Reputation: 199
I have a number of shapefiles which were created in ArcGIS to be used in some mapping work I am doing in R. I have used readOGR to bring the shapefiles into R but am having trouble with one which has different coordinates than the others.
I have named the object "core" and it is a SpatialLinesDataFrame with structure:
> str(core)
Formal class 'SpatialLinesDataFrame' [package "sp"] with 4 slots
..@ data :'data.frame': 1 obs. of 3 variables:
.. ..$ Id : int 0
.. ..$ CONTOUR : num 39282
.. ..$ ISOPLETH: num 0.5
..@ lines :List of 1
.. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
.. .. .. ..@ Lines:List of 1
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:47, 1:2] 243 243 243 243 243 ...
.. .. .. ..@ ID : chr "0"
..@ bbox : num [1:2, 1:2] 239.3 31.4 243.4 34.8
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0"
My problem is that the coordinates for this object are in a different format than my map (and my other shapefiles). The longitudes for my map are, for example -125.1, -125.4 etc. Whereas the coordinates of "core" are 243.1, 243.5 etc. I have successfully been able to pull out all the coordinates from "core" and subtract 360 from them, which gives them the proper values to match my map with this code:
negs <- as.matrix(coordinates(core[1,])[[1]][[1]])
negs[,1] <- (negs[,1])-360
However I don't seem to be able to simply overwrite the original coordinates from "core" using something like this:
coordinates(core) <- negs
Is there some other way that I can change the coordinates of "core" so that it can be plotted with my other shapefiles?
Apologies for not including a reproducible data-set but I am unsure how to share shapefiles here.
If it helps the unconverted coordinates of "core" are:
> coordinates(core)
[[1]]
[[1]][[1]]
[,1] [,2]
[1,] 243.3805 31.91768
[2,] 243.4178 31.97442
[3,] 243.4289 32.62201
[4,] 243.4422 32.66768
[5,] 243.4256 32.71663
[6,] 243.2984 33.24151
[7,] 243.2184 33.41768
[8,] 243.0326 33.72568
[9,] 242.7246 33.90682
[10,] 242.4953 33.93843
[11,] 242.1447 33.99751
[12,] 241.9746 34.02227
[13,] 241.8334 34.02653
[14,] 241.6953 34.16768
[15,] 241.4555 34.39860
[16,] 241.2246 34.51035
[17,] 240.9792 34.67234
[18,] 240.6061 34.78613
[19,] 240.4746 34.84237
[20,] 240.3618 34.80495
[21,] 239.8966 34.74563
[22,] 239.7246 34.63816
[23,] 239.4505 34.44177
[24,] 239.2847 34.16768
[25,] 239.3818 33.82493
[26,] 239.7245 33.41769
[27,] 239.7246 33.41768
[28,] 239.7246 33.41767
[29,] 239.7246 33.41767
[30,] 239.7246 33.41767
[31,] 240.1623 33.10541
[32,] 240.4746 33.01726
[33,] 240.8026 32.99571
[34,] 240.9357 32.95650
[35,] 241.2246 32.92716
[36,] 241.3303 32.77337
[37,] 241.3518 32.66768
[38,] 241.4826 32.40961
[39,] 241.5369 32.23003
[40,] 241.9203 31.91768
[41,] 241.9404 31.88350
[42,] 241.9746 31.86257
[43,] 242.2367 31.65557
[44,] 242.3589 31.55204
[45,] 242.7246 31.38663
[46,] 243.0942 31.54808
[47,] 243.3805 31.91768
Upvotes: 2
Views: 1504
Reputation: 199
To make this question potentially useful for others I will post the answer that I came up with here.
The difficulty was in properly accessing and then replacing the coordinates of the SpatialLinesDataFrame. First I needed to pull the coordinates out of "core" as a matrix and assign them to a new object which could be edited as I had done before:
negs <- as.matrix(coordinates(core[1,])[[1]][[1]])
To change longitude values from the 360 degree format to the 180, -180 format that the rest of my shapefiles were in a simple subtraction was all that was needed:
negs[,1] <- (negs[,1])-360
Finally, the step that had me stumped was to reassigning these new corrected coordinates back into the original SpatialLinesDataFrame:
core@lines[[1]]@Lines[[1]]@coords[] <- negs
I hope this is able to help others in changing the coordinates of shapefiles from within R.
Upvotes: 1