3
 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

7

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
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.