0
votes

I have two data frames each 1000 x 1000, one data frame (lon) with longitudes and one data frame (lat) with latitudes, specifying coordinates of a pixel (or raster) array and I'm stuck with how to combine the two data frames into one 1000 x 1000 spatialpointsdataframe (or equivalent). The first cell of the spatialpointsdataframe would have coordinates specified by lon[1,1] and lat[1,1] etc. Is there a simple way to do this? I think I can do it column by column and then binding the spatial point objects together sequentially, but this seems a bit long winded. Any ideas?

Tried as suggested:

lat.t <- data.frame(seq(10, 15, 1), seq(20, 25, 1), 
    seq(30, 35, 1),seq(40, 45, 1))
colnames(lat.t) <- c("1", "2", "3", "4")
lon.t <- data.frame(seq(90, 95, 1), seq(100, 105, 1), 
seq(110, 115, 1), seq(120, 125, 1))
colnames(lon.t) <- c("1", "2", "3", "4")
coords <- cbind(lon = unlist(lon.t), lat= unlist(lat.t))
sp.ob <- SpatialPoints(coords)

which gives a two column sp.ob with 16 rows when I think I need a 4 x 4 so that the lon/lat match the raster.

If I do just:

coords <- cbind(lon =lon.t , lat= lat.t)
sp.ob <- SpatialPoints(coords)

I do get a 4 x 4 but with all lon in first 4 columns then all lat in last 4, and I think this doesn't give me the right lon/lat pairs if I look at the contents with:

sp.ob[1]

for example.

3
Please provide a working example.dayne
Not sure what is meant by a "spatialpointsdataframe" that is 1000x1000. I thought that type of object was recorded in "long" format. Why not make toy versions of lat and lon that are 5 x 5 and show what is desired.IRTFM
Many thanks for the suggestion- that was a useful prod to get me to think a little more clearly!chrisR

3 Answers

1
votes

Try this :

lon <- data.frame(matrix(rnorm(10000), 100, 100))
lat <- data.frame(matrix(rnorm(10000), 100, 100))

cbind(lon = unlist(lon), lat = unlist(lat))

Or :

cbind(lon = unlist(t(lon)), lat = unlist(t(lat)))

Depending of the order wanted (by line or by column)

0
votes

My initial question was about how to combine separate lon/lat data frames into one so that I could add these to specify relevant coordinates in a raster. The underlying problem is that I have a tiff image representing a polar stereographic (antarctic) projection of 1000 x 1000 pixels and separate files for longitude and latitude, each 1000 x 1000 elements matching the pixel image. @Gilles pointed out a simple data frame conversion (basic R !) that I could use to convert the 1000 x 1000 lon/lat dataframes to get x and y coordinates in a form that I could combine with a data frame conversion of the rastered tiff to give a file with xyz columns. I can then use rasterFromXYZ to complete my task. However, the coordinates are not quite regular but 'griddify' may solve that problem. Answers to my initial question were greatly appreciated!

0
votes

The underlying problem is that I have a tiff image representing a polar stereographic (antarctic) projection of 1000 x 1000 pixels and separate files for longitude and latitude, each 1000 x 1000 elements matching the pixel image

What you need to do is

library(raster)
r <- raster("file.tif")

Check the coordinate reference system (projection)

crs(r)

If it has been set correctly to polar stereographic, you are good to go; otherwise you can set it with the appropriate description. Something like

crs(r) <- "+proj=stere +lat_0=-90 +lon_0=0  +lat_ts=-71 +datum=WGS84 +units=m"

If you want to transform to longitude / latitude, try:

x <- projectRaster(r, "+proj=longlat +datum=WGS84")

And see ?projectRaster for additional parameters.