Hello guys.
I need to create 20X30m polygons at random positions within a predefined boundary. And I also need the number of polygons created equal 25% of the area.
Any suggestion?
Nice! Now if you want Xander Bakker try option 4 (spaced points ) in my Point Tools for ArcGIS Pro if you are looking to give 2.1 a test ride. Point spacing should be guaranteed, then buffer by 1/2 the distance should also ensure no overlaps
I thought I would have a go at it, but there are a number of things that would need to be resolved.
This is the code I came up with, but it is far from fast and should not be used for a "large" set of data:
<SPAN class="keyword token">import</SPAN> arcpy <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> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> <SPAN class="comment token"># settings</SPAN> fc_in <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\RandRectangles\data.gdb\areas'</SPAN> fc_tmp <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\RandRectangles\data.gdb\rectangles_tmp'</SPAN> fc_sel <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\RandRectangles\data.gdb\rectangles_tmp_sel'</SPAN> fc_out <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GeoNet\RandRectangles\data.gdb\rectangles_v01'</SPAN> rect_width <SPAN class="operator token">=</SPAN> <SPAN class="number token">20</SPAN> rect_height <SPAN class="operator token">=</SPAN> <SPAN class="number token">30</SPAN> min_area_frac <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.25</SPAN> <SPAN class="comment token"># Some asumptions:</SPAN> <SPAN class="comment token"># rectangles should not be reused</SPAN> <SPAN class="comment token"># rectangles have common origin XY</SPAN> <SPAN class="comment token"># minimum area of 25% should be covered</SPAN> sr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>fc_in<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference ext <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>fc_in<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>extent <SPAN class="comment token"># create a tmp featureclass with the rectangles</SPAN> ext_fin <SPAN class="operator token">=</SPAN> ReshapeExtent<SPAN class="punctuation token">(</SPAN>ext<SPAN class="punctuation token">,</SPAN> rect_width<SPAN class="punctuation token">,</SPAN> rect_height<SPAN class="punctuation token">)</SPAN> feats <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> c <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN>int<SPAN class="punctuation token">(</SPAN>ext_fin<SPAN class="punctuation token">.</SPAN>width <SPAN class="operator token">/</SPAN> rect_width<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> r <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN>int<SPAN class="punctuation token">(</SPAN>ext_fin<SPAN class="punctuation token">.</SPAN>height <SPAN class="operator token">/</SPAN> rect_height<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> polygon <SPAN class="operator token">=</SPAN> CreateRectanglePolygon<SPAN class="punctuation token">(</SPAN>ext_fin<SPAN class="punctuation token">.</SPAN>XMin <SPAN class="operator token">+</SPAN> c <SPAN class="operator token">*</SPAN> rect_width<SPAN class="punctuation token">,</SPAN> ext_fin<SPAN class="punctuation token">.</SPAN>YMin <SPAN class="operator token">+</SPAN> r <SPAN class="operator token">*</SPAN> rect_height<SPAN class="punctuation token">,</SPAN> ext_fin<SPAN class="punctuation token">.</SPAN>XMin <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>c<SPAN class="operator token">+</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> rect_width<SPAN class="punctuation token">,</SPAN> ext_fin<SPAN class="punctuation token">.</SPAN>YMin <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>r<SPAN class="operator token">+</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> rect_height<SPAN class="punctuation token">,</SPAN> sr<SPAN class="punctuation token">)</SPAN> feats<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>polygon<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN>feats<SPAN class="punctuation token">,</SPAN> fc_tmp<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># now select only the ones that overlap any area polygon</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>fc_in<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'area_lyr'</SPAN><SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>fc_tmp<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'rect_lyr'</SPAN><SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"rect_lyr"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"INTERSECT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"area_lyr"</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'rect_lyr'</SPAN><SPAN class="punctuation token">,</SPAN> fc_sel<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># create a dictionary from the selected reactangles</SPAN> dct_rect <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="punctuation token">:</SPAN> r<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</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_sel<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'OID@'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># loop through polygons and determine which to use</SPAN> lst_oid_taken <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> lst_results <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</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_in<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'OID@'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'SHAPE@'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> curs<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> curs<SPAN class="punctuation token">:</SPAN> oid_area <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> area_pol <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> dct_overlap <SPAN class="operator token">=</SPAN> GetDictOfRectanglesForArea<SPAN class="punctuation token">(</SPAN>area_pol<SPAN class="punctuation token">,</SPAN> dct_rect<SPAN class="punctuation token">,</SPAN> lst_oid_taken<SPAN class="punctuation token">)</SPAN> lst_selection<SPAN class="punctuation token">,</SPAN> lst_oid_taken <SPAN class="operator token">=</SPAN> GetRandomRectanglesForArea<SPAN class="punctuation token">(</SPAN>area_pol<SPAN class="punctuation token">,</SPAN> dct_overlap<SPAN class="punctuation token">,</SPAN> lst_oid_taken<SPAN class="punctuation token">,</SPAN> min_area_frac<SPAN class="punctuation token">)</SPAN> lst_results<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>oid_area<SPAN class="punctuation token">,</SPAN> lst_selection<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> result <SPAN class="keyword token">in</SPAN> lst_results<SPAN class="punctuation token">:</SPAN> oid_area <SPAN class="operator token">=</SPAN> result<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> lst_selection <SPAN class="operator token">=</SPAN> result<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> oid_rect <SPAN class="keyword token">in</SPAN> lst_selection<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{0}\t{1}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>oid_rect<SPAN class="punctuation token">,</SPAN> oid_area<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">GetRandomRectanglesForArea</SPAN><SPAN class="punctuation token">(</SPAN>area<SPAN class="punctuation token">,</SPAN> dct_overlap<SPAN class="punctuation token">,</SPAN> lst_oid_taken<SPAN class="punctuation token">,</SPAN> min_area_frac<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">import</SPAN> random lst_selection <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> lst_oid <SPAN class="operator token">=</SPAN> dct_overlap<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> polygon_area <SPAN class="operator token">=</SPAN> area<SPAN class="punctuation token">.</SPAN>area tot_rect_area <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> fraction <SPAN class="operator token">=</SPAN> tot_rect_area <SPAN class="operator token">/</SPAN> polygon_area cnt <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"\nGetRandomRectanglesForArea"</SPAN> <SPAN class="keyword token">while</SPAN> fraction <SPAN class="operator token"><</SPAN> min_area_frac<SPAN class="punctuation token">:</SPAN> cnt <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - cnt:"</SPAN><SPAN class="punctuation token">,</SPAN> cnt i <SPAN class="operator token">=</SPAN> random<SPAN class="punctuation token">.</SPAN>randrange<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> len<SPAN class="punctuation token">(</SPAN>lst_oid<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - i:"</SPAN><SPAN class="punctuation token">,</SPAN> i oid <SPAN class="operator token">=</SPAN> lst_oid<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="string token">" - oid:"</SPAN><SPAN class="punctuation token">,</SPAN> oid <SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> oid <SPAN class="keyword token">in</SPAN> lst_oid_taken<SPAN class="punctuation token">:</SPAN> rectangle <SPAN class="operator token">=</SPAN> dct_overlap<SPAN class="punctuation token">[</SPAN>oid<SPAN class="punctuation token">]</SPAN> overlap_area <SPAN class="operator token">=</SPAN> GetOverlapArea<SPAN class="punctuation token">(</SPAN>area<SPAN class="punctuation token">,</SPAN> rectangle<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - overlap_area:"</SPAN><SPAN class="punctuation token">,</SPAN> overlap_area tot_rect_area <SPAN class="operator token">+=</SPAN> overlap_area <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - tot_rect_area:"</SPAN><SPAN class="punctuation token">,</SPAN> tot_rect_area fraction <SPAN class="operator token">=</SPAN> tot_rect_area <SPAN class="operator token">/</SPAN> polygon_area <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" - fraction:"</SPAN><SPAN class="punctuation token">,</SPAN> fraction lst_oid_taken<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>oid<SPAN class="punctuation token">)</SPAN> lst_selection<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>oid<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> cnt <SPAN class="operator token">></SPAN> <SPAN class="number token">100</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">break</SPAN> <SPAN class="keyword token">return</SPAN> lst_selection<SPAN class="punctuation token">,</SPAN> lst_oid_taken <SPAN class="keyword token">def</SPAN> <SPAN class="token function">GetOverlapArea</SPAN><SPAN class="punctuation token">(</SPAN>area<SPAN class="punctuation token">,</SPAN> rectangle<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> rectangle<SPAN class="punctuation token">.</SPAN>within<SPAN class="punctuation token">(</SPAN>area<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> rectangle<SPAN class="punctuation token">.</SPAN>area <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> overlap <SPAN class="operator token">=</SPAN> area<SPAN class="punctuation token">.</SPAN>intersect<SPAN class="punctuation token">(</SPAN>rectangle<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> overlap<SPAN class="punctuation token">.</SPAN>area <SPAN class="keyword token">def</SPAN> <SPAN class="token function">GetDictOfRectanglesForArea</SPAN><SPAN class="punctuation token">(</SPAN>area<SPAN class="punctuation token">,</SPAN> dct<SPAN class="punctuation token">,</SPAN> lst_done<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> dct_overlap <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">for</SPAN> oid<SPAN class="punctuation token">,</SPAN> rectangle <SPAN class="keyword token">in</SPAN> dct<SPAN class="punctuation token">.</SPAN>items<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> oid <SPAN class="keyword token">in</SPAN> lst_done<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> rectangle<SPAN class="punctuation token">.</SPAN>within<SPAN class="punctuation token">(</SPAN>area<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> dct_overlap<SPAN class="punctuation token">[</SPAN>oid<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> rectangle <SPAN class="keyword token">elif</SPAN> rectangle<SPAN class="punctuation token">.</SPAN>overlaps<SPAN class="punctuation token">(</SPAN>area<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> dct_overlap<SPAN class="punctuation token">[</SPAN>oid<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> rectangle <SPAN class="keyword token">return</SPAN> dct_overlap <SPAN class="keyword token">def</SPAN> <SPAN class="token function">CreateRectanglePolygon</SPAN><SPAN class="punctuation token">(</SPAN>xmin<SPAN class="punctuation token">,</SPAN> ymin<SPAN class="punctuation token">,</SPAN> xmax<SPAN class="punctuation token">,</SPAN> ymax<SPAN class="punctuation token">,</SPAN> sr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> points <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>xmin<SPAN class="punctuation token">,</SPAN> ymin<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>xmin<SPAN class="punctuation token">,</SPAN> ymax<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>xmax<SPAN class="punctuation token">,</SPAN> ymax<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>xmax<SPAN class="punctuation token">,</SPAN> ymin<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>xmin<SPAN class="punctuation token">,</SPAN> ymin<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">return</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Polygon<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Array<SPAN class="punctuation token">(</SPAN>points<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> sr<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">ReshapeExtent</SPAN><SPAN class="punctuation token">(</SPAN>ext<SPAN class="punctuation token">,</SPAN> width<SPAN class="punctuation token">,</SPAN> height<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> xmin_new <SPAN class="operator token">=</SPAN> divmod<SPAN class="punctuation token">(</SPAN>ext<SPAN class="punctuation token">.</SPAN>XMin<SPAN class="punctuation token">,</SPAN> width<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">*</SPAN> width ymin_new <SPAN class="operator token">=</SPAN> divmod<SPAN class="punctuation token">(</SPAN>ext<SPAN class="punctuation token">.</SPAN>YMin<SPAN class="punctuation token">,</SPAN> height<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">*</SPAN> height xmax_new <SPAN class="operator token">=</SPAN> divmod<SPAN class="punctuation token">(</SPAN>ext<SPAN class="punctuation token">.</SPAN>XMax<SPAN class="punctuation token">,</SPAN> width<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">*</SPAN> width <SPAN class="operator token">+</SPAN> width ymax_new <SPAN class="operator token">=</SPAN> divmod<SPAN class="punctuation token">(</SPAN>ext<SPAN class="punctuation token">.</SPAN>YMax<SPAN class="punctuation token">,</SPAN> height<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">*</SPAN> height <SPAN class="operator token">+</SPAN> height <SPAN class="keyword token">return</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Extent<SPAN class="punctuation token">(</SPAN>xmin_new<SPAN class="punctuation token">,</SPAN> ymin_new<SPAN class="punctuation token">,</SPAN> xmax_new<SPAN class="punctuation token">,</SPAN> ymax_new<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></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><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>
The result is list of rectangle OIDs with the corresponding area OIDs which I joined back to the rectangles (since I was too lazy to create a proper output featureclass.
The steps are more or less the following:
1) a tmp featureclass with rectangles is created using lower left XY which is a multiple of the width and height and covers the entire area:
2) The rectangle features that overlap the areas are selected and read into a dictionary:
3) random rectangles are selected until at least 25% overlap is obtained (taking only the overlapping area of the rectangles into account):
There are still some situations that are probably not desired. Such as rectangles that have a very small overlap or have a larger overlap with another polygon and that might not be what you want:
So it will probably need to be optimized further.
That might entail 'humanware' to provide some guidance. You could however, determine the extent of the base polygon then use that extent to set the bounds of the fishnet. You would just want to ensure that your fishnet has sufficient rows and columns that you could get a 25% sample of rectangles within the base polygon. That is not a problem since 'select by spatial location' with a 'completely within' control would ensure that you select only polygons within the base polygon, then continue to select polygons perhaps incrementally, until the 25% areal coverage is met.
I'd like the shape of the polygons to be rectangular (20 X 30 meters). And, where would the "fishnet" look for the area of the "base polygon" to know how many polygons (20X30m) to be 25% sampled area of the "base polygon"?
you could generate a fishnet within the area that more or less covers it. Assign a random number to each grid square then sample 25% and dump the rest.
Also, random polygons could also be accomplished by buffering 25 random points that are at a distance no-closer-than (I have code for this in python)
Do you want random shapes? (not going to happen easily)
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.