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

2354
7
Jump to solution
06-15-2020 11:58 AM
NealBanerjee
Occasional Contributor

Hello

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

1 Solution

Accepted Solutions
JeffreyBurka
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.

 

calcExtentValue.png

View solution in original post

7 Replies
DavidPike
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
NealBanerjee
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
JeffreyBurka
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.

 

calcExtentValue.png

JohnGargiulo
New Contributor III

This worked great! Thank you for posting Jeffrey. 

0 Kudos
bdewitt_olsson
New Contributor III

Thanks for this workaround! Create Fishnet and Grid Index Features both have this same problem in ModelBuilder. It would be nice if the input extent text auto-populated when running the model like it does when you run the standalone tool. It seems silly to need a multiple-line Python script just to pass on the extent of a feature class to a tool, but I haven't found a better way.

0 Kudos
AlbertoCañivano2
New Contributor II

I am trying to get the extension using this post.
From a Feature Set that the user will populate (add a colon) and use as an extension to get a slope smaller in size.

AlbertoCaivano2_0-1678108766257.pngAlbertoCaivano2_1-1678108801068.png


The problem is that I only get the extension of the first point when I use the Feature set (append). And not the extension of the two generated points; so the cut extension of the slope raster is smaller than the one I am interested in.

AlbertoCaivano2_2-1678108855611.png


Can you find the reason why? Thank you

 

0 Kudos
curtvprice
MVP Esteemed Contributor

Your cursor is capturing the shape of the first point. I believe that if you want the extent of the union of both points, you want to get the extent from arcpy.Describe() on the feature set.

0 Kudos