with fiona.open(constants.file_combined, 'r') as source:
filtered = filter(lambda f: f['properties']['NAME'] == name_dm1, source)
# **source.meta is a shortcut to get the crs, driver, and schema
# keyword arguments from the source Collection.
with fiona.open(out_dir + os.sep + name_dm1 + '.shp', 'w', **source.meta) as sink:
for feature in filtered:
try:
sink.write(feature)
except Exception, e:
# Writing uncleanable features to a different shapefile
# is another option.
print out_dir + os.sep + name_dm1 + '.shp'
I have a shapefile from which I want to select based on presence of certain values in a column called 'NAME'.
However, in the code above, it creates a shapefile with an empty attribute table even though I am sure there is data in the original shapefile.
asked Mar 13, 2016 at 2:06
1 Answer 1
Much more easily:
with fiona.open("a_shapefile.shp") as input:
meta = input.meta
with fiona.open('a_shapefile2.shp', 'w',**meta) as output:
for feature in input:
if feature['properties']['NAME']== name_dm1:
output.write(feature)
answered Mar 13, 2016 at 8:18
-
4Might be nice to use filter() as in gis.stackexchange.com/questions/91676/… / toblerity.org/fiona/manual.html#slicing-and-masking-iteratorsbugmenot123– bugmenot1232016年10月20日 19:51:00 +00:00Commented Oct 20, 2016 at 19:51
lang-py