I have a layer of polygons and would like to be able to plot the extent boundary rectangle on a map. I have my polygons in a Spatially Enabled DataFrame (polygonSDF = poygonFeatureSet.sdf) that I can successfully plot using spatial.plot(). I've so far tried two different potential approaches to this problem and in both cases have run into errors I haven't been able to solve. I don't want to use draw() as described in the Spatially Enabled DataFrames Advanced Topics tutorial because then you lose the attribute information of the original polygon, and just have a static graphic.
Can anyone either point out a fix to one of the approaches described below, or suggest an alternate way to solve this problem? I'm working with ArcGIS API for Python 1.6.1 in the ArcGIS 10.7.1 Jupyter Notebook set-up on Windows 10.
I'm calculating and storing the extent data as follows:
extentList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> f <SPAN class="keyword token">in</SPAN> polygonFeatureSet<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Calculate the extent</SPAN>
<SPAN class="punctuation token">(</SPAN>w<SPAN class="punctuation token">,</SPAN> s<SPAN class="punctuation token">,</SPAN> e<SPAN class="punctuation token">,</SPAN> n<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN> Geometry<SPAN class="punctuation token">(</SPAN>f<SPAN class="punctuation token">.</SPAN>geometry<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>geoextent
extent <SPAN class="operator token">=</SPAN> Polygon<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"rings"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN>e<SPAN class="punctuation token">,</SPAN> s<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN>w<SPAN class="punctuation token">,</SPAN> s<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN>w<SPAN class="punctuation token">,</SPAN> n<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN>e<SPAN class="punctuation token">,</SPAN> n<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>e<SPAN class="punctuation token">,</SPAN> s<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"spatialReference"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"wkid"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="number token">4326</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Add the extent to extentList</SPAN>
extentList<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>extent<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Add the extents to the dataframe as a new column</SPAN>
polygonSDF<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"MBRextent"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> extentList<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>Approach 1: Specify which column of geometry plot() uses
Issue: plot() doesn't appear to have this functionality, and just uses SHAPE automatically
Approach 2: Make a copy of my polygon SEDF, and then set the SHAPE column to store the extent data instead
Issue: Despite extentList containing valid Geometry objects, once loaded into the SEDF the MBRextent column is of type 'object', and thus is unable to be recognized and used as geometry.
<SPAN class="comment token"># Attempt 1</SPAN>
copySDF<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'SHAPE'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> copySDF<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'MBRextent'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Attempt 1 Error</SPAN>
AttributeError<SPAN class="punctuation token">:</SPAN> Cannot use <SPAN class="string token">'geom'</SPAN> accessor on objects of dtype <SPAN class="string token">'object'</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="comment token"># Attempt 2</SPAN>
copySDF<SPAN class="punctuation token">.</SPAN>set_geometry<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'MBRextent'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Attempt 2 Error</SPAN>
TypeError<SPAN class="punctuation token">:</SPAN> Input geometry column must contain valid geometry objects<SPAN class="punctuation token">.</SPAN>
<SPAN class="comment token"># Attempt 3</SPAN>
extentDF <SPAN class="operator token">=</SPAN> pd<SPAN class="punctuation token">.</SPAN>DataFrame<SPAN class="punctuation token">(</SPAN>polygonSDF<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"MBRextent"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>set_geometry<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"MBRextent"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Attempt 3 Error</SPAN>
TypeError<SPAN class="punctuation token">:</SPAN> Input geometry column must contain valid geometry objects<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>
Approach 2B:
To try to rectify the issues caused by the extent being of type 'object' I've tried explicitly setting the column type to Geometry.
Issue: Geometry type is not being recognized by pandas methods
<SPAN class="comment token"># Attempt 1 - replacement of Line 12 when creating the extent column</SPAN>
polygonSDF<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"MBRextent"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> pd<SPAN class="punctuation token">.</SPAN>Series<SPAN class="punctuation token">(</SPAN>extentList<SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN>Polygon<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Attempt 1 error</SPAN>
TypeError<SPAN class="punctuation token">:</SPAN> dtype <SPAN class="string token">'<class '</SPAN>arcgis<SPAN class="punctuation token">.</SPAN>geometry<SPAN class="punctuation token">.</SPAN>_types<SPAN class="punctuation token">.</SPAN>Geometry<SPAN class="string token">'>'</SPAN> <SPAN class="operator token">not</SPAN> understood
<SPAN class="comment token"># Attempt 2 - setting type after creation of extent column</SPAN>
polygonSDF<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'MBRextent'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>astype<SPAN class="punctuation token">(</SPAN>Geometry<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Attempt 2 error</SPAN>
TypeError<SPAN class="punctuation token">:</SPAN> dtype <SPAN class="string token">'<class '</SPAN>arcgis<SPAN class="punctuation token">.</SPAN>geometry<SPAN class="punctuation token">.</SPAN>_types<SPAN class="punctuation token">.</SPAN>Geometry<SPAN class="string token">'>'</SPAN> <SPAN class="operator token">not</SPAN> understood
<SPAN class="comment token"># Attempt 3 - setting type after creation of extent column</SPAN>
polygonSDF<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"MBRextent"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>astype<SPAN class="punctuation token">(</SPAN>polygonSDF<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'SHAPE'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>dtype<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Attempt 3 error</SPAN>
NotImplementedError<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>