I am using the following code to first divide a vector layer to many polygons and after that use them to clipping parts of a raster layer. The Name
attribute in the vector data has a text data type
. I am not sure why I receive the error of 'NoneType' object is not subscriptable
. Can anyone help me to solve this error?
import glob
import gdal
import fiona
with fiona.open('E:/data/vector.shp', 'r') as dst_in:
for index, feature in enumerate(dst_in):
with fiona.open('E:/data/polygons/NO_{}.shp'.format(index), 'w', **dst_in.meta) as dst_out:
dst_out.write(feature)
polygons = glob.glob('E:/data/polygons/*.shp') ## Retrieve all the .shp files
for polygon in polygons:
feat = fiona.open(polygon, 'r')
name = feat['properties']['Name']
command = 'gdalwarp -dstnodata -9999 -cutline {} ' \
'-crop_to_cutline -of GTiff E:/data/raster.tif E:/data/outputraster/NO_{}.tif'.format(polygon, name)
TypeError Traceback (most recent call last)
<ipython-input-3-ad6d7471adf6> in <module>
1 for polygon in polygons:
2 feat = fiona.open(polygon, 'r')
----> 3 name = feat['properties']['Name']
4 command = 'gdalwarp -dstnodata -9999 -cutline {} ' \
5 '-crop_to_cutline -of GTiff E:/data/raster.tif E:/data/outputraster/NO_{}.tif'.format(polygon, name)
TypeError: 'NoneType' object is not subscriptable
Best Answer
polygons
in your script are not polygons/features. They are shapefiles.fiona.open
returns an openedCollection
object. That meansfeat
in your code is not a feature, it is a feature collection. You must iterate over the collection to get features.Use like this: