2

I am having an issue I have not had before creating a class with a method as below;

class Points:

def __init__(self, inFC, buffDist, sector): #Must have a set of points and a value to buffer these by. Also, sector.

    self.inFC = inFC
    self.buffDist = buffDist
    self.sector = sector

def getCoords(self): #getting the coordinates of the points.

    fc = self.inFC
    fields = ['SHAPE@XY']

    coordsList = []

    with arcpy.da.SearchCursor(fc, fields) as rows:
        coordsList = [r[0] for r in rows]
    self.coordsList = coordsList
    del coordsList

The class is created and the method called from this code at the end of my script;

if __name__ == '__main__':

inFC = arcpy.GetParameterAsText(0)
buffDist = arcpy.GetParameterAsText(1)
sector = arcpy.GetParameterAsText(2)
outFC_data = arcpy.GetParameterAsText(3)

fcName = outFC_data.rpartition("\\")[2]
fcPath = outFC_data.rpartition("\\")[0]
outFC = arcpy.CreateFeatureclass_management(fcPath, fcName, "POLYGON")

pointobject = Points(inFC, buffDist, sector)
pointobject.getCoords()

This returns the error "fc is not defined" at the line fc = self.inFC.

OR

if I remove "fc = self.InFC" and replace fc with self.inFC within SearchCursor then I get an error at the line "with arcpy.da.SearchCursor(self.inFC, fields)" that states "self.inFC is not defined".

I have tried pasting the class definition directly into the python interpreter (ArcCatalog->Geoprocessing->Python) but the errors remain the same, so I do not even get to the point of creating an instance of the class, the error is within the actual code of my class and method definition.

What is the error in my code or my approach?

1 Answer 1

3

If you are using Python 2.x I believe you need to use the object base class. Also, you can greatly simplify your code:

class Points(object):

    def __init__(self, inFC, buffDist, sector): #Must have a set of points and a value to buffer these by. Also, sector.

        self.inFC = inFC
        self.buffDist = buffDist
        self.sector = sector
        self.coordlist = []

    def getCoords(self): #getting the coordinates of the points.

        with arcpy.da.SearchCursor(self.inFC, ['SHAPE@XY']) as rows:
            self.coordsList = [r[0] for r in rows]

if __name__ == '__main__':

    inFC = arcpy.GetParameterAsText(0)
    buffDist = arcpy.GetParameterAsText(1)
    sector = arcpy.GetParameterAsText(2)
    outFC_data = arcpy.GetParameterAsText(3)

    fcName = os.path.basename(outFC_data)
    fcPath = os.path.dirname(outFC_data)
    outFC = arcpy.CreateFeatureclass_management(fcPath, fcName, "POLYGON")

    pointobject = Points(inFC, buffDist, sector)
    pointobject.getCoords()

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.