TransWikia.com

Plotting polygon bounding box using ArcGIS API for Python

Geographic Information Systems Asked by ycartwhelen on June 27, 2021

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
     extentList.append(extent)
 
# 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
copySDF.set_geometry(['MBRextent'])

# 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
polygonSDF['MBRextent'].astype(Geometry)

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



# Attempt 3 - setting type after creation of extent column
polygonSDF["MBRextent"].astype(polygonSDF['SHAPE'].dtype)

# Attempt 3 error
NotImplementedError: 

Note: This same question has also been posted on GeoNet. Trying to get wider visibility here due to lack of answers so far.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP