Problems deleting records from a Shapefile using Editor() class in pyShp

219 Views Asked by At

I'm using pyShp to add three polygons in a new shapefile. I added the geometry and the records (attributes). So far no problem.

Later, I wanted to use the Editor() class to delete one of the three polygons. If I try to delete the last one 'P3', it works fine.

The problem appears when I try to delete any other of the polygons. For instance, if I delete the first one, the shape disappear but its attributes remain so the 'P2' polygon inherit them. The record that is deleted is the last one (originally linked to 'P3').

import shapefile
w = shapefile.Writer(shapefile.POLYGONZ)
print(w.shapeType)
w.poly(parts=[[[0,0],[5,0],[5,5],[0,5]]])
w.poly(parts=[[[10,0],[15,0],[15,10],[10,5]]])
w.poly(parts=[[[5,10],[10,10],[10,15],[5,15]]])

w.field('NAME', 'C',40)
w.record('P1')
w.record('P2')
w.record('P3')
w.save("polygons")

edit = shapefile.Editor("polygons")
edit.delete(0)
edit.save("polygonDeleted")

I know I could select the two shapes that I want to keep and write them in a new shapefile but I'd like to understand why the Editor() class doesn't work as expected.

1

There are 1 best solutions below

0
On

The newest version of pyshp removed the Editor() class so is no longer avalaible.