1

I'm not understanding fully how extent is supposed to work in ArcPy/Python. I can't seem to get it to work in a function so I can filter it through my feature classes (FCs). I have read the ESRI help files, but remain confused.

Below is a description of what I am trying to accomplish, followed by the little code I have so far. I think it is an issue with my understanding of how this operation works, but any/all advice is welcome.

A script that will prompt for the name of a File Geodatabase and an XMIN, YMIN, XMAX, YMAX set of coordinates. Then loop over each feature class and determine if the extent of each feature class overlaps this extent. If so, it will also determine how many features are within that extent. *** Note: It should report 0 for any feature classes that process through the extent but have no features in the extent. Also, check if any of the following geometry operations exist: within, contains, or overlaps.

    import arcpy, os
    from arcpy import env
    arcpy.env.overwriteOutput = True

    #Set user-defined inputs
    gdb = r"F:\....\&&&.gdb"
    XMin = 1260000
    YMin = 337000
    XMax = 1280000
    YMax = 360000

    # Set extent
    def Extent():
        XMin = 1260000
        YMin = 337000
        XMax = 1280000
        YMax = 360000
        try:
             arcpy.env.extent = arcpy.Extent(XMIN,YMIN,XMAX,YMAX)
             extent = arcpy.env.extent
             return extent
        except:
            print ("An Error has occurred in Extent().")
    extent = Extent()
    print extent

    #List available Feature Classes in a List
    def FCs_in_GDB(gdb):
        try:
             arcpy.env.workspace = gdb
             print (arcpy.env.workspace)
             fcs = []
            for fds in arcpy.ListDatasets('','feature') + ['']:
                for fc in arcpy.ListFeatureClasses('','',fds):
                    fcs.append(os.path.join(fds, fc))
            return fcs
         except:
             print ("An Error has occurred in FCs_in_GDB().")
    fcs = FCs_in_GDB(gdb)
    for fc in fcs:
         print fc
1

Found my answer, was just a confusion on my part. Obviously a lot of brain work and ArcGIS help was involved, so if someone cares to explain further what exactly I did, it may help to clarify things for others with the same confusions I had.

For those interested, here is the solution I came up with:

#Define Geodatabase and workspace
    #gdb = r"F:\...\####.gdb"
    env.workspace = gdb
    env.scratchworkspace = gdb
    # Set extent
    extent = arcpy.Extent(XMIN,YMIN,XMAX,YMAX)
#List available Feature Classes in a List
    def FCs_in_GDB(gdb):
        try:
            fcs = []
            for fds in arcpy.ListDatasets('','feature') + ['']:
                for fc in arcpy.ListFeatureClasses('','',fds):
                    fcs.append(os.path.join(fds, fc))
            return fcs
        except:
            print ("An Error has occured in FCs_in_GDB().")
    fc_list = FCs_in_GDB(gdb)
#Process Describe() filter on Count function
    for fc in fc_list:
        try:
            name = arcpy.Describe(fc).name
            x = arcpy.Describe(fc).extent
            overlaps = x.overlaps(extent)
            contains = x.contains(extent)
            within = x.within(extent)
            if overlaps==True or contains==True or within==True:
                count = arcpy.GetCount_management(fc)
        except:
            print ("An Error has occurred in fc_list().")
0

To understand extents in ArcPy I would recommend reviewing:

Tools that honor the Output Extent environment will only process features or rasters that fall within the extent specified in this setting.

The Output Extent environment setting defines what features or rasters will be processed by a tool. It is useful when you need to process only a portion of a larger dataset. You can think of this setting as a rectangle used to select input features and rasters for processing. Any feature or raster that passes through the rectangle will be processed and written to output. Note that the rectangle is used only to select features, not clip them. The extent of the output dataset will typically be larger than the Output Extent setting to account for features that pass through the extent rectangle.

An extent is a rectangle specified by providing the coordinate of the lower left corner and the coordinate of the upper right corner in map units.

To find out how many features pass through an extent there are at two ways that I have used in the past, which depends on the situation:

  • set arcpy.env.extent(), and then use CopyFeatures followed by GetCount - this is good if not too many features are copied
  • create a rectangle geometry and then use it as the select_features in Select Layer By Location, followed by Get Count
  • I reviewed those documents, which actually led me to my confusion. And then I thought about your Select by Location option, but wondered if could use the Extent object as that rectangle. Then I realized that by setting the Extent, I was setting an environmental setting; therefore, most geoprocessing process would automatically output in that Extent, thus allowing me to ignore having to create a function to filter the FCs through such a rectangle. I then took that thought process, and applied it to the Describe function to eventually get my answer. – catalyst123 Sep 27 '15 at 0:01
  • That being said, Thank you. Writing my problem out, and reading/reviewing your response allowed me to put 1+1 together – catalyst123 Sep 27 '15 at 0:02
  • Excellent. It would be great if you could write up a self-answer to your question. It is perfectly permissible to do so and is encouraged. It will help you to build reputation and help unlock many more of the benefits this site can provide, and it will help future visitors with the same problem. – PolyGeo Sep 27 '15 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.