0

I have found a way to give a PNG the CRS and location of a geolocated TIFF, and to convert the PNG to TIFF. Now I have to apply it to different PNGs in the same folder (all named Pt6_frame1, Pt6_frame2, etc.) and write the individual new TIFF files as Pt6_frame1_geolocated, Pt6_frame2_geolocated, etc. Does anyone have an idea of how to do so? I have tried the code in various posts, using for loops and functions, but I get various errors.

## Loading image of interest as stack

R_test <- 
  stack("Pt6_clear.png")

# Give image of interest the lat/lon coords of the geolocated image as appear in its metadata in the console

extent(R_test) <- c(-9073461, -9073269, 2974255, 2974366)

# Copy the projection data of the geolocated image as appears in the console to the image of interest

projection(R_test) <- CRS("+proj=merc +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")

## Write new geolocated image into a file

writeRaster(R_test, "R_test_geolocated.tif",
            format="GTiff",
            overwrite=TRUE,
            NAflag=-9999) 

1 Answer 1

0

Solved!

library(raster)
library(rgdal)
library(ggplot2)
library(dplyr)
library(OpenImageR)

geolocate <- function(input) {
## Loading image of interest as raster
R_test <- 
  stack(input)
R_test ## It does not have any CRS
plot(R_test)

# Give image of interest the lat/lon coords of the geolocated image as appear in its metadata in the console
extent(R_test) <- c(-9073461, -9073269, 2974255, 2974366)
# Copy the projection data of the geolocated image as appears in the console to the image of interest 
projection(R_test) <- CRS("+proj=merc +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")

## Write new geolocated image into a file

writeRaster(R_test,
            filename=sub(".png", "_geolocated.tif", input),
            format="GTiff",
            overwrite=TRUE,
            NAflag=-9999)
}


filenames <- list.files(path = ".",  #The point means the working directory
                        # Now follows a regular expression that matches:
                        pattern = ".png",
                        full.names = TRUE)
for (f in filenames) {
  geolocate(f)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.