Skip to content

Instantly share code, notes, and snippets.

@pepijn-devries
pepijn-devries / contoursToSP01.r
Last active August 29, 2015 14:26
ContourLines to SpatialPolygons
#required packages:
require(raster)
require(maptools)
require(Grid2Polygons)
#define breaks for the contour lines:
breaks <- seq(80, 200, length.out = 15)
#first convert the volcano matrix to a raster
poly_volcano <- raster(volcano)
@pepijn-devries
pepijn-devries / contoursToSP02.r
Last active August 29, 2015 14:26
ContourLines to SpatialPolygons
#create a slightly larger matrix and fill with relatively small values (60):
volcano2 <- matrix(60, nrow(volcano) + 2, ncol(volcano) + 2)
#copy the original matrix into the center of the new matrix:
volcano2[-nrow(volcano2),][,-ncol(volcano2)][-1,][,-1] <- volcano
#repeat the previous conversion steps and plot:
poly_volcano <- raster(volcano2)
poly_volcano <- rasterToContour(poly_volcano, levels = breaks)
poly_volcano <- SpatialLines2PolySet(poly_volcano)
for (i in 1:length(poly_volcano))
{
temp <- poly_volcano@polygons[[i]]@Polygons
for (j in 1:length(temp))
{
if (length(temp) > 1)
{
for (k in 1:length(temp))
{
if (j != k)
@pepijn-devries
pepijn-devries / contoursToSP04.r
Created August 8, 2015 14:22
ContourLines to SpatialPolygons
#create bounding box to which the final polygons needs to be clipped:
CP <- as(extent(0 + 2/ncol(volcano2), 1 - 2/ncol(volcano2), 0 + 2/nrow(volcano2), 1 - 2/nrow(volcano2)), "SpatialPolygons")
#clip polygons and plot:
final_volcano <- gIntersection(poly_volcano, CP, byid=TRUE)
png("volcano04.png", 200, 200)
par(oma = c(0,0,0,0), mar = c(0,0,0,0))
plot(final_volcano, col = terrain.colors(length(poly_volcano)), border = NA)
dev.off()
#required packages:
require(jpeg)
#Read a jpeg from a website and store as matrix "mat_clown"
con <- file("http://4.bp.blogspot.com/-uObQLjPG8Xg/VceaZNyI-NI/AAAAAAAAx0o/ZlcTTB1pomU/s1600/fourier_efteling.jpg", "rb")
#make sure 'n' is big enough to hold your entire file:
jpg_clown <- readBin(con, "raw", n = 3e4)
close(con)
rm(con)
mat_clown <- readJPEG(jpg_clown)
#checker board pattern used to centre the Fourier spectrum:
checkerboard <- (-1)^(row(mat_clown) + col(mat_clown))
#modify matrix with checkerboard pattern:
mat_checker <- mat_clown*checkerboard
#Discrete fast Fourier transform:
mat_fft <- fft(mat_checker)
#magnitude in frequency domain
#select peaks in spectrum to filter out
#click on each peak, then end the locator
peaks <- data.frame(locator())
#function to create a filter for circle shaped peaks in the spectrum
#dimensions: dimensions of the spectrum matrix
#x: x position of the centre of the circle to be filtered out
#y: y position of ...
#r: radius of circle to be filtered out of the spectrum
#gaussian: flag that indicates whether the circle should have a Gaussian density or not
#reconstruct fourier transformed image from filtered spectrum:
re_construct <- cos(phase)*magni_filter
im_construct <- sin(phase)*magni_filter
mat_fft_construct <- matrix(complex(real = re_construct, imaginary = im_construct), nrow(mat_clown), ncol(mat_clown))
#Apply inversed Fourier transform to obtain the filtered image:
mat_clown_constr <- Re(fft(mat_fft_construct, inv = T))*checkerboard/prod(dim(mat_clown))
#normalize the resulting matrix between 0 and 1
mat_clown_constr <- (mat_clown_constr - min(mat_clown_constr)) /
# required packages
require(rgdal)
require(OpenStreetMap)
# select files for which geotags need to
# be extracted and store filenames in data.frame
file_info <- data.frame(file.name = choose.files(filters = Filters["jpeg",]))
# function to get a parameter value from EXIF code,
# x = a vector of character strings representing the EXIF codes of a file
# parameter = character string representing the parameter
# for which you want to extract the value
# the literal character string is returned for the requested parameter
get_EXIF_value <- function(x, parameter)
{
line <- x[grepl(paste("EXIF_", parameter, "=", sep = ""), x)]
if (!is.null(line) && length(line) > 0)
{