I am trying to write a script that will calculate multiple mean centers of a group of shapefiles. I have multiple shapefiles that each need a mean center calculated.
We shall rule out the center of the file extent obviously.
And you have looked at the tools in here ... An overview of the Spatial Statistics toolbox—Help | ArcGIS for Desktop
What type of features are they? Points, polylines or polygons?
The reason why I ask is because if it is the latter two, you are going to get a different value if you use all the points in a poly* feature than if you take mean center of the feature centers. This will obviously compounded by the introduction of multiple input files.
So having said that, how adept are you at scripting since both options can be done using numpy which will save you a load of searchcursor stuff. having said that you can calculate the centroids of your poly* features then get a field summary for each file. There are other options, but what you actually need will determine what method you should use.
This might do the trick:
or just:
import arcpy
meancenter = MeanCenter_stats (Input_Feature_Class, Output_Feature_Class, {Weight_Field}, {Case_Field}, {Dimension_Field})
If you don't want to program, just merge the shapefiles into a single feature class and use the Mean Center—Help | ArcGIS Desktop (in case you want to obtain 1 mean center of all the points you have in multiple shapefiles)
If you prefer using code you could use something like this and assuming that all shapefiles have the same coordinate system:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">import</SPAN> os ws <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\Folder\Where\Shapefiles\Are\Stored'</SPAN> fc_out <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\Folder\Where\Shapefiles\Are\Stored\MeanCenter.shp'</SPAN> <SPAN class="comment token"># create list of featureclasses</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> ws lst_fc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># loop through each featureclass and create list if (x,y)</SPAN> lst_all_xy <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> fc_name <SPAN class="keyword token">in</SPAN> lst_fc<SPAN class="punctuation token">:</SPAN> fc <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>ws<SPAN class="punctuation token">,</SPAN> fc_name<SPAN class="punctuation token">)</SPAN> lst_xy <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>r<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> r <SPAN class="keyword token">in</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">'SHAPE@XY'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> lst_all_xy<SPAN class="punctuation token">.</SPAN>extend<SPAN class="punctuation token">(</SPAN>lst_xy<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># create mean center</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>lst_all_xy<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN> lst_x <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>xy<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> xy <SPAN class="keyword token">in</SPAN> lst_all_xy<SPAN class="punctuation token">]</SPAN> lst_y <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>xy<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> xy <SPAN class="keyword token">in</SPAN> lst_all_xy<SPAN class="punctuation token">]</SPAN> pnt <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>sum<SPAN class="punctuation token">(</SPAN>lst_x<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">/</SPAN>float<SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>lst_x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> sum<SPAN class="punctuation token">(</SPAN>lst_y<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">/</SPAN>float<SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>lst_y<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> sr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference pntg <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>PointGeometry<SPAN class="punctuation token">(</SPAN>pnt<SPAN class="punctuation token">,</SPAN> sr<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>pntg<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> fc_out<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Change the paths on line 5 and 6
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.