rDGAL, Tiff Files, and WorldFile

元气小坏坏 提交于 2019-12-11 04:45:16

问题


I have a set of tiff files that display convective weather across the continental US (NAD83 projection) in pixel locations from Iowa State University. My goal is the transformation of the pixel locations to lat/lon data. I read in the tiff file data as a SpatialGridDataFrame with...

imageData   = readGDAL( fileNameDir, silent = TRUE )

I read somewhere that readGDAL will seek a World File if no projection data exist in the tiff file, so I created such a file (nad83WorldFile.wld) with the requisite information, see info at ESRI. I put the wld file in the same directory as my R scripts. The coefficients for the wld file are:

A = 0.01 
B = 0.0 
C = 0.0 
D = -0.01 
E = -126.0 
F = 50.0

I seek advice and guidance on the pixel-to-lat/lon projection. A data file for the readGDAL example of fileNameDir and documentation on the World File format are provided in the hypertext links above. I had to change the file extension from *.png to *.tiff.


回答1:


Normally, if you know that your data are projected, but that this projection isn't part of your tif file, your can simply add it in your R object after the import:

proj4string(imageData) <- CRS("your projection")

I like using EPSG for that, if your tif was in GoogleEarth projection for example I would do:

proj4string(imageData) <- CRS("+init=EPSG:4326")

Just find what you NAD83 exact projection is (this site can help http://spatialreference.org/).

Then you can reproject it in the your choice of projection:

imageDataProj <- spTransform(imageDataProj, CRS("your new projection"))

As a side note, I always prefer using the raster package for handling raster formats. However, changing the projection of a big raster file with R can be fastidious, so now I use GDAL directly (through gdalwarp). You can call all gdal options quite easily in R with the gdalUtils package but you'll have to import the results back into R after hand.

EDITS following comment from OP:

Using the raster package:

library(raster)

Loading the tif:

rr <- raster("C:\\temp\\n0r_201601011100.tif")

Save you pixel coordinates equations in functions. Noticed I changed the Lat function (removed the negative sign, it didn't work with it, you'll have to validate that)

Lon = function(JJ) 0.01 * JJ + 162
Lat = function(II) 0.01 * II + 50.0

Get the extent of your raw raster in pixel coordinates:

ext.rr <- extent(rr)

Prepare a new empty raster which will be projected, have the good resolution and extent:

rr2 <- raster(nrows=nrow(rr), ncols=ncol(rr), xmn=Lon(ext.rr@xmin), xmx=Lon(ext.rr@xmax), ymn=Lat(ext.rr@ymin), ymx=Lat(ext.rr@ymax))

Fill this new raster with your modified values (following the equation you gave in the comments):

values(rr2) <- (values(rr) - 7) * 5

And you get:

rr2
   class       : RasterLayer 
   dimensions  : 2600, 6000, 15600000  (nrow, ncol, ncell)
   resolution  : 0.01, 0.01  (x, y)
   extent      : 162, 222, 50, 76  (xmin, xmax, ymin, ymax)
   coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
   data source : in memory
   names       : layer 
   values      : -35, 50  (min, max)

Notice that the lat-long projection was automatically pick-up by the raster function. Hopefully it's what you are looking for.



来源:https://stackoverflow.com/questions/42100791/rdgal-tiff-files-and-worldfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!