Instead of two shapefiles that are saved in fcList
in this loop I would like to do KernelDensity on the unique values of one shapefile.
Does anyone have an idea how this might work?
import arcpy,os
from arcpy import env
env.workspace = r'C:\Users\lhark\Desktop\SHK\ArcGis_Proj_Canada\shapefiles'
outworkspace = r'C:\Users\lhark\Desktop\SHK\ArcGis_Proj_Canada\KernelDensity'
fcList = arcpy.ListFeatureClasses ()
for fc in fcList:
outKDens = KernelDensity(fc,"NONE",4670)
outKDens.save(outworkspace + "/" + os.path.splitext(fc)[0] + "_kd.tif"
2 Answers 2
Although Hornbydd is absolutely correct (+1 for that by the way), the first part of the question is to loop through all the unique values in a field.. this snippet should be usable in your existing code:
FieldToUse = "Field" # change this to your field name with the unique values
for fc in fcList:
uVals = [] # new empty list
# loop through each feature with a searchcursor
with arcpy.da.SearchCursor(fc,FieldToUse) as SCur:
for row in SCur:
# check if the value is already in the list
if row[0] not in uVals:
uVals.append(row[0]) # add new value to list
#loop through each unique field value
for FieldValue in uVals:
# create a layer with a where clause, this is for numeric fields
arcpy.MakeFeatureLayer_management(fc,"Layer","{} = {}".format(FieldToUse,FieldValue))
# use "{} = \'{}\'".format(FieldToUse,FieldValue) to quote the value
# if the input field is a string field
OutTIFFname = "{}_{}_kd.tif".format(os.path.splitext(fc)[0],FieldValue)
outKDens = KernelDensity("Layer","NONE",4670)
outKDens.save(os.path.join(outWorkspace,OutTIFFname))
del "Layer" # clean up when you're done
del outKDens
The process is fairly straightforward:
- Start with a new empty list:
uVals = []
. This instructs python that the new variable will be a list containing no elements. - Open a search cursor on your feature class and loop through each feature; because you're not changing anything this is surprisingly quick.
- If the value of your field is not already in the list then add (append) it to the list, that way there is only one instance of each unique value. Note that the list is not sorted and can appear in any order.
- Loop through the unique value list and for each unique field value create a feature layer with a where clause. I am using string.format to create the where clause which I find tidier than simple concatenation (with '+' operator). I have changed the name of the outputs to include the unique value of the field that was used for each raster. I have also used os.path.join to concatenate your output path and file name; join uses the correct character required by your operating system when appending which makes the script more robust.
-
1I would use
uVals = set()
withadd
method to avoid complications (and it is more pythonic, they say).fatih_dur– fatih_dur2018年02月19日 06:02:57 +00:00Commented Feb 19, 2018 at 6:02 -
I´m getting this Error now: ExecuteError: ERROR 000725: Output Layer: Dataset out_VHQ_5 already exists and the Loop stops ("out_VHQ_5" is what i put into "Layer")Lutz– Lutz2018年02月19日 14:43:22 +00:00Commented Feb 19, 2018 at 14:43
-
If you've changed the name of the layer from "layer" to "out_VHQ_5" you should also change del "layer" to del "out_VHQ_5". You could also set your environment overwriteOutput to True to stop getting the error - though sometimes you don't want to overwrite automatically to ensure you're not wiping existing data erroneously.Michael Stimson– Michael Stimson2018年02月19日 21:37:42 +00:00Commented Feb 19, 2018 at 21:37
If you read the help file for this tool and look at the syntax section what does it takes as input? A Feature Layer. Unlike a Feature Class, you can do selections on Feature Layers. As all geo-processing tools honour selections first all you need to do is select the points of interest and have that selection feed into the Kernel Density tool.
- So you need to convert you FeatureClass to a FeatureLayer using the Make FeatureLayer tool
- Select by Atttibute to create the selection of interest
- Feed the layer with it's selection into Kernel Density tool.
-
KD also accepts feature classes and shapefiles in addition to feature layers.2018年02月18日 17:00:53 +00:00Commented Feb 18, 2018 at 17:00
-
1True but @Lasse wants to process subsets of their data and passing selections into the tool is a way of doing it without having to create intermediate datasets. They can wrap all that up in their loop.Hornbydd– Hornbydd2018年02月18日 18:35:58 +00:00Commented Feb 18, 2018 at 18:35
Explore related questions
See similar questions with these tags.
shapefile
folder, does the Full-Service-Restaurants value appear in more than one shapefile?