I have the following polygons shapefile consisting of polygons of the five boroughs of New York City from "NYC Open Data", linked here: https://data.cityofnewyork.us/City-Government/Borough-Boundaries/tqmj-j8zm
I want to take this shapefile and rasterize it to a .tif
file using gdal
in Python.
I am using the following python script example to accomplish this task, cited from "Python GDAL/OGR Cookbook 1.0 documentation", linked here: https://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html
This is what I am using:
from osgeo import gdal, ogr
gdal.UseExceptions()
# Define pixel_size and NoData value of new raster
pixel_size = 25
NoData_value = -9999
# Filename of input OGR file
vector_fn = 'C:/Users/MyName/Desktop/NYC_GIS/Boroughs_Test/geo_export_d92842a7-2631-4fe5-ab83-ee070743980c.shp'
# Filename of the raster Tiff that will be created
raster_fn = 'test.tif'
# Open the data source and read in the extent
source_ds = ogr.Open(vector_fn)
source_layer = source_ds.GetLayer()
x_min, x_max, y_min, y_max = source_layer.GetExtent()
# Create the destination data source
x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)
# Rasterize
gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[0])
However, this returns the following error message:
RuntimeError Traceback (most recent call last)
<ipython-input-32-0e6d6c7404d4> in <module>
21 x_res = int((x_max - x_min) / pixel_size)
22 y_res = int((y_max - y_min) / pixel_size)
---> 23 target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res, 1, gdal.GDT_Byte)
24 target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
25 band = target_ds.GetRasterBand(1)
~\Anaconda3\lib\site-packages\osgeo\gdal.py in Create(self, *args, **kwargs)
1892 def Create(self, *args, **kwargs):
1893 """Create(Driver self, char const * utf8_path, int xsize, int ysize, int bands=1, GDALDataType eType, char ** options=None) -> Dataset"""
-> 1894 return _gdal.Driver_Create(self, *args, **kwargs)
1895
1896
RuntimeError: Attempt to create 0x0 dataset is illegal,sizes must be larger than zero.
How do I address this error so that the raster .tif
file is correctly created and sent to my directory folder? I am not sure why this message is telling me I am creating a 0x0 raster file or what is wrong with the raster I am creating, since it seems like all I have to do to run this code is simply entering my input shapefile, which it seems like I did correctly.
UPDATE (with second attempt at code, incorporating pixel size and setting the projection):
from osgeo import gdal, ogr
# Define NoData value of new raster
NoData_value = -9999
# Filename of input OGR file
vector_fn = 'C:/Users/MyName/Desktop/NYC_GIS/Boroughs_Test/geo_export_d92842a7-2631-4fe5-ab83-ee070743980c.shp'
# Filename of the raster Tiff that will be created
raster_fn = 'C:/Users/MyName/Desktop/NYC_GIS/Boroughs_Test/test2.tif'
# Open the data source and read in the extent
source_ds = ogr.Open(vector_fn)
source_layer = source_ds.GetLayer()
proj = source_layer.GetSpatialRef()
x_min, x_max, y_min, y_max = source_layer.GetExtent()
# Create the destination data source
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, 100, 100, 1, gdal.GDT_Byte)
target_ds.SetProjection(proj.ExportToWkt())
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)
# Rasterize
gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[0])
This code runs and outputs a 0
. I am not sure what this means. I then go to open test2.tif
in QGIS, and all I see is a black square with all 0
values, not the rasterized polygon shapes I expect.
gdal.UseExceptions()
and pasted the new error traceback message in my post. The error is telling my my 0x0 dataset is illegal, and I am not sure what this means, but perhaps it could be an error with my projection. I am trying to figure out what this means, or which step resulted in this problem raster.gdal.Rasterize()
suggestion and it works, and produces the.tif
raster like I want, but when I open the file in QGIS, it is as well just a black square, with all 0 value pixels. I tried leavingxRes
andyRes
blank, but that didn't work. I then tried setting them both to 100, but then the black square was far beyond the extent of the NYC polygons. I am just trying to get the output raster to match the size/extent of the input shapefile.