You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This script was developed to correct the extent of feature classes in a database (works on any geodatabase, not just GeMS).
Extents are not automatically updated for a feature class when it is edited or copied and this can lead to unexpected results from the Build Metadata tool when it calculates the bounding coordinates of a database. It is also probably a good idea for the "hygiene" of a finished GeMS database to have the extents of all feature classes correctly defined.
# RecalculateFCExtents.py
# Andrew L. Wunderlich
# [email protected]
# 12/4/2023
import arcpy
import os
import sys
import GeMS_utilityFunctions as GuF
# When adding to a toolbox:
# GENERAL:
# Name: RecalculateFCExtents
# Label: Recalculate Feature Class Extents
# Description: Cycles through all feature classes in a geodatabase and updates their extent.
# This tool modifies the input geodatabase. ALWAYS back up your
# datasets before using a tool that modifies any of the inputs!!
#
# From the Esri ArcGIS Pro tool reference:
# A feature class has a spatial extent that is based on all the coordinates in the feature class. This
# spatial extent is used when adding a feature class to a map to recenter and display all the features. Rather than
# examining every feature in the feature class each time the feature class is added to a map (a potentially long
# process), a feature class has an extent property containing the last known spatial extent. However, this extent
# property is not always updated when features in the feature class are edited. This means that the values in the
# extent property may not contain the actual spatial extent of the features. The Recalculate Feature Class Extent
# tool reads all the features and updates the extent property.
#
# SOURCE: Script File: point to this file
# PARAMETERS: Input Database, Workspace
# Set up workspace
# Use sys.argv[1] when running script from toolbox
# inWorkspace = r"c:\temp\testing\input.gdb"
inWorkspace = sys.argv[1]
# Set the workspace for reading the input features
arcpy.env.workspace = inWorkspace
GuF.addMsgAndPrint('Input workspace ' + inWorkspace)
# Create an empty list of all input datasets (paths) that are successfully updated
updatedDatasets = []
# Create an empty list of all input datasets (paths) that were skipped
skippedDatasets = []
# Walk through input database, find all feature classes, and update their extent
for root, dirs, datasets in arcpy.da.Walk(inWorkspace, datatype="FeatureClass"):
if len(dirs) > 0:
#GuF.addMsgAndPrint('Items within feature datasets:')
for fds in dirs:
#GuF.addMsgAndPrint('Feature dataset: ' + fds)
for root2, dirs2, datasets2 in arcpy.da.Walk(fds):
for ds2 in datasets2:
#GuF.addMsgAndPrint(' Item: ' + os.path.join(ds2))
# Try to update the extent of the dataset to the target database
try:
arcpy.management.RecalculateFeatureClassExtent(ds2)
#GuF.addMsgAndPrint(' ...extent successfully updated')
updatedDatasets.append(os.path.join(root2, ds2))
except:
#GuF.addMsgAndPrint(' ...update extent failed')
skippedDatasets.append(os.path.join(root2, ds2))
# Look in the root of the database for stand-alone datasets
if len(datasets) > 0:
#GuF.addMsgAndPrint('Stand-alone datasets (FCs, tables, etc.) in ' + inWorkspace.split('\\')[-1] + ':')
for ds in datasets:
#GuF.addMsgAndPrint(' Item: ' + os.path.join(ds))
try:
arcpy.management.RecalculateFeatureClassExtent(ds)
#GuF.addMsgAndPrint(' ...extent successfully updated')
updatedDatasets.append(os.path.join(root.split('\\')[-1], ds))
except:
#GuF.addMsgAndPrint(' ...update extent failed')
skippedDatasets.append(os.path.join(root.split('\\')[-1], ds))
break
# Show user list of skipped datasets
if len(skippedDatasets) > 0:
GuF.addMsgAndPrint(
'Skipped datasets (that failed to update) from input database ' + inWorkspace.split('\\')[-1] + ':')
for skipped in skippedDatasets:
GuF.addMsgAndPrint(' ' + skipped)
# Show user list of updated datasets
if len(updatedDatasets) > 0:
GuF.addMsgAndPrint(
'Feature classes with successfully updated extents from input database ' + inWorkspace.split('\\')[-1] + ':')
for updated in updatedDatasets:
GuF.addMsgAndPrint(' ' + updated)
# Final messages
GuF.addMsgAndPrint(
str(len(updatedDatasets)) + ' datasets from input database ' + inWorkspace.split('\\')[-1] +
' have successfully updated extents!')
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This script was developed to correct the extent of feature classes in a database (works on any geodatabase, not just GeMS).
Extents are not automatically updated for a feature class when it is edited or copied and this can lead to unexpected results from the Build Metadata tool when it calculates the bounding coordinates of a database. It is also probably a good idea for the "hygiene" of a finished GeMS database to have the extents of all feature classes correctly defined.
Beta Was this translation helpful? Give feedback.
All reactions