How to obtain number of cells in neighborhood from NbrWeight neighborhood object?

2766
4
Jump to solution
10-15-2015 10:06 AM
AmelieDavis
New Contributor II

Hi,

I'd like to get the first two numbers in the .txt file that is loaded as by NbrWeight (neighborhood object weights) for the focal statistics tool.

I want to use them to calculate the area of the rectangle that is the neighborhood.

How do I easily access the values?

Here is what I have so far:

inWeightFile = arcpy.GetParameterAsText(5)

if inWeightFile == '#' or not inWeightFile:

    inWeightFile = "C:\\temp\\PPR\\Test\\bee_kernel_2_5km.txt" # provide a default value if unspecified

# Create the Neighborhood Object for the buffer

myNbrWeight = arcpy.sa.NbrWeight(inWeightFile)

# Get number of cells in the buffer

NbrCellsBuffer = 65 * 65 # XXX need to fix this. Should come from first two numbers in inWeightFile

# Process: Focal Statistics - within buffer calculate sum of f_rc1_H1 with exponential decay function and divide by number of cells in buffer

arcpy.AddMessage("Calculate proportional cover of high floral quality classes within buffer for first floral season")

f_rc_f_H1 = (arcpy.sa.FocalStatistics(f_rc1_H1, myNbrWeight, "SUM", "NODATA") * outConstRaster) / NbrCellsBuffer

Any help is greatly appreciated.

Best,

Amelie

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor
ncols, nrows = [int(s) for s in open(inWeightFile).readlines()[0].strip().split()]
NbrCellsBuffer = ncols * nrows

The above opens the file and grabs the first line, strips off the newline ('\n') character and splits it into a list of two strings at the space then uses python list comprehension to convert the strings to integer values.

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

showing the text file would be useful

0 Kudos
AmelieDavis
New Contributor II

yes, sorry.

The text file is just the kernel they talked about here  ArcGIS Desktop saved as a .txt.

I'm attaching my kernel (it's a bit more complicated than the examples provided in the help).

Amelie

0 Kudos
Luke_Pinner
MVP Regular Contributor
ncols, nrows = [int(s) for s in open(inWeightFile).readlines()[0].strip().split()]
NbrCellsBuffer = ncols * nrows

The above opens the file and grabs the first line, strips off the newline ('\n') character and splits it into a list of two strings at the space then uses python list comprehension to convert the strings to integer values.

curtvprice
MVP Esteemed Contributor

Amelie,

Irregular neighborhoods could be implemented better in arcpy. Here's more fun I've had with them:

Working with kernel files in arcpy: NbrIrregular bug

That is one massive kernel file!