Quantcast
Channel: Question and Answer » gdal
Viewing all articles
Browse latest Browse all 397

Creating records in output shapefile using Python

$
0
0

I have a list (output from a previous loop) containing attributes from an input shapefile. What I am stuck with is inputting those attributes to a new shapefile.

I have plotted the point data using w.point(x, y) and also created the relevant fields w.field() from the input shapefile fields.

Where I am stuck is with the copy of the records, which is giving me an Error related to Deletion Flag.

AttributeError: ‘tuple’ object has no attribute ‘startswith’

I am pasting a section of the code with my queries.

from datetime import datetime
import osgeo.ogr, osgeo.ogr
from osgeo import ogr
from osgeo import gdal
import shapefile
import os

sf = shapefile.Reader("-- Input File Location --") # Reading the Shapefile
fields = sf.fields # Reading the attribute fields
records = sf.records() # Reading the records of the features
shapRecs = sf.shapeRecords() # Read Geometry and Records simultaneously 

w = shapefile.Writer(shapefile.POINT)
w.autoBalance = 1
result1 = []
result2 = [] # ??? Is the error because we cannot query for list in a list ???

print '** Call function pt(p) where p is node index... **' # Prompting the user to 
                                                           # call the function with 
                                                           # the required number of nodes
def pt(p):
    for i in range(len(shapRecs)):
        u = shapRecs[i].shape.points[p-1]   # Gives the XY Coordinates of
                                            # the queried points

        v = shapRecs[i].record[0:]          # Generating the Attributes
        result1.append(u)
        result2.append(v)

        w.point(u[0], u[1]) # Create points from coordinates each time the loop runs
        w.autoBalance = 1

    # -- Adding the fields
    w.field(fields[0]) # Adding the first field, WITH Deletion Flag (???)
    for i in range(1, len(fields)): # Loop begins from the second field (???)
        w.field(fields[i])
        w.autoBalance = 1

    # CODE SHOWS THE ERROR STATED ABOVE FROM THE w.record() statement
    # -- Adding the records 
    for i in range(len(result2)):
        w.record(*result2[i])       
        w.autoBalance = 1

    w.save('-- Output File Location --')    

Viewing all articles
Browse latest Browse all 397

Trending Articles