Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hypertidy/affinity
Simple grid tools, including offset/scale georeference
https://github.com/hypertidy/affinity
Last synced: 3 months ago
JSON representation
Simple grid tools, including offset/scale georeference
- Host: GitHub
- URL: https://github.com/hypertidy/affinity
- Owner: hypertidy
- Created: 2016-01-08T05:34:48.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-15T01:52:14.000Z (over 3 years ago)
- Last Synced: 2024-05-21T02:12:51.321Z (6 months ago)
- Language: R
- Homepage: http://hypertidy.github.io/affinity/
- Size: 4.35 MB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
---
output: github_document
---[![Travis build status](https://travis-ci.org/hypertidy/affinity.svg?branch=master)](https://travis-ci.org/hypertidy/affinity)[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/hypertidy/affinity?branch=master&svg=true)](https://ci.appveyor.com/project/hypertidy/affinity)[![Codecov test coverage](https://codecov.io/gh/hypertidy/affinity/branch/master/graph/badge.svg)](https://codecov.io/gh/hypertidy/affinity?branch=master)[![CRAN status](https://www.r-pkg.org/badges/version/affinity)](https://CRAN.R-project.org/package=affinity)
[![R-CMD-check](https://github.com/hypertidy/affinity/workflows/R-CMD-check/badge.svg)](https://github.com/hypertidy/affinity/actions)```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# affinityThe goal of affinity is to provide the basic tools used for raster grid georeferencing. This includes:
* the affine transform
* the world file model
* the GDAL RasterIO window model
* basic tools for performing grid calculations
* simple control point georeferencing for un-mapped rasters.The main use at the moment is the ability to get a geotransform from an extent and dimension, this makes it easy to drive GDAL functions and to compare
with the RasterIO logic in the sf package raster reader.The main functions for georeferencing are `affinething()` to collect drawn points interactively from an un-mapped raster image and `domath()` to calculate the extent of the raster in geographic terms and `assignproj()` to apply a map projection inline. There are some other experimental functions to write GDAL VRT `gdalvrt()` and to store some known cases for unmapped image sources.
The basic tools still rely on the raster package.
## Installation
You can install the dev version of affinity from [GitHub](https://github.com/hypertidy/affinity/) with:
``` r
devtools::install_github("hypertidy/affinity")
```## Example
This examples takes an an un-mapped raster and [georefences](https://en.wikipedia.org/wiki/Georeferencing) it by defining some control points for a simple (offset and scale) [affine transformation](https://en.wikipedia.org/wiki/Affine_transformation).
Generally, we want **diagonal points**, so I tend to think "southwest" and "northeast", it doesn't really matter where they are as long as there's some pixels between them in both directions. Monterey Bay is very recognizable so I read off some long-lat control points using [mapview](https://r-spatial.github.io/mapview/).
```{r example}
library(affinity)
#data("montereybay", package = "rayshader")
data("monterey", package = "affinity")library(raster)
## we know that rayshader works transpose
r <- t(raster(monterey))prj <- "+proj=longlat +datum=WGS84"
## the north tip of Pacific Grove
sw <- c(-121.93348, 36.63674)
## the inlet at Moss Landing
ne <- c(-121.78825, 36.80592)
#mapview::mapview(c(sw[1], ne[1]), c(sw[2], ne[2]), crs = prj)
```We can obtain raw (graphics) coordinates of those locations from our image, by plotting it and clicking twice with `affinething()`.
Note the order, the first point is "sw" and the second is "ne" - the order is not important but it must match.
```{r affine-thing, eval = FALSE}
## mask the raster so we can see easily where we need to click
xy <- affinething(r > 0)```
In this example the points are
```{r affine-thing-points}
# xy <- structure(c(0.65805655219227, 0.858931100128933, 0.367586425626388,
# 0.589597209007295), .Dim = c(2L, 2L), .Dimnames = list(NULL,
# c("x", "y")))
xy <- structure(c(0.440321955368224, 0.580740883913579, 0.453408220576972,
0.625733980086801), .Dim = c(2L, 2L), .Dimnames = list(NULL,
c("x", "y")))
```Please note that if the matrix changes, then the values above will have to change as well (this has happened).
```{r affine-thing2, eval = TRUE, echo = FALSE}
plot(r > 0)
points(xy)
text(xy, lab = 1:2, pos = 4)
```Now we have everything we need to re-map our raster! We don't need to project our points as the known locations are in the same
coordinate system as the source data. (In other situations we might georeference using a graticule on a projected map.)```{r affine-remap}
mapped <- assignproj(setExtent(r, domath(rbind(sw, ne), xy, r, proj = NULL)), prj)m <- rnaturalearth::ne_countries(country = "United States of America", scale = 10)
plot(mapped, col = viridis::viridis(30))
plot(m, add = TRUE)
contour(mapped, levels = -100, lty = 2, add = TRUE)#mv <- mapview::mapview(mapped)
```
```{r, eval = FALSE, include = FALSE, echo = FALSE}
## Old rough exampler <- raster("dev/www.bom.gov.au/charts_data/IDY20001/current/windarrow/10m/2015-12-21/IDY20001.windarrow-10m.066.png")
a <- regions("windAntarctica")
projection(r) <- a$proj
extent(r) <- a$extent
plot(r)
library(maptools)
data(wrld_simpl)
library(rgdal)
w <- spTransform(wrld_simpl, projection(r))
plot(w, add = TRUE)ex <- domath(pts, rawxy)
gdalvrt(r, a_ullr = c(xmin(ex), ymax(ex), xmax(ex), ymin(ex)),
a_srs = "\"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0\"")```
---
Please note that the 'affinity' project is released with a [Contributor Code of Conduct](https://github.com/hypertidy/affinity/blob/master/CODE_OF_CONDUCT.md). By contributing to this project, you agree to abide by its terms.