3

I have downloaded a shapefile (.shp) that I'd like to read in using fiona.open and delete records. The reason I'd like to delete records is that some records have "None" geometries.

So I want to delete a record if it doesn't have a geometry: {'geometry': None, 'type': 'Feature', 'id': etc etc etc}

Here's my code:

import fiona
with fiona.open('data/ssc_2016_aust_shape/SSC_2016_AUST.shp') as file:
    for p in file: # for every record - note that p is a dictionary 
        if p["geometry"] is None: # if p doesn't have a geometry
            del p # then delete the entire record in the shape file 

I'd like for the original shapefile (.shp) to be overwritten (this is important).

You can download the shapefile here: http://www.abs.gov.au/AUSSTATS/[email protected]/DetailsPage/1270.0.55.003July%202016?OpenDocument

The file on the ABS website is called "State Suburbs ASGS Ed 2016 Digital Boundaries in ESRI Shapefile Format" in the form of a ZIP file.

2 Answers 2

5

You can't, directly. See the fiona user manual:

1.6 Writing Vector Data

A vector file can be opened for writing in mode 'a' (append) or mode 'w' (write).

Note:

The in situ “update” mode of OGR is quite format dependent and is therefore not supported by Fiona.

...

You could copy the valid records to a temporary dataset (see Writing New Files) and then copy it over the original, or you could just operate on the valid records only (see filtering topic - Slicing and masking iterators).

For example (based on the fiona user manual):

#Note completely untested...

with fiona.open(inshp) as source, fiona.open(outshp, 'w',
                                             driver=source.driver,
                                             crs=source.crs,
                                             schema=source.schema) as dest:
    for feat in source:
        if feat["geometry"] is not None:
            dest.write(feat)
2
  • Luke, I'd really appreciate your help: How do I move the elements of one shape file into the other? is this an easy task?
    – nerde1234
    Sep 29, 2016 at 6:26
  • Basically, i want to create a new shape file. then use the old shape file to move records across. in particular, i only want records with a non-null geometry (or maybe even records with a Polygon geometry)
    – nerde1234
    Sep 29, 2016 at 6:32
1

According to the accepted answer, you cannot do what you want using fiona.

I would advise you to use GeoPandas instead.

You can open the Shapefile file and remove a row by a condition.

PS: I know this question was asked 3 years ago, but I'm answering it in order to help others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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