[R-sig-Geo] convert a shapefile to geotiff with type = "Float32"

Hailey Eckstrand haileyeckstrand at gmail.com
Tue Feb 9 01:24:20 CET 2010


So, after reading more posts and getting help from Robert, I guess
this is a pretty common problem and as of now, this is the easiest
raster type to load into ArcGIS. In case anyone needs this code, this
is what I ended up doing:
 library("rgdal")
## load in my shapefile
shp <- readOGR("/home/data3/projects/hydrology/vic/output/water_balance/peace_cc/summary",
"peace_base_et_6190")

## Convert it to a SpatialGridDataGrame, so I can writeGDAL
gridded(shp) <- TRUE
shp.grid <- as(shp, "SpatialGridDataFrame")
writeGDAL(shp.grd["jan"],
"/home/data2/gis/AT_Hailey/r_rasters/test.tif", drivername = "GTiff",
type = "Float32", mvFlag = 1e20)

Then to load it into ArcGIS.. add the geotiff, and usually it comes in
with incorrect min & max values.
Right click the file > Properties > Symbology
In the Stetched menu, under Stretch, select Type: Custom and then
click Yes when it asks you if you want to compute a new histogram.
Then click apply and then click OK and now you have forced ArcGIS to
display the true min & max.
That worked for me, I am using ArcGIS 9.3
Hailey

On Mon, Feb 8, 2010 at 2:08 PM, Hailey Eckstrand
<haileyeckstrand at gmail.com> wrote:
> Hello again,
> My writeGDAL statement was slightly wrong.. I didn't have the NA
> values in quotes.
> it should be:
> # attempt to write out a geotiff
>  writeGDAL(shp.grd, "/home/data2/gis/AT_Hailey/r_rasters/test.tif",
> drivername = "GTiff", type = "Float32", mvFlag = 'NA', options=NULL,
> copy_drivername = "GTiff")
>
> I can now load the tiff file into QGis correctly, but still not ArcMap.
> Has anyone found a grid type that loads into Arc smoothly?
> Hailey
>
>
> On Mon, Feb 8, 2010 at 11:56 AM, Hailey Eckstrand
> <haileyeckstrand at gmail.com> wrote:
>> Hello List,
>> I am trying to load in a shapefile and then write it out as a geoTiff.
>> I am using the package rgdal.
>>
>> library("rgdal")
>> ## load in my shapefile
>> shp <- readOGR("/home/data3/projects/hydrology/vic/output/water_balance/peace_cc/summary",
>> "peace_base_et_6190")
>>
>> # Just to be clear, here is the structure of my data
>> str(shp)
>> Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
>>  ..@ data       :'data.frame': 3975 obs. of  13 variables:
>>  .. ..$ jan: num [1:3975] 12 12.4 12.3 12.3 12.5 ...
>>  .. ..$ feb: num [1:3975] 12.8 13.2 13 13.1 13.5 ...
>>  .. ..$ mar: num [1:3975] 16.3 16.6 16.8 16.8 17.1 ...
>>  .. ..$ apr: num [1:3975] 31.2 33.7 30.1 28.1 29.2 ...
>>  .. ..$ may: num [1:3975] 106 111 101 99 103 ...
>>  .. ..$ jun: num [1:3975] 99.4 116 121.1 115.1 108 ...
>>  .. ..$ jul: num [1:3975] 57.8 56.9 74.7 69.5 65 ...
>>  .. ..$ aug: num [1:3975] 53.8 51.2 59.5 58.6 57.3 ...
>>  .. ..$ sep: num [1:3975] 44 40.6 52.8 49.4 49.2 ...
>>  .. ..$ oct: num [1:3975] 32.4 32 35.3 34.1 34.9 ...
>>  .. ..$ nov: num [1:3975] 12.6 12.9 12.9 12.9 13.4 ...
>>  .. ..$ dec: num [1:3975] 12.8 13.2 13.2 13.1 13.6 ...
>>  .. ..$ sum: num [1:3975] 491 510 542 522 517 ...
>>  ..@ coords.nrs : num(0)
>>  ..@ coords     : num [1:3975, 1:2] -123 -123 -122 -123 -123 ...
>>  .. ..- attr(*, "dimnames")=List of 2
>>  .. .. ..$ : NULL
>>  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>>  ..@ bbox       : num [1:2, 1:2] -127.7 54.2 -120.6 58
>>  .. ..- attr(*, "dimnames")=List of 2
>>  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>>  .. .. ..$ : chr [1:2] "min" "max"
>>  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>>  .. .. ..@ projargs: chr " +proj=longlat +ellps=WGS84 +datum=WGS84
>> +no_defs +towgs84=0,0,0"
>>
>> # Convert it to a SpatialGridDataGrame, so I can writeGDAL
>> gridded(shp) <- TRUE
>> shp.grid <- as(shp, "SpatialGridDataFrame")
>>
>> # structure of my SpatialGridDataFrame
>> str(shp.grid)
>> Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
>>  ..@ data       :'data.frame': 7130 obs. of  13 variables:
>>  .. ..$ jan: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ feb: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ mar: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ apr: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ may: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ jun: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ jul: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ aug: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ sep: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ oct: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ nov: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ dec: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  .. ..$ sum: num [1:7130] NA NA NA NA NA NA NA NA NA NA ...
>>  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
>>  .. .. ..@ cellcentre.offset: Named num [1:2] -127.7 54.2
>>  .. .. .. ..- attr(*, "names")= chr [1:2] "coords.x1" "coords.x2"
>>  .. .. ..@ cellsize         : Named num [1:2] 0.0625 0.0625
>>  .. .. .. ..- attr(*, "names")= chr [1:2] "coords.x1" "coords.x2"
>>  .. .. ..@ cells.dim        : Named int [1:2] 115 62
>>  .. .. .. ..- attr(*, "names")= chr [1:2] "coords.x1" "coords.x2"
>>  ..@ grid.index : int(0)
>>  ..@ coords     : num [1:2, 1:2] -127.7 -120.6 54.2 58
>>  .. ..- attr(*, "dimnames")=List of 2
>>  .. .. ..$ : NULL
>>  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>>  ..@ bbox       : num [1:2, 1:2] -127.8 54.2 -120.6 58.1
>>  .. ..- attr(*, "dimnames")=List of 2
>>  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>>  .. .. ..$ : chr [1:2] "min" "max"
>>  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>>  .. .. ..@ projargs: chr " +proj=longlat +ellps=WGS84 +datum=WGS84
>> +no_defs +towgs84=0,0,0"
>>
>> # attempt to write out a geotiff
>> writeGDAL(shp.grd, "/home/data2/gis/AT_Hailey/r_rasters/test.tif",
>> drivername = "GTiff", type = "Float32", mvFlag = NA, options=NULL,
>> copy_drivername = "GTiff")
>>
>>
>> The file that I write out only has NA values. I realize something is
>> wrong as I haven't figured out how to define which column I'd like to
>> convert.
>> Thanks in advance,
>> Hailey
>>
>



More information about the R-sig-Geo mailing list