Hi Mike,
I know this is a long call since the subject is old but... I am trying to do something very similar and I'm wondering if you ever wrote that script and would like to share it? Was this work published?
Thanks in advance
Mel
Fun Python task! I think it should go something like this:
<SPAN class="keyword token">import</SPAN> random pa <SPAN class="operator token">=</SPAN> <SPAN class="string token">'protected_areas'</SPAN> <SPAN class="comment token"># protected areas</SPAN> sr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>pa<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference <SPAN class="comment token"># spatial ref</SPAN> sa <SPAN class="operator token">=</SPAN> <SPAN class="string token">'study_area'</SPAN> <SPAN class="comment token"># study area</SPAN> extent <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>sa<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>extent <SPAN class="comment token"># study area extent</SPAN> sa_geom <SPAN class="operator token">=</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="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>sa<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN>spatial_reference<SPAN class="operator token">=</SPAN>sr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># study area geometry</SPAN> new_polys <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># placeholder</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">rotate</SPAN><SPAN class="punctuation token">(</SPAN>poly<SPAN class="punctuation token">,</SPAN>centroid<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># magic function</SPAN> ang <SPAN class="operator token">=</SPAN> random<SPAN class="punctuation token">.</SPAN>random<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="operator token">*</SPAN> math<SPAN class="punctuation token">.</SPAN>pi <SPAN class="comment token"># random rotation angle in radians</SPAN> new_array <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># placeholder</SPAN> rnd_x <SPAN class="operator token">=</SPAN> random<SPAN class="punctuation token">.</SPAN>uniform<SPAN class="punctuation token">(</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMin<SPAN class="punctuation token">,</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMax<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># random x coordinate for new centroid</SPAN> rnd_y <SPAN class="operator token">=</SPAN> random<SPAN class="punctuation token">.</SPAN>uniform<SPAN class="punctuation token">(</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMin<SPAN class="punctuation token">,</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMax<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># random y coordinate for new centroid</SPAN> rnd_centroid <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>rnd_x<SPAN class="punctuation token">,</SPAN>rnd_y<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># centroid point</SPAN> <SPAN class="keyword token">for</SPAN> part <SPAN class="keyword token">in</SPAN> poly<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># for each polygon part</SPAN> <SPAN class="keyword token">for</SPAN> pnt <SPAN class="keyword token">in</SPAN> part<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># for each vertex</SPAN> x_trans <SPAN class="operator token">=</SPAN> pnt<SPAN class="punctuation token">.</SPAN>X <SPAN class="operator token">-</SPAN> centroid<SPAN class="punctuation token">.</SPAN>X <SPAN class="comment token"># normalize to zero</SPAN> y_trans <SPAN class="operator token">=</SPAN> pnt<SPAN class="punctuation token">.</SPAN>Y <SPAN class="operator token">-</SPAN> centroid<SPAN class="punctuation token">.</SPAN>Y <SPAN class="comment token"># normalize to zero</SPAN> x_transprime <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>math<SPAN class="punctuation token">.</SPAN>cos<SPAN class="punctuation token">(</SPAN>ang<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> x_trans<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> <SPAN class="punctuation token">(</SPAN>math<SPAN class="punctuation token">.</SPAN>sin<SPAN class="punctuation token">(</SPAN>ang<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> y_trans<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># new x coord, from zero</SPAN> y_transprime <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>math<SPAN class="punctuation token">.</SPAN>sin<SPAN class="punctuation token">(</SPAN>ang<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> x_trans<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>math<SPAN class="punctuation token">.</SPAN>cos<SPAN class="punctuation token">(</SPAN>ang<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> y_trans<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># new y coord, from zero</SPAN> x_prime <SPAN class="operator token">=</SPAN> x_transprime <SPAN class="operator token">+</SPAN> rnd_centroid<SPAN class="punctuation token">.</SPAN>X <SPAN class="comment token"># move to new centroid x</SPAN> y_prime <SPAN class="operator token">=</SPAN> y_transprime <SPAN class="operator token">+</SPAN> rnd_centroid<SPAN class="punctuation token">.</SPAN>Y <SPAN class="comment token"># move to new centroid y</SPAN> new_array<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>x_prime<SPAN class="punctuation token">,</SPAN> y_prime<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># add to array</SPAN> candidate_poly <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Polygon<SPAN class="punctuation token">(</SPAN>new_array<SPAN class="punctuation token">,</SPAN>sr<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># make polygon from array</SPAN> <SPAN class="keyword token">for</SPAN> new_poly <SPAN class="keyword token">in</SPAN> new_polys<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># compare to polygons already added to new_polys</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> candidate_poly<SPAN class="punctuation token">.</SPAN>disjoint<SPAN class="punctuation token">(</SPAN>new_poly<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">or</SPAN> <SPAN class="operator token">not</SPAN> sa_geom<SPAN class="punctuation token">.</SPAN>contains<SPAN class="punctuation token">(</SPAN>candidate_poly<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># check if overlap with polygons and is fully inside study area</SPAN> candidate_poly <SPAN class="operator token">=</SPAN> rotate<SPAN class="punctuation token">(</SPAN>poly<SPAN class="punctuation token">,</SPAN>centroid<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># if the new polygon violates criteria, call function again</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">(</SPAN>candidate_poly<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># return the final good polygon</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>pa<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN>spatial_reference<SPAN class="operator token">=</SPAN>sr<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># for each polygon</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> centroid <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>centroid <SPAN class="comment token"># calculate centroid</SPAN> new_polys<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>rotate<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>centroid<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># add returned good polygon to list</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN>new_polys<SPAN class="punctuation token">,</SPAN>r<SPAN class="string token">'in_memory\new_polys'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># write to disk</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></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
* rotation math from here: Rotating Features in ArcGIS for Desktop using ArcPy? - Geographic Information Systems Stack Exchange
Oh wow, thank you very much for this (and so quick), I'm looking forward to try it! May I ask what you used it for (and if the work can be read somewhere - just curious)?
Errr... I used it for making those example images. I just made it up. This is all there is to it!
So I think I understand your script and that kind of works thanks - but...
1. It looks like the small polygons (protected areas) need to be single part for it to work, even though it looks like the loop does look at multipart (?). I tried with sample data - did not work with multipart but worked fine with single.
2. My new random polygons always fall within the extent of the study area, but not always within the actual study area (which is a complex multipart polygon too). Although only using a large single part study area still did not work.
Note that I'm a Python newbie (but work well in R) so obvious things to you might not be obvious to me
I'll then have to loop that randomisation n times and calculate values based on intersection between new polygons and other layers for every randomisation... steep learning curve but will get there!
Thank you for your script...huge help for someone that has never used Python! As Melanie mentioned below, the script mostly works but random polygons are only completely within my defined study area polygon about 30-40% of the time. For those that aren't, at least some part of the random polygon is within the study area. I could generate a large sample of the random polygons and simply discard those that aren't completely contained, but do you see anything obvious in your original script that would explain what's going on. Again, many thanks!
Did you ever figure out your second issue? I'm having the same problem. Thanks!
Hey all,
Thanks @Darren Wiens for this really cool code. I was helping my friend figuring out this code. The biggest issue to me is that the random polygon would not fall in the study area polygon. So I added a try/except and if/else statement to check and return until every polygon fall in the study area. Again, I have a simply polygons for test so it might not working if you have huge amount of polygon, and these polygons should be singlepart and no hollow inside. Also, I remove the "disjoint" function for checking if the result polygon is overlap or not. I hope this help
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> da <SPAN class="keyword token">import</SPAN> random <SPAN class="keyword token">import</SPAN> math <SPAN class="comment token">#set-up environment</SPAN> workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"...:/.../..."</SPAN> <SPAN class="comment token">#location of your files</SPAN> pa <SPAN class="operator token">=</SPAN> workspace <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\\'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"polygons.shp"</SPAN> <SPAN class="comment token"># polygons to randomly rotate and move</SPAN> sr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>pa<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference <SPAN class="comment token"># spatial reference</SPAN> sa <SPAN class="operator token">=</SPAN> workspace <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\\'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"study_area.shp"</SPAN> <SPAN class="comment token"># study area</SPAN> extent <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>sa<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>extent <SPAN class="comment token"># study area extent</SPAN> sa_geom <SPAN class="operator token">=</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="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>sa<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN>spatial_reference<SPAN class="operator token">=</SPAN>sr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># study area geometry</SPAN> new_polys <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># placeholder</SPAN> <SPAN class="comment token"># magic function</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">rotate</SPAN><SPAN class="punctuation token">(</SPAN>poly<SPAN class="punctuation token">,</SPAN>centroid<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#ang = random.random() * 2 * math.pi # random rotation angle in radians if you want to have unified angle to result polygons </SPAN> new_array <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># placeholder</SPAN> rnd_x <SPAN class="operator token">=</SPAN> random<SPAN class="punctuation token">.</SPAN>uniform<SPAN class="punctuation token">(</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMin<SPAN class="punctuation token">,</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMax<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># random x coordinate for new centroid</SPAN> rnd_y <SPAN class="operator token">=</SPAN> random<SPAN class="punctuation token">.</SPAN>uniform<SPAN class="punctuation token">(</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMin<SPAN class="punctuation token">,</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMax<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># random y coordinate for new centroid</SPAN> rnd_centroid <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>rnd_x<SPAN class="punctuation token">,</SPAN>rnd_y<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># random centroid point</SPAN> <SPAN class="keyword token">while</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> part <SPAN class="keyword token">in</SPAN> poly<SPAN class="punctuation token">:</SPAN><SPAN class="comment token">#for each polygon part</SPAN> ang <SPAN class="operator token">=</SPAN> random<SPAN class="punctuation token">.</SPAN>random<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="operator token">*</SPAN> math<SPAN class="punctuation token">.</SPAN>pi <SPAN class="comment token"># random rotation angle in radians # set as 0 if you don't need to rotate polygon</SPAN> <SPAN class="keyword token">for</SPAN> pnt <SPAN class="keyword token">in</SPAN> part<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># for each vertex</SPAN> x_trans <SPAN class="operator token">=</SPAN> pnt<SPAN class="punctuation token">.</SPAN>X <SPAN class="operator token">-</SPAN> centroid<SPAN class="punctuation token">.</SPAN>X <SPAN class="comment token"># normalize to zero</SPAN> y_trans <SPAN class="operator token">=</SPAN> pnt<SPAN class="punctuation token">.</SPAN>Y <SPAN class="operator token">-</SPAN> centroid<SPAN class="punctuation token">.</SPAN>Y <SPAN class="comment token"># normalize to zero</SPAN> x_transprime <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>math<SPAN class="punctuation token">.</SPAN>cos<SPAN class="punctuation token">(</SPAN>ang<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> x_trans<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> <SPAN class="punctuation token">(</SPAN>math<SPAN class="punctuation token">.</SPAN>sin<SPAN class="punctuation token">(</SPAN>ang<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> y_trans<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># new x coord, from zero</SPAN> y_transprime <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>math<SPAN class="punctuation token">.</SPAN>sin<SPAN class="punctuation token">(</SPAN>ang<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> x_trans<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>math<SPAN class="punctuation token">.</SPAN>cos<SPAN class="punctuation token">(</SPAN>ang<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> y_trans<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># new y coord, from zero</SPAN> x_prime <SPAN class="operator token">=</SPAN> x_transprime <SPAN class="operator token">+</SPAN> rnd_centroid<SPAN class="punctuation token">.</SPAN>X <SPAN class="comment token"># move to new centroid x</SPAN> y_prime <SPAN class="operator token">=</SPAN> y_transprime <SPAN class="operator token">+</SPAN> rnd_centroid<SPAN class="punctuation token">.</SPAN>Y <SPAN class="comment token"># move to new centroid y</SPAN> new_array<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>x_prime<SPAN class="punctuation token">,</SPAN> y_prime<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># add to array</SPAN> temp_poly <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Polygon<SPAN class="punctuation token">(</SPAN>new_array<SPAN class="punctuation token">,</SPAN>sr<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># make a temporarily polygon for test</SPAN> <SPAN class="keyword token">if</SPAN> sa_geom<SPAN class="punctuation token">.</SPAN>contains<SPAN class="punctuation token">(</SPAN>temp_poly<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">is</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'inside'</SPAN> <SPAN class="keyword token">break</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">raise</SPAN> Exception <SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'outside sa, start over...'</SPAN> <SPAN class="keyword token">return</SPAN> rotate<SPAN class="punctuation token">(</SPAN>poly<SPAN class="punctuation token">,</SPAN>centroid<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#here you start over and run the loop again... until all the polygon fall in study area.</SPAN> <SPAN class="keyword token">break</SPAN> good_poly <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Polygon<SPAN class="punctuation token">(</SPAN>new_array<SPAN class="punctuation token">,</SPAN>sr<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># make all good polygons (indside study area) from array</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">(</SPAN>good_poly<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># return the final good polygon</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"start processing..."</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>pa<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">,</SPAN>spatial_reference<SPAN class="operator token">=</SPAN>sr<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># for each polygon</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> centroid <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>centroid <SPAN class="comment token"># calculate centroid</SPAN> new_polys<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>rotate<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>centroid<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># add returned good polygon to list</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN>new_polys<SPAN class="punctuation token">,</SPAN> workspace <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\\'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"TheResult.shp"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># write to disk</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"done"</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></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><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Thanks both Yin-hsuen Chen and @Darren for the code.
I am using this same code for a very large amount of polygons, and it is still working great. However, as expected, it is taking very long. Do you have any suggestions that might speed up the execution?
Thank you!
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.