5

I need to convert files. I'm trying to use gdal_rasterize

gdal_rasterize -b 1 -burn 0 -l file file.shp work.tif

and the output is the following:

ERROR 10: Pointer 'hDS' is NULL in 'GDALGetProjectionRef'. Warning 1: The input vector layer has a SRS, but the output raster dataset SRS is unknown. Ensure output raster dataset has the same SRS, otherwise results might be incorrect. Segmentation fault (core dumped)

1
  • 1
    Does work.tif already exist? Can you add the output of ogrinfo -so file.shp file to your question.
    – user2856
    Commented Dec 23, 2016 at 0:53

3 Answers 3

8

To solve your projection error, you could obtain the projection information from an existing ("reference") image prior to rasterisation. This ensures that the output raster has the same projection, extent and pixel resolution as other images you are currently working with.

Here is an example of how this can be achieved in Python/GDAL. I have tested it on point shapefiles only, but it should work for polygons as well.

# A script to rasterise a shapefile to the same projection & pixel resolution as a reference image.
from osgeo import ogr, gdal
import subprocess

InputVector = 'VectorName.shp'
OutputImage = 'Result.tif'

RefImage = 'Image_Name.tif'

gdalformat = 'GTiff'
datatype = gdal.GDT_Byte
burnVal = 1 #value for the output image pixels
##########################################################
# Get projection info from reference image
Image = gdal.Open(RefImage, gdal.GA_ReadOnly)

# Open Shapefile
Shapefile = ogr.Open(InputVector)
Shapefile_layer = Shapefile.GetLayer()

# Rasterise
print("Rasterising shapefile...")
Output = gdal.GetDriverByName(gdalformat).Create(OutputImage, Image.RasterXSize, Image.RasterYSize, 1, datatype, options=['COMPRESS=DEFLATE'])
Output.SetProjection(Image.GetProjectionRef())
Output.SetGeoTransform(Image.GetGeoTransform()) 

# Write data to band 1
Band = Output.GetRasterBand(1)
Band.SetNoDataValue(0)
gdal.RasterizeLayer(Output, [1], Shapefile_layer, burn_values=[burnVal])

# Close datasets
Band = None
Output = None
Image = None
Shapefile = None

# Build image overviews
subprocess.call("gdaladdo --config COMPRESS_OVERVIEW DEFLATE "+OutputImage+" 2 4 8 16 32 64", shell=True)
print("Done.")
1
  • This is the most completed solution I have seen. But I have a question about that what if I don't have a reference tif file. What can I do? Commented May 9, 2018 at 13:35
7

Assuming "work.tif" does not already exist...

You will get that error if you are trying to rasterize into an output raster that doesn't exist.

C:\Temp>dir /b work.tif
File Not Found

C:\Temp>gdal_rasterize -b 1 -burn 0 file.shp work.tif
ERROR 10: Pointer 'hDS' is NULL in 'GDALGetProjectionRef'.

Warning 1: The input vector layer has a SRS, but the output raster dataset SRS is unknown.
Ensure output raster dataset has the same SRS, otherwise results might be incorrect.

If you want GDAL to create your output raster for you, you need to specify the output extent and pixel size.

You will also need to either change your burn value from 0 (as that's the default background/initial value) or change the background/initial value from 0 to something else.

Something like:

ogrinfo -so file.shp file

INFO: Open of `file.shp'
      using driver `ESRI Shapefile' successful.

Layer name: file
Metadata:
  DBF_DATE_LAST_UPDATE=2016-12-21
Geometry: Polygon
Feature Count: 3
Extent: (-107.098004, 41.168652) - (-104.171267, 44.190567)
Layer SRS WKT:
GEOGCS["GCS_WGS_1984",
    DATUM["WGS_1984",
        SPHEROID["WGS_84",6378137.0,298.257223563]],
    PRIMEM["Greenwich",0.0],
    UNIT["Degree",0.0174532925199433]]
OID_: Integer (9.0)


gdal_rasterize -te -107.2, 41.0, -104.1, 44.2 -tr 0.01 0.01 -burn 255 file.shp work.tif

Or

gdal_rasterize -te -107.2, 41.0, -104.1, 44.2 -tr 0.01 0.01 -burn 0 -init 255 file.shp work.tif
1
  • +1 Looks like this answers to this post, too.
    – Kazuhito
    Commented Dec 23, 2016 at 1:33
0

You can use http://www.gdal.org/gdal_rasterize.html FME with the Mapnik Rasterizer or Global Mapper or another GIS software package like QGIS

1
  • I need convert from console, because after the save in format .tif, and verify the corner coordinates, and I use this page for guide Commented Dec 22, 2016 at 21:07

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.