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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 11, 2017 at 8:50
1
  • What version of ArcGIS are you running? Commented May 11, 2017 at 20:11

1 Answer 1

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
answered May 11, 2017 at 9:08
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.