[R-sig-Geo] Saving jpeg world files

Roger Bivand Roger.Bivand at nhh.no
Wed Nov 29 09:12:22 CET 2006


On Wed, 29 Nov 2006, Mulholland, Tom wrote:

> I am beginning to get a little bit bog eyed with the myriad of options
> there are within the various spatial packages. I am trying to find ways
> of porting the results I get within R into a GIS environment and I can't
> seem to see anything that would let me write a jpeg file with an
> appropriate world file.

The most direct route is to use a SpatialGridDataFrame or equivalently 
SpatialPixelDataFrame, and writeGDAL() in rgdal. That gives very good 
support for GeoTiff files. The JPEG driver is present, but much more 
limited than GeoTiff - it has to be either single-band or three-band, and 
only Byte data. For released rgdal, you would write the jpeg from a single 
variable by stretching it over 0-255, taking integers, writing as GeoTiff 
to a temp file, opening the file as a GDAL data set, copying to JPEG, 
and saving:

library(sp)
library(rgdal)
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
# quantize
rd <- range(meuse.grid$dist)
meuse.grid$idist <- as.integer(255*(meuse.grid$dist-rd[1])/(rd[2]-rd[1]))
# write temporary GTiff
tf <- tempfile()
writeGDAL(meuse.grid["idist"], tf, drivername="GTiff", type="Byte")
# open temporary file as GDAL dataset and copy to JPEG
x <- GDAL.open(tf)
dx <- copyDataset(x, driver="JPEG", options="WORLDFILE=YES")
# and write out
td <- tempdir()
tf1 <- paste(td, "output.jpg", sep="/")
saveDataset(dx, tf1, options="WORLDFILE=YES")
GDAL.close(x)
GDAL.close(dx)
# check to see if worldfile present and OK (*.wld use cell centre 
# registration, the bounding box goes to the cell boundaries
list.files(td)
slot(meuse.grid, "grid")
file.show(paste(td, "output.wld", sep="/"))

Had rgdal and the underlying GDAL library allowed jpeg files to be written 
directly, this would be simpler. However, you may find that the software 
that reads jpeg files also reads GeoTiff files, which are far more 
flexible, taking many more data types, including floating point values.

For those interested in image overlays in Google Earth, this recipe lets 
you make PNG files too (but so far I haven't got GE to read a worldfile to 
place the image, and of course GE wants geographical coordinates). PNG 
support an alpha channel too.

Hope this isn't too complicated. We probably need an interface between im 
and sp classes so that getting from im to GDAL is simpler.

Roger


> 
> I have managed to create an ASCIIGrid but my first tests seem to
> indicate that not all grids are created equally. My experience with some
> of the "free" windows GIS applications is that jpg files seem to be more
> universally supported, so I thought that might be a more fruitful way to
> go. I will then have to worry less about making sure my output is the
> type that can be imported.
> 
> It looks to me as if the class "im" in spatstat is conceptually
> straightforward to process. It looks as if all the attributes are there
> to create a world file. Since my data is already projected it would seem
> that I should be able to simply use the jpeg device for output (not that
> I have any idea how I'll go about that) and create a text file to go
> along with it.
> 
> The first question is 'Am I missing something really simple?' and the
> second is "Is there a straightforward solution that I have already
> missed."
> 
> Tom
> 
> platform       i386-pc-mingw32             
> arch           i386                        
> os             mingw32                     
> system         i386, mingw32               
> status                                     
> major          2                           
> minor          4.0                         
> year           2006                        
> month          10                          
> day            03                          
> svn rev        39566                       
> language       R                           
> version.string R version 2.4.0 (2006-10-03) 
> 
> Tom Mulholland
> Senior Demographer
> Applied Research and Modelling
> State and Regional Policy
> Department for Planning and Infrastructure
> Perth, Western Australia
> +61 (08) 9264 7936
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list