1

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

Here is an example image I am working withenter image description here

1 Answer 1

4

Write it with rgdal instead

rgdal::writeGDAL(as(Converted.proj, "SpatialGridDataFrame"), 
 paste("~/Desktop/Heinz_Yan/Final_1",".asc", sep=""), 
 drivername = "AAIGrid")

but see caveats here: http://gdal.org/frmt_various.html#AAIGrid

This is a baad format, it's very limited and whether this GDAL-use of DX and DY pixel sizes will work depends on the next software in your workflow. I'd consider a decent format, like GeoTIFF that supports these basics, or work to ensure your cell sizes are square.

There's a couple of miscellaneous things in your workflow that I'd change, but aren't super important:

  • you should be able to combine the legend removal and index-crop in one step

  • you can brick("Temporary.png") read all bands in one object

3
  • Great. It worked perfectly. I have used brick() in the past, but I was confused on how to define & display only a specific color band that I wanted to show, such as I did when I wrote this: #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 Nonethless, I am learning this more and more. Thanks once again for your quick help
    – Andy
    Commented Jan 16, 2018 at 12:13
  • Yeah I would think that working in non-square pixels are going to cause some pretty severe issues/ limitations down the line. I would also recommend reading up on image formats and their limitations. Commented Jan 16, 2018 at 16:31
  • It does work but it is very slow
    – HubertL
    Commented Jun 7, 2018 at 20:00

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.