wiki:WikiStart/Ruby/Map

The slam/envire package uses the GDAL library to save and restore raster data including georeferencing information. Fortunately, there are ruby bindings to this library.

Installation

On Debian/Ubuntu: libgdal-ruby1.8 (no 1.9 packages are available yet)

Looking at a map

To get information about a given map, the gdalinfo program is pretty useful

gdalinfo <mapfile>

For instance

[.../maps/tt_mls_slopes]% gdalinfo envire::Grid_h_3_traversability_class.tiff                                                 
Driver: GTiff/GeoTIFF
Files: envire::Grid_h_3_traversability_class.tiff
Size is 787, 792
Coordinate System is:
PROJCS["WGS 84 / UTM zone 32N",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0],
        UNIT["degree",0.0174532925199433],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",9],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AUTHORITY["EPSG","32632"]]
Origin = (-75.607098972924575,-69.087075775369726)
Pixel Size = (0.200000000000000,0.200000000000000)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  ( -75.6070990, -69.0870758) (  4d30'38.08"E,  0d 0'2.24"S)
Lower Left  ( -75.6070990,  89.3129242) (  4d30'38.08"E,  0d 0'2.90"N)
Upper Right (  81.7929010, -69.0870758) (  4d30'43.16"E,  0d 0'2.24"S)
Lower Right (  81.7929010,  89.3129242) (  4d30'43.16"E,  0d 0'2.90"N)
Center      (   3.0929010,  10.1129242) (  4d30'40.62"E,  0d 0'0.33"N)
Band 1 Block=787x10 Type=Byte, ColorInterp=Gray
  NoData Value=0

One can also use the -mm option to get the min/max value per-band (not computed by default as it is pretty expensive on big maps)

Usage

Simple usage:

require 'envire/gdal'
map = Gdal::Gdal.open(path)
map.number_of_bands
map.xsize => width
map.ysize => height
map.data_type => Ruby datatype corresponding to the band's datatype
map.read_band(0, 10, 10, 200, 200) => array of values

Looking at the file's projection

map.get_projection => Gdal::Osr projection object (see below)
map.get_geo_transform => transform
map.set_geom_transform(*transform)

The geotransform is an affine transformation between world coordinates and image coordinates. The transformation between pixel/line (P,L) raster space, and projection coordinates (Xp,Yp) space is given by:

Xp = transform[0] + P*transform[1] + L*transform[2];
Yp = transform[3] + P*transform[4] + L*transform[5];

For projection manipulation, also see Georeference

Last modified 8 years ago Last modified on 04/11/11 13:39:54