2

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)

here is an example of a map I am working with

enter image description here

6
  • 1
    If I understand it right, you defined your boundaries by one coordinate system (SR-ORG:7852) but then you assigned a different one to it. I assume 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.
    – Janina
    Commented Jan 5, 2018 at 13:06
  • Hello This was the right answer, and exactly what I was trying to do! Thanks a lot for your help. May I ask, where did you come up with the answer? I would like to know because I will be doing this for other countries as well. Thanks again
    – Andy
    Commented Jan 5, 2018 at 13:23
  • @Janina please, post your comment as an answer.
    – aldo_tapia
    Commented Jan 5, 2018 at 13:44
  • @aldo_tapia yes, I wasn't sure if that would solve everything. But the OP managed to post the answer before me :-) The whole workflow seems a bit shaky to me but I'd have to see the data.
    – Janina
    Commented Jan 5, 2018 at 14:16
  • @Andy right there on the link you provided (SR-ORG:7852) there is a list of the coordinate system definitions used by different softwares. If you click on the Proj4 you will get directly the string which is used in R.
    – Janina
    Commented Jan 5, 2018 at 14:19

1 Answer 1

2

The answer was written by Janina, and it was how the boundaries of the coordinate system were assigned.

I changed this, and it works great now!

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 and should be more appropriate

Many thanks for the quick tip :)

Your Answer

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

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