1

I read the post Accessing attribute table within shapefile and replace values?, but I don't know how to include a "Where clause".

I want to update the attribute table of a shapefile. In SQL the Code looks like this:

alter table point_shape
    add buffer number;

update point_shape
    set buffer = a/2
    where a >0;     


alter table line_shape
    add buffer number;

update line_shape
    set buffer = a/2
    where a >0; 

alter table point_shape
    add buffer number;

update point_shape
    set buffer = sqrt(b)/acos(-1)
    where b is not null and b >0;


alter table line_shape
    add buffer number;

update line_shape
    set buffer = b/shape_len
    where b is not null and a = 0 and b>0;

I translated the first update statement:

cur = arcpy.UpdateCursor(point_shape)

for row in cur:
    row.setValue('buffer', row.getValue('a') / 2)
    cur.updateRow(row)

But where can I include the where statement?

  • What version of ArcGIS are you running? – Midavalo May 11 '17 at 20:11
1

Use the newer data-access cursor, they are faster. For example the UpdateCursor:

UpdateCursor establishes read-write access to records returned from a feature class or table

And an if statement:

import arcpy

point_shape=r'C:\path\shapefile.shp'

with arcpy.da.UpdateCursor(point_shape,['buffer','a']) as cursor:
    for row in cursor:
        if row[1]>0: #row[1] = the 'a' field
            row[0]=row[1]/2 #row[0] = the 'buffer' field
            cursor.updateRow(row) #update the 'buffer' field with new value
| improve this answer | |

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.