4

I want to perform a selective search cursor for a feature class for a given extent. I have tried setting env.extent in the below code but it is returning the whole feature class instead of a fixed extent.

I am using ArcGIS 10.3 and the featureclass is in PostGIS.

import arcpy
from arcpy import env

# Set the workspace
env.workspace = "C:\Users\ADevra\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\Connection to DAT-PGTEST-01.sde"
#Setting the Environment extent
arcpy.env.extent = arcpy.Extent(438859, 184490, 445034, 190295)

fc1 = "postgretest.postgres.boundary_line"

##Select the features in a given extent
with arcpy.da.SearchCursor(fc1,["fid"]) as innercursor:
    for searchrow in innercursor:
        print searchrow[0]

del(innercursor)

UPDATED ANSWER:

import arcpy

arcpy.env.workspace = r'DAT-PGTEST-01.sde'
extentPoly = "Test Shapefileszextent.shp"
fc = r'postgretest.postgres.boundary_line'
dataset = "postgretest.postgres_tmp.boundaryline"
desc = arcpy.Describe(dataset)
extent = desc.extent

array = arcpy.Array()
# Create the bounding box
array.add(extent.lowerLeft)

array.add(extent.lowerRight)
array.add(extent.upperRight)
array.add(extent.upperLeft)
# ensure the polygon is closed
array.add(extent.lowerLeft)
# Create the polygon object
polygon = arcpy.Polygon(array)
array.removeAll()
# save to disk
arcpy.CopyFeatures_management(polygon, extentPoly)
del polygon

arcpy.MakeFeatureLayer_management(in_features=fc, out_layer='fclyr')

arcpy.SelectLayerByLocation_management(in_layer='fclyr',    overlap_type='INTERSECT', select_features=extentPoly)

    for rows in arcpy.da.SearchCursor('fclyr', ["postgretest.postgres.boundary_line.fid"]):
    print rows[0]
  • All Python coding questions here in GIS SE are expected to include Python code. You should always specify the software and version in the body of the question. Please edit the question to contain these things. – Vince May 19 '16 at 11:03
  • @Vince : I have edited my question now, thanks Astha – Astha Devra May 19 '16 at 11:23
6

Create a Feature layer and then use Select Layer by location with a polygon of your extent. Example:

import arcpy

arcpy.env.workspace = r'D:\test.gdb'

fc = r'Polygon'
fcselectionpolygon = r'selection'

arcpy.MakeFeatureLayer_management(in_features=fc, out_layer='fclyr')

arcpy.SelectLayerByLocation_management(in_layer='fclyr', overlap_type='INTERSECT', select_features=fcselectionpolygon)

for rows in arcpy.da.SearchCursor('fclyr',["KOMMUNKOD"]):
    print rows[0]

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.