ArcPy.mp Get Selected Features Extent

3717
7
01-30-2020 10:00 AM
EmmaBell-Carruthers1
New Contributor II

I am trying to get the extent of the selected features in arcpy to pass into CreateFishnet. Currently I have it working so it will only create a fishnet for the last selected feature because it is just looping through the rows of the features. I have copied the code below. Any help would  be great. 

import arcpy


arcpy.env.overwriteOutput = True

cellSize = 50
sa = 2500
fullPlots = 0
halfPlots = 0

blockNum = arcpy.da.SearchCursor("L3OpSilv", "BLOCK_").next()[0]
whereClause = "BLOCK_ = '{0}'".format(blockNum)
#arcpy.AddMessage(whereClause)
with arcpy.da.SearchCursor('L3OpSilv', ["BLOCK_","SHAPE@"], whereClause) as rows:
    for row in rows:
        pLowerLeft = str(row[1].extent.lowerLeft)
        pUpperRight = str(row[1].extent.upperRight)
        fullExtent = row[1].extent
        extXMin = row[1].extent.XMin
        #arcpy.AddMessage(row[1].extent.YMin)
        extYMin = str(row[1].extent.YMin + 10)
        #arcpy.AddMessage(extYMin)
yAxisCoord = (str(extXMin) + " " + str(extYMin))

arcpy.management.CreateFishnet('fullextentfishnet', pLowerLeft, yAxisCoord, cellSize, cellSize, None, None, pUpperRight, 'NO_LABELS', fullExtent, 'Polygon')
fishnetclipped = arcpy.Clip_analysis('fullextentfishnet', 'L3OpSilv', 'fishnetclip')
0 Kudos
7 Replies
DavidPike
MVP Frequent Contributor

What do you want to happen or what is not working? I'm sorry I don't understand the question.

0 Kudos
DavidPike
MVP Frequent Contributor
import arcpy


arcpy.env.overwriteOutput = True

cellSize = 50
sa = 2500
fullPlots = 0
halfPlots = 0

blockNum_list = [ ]

with arcpy.da.SearchCursor("L3OpSilv", "BLOCK_") as cursor:
    for row in cursor:
        blockNum_list.append(row[0])

blockNum_set = set(blockNum_list)

for blockNum in blockNum_set :

    whereClause = "BLOCK_ = " + "'" + blockNum + "'"

    counter == 0

    with arcpy.da.SearchCursor('L3OpSilv', ["BLOCK_","SHAPE@"], whereClause) as cursor:
        for row in cursor:
            pLowerLeft = str(row[1].extent.lowerLeft)
            pUpperRight = str(row[1].extent.upperRight)
            fullExtent = row[1].extent
            extXMin = row[1].extent.XMin
            #arcpy.AddMessage(row[1].extent.YMin)
            extYMin = str(row[1].extent.YMin + 10)
            #arcpy.AddMessage(extYMin)
            yAxisCoord = (str(extXMin) + " " + str(extYMin))

            counter += 1

            full_fishnet_path = "Fishnet_" + blockNum + "_" + str(counter)
            clipped_fishnet_path = "Fishnet_" + blockNum + "_" + str(counter) + "clip"

            arcpy.management.CreateFishnet(full_fishnet_path, pLowerLeft, yAxisCoord,       cellSize, cellSize, None, None, pUpperRight, 'NO_LABELS', fullExtent, 'Polygon')
            fishnetclipped = arcpy.Clip_analysis(clipped_fishnet_path, 'L3OpSilv', 'fishnetclip')
EmmaBell-Carruthers1
New Contributor II

Currently if I have 3 separate polygons features selected all in the same layer. The fishnet is only created over the last feature that is selected and my other 2 selected polygons have no fishnet overlapping them. I believe because I am looping through the selected rows and the last feature extent values are getting passed into the fishnet geoprocessing tool. I would like to get the extent of the selected features not just the last selected one. 

I am unsure of how to get the extent (the LowerLeft, Upper Right, Xmin....etc) of the selected features. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you were using ArcMap, then it would be straightforward with Layer.getSelectedExtent().  Unfortunately, for reasons I don't understand, that hasn't been ported to ArcGIS Pro yet.

Although Python reduce has fallen out of favor, I find there are times where it is the most straightforward solution, like in this case.

import arcpy
from functools import reduce

fc = # path to feature class, or layer name if run within Pro
sql = # SQL WHERE clause to select records

with arcpy.da.SearchCursor(fc, "SHAPE@", sql) as cur:
    fullExtent = reduce(arcpy.Geometry.union, (shp for shp, in cur)).extent
 
Jack_Zhang
Occasional Contributor

Thanks for the solution. I agree found it's very inconvenient to lose some of the basic functions in arcpy.mapping module , which makes it unnecessarily difficult to deal with the layer-type result returned by the Toolbox arcpy functions. 

0 Kudos
DavidPike
MVP Frequent Contributor

Ah I've gotcha. I'm unsure if features in a FC have an extent object.

You could turn your FC into a feature layer then export each feature as it's own FC then grab the extents that way? Do everything in memory.

0 Kudos
EmmaBell-Carruthers1
New Contributor II

Thanks for the help! I was able to use the code provided by David and add to is so I could get the results I needed. It isn't the most straight forward way but it works! Thanks again. 


					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
0 Kudos