Dylan_w
Dylan_w

Reputation: 482

Add projection imported .asc file

I'm trying to import an .asc file in python to clip it with a shapefile. For the clipping I'll use:

import earthpy.clip as cl
clip = cl.clip_shp(shp_file, asc_file)

However this won't work since my .asc doesn't have a CRS. This is how the header of the .asc looks like:

ncols         1900
nrows         1400
xllcorner     182900
yllcorner     326300
cellsize      10
NODATA_value  -999.990

This is how I import the .asc file

import rasterio as rio
asc_loc = r'file.asc'
raster = rio.open(asc_loc)
print(raster.crs)

The print shows none

Question: how can I add a the CRS to an imported .asc file? (Preferably with rastario or geopandas.)

Upvotes: 3

Views: 2457

Answers (2)

LuisTavares
LuisTavares

Reputation: 2196

To add a CRS to a raster

import rasterio.crs

crs = rasterio.crs.CRS({"init": "epsg:19914"})  
with rasterio.open('/path/to/file.format') as src:
    src.crs = crs
print (src.crs)

If that doesn't work, and since the CRS will never be saved to an asc.file,

better use gdal_translate first, from the command line, to convert to Geotiff, before using the raster with rasterio:

gdal_translate -of "GTiff" -a_srs EPSG:19914 in.asc out.tif

Upvotes: 3

Zeeshan
Zeeshan

Reputation: 1166

Looks like you are missing a .prj file.

If you have the .prj file, it should be stored along with your .asc file having same name

raster_image.asc
raster_image.prj

.prj file will contain the spatial reference information.

Upvotes: 1

Related Questions