1

I've used this code to assign a CRS to my raster in R and then export it:

crs(rcE) <- crs('+init=EPSG:4326')

writeRaster(rcE, "path/to/my/folder/filename.asc", format="ascii")

However, when I upload the raster to QGIS it displays this message:

enter image description here

When I click on the "?" visible on the image and set the CRS for that one I'm interested in, the error message disapears. But if I remove the layer and add it again, the error returns.

How can I permanently assign a projection to my raster file, so that every time I open/use it the CRS is there?

4
  • Why do you want to assign a CRS? What is your workflow? See: gis.stackexchange.com/a/410754/88814
    – Babel
    Commented Sep 2, 2021 at 17:08
  • @Babel I have a binary habitat suitability raster (0 for unsuitable habitat, 1 for suitable). I want to convert the raster to a shapefile and then extract the areas with value=1 from that shapefile. I assumed I needed to assign a CRS. Don't I?
    – Spectron
    Commented Sep 2, 2021 at 17:12
  • There is an Assign projection tool in the GDAL section of QGIS.
    – John
    Commented Sep 2, 2021 at 17:18
  • The question was more: doesn't it have already a CRS assigned? As log as we don't know exactly what data you have and what your workflow is, it's difficult to say.
    – Babel
    Commented Sep 2, 2021 at 17:29

1 Answer 1

4

ASC files:

writeRaster(rcE, "path/to/my/folder/filename.asc", format="ascii")

don't store projections. Save as a GeoTIFF (.tif) and you'll get a raster file with CRS in its metadata that will read correctly into QGIS (and back into R).

writeRaster(rCE, "/path/filename.tif")

should be sufficient.

That said, there is a way to assign a projection to a .asc file, and that is to create a .prj file, much like you'd have with a shapefile.

Create a set of point from your raster, then check the CRS, then save as a shapefile:

> pts = as(r,"SpatialPoints")
> crs(pts)
CRS arguments: +proj=longlat +datum=WGS84 +no_defs 
> shapefile(pts, "pts.shp")

Now copy the file pts.shp into the same folder as your raster, give it the same name as your raster but with a .prj extension.

Now QGIS (and other GDAL tools) will interpret that file as the projection of the .asc file.

This is not ideal, because now you have two files to keep together. And .asc files are grim, because you can't store multiple bands or have rasters with non square (eq 12m x 24m) cells. But that knowledge might get you out of a spot if you need it.

1
  • thank you very much! Converting to .tif solved the problem
    – Spectron
    Commented Sep 2, 2021 at 22:06

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.