Select to view content in your preferred language

ArcGIS API for Python - plotting extent rectangle of a polygon

08-13-2020 08:53 AM
New Contributor II

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 = []
for f in polygonFeatureSet.features:
     # Calculate the extent
     (w, s, e, n) = Geometry(f.geometry).geoextent
     extent = Polygon({
     "rings" : [[[e, s],[w, s],[w, n],[e, n], [e, s]]],
     "spatialReference" : {"wkid" : 4326} })

     # Add the extent to extentList
# Add the extents to the dataframe as a new column
polygonSDF["MBRextent"] = extentList‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

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.

# Attempt 1
copySDF['SHAPE'] = copySDF['MBRextent']# Attempt 1 Error
AttributeError: Cannot use 'geom' accessor on objects of dtype 'object'.‍‍‍‍‍‍‍‍

# Attempt 2

# Attempt 2 Error
TypeError: Input geometry column must contain valid geometry objects.

# Attempt 3
extentDF = pd.DataFrame(polygonSDF["MBRextent"]).set_geometry(["MBRextent"])

# Attempt 3 Error
TypeError: Input geometry column must contain valid geometry objects.‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

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

# Attempt 1 - replacement of Line 12 when creating the extent column
polygonSDF["MBRextent"] = pd.Series(extentList, dtype=Polygon)

# Attempt 1 error
TypeError: dtype '<class 'arcgis.geometry._types.Geometry'>' not understood

# Attempt 2 - setting type after creation of extent column

# Attempt 2 error
TypeError: dtype '<class 'arcgis.geometry._types.Geometry'>' not understood

# Attempt 3 - setting type after creation of extent column

# Attempt 3 error
NotImplementedError: ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
0 Replies