Calculate and use extent of feature class in model builder for geoprocessing task

06-15-2020 11:58 AM
Occasional Contributor


I have a series of models.  One of the models creates/overwrites a feature class based on a user selection of features.  A second tool is trying to run an 'Union' tool using the extents of the feature class mentioned.  Initially I had extents of feature class as an environment input into Union in my model, however, I found that I have to revalidate the model every time before I run, otherwise it just uses the same extents coordinates. 

I tried using the 'Calculate Value' saving value as 'Extent' object to recalculate the extent and use that everytime, but it doesnt work.  It runs without errors but does not appear to use the extents Im trying to pass through.  Ive tried several syntax options but none work.

Any ideas on how to do this?  Below is very simple model

0 Kudos
4 Replies
MVP Frequent Contributor

Those extent objects can be a bit tricky, I might amend it to something like:

#create a copy of extent object to modify

new_extent = objDes.extent

#set values for extent object

new_extent.XMin = objDes.extent.XMin

new_extent.XMax = objDes.extent.XMax

new_extent.YMin = objDes.extent.YMin

new_extent.YMax = objDes.extent.YMax

return new_extent

No idea if that will work as I struggle with these also.  You may just want to run the 'Feature envelope to polygon' tool on the data then use that polygon as the extent if not.

0 Kudos
Occasional Contributor

Hi David - thanks for the suggestion, but unfortunately trying to set the values didnt seem to work either. Ill look into the envelope option - that sounds promising.

Thanks again

0 Kudos
New Contributor III

(I realize this is a somewhat old question, but having just run into the issue today, I wanted to make sure there's a googleable answer for the next time it stymies me!)

This is a weird one, and the esri documentation is uncharacteristically unhelpful. Here's what I found that worked...

Basically, the function needs to return a string, not an extent object; the output type is being set to enable you to chain items in ModelBuilder but isn't actually relevant to the value you return! What worked for me was to return a string in the format "xmin ymin xmax ymax" and set the output type to Extent. Arcpy/ModelBuilder are smart enough to know from the output type that you should be able to use the output of your function as input to an Extent parameter of another tool, and are also smart enough to handle the coercion from the string formatting of the extent to the expected Extent object.



New Contributor III

This worked great! Thank you for posting Jeffrey. 

0 Kudos