rewrite a module for a toolbox script in python

457
2
10-02-2011 12:18 AM
SylviaNiderla
New Contributor
I have the following working script - here is an exerpt
rowCursor = arcpy.UpdateCursor(inputFC)
for row in rowCursor:
    geom = row.getValue(desc.shapeFieldName)
    from_point = geom.centroid
    near_pts = 0
    for to_point in coord_pairs:
        distance = math.sqrt(pow((to_point.X - from_point.X), 2) + pow((to_point.Y - from_point.Y), 2))
        if distance <= inputdistance:
            near_pts += 1
    row.cnt = near_pts - 1 # Subtract 1 to remove the measurement to itself
    rowCursor.updateRow(row)
del rowCursor 


I want to add it as a toolbox script in arcmap instead of running it from the python window. I've tried to rewrite it but I keep getting an error, so its wrong somehow. I have the getparameter as text part at the start, and it keeps erroring in this part. I think its where I have placed the setValue - but I have tried to adjust it and it keeps erroring.

    rowCursor = arcpy.UpdateCursor(inputFC)
    for row in rowCursor:
        geom = row.getValue(desc.shapeFieldName)
        from_point = geom.centroid
        near_pts = 0
        for to_point in coord_pairs:
            row.getValue = math.sqrt(pow((to_point.X - from_point.X), 2) + pow((to_point.Y - from_point.Y), 2))
            if distance <= inputdistance:
            near_pts += 1
            near_pts - 1
            row.setValue("Cnt")
            del rowCursor
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus
this line
near_pts - 1
is wrong

this line
row.setValue("Cnt")
will set everthing in that row to the text value

this line
del rowCursor
will delete the cursor on the first pass

Edit
additionally, the indentation after the if statement is incorrect
0 Kudos
SylviaNiderla
New Contributor
ah I put that row.setValue("Cnt") in there - really I want something like the output of the previous function e.g row.setValue("Cnt",distance) but that doesn't work either.

the nearpoint - 1 works when I run the script in a python window - its taking away the distance of points from a point (I think?).
0 Kudos