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.

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.