How to use rasters in Django depends on your use case of course. In the simplest case, if your rasters are relatively small and do not need tiling, you can simply declare a model with a raster field and assign rasters to it.
# models.py
from django.contrib.gis.db import models
class RasterWithName(models.Model):
raster = models.RasterField()
name = models.TextField()
# In the django shell
from django.contrib.gis.gdal import GDALRaster
# Open raster file using GDALRaster.
raster = GDALRaster('/path/to/your/raster.tif')
# Store file content in database using the raster field.
RasterWithName.objects.create(raster=raster, name='My new raster')
Loading a raster into postgis is as simple as that. Note that like that every time you will get one instance of the RasterWithName model, it will load the entire raster. This works fine for smaller rasters, but for large ones this might be clunky, depending on how you want to use it.
Saving one model instance does load the data into postgres, its not more complicated than that. However, you still do not have views or url endpoints to render the raster on the web. You can use external software to serve the rasters (such as mapserver for instance).
If you want to stay within the Django universe, I would use the django-raster package. It has a tiling engine, which splits your rasters into tiles instead of loading them as one object. It also has a views and url endpoints that are useful to display the rasters in a web map through a z-x-y tile scheme. The main limitation of the django-raster package at the moment is that it always stores the raster in the Web Mercator projection (srid 3857).
To use django-raster, install the package and then create a RasterLayer object through the admin interface. When creating a raster layer you can upload your raster file in the rasterfile field. Django-raster will then automatically parse the raster and create tiles that are stored in the RasterTile model. On the admin page of the RasterLayer instance, you can consult the parse log to see if the parsing was successful.
Once a layer is parsed, you can directly use the raster layer in a javascript environment (such as openlayers or leaflet) by using the tiles end point.
The parsing of the raster layer can be a long process if the file is large. For larger rasters, the parsing during web request might time out. You can configure your application to use celery for asynchronous parsing (see this section of the installation). Alternatively, you can create your raster layers in the django shell instead of using the admin interface.
Disclaimer: I am the author of the django-raster package, so my recommendation might be biased.
layer_idtranslates for specific cases though. Is that just the name of theRasterLayerobject? My edit in the original question details what I'm confused about. – jbukoski May 19 '17 at 17:14layer_idis the primary key of the raster layer object you want to display. You can use the filename of the raster layer as well, but that might get removed as it can lead to confusion (the file name changes when uploading a different file etc). – yellowcap May 22 '17 at 13:27