I've been on this site blogging about raster files over the week, & this community has proved to be a lot of help
I currently have a map (see below), that I have converted to a raster file, manipulated the map to extract the data I want, and now I just want to save the file, but I keep getting this error message:
"Error in .startAsciiWriting(x, filename, ...) :
x has unequal horizontal and vertical resolutions. Such data cannot be stored in arc-ascii format"
I have used this code for other images, and it worked just fine. But for some reason, with this one, I cannot do the last step & save the file.
Does anyone see why this would happen? And if it would help, I would be happy to post code with an image that did work.
Here is my code:
library(raster)
library(maps)
library(magick)
library(rgdal)
# first need to load working directory
setwd("working here")
# i set this up in a way to use it in a loop later on.
Species_id<-1
Species_i <- image_read(paste("Species (", Species_id, ").jpg", sep=""))
Species_i_rotated<-image_rotate(Species_i,-4) # the -4 is the rotation
image_write(Species_i_rotated, path = "Temporary.png", format = "png")
#Read the temporary image as a raster format using the raster package
#Specify the image band that you are interested in
ConvertedR <- raster("Temporary.png", band = 1)
ConvertedG <- raster("Temporary.png", band = 2)
ConvertedB <- raster("Temporary.png", band = 3)
#isolate color frequency of interest
RGB_selection <- ConvertedR >= 0 & ConvertedR < 50 & ConvertedG >= 0 & ConvertedG < 30 & ConvertedB>=60 & ConvertedB <=90
RGB_selection[100:200,300:400] <- 0 # gets rid of the legend
# Cut the raster to fit as precisely as possible the border of Serbia
ext<-c(51,478,104, 642) # pre-tested in different code. Double checked and it's fine for what I have
Converted.cr<-crop(RGB_selection, ext)
#Define the coordinates of the borders of the rasters using the sr-org projection 8681 - bosnia.Those need to be adjusted to ensure a final match
# to the data, found @ this website: http://spatialreference.org/ref/sr-org/8681/
xmin(Converted.cr) <- 6588937.517403-20000
xmax(Converted.cr) <- 6907022.282226
ymin(Converted.cr) <- 4668568.705537
ymax(Converted.cr) <- 5117761.487247
#Project the raster into the new coordinate system
crs(Converted.cr) <- "+proj=tmerc +lat_0=0 +lon_0=18 +k=0.9999 +x_0=6500000 +y_0=0 +ellps=bessel +towgs84=682.0000,-203.0000,480.0000,0.000000,0.000000,0.000000,0.00000000 +units=m +no_defs "
newproj<-"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
Converted.proj <- projectRaster(Converted.cr, crs=newproj)
#Plot the species map with our adjusted raster
plot(Converted.proj, main="5>25%")
map(region="Serbia", add=T)
# match is great, so save project
writeRaster(Converted.proj,paste("~/Desktop/Heinz_Yan/Final_1",".asc", sep=""), type="ascii", overwrite=TRUE)
# Output
Error in .startAsciiWriting(x, filename, ...) :
x has unequal horizontal and vertical resolutions. Such data cannot be stored in arc-ascii format