I am new to this site, and in general, to coding for what I would like to do.
I have used this code in the past, and it worked for when I was working with a map of China, but now I would like to use it for a different region of the world, and I am stuck
I have a 3 part question:
1.) I have a map of Papua New Guinea in GIF format that I would like to convert to a raster (easy)
2) Define coordinates of raster file
3) Lay over a map of Papua New Guinea and make sure it matches the raster of Papua New Guinea that I have created.
Step 1 is done, step 2 I believe is fine (please tell me if you see an error), however, there is a bug between step 2 and 3.
I am using R, and here is the code I have written so far
library(raster)
library(maps)
library(magick)
library(rgdal)
setwd( "where I am working from ")
#upload image
Species_id<-1
Species_i <- image_read(paste("Species (", Species_id, ").gif", sep=""))
Species_i_rotated<-image_rotate(Species_i,0)
image_write(Species_i_rotated, path = "Temporary.png", format = "png")
#create raster
Converted<-raster("Temporary.png", band=1)
# cut raster to fit precisely as possible the borders of Papua new guinea
ext<-c(26,445,0, 275)
Converted.cr<-crop(Converted, ext)
plot(Converted.cr)
#### This is where step 2 starts and the issue may stem from here, however, I have followed this procedure in the past, and it worked just fine
#Define the coordinates of the borders of the rasters using the sr-org projection 7852 - Australia - modified wgs84
# borders defined manually here http://spatialreference.org/ref/sr-org/7852/
xmin(Converted.cr) <- 41368.792496
xmax(Converted.cr) <- 1715419.242596
ymin(Converted.cr) <- 8696732.363608
ymax(Converted.cr) <- 9783651.873356
# This is where my code will NOT work, and I suspect its because I used this when I was working with China, and something needs to be adjusted, Nonetheless, I get an error:
crs(Converted.cr) <- "+proj=aea +lat_1=15 +lat_2=65 +lat_0=30 +lon_0=95 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_def"
newproj<-"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
Converted.proj <- projectRaster(Converted.cr, crs=newproj)
Error in if (maxy == miny) { : missing value where TRUE/FALSE needed
# Here is the end goal:
plot(Converted.proj)
map(region="Papua New Guinea", add=T)
crs(Converted.cr) <- "+proj=tmerc +lat_0=0 +lon_0=145 +k=1 +x_0=500000 +y_0=10000000 +ellps=WGS84 +units=m +no_defs"
(this is the proj4string from your link) should be more appropriate.