Select to view content in your preferred language

Calculating moving average on fishnet vector data

2217
7
10-12-2016 12:22 AM
OscarMonell
Esri Contributor

I have users interested in calculating a moving average on fishnet vector data (squares with statistical data). Each vector grid contains one or more columns with numerical values (population data). Converting to raster data is not an option at this time.

One user created an ArcView script "back in the days".

Any suggestions?

Peter Gustafsson

Daniel Nilsson

0 Kudos
7 Replies
DanielNilsson
New Contributor

To do this I would create a python script that steps through the polygons, creates a temporary midpoint circle or buffer (or whatever shape you would like) with a desired radius. Then use this temporary shape to select the grid squares underneath (use a copy/clone as you already are stepping through the same dataset). Then summarize the desired attribute/attributes on that particular selection and store them either in a new dataset or preferably as new columns in the original grid dataset. Both sums and averages could be stored (the nominator area for the average can either come from the circular shape or be calculated as a sum of areas of the selected grids)

Then the script moves on to the next grid until all is stepped through.

Which selection type you use for selecting the grids must of course be desided (either "completely inside" or just plain intersect).

The summarized values stored as the result for each grid must be thoroughly explained as they have really nothing to do with the original grid size except that the grid size, is the one used as the "moving steps" for the analysis.

I'm not a python programmer myself so I can't help you with any actual coding.

/Daniel

Skickat från min Samsung-enhet

0 Kudos
DarrenWiens2
MVP Honored Contributor

Here is a Python translation of Daniel's recipe above. It works, technically, but is exceedingly slow because there is a selection for every fishnet cell. A better solution would involve reading the fishnet features into a data structure of some sort and operating on that, but that gets quite a bit more complicated.

fc = 'fishnet' # feature layer
with arcpy.da.UpdateCursor(fc,['SHAPE@','MEAN']) as cursor: # get each fishnet geometry and field to store mean
    for row in cursor: # loop
        arcpy.SelectLayerByLocation_management(fc,"WITHIN_A_DISTANCE",row[0],'100',"NEW_SELECTION") # make selection
        sum = 0 # initialize sum
        count = int(arcpy.GetCount_management(fc).getOutput(0)) # count number of selected features
        with arcpy.da.SearchCursor(fc,['POP']) as cursor2: # loop through selected features, reading population value
            for row2 in cursor2:
                sum += row2[0] # add to current sum
        row[1] = sum/count # calculate and store mean
        cursor.updateRow(row) # write mean to feature layer
OscarMonell
Esri Contributor

Thank's Darren! 

Will check with my Python staff if this is something to start with!

0 Kudos
JenoraD_Acosta
Deactivated User

The Focal Statistics tool will do this.  Maybe helpful?

0 Kudos
OscarMonell
Esri Contributor

Yes, 

except that it only works with raster datasets. 

Our use case is looking for the same toolsets but with vector based regular shapes (just like a raster, but as polygons).

0 Kudos
curtvprice
MVP Esteemed Contributor

If it is a regular grid, why do you have to use polygons? If it's a license issue, the Feature To Raster tool does not require a spatial analyst license, and neither does RasterToNumpyArray, so you can do this kind of thing fairly easily with numpy arrays. 

If you are stuck with it, Darren's approach will work faster if you turn off Arcmap drawing (if running from arcmap) with arcpy.env.AddOutputsToMap = False, and if you copy your polygon dataset into in_memory instead of on disk.

ChrisDonohue__GISP
MVP Alum

Any chance you still have access to ArcView 3.x?  If so, the option of doing it in Avenue would be available.  Might be worth a try if you have the script already and don't need to modernize.

Chris Donohue, GISP

0 Kudos