Learn R Programming

gdalraster (version 1.11.1)

get_pixel_line: Raster pixel/line from geospatial x,y coordinates

Description

get_pixel_line() converts geospatial coordinates to pixel/line (raster column, row numbers). The upper left corner pixel is the raster origin (0,0) with column, row increasing left to right, top to bottom.

Usage

get_pixel_line(xy, gt)

Value

Integer matrix of raster pixel/line.

Arguments

xy

Numeric matrix of geospatial x,y coordinates in the same spatial reference system as gt (or two-column data frame that will be coerced to numeric matrix).

gt

Either a numeric vector of length six containing the affine geotransform for the raster, or an object of class GDALRaster from which the geotransform will be obtained (see Note).

See Also

GDALRaster$getGeoTransform(), inv_geotransform()

Examples

Run this code
pt_file <- system.file("extdata/storml_pts.csv", package="gdalraster")
# id, x, y in NAD83 / UTM zone 12N
pts <- read.csv(pt_file)
print(pts)

raster_file <- system.file("extdata/storm_lake.lcp", package="gdalraster")
ds <- new(GDALRaster, raster_file)
gt <- ds$getGeoTransform()
get_pixel_line(pts[, -1], gt)

# or, using the class method
ds$get_pixel_line(pts[, -1])

# add a point outside the raster extent
pts[11, ] <- c(11, 323318, 5105104)
get_pixel_line(pts[, -1], gt)

# with bounds checking on the raster extent
ds$get_pixel_line(pts[, -1])

ds$close()

Run the code above in your browser using DataLab