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
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.
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
Thank's Darren!
Will check with my Python staff if this is something to start with!
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 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'fishnet'</SPAN> <SPAN class="comment token"># feature layer</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'MEAN'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># get each fishnet geometry and field to store mean</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># loop</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation_management<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"WITHIN_A_DISTANCE"</SPAN><SPAN class="punctuation token">,</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'100'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># make selection</SPAN> sum <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="comment token"># initialize sum</SPAN> count <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>getOutput<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># count number of selected features</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'POP'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor2<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># loop through selected features, reading population value</SPAN> <SPAN class="keyword token">for</SPAN> row2 <SPAN class="keyword token">in</SPAN> cursor2<SPAN class="punctuation token">:</SPAN> sum <SPAN class="operator token">+=</SPAN> row2<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># add to current sum</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> sum<SPAN class="operator token">/</SPAN>count <SPAN class="comment token"># calculate and store mean</SPAN> cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># write mean to feature layer</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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).
The Focal Statistics tool will do this. Maybe helpful?
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
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.