Alternatives to using Arcpy

GDAL is the tool to use. In fact that entire call is one line for gdal_rasterize:

gdal_rasterize -l mask -i -burn -9999 mask.shp elevation.tif

if you knew the no data value of the dem

For some python control:

lyr = 'mask'
shp = 'mask.shp'
dem = 'elevation.tif'
ndv = -9999
p = os.Popen('gdal_rasterize -l %s -i -burn %d %s %s' % (lyr,ndv,shp,dem)

where your variables could be set in python

For full python:

from osgeo import gdal, ogr
from osgeo.gdalconst import *
shp = ogr.Open('mask.shp')
lyr = shp.GetLayer('mask')
dem = gdal.Open('elevation.tif', GA_Update)
ndv = dem.GetRasterBand(1).GetNoDataValue()
gdal.RasterizeLayer(dem, 1, lyr, None, ndv) # other options, such as transformer func, creation options...
dem = None

I just took a quick peek at the syntax for the C API, so my syntax for python is probably off a little. See gdal_alg.h: http://gdal.org/gdal__alg_8h.html


You will find a number of other similar questions on this site that ask the same basic question and have very good references. The most similar (and detailed) is:

  • What are the Python tools/modules/add-ins crucial in GIS?

Others include:

  • Python Script examples for geoprocessing shapefiles without using arcpy
  • Pure Python Library for Geometry Operations
  • What tools in Python are available for doing great circle distance + line creation?
  • Python module to delete SHP features (without Desktop GIS installed)

A good starting point would be the Geospatial Data Abstraction Library. It is actually made up oftwo libraries -- GDAL for manipulating geospatial raster data and OGR for manipulating geospatial vector data but people usually just call it GDAL.

There's a geoprocessing with Python using open source GIS course at the Utah State University. You might want to check it out, too.