1

I have a shapefile in which there are a series of polygons that I have to attribute to the name of a project which are recorded in 6 attribute fields. For each polygon there might be one to 6 projects associated. I need for each instance of a project name to have one polygon (up to 6 identical but for the name of the project) associated so that I can later dissolve the polygons according to the projects names.

I've used the pyshp module but I encounter a problem with the writing of the new polygon shapes based on the existing polygons. I read the existing using shapes[i].parts but it returs [0] to most of the shapes. Could you please give me a hand solving this issue ?

Here is my code :

import shapefile


#### Set input and target feature
inputShp = r"Pre-treatment\FP_BVC_Polygons.shp"
targShp= "Pre-treatment\FP_BVC_Polygons_test"
sf= shapefile.Reader(inputShp)
shapes= sf.shapes()
records = sf.records()

fields=sf.fields
w=shapefile.Writer(shapefile.POLYGON)

for i in range(0,len(records)):
    for j in range(12,19):
        if records[i][j] != "0":
            listGeom.append(list(shapes[i].points))
            listRecord.append(records[i])

del i,j

for i in range(0, len(listGeom)):
    w.poly(points=listGeom[i])


for i in range(0,len(fields)):
    w.field(fields[i][0],fields[i][1],fields[i][2],fields[i][3])


for rec in listRecord:
    w.record(rec[0],rec[1],rec[2],rec[3],rec[4],rec[5],rec[6],rec[7],rec[8],
         rec[9],rec[10],rec[11],rec[12],rec[13],rec[14],rec[15],rec[16],rec[17],rec[18])

w.save(targShp)
1

I don't understand your script

To extract specific rows from values ( if records[i][j] != "0":)

import shapefile
sf= shapefile.Reader("polygons.shp")
fields = sf.fields[1:] 
w = shapefile.Writer(shapefile.POLYGON)
w.fields = fields
for rec in sf.shapeRecords():
   if rec.record[3] > 0:
      w.records.append(rec.record)
      w._shapes.append(rec.shape)
w.save("duplicate")

You can also use a dictionary

field_names = [field[0] for field in fields]
for rec in sf.shapeRecords():
    atr = dict(zip(field_names, rec.record))
    if atr['test'] > 0:
       ....

If you want to select columns in a new shapefile

w = shapefile.Writer(shapefile.POLYGON)
fields2 = [fields[1], fields[3]]
w.fields = fields2
for rec in sf.shapeRecords():
   if rec.record[3] > 0:
       w.records.append([rec.record[1],rec.record[3]])
       w._shapes.append(rec.shape)
w.save("duplicate2")

If you want to add a new field

fields = sf.fields[1:] 
fields2 = [fields[1], fields[3]]
# new field
fields2.append(['test', 'N', 10, 0])
w = shapefile.Writer(shapefile.POLYGON)
w.fields = fields2
for rec in sf.shapeRecords():
   if rec.record[3] > 0:
       w.records.append([rec.record[1],rec.record[3], 3])
       w._shapes.append(rec.shape)
w.save("duplicate3")
  • Thank you so much I managed to do what I wanted. Here is my code based on your suggestion : import shapefile ## Set input and target feature inputShp = r"Polygons.shp" targShp= "Polygons_test" sf= shapefile.Reader(inputShp) fields = sf.fields[1:] w=shapefile.Writer(shapefile.POLYGON) w.fields = fields for rec in sf.shapeRecords(): for i in range(12,19): if rec.record[i] != "0": w.records.append(rec.record) w._shapes.append(rec.shape) w.save(targShp) – user3584444 Dec 12 '16 at 9:44

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.