I have an app that searches for a particular species from a point file
Once found it creates graphics for the point locations and grid polygons that encompass those points.
What I want to do now is symbolize those polygons based on how many points are found within them. I am not sure how to approach this. I think I would have to read through each polygon and determine how many points are in there and set the symbology?
So the result would be a graphics layer of polygons of varying color based on how many points are within them
Anyone have any ideas?
Solved! Go to Solution.
OK I think I have it working now...Now onto the next piece
# COUNT NUMBER OF RECORDS FOR EACH SPECIES
def unique_values(table, field):
expressionValue = table
species = field
expressionValue2 = "common_name = '{}'".format(species)
with arcpy.da.SearchCursor(bba_Points, "common_name", where_clause=expressionValue2) as cursor:
for row in cursor:
print row[0]
OK I am trying to get a tally of each species and their counts...
But I just get a big list of each one as it passes through the loop....not totals....where am I going wrong
# COUNT NUMBER OF RECORDS FOR EACH SPECIES
def unique_values(table, field):
resultspecies = ""
expressionValue = table
species = field
expressionValue2 = "common_name = '{}'".format(species)
with arcpy.da.SearchCursor(Points, "common_name", where_clause=expressionValue2) as cursor:
for row in cursor:
resultspecies = row[0]
rowCount = 0
rowCount = rowCount + 1
print "There are " + str(rowCount) + " of " + resultspecies
# GET UNIQUE SPECIES FROM TABLE
for feature in arcpy.ListFeatureClasses():
#print feature
with arcpy.da.SearchCursor(Points,"common_name", sql_clause=(None,'ORDER BY common_name DESC')) as SCur:
for row in SCur:
if not row[0] in bList: # if not in list then add to list
bList.append(row[0])
species = row[0]
expressionValue = "common_name = '{}'".format(species)
uniques = unique_values(expressionValue, species)
Now that I have the Unique Species I need to pass them 1 by one to a function that will either QUERY the point Feature Class and do something to JUST those records or Create a DEFINITION query and do something with just those point records.
In the DEF I have a unique Species name sent to it. Whats the best way to query the points Feature Class and just work with those records?
Query or Definition Query....any examples?
If I could figure out how to create a definition query based on a field value WHERE clause and then run code against just those records I would be ecstatic....
First off THANK YOU ALL FOR YOUR THOUGHTS AND COMMENTS
They really helped steer me in the path and direction I took...
OK this is what I did and its working....
I used the If Then to change the TYPE value in the Data to a value that would work as a field name...so I can update the Polygon Layer as some of the TYPE values were really long
import arcpy
arcpy.env.workspace = r"E:/xxxxx/xxxx/Projects/xxx/xxx.sde"
Points = r"E:/xxxxx/xxxx/Projects/xxx/xxx.sde/Locations"
Grids = r"E:/xxxxx/xxxx/Projects/xxx/xxx.sde/Grids"
print "Started"
bList = []
expression = ""
expressionValue = ""
type= ""
rowCount = 0
uniquetypeList = []
countList = []
txt_list = ""
# COUNT NUMBER OF RECORDS FOR EACH TYPE
def getFieldName(type):
if type == "Needs Repair":
return "NeedsRepair"
elif type == "Broken":
return "Broken"
elif type == "Needs Replacement":
return "NeedsReplacement"
elif type == "Schedule Fix":
return "ScheduleFix"
else:
return "nothing"
def processTypeUpdate(expressionValue, typeValues, fieldName):
expressionValue2 = expressionValue
typeName = typeValues
fieldUpdateName = fieldName
rowCount = 0
arcpy.Delete_management('in_memory/PointsInPolys')
arcpy.Delete_management('in_memory/SS_PointsInPolys')
arcpy.Delete_management('points_lyr')
arcpy.Delete_management('points_lyr2')
arcpy.MakeFeatureLayer_management(r'E:/xxxxx/xxxx/Projects/xxx/xxx.sde/Locations_Test', 'points_lyr')
results = arcpy.SelectLayerByAttribute_management ('points_lyr', 'NEW_SELECTION', expressionValue2)
arcpy.MakeFeatureLayer_management(results, 'points_lyr2')
polygonID = "ID" ## unique polygon field name
countField = fieldUpdateName # The field name in the Grids FC to update
expression = "recalc(!FREQUENCY!)"
codeblock = """def recalc(freq):
if freq > -1:
return freq
else:
return 0"""
arcpy.SpatialJoin_analysis('points_lyr2', Grids, "in_memory/PointsInPolys")
### case field returns count per unique UID
arcpy.Statistics_analysis ("in_memory/PointsInPolys", "in_memory/SS_PointsInPolys", [[polygonID, "Count"]], polygonID)
arcpy.JoinField_management(Grids, polygonID, "in_memory/SS_PointsInPolys", polygonID, "FREQUENCY")
arcpy.CalculateField_management(Grids, countField, expression, "PYTHON", codeblock)
arcpy.DeleteField_management(Grids, "FREQUENCY")
# GET THE CODES FROM THE RESULTS OF THE SELECTION
rows = arcpy.SearchCursor('points_lyr2',"","","type_name")
for row in rows:
rowCount = rowCount + 1
typeVal = str(row.type_name)
countList.append(typeVal)
txt_list = ','.join(countList)
print "number of " + typeVal + " : " + str(rowCount)
arcpy.Delete_management('in_memory/PointsInPolys')
arcpy.Delete_management('in_memory/SS_PointsInPolys')
arcpy.Delete_management('points_lyr')
arcpy.Delete_management('points_lyr2')
# TAKE TYPE NAME AND GET THE CORRECT FIELD NAME
def processtypeNames(expressionValue, typeValues):
type= str(typeValues)
expressionValue2 = "type_name = '{}'".format(type)
# GET THE CORRECT FIELD NAME
fieldName = getFieldName(type)
if fieldName == "nothing":
print "No Value"
else:
# Call the function to actually update the Field Values
uniques2 = processtypeUpdate(expressionValue2, type, fieldName)
print "corrected field name is: " + fieldName
def uniquetypeList(uniquetypeValues):
typeList = uniquetypeValues
#print typeList
for type in typeList:
#print type
expressionValue = "type_name = '{}'".format(type)
uniques = processtypeNames(expressionValue, type)
# GET UNIQUE TYPS FROM FC CREATE LIST
for feature in arcpy.ListFeatureClasses():
#print feature
with arcpy.da.SearchCursor(Points,"type_name", sql_clause=(None,'ORDER BY type_name ASC')) as SCur:
for row in SCur:
if not row[0] in bList: # if not in list then add to list
bList.append(row[0])
type = row[0]
del SCur
uniquetype = uniquetypeList(bList)