Running Viewshed on one feature at time in shapefile?
I've never used the viewshed tool so I can't speak to the specifics of using that tool, however in regards to your suggestion in this question I will add an answer here to build on KHibma's solution.
The first thing we need to address is the format of your SQL query: "[OID] = count"
. As it stands, this query will fail since "count", as represented here, is a string and strings need to be surrounded by single quotes: "[OID] = 'count'"
. Now, the second reason this query will fail is that OID
is an integer field and as such there will be no OID
value equal to "count"
. This is where string formatting comes into play: '"OID" = {}'.format(i)
or '"OID" = {0}'.format(i)
for those using Python 2.6.x (Use quotation marks to enclose the fieldname when using a shapefile. See ArcGIS help.).
To avoid certain pitfalls of making queries on the OBJECTID
field, we can nest the loop into a cursor to make sure that we are only querying OID
values that exist:
arcpy.MakeFeatureLayer_management ("C:/data/pts.shp", "pts")
with arcpy.da.SearchCursor('pts',['OID']) as cursor:
for row in cursor:
oid = row.OID # or whatever your OBJECTID field is called
arcpy.SelectLayerByAttribute_management ("pts", "NEW_SELECTION", '"OID" = {}'.format(oid))
outViewshed = Viewshed("elevation","pts",2,"CURVED_EARTH",0.15)
outViewshed.save("C:/sapyexamples/output/outvwshd"+oid)
OK, borrowing from the answer from @Jason, I was able to tweak it a bit to finally come up with a script that works great. I also switched it to use FID instead of OID. Either way, it will work.
arcpy.MakeFeatureLayer_management (inPoints, "pts")
with arcpy.da.SearchCursor('pts',['FID']) as cursor:
for row in cursor:
fid = str(row[0]) ### Must be string
print fid
arcpy.SelectLayerByAttribute_management ("pts", "NEW_SELECTION", '"FID" = {}'.format(fid))
outViewshed = Viewshed(inDEM,"pts",1,"CURVED_EARTH",0.15)
outViewshed.save("C:/Python/output/viewsheds/"+fid)
Perhaps this will help someone in the future. Thanks everyone!