Trouble with selecting features by location using arcpy?

853
13
Jump to solution
10-03-2019 09:52 AM
RobertPhillips
Occasional Contributor III

Hi,

So I am trying to figure out how to get the date for a selected feature using the spatial location using arcpy. I keep running into an issue where the MakeFeatureLayer keeps returning an error and I am not sure as to why. I followed several examples and yet I still keep getting an error. I would greatly appreciate any help with this.

import arcpy
import os

workspace = r'Database Connections\Some Connection.sde'
fcs = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass")

Dist_LL = "Database Connections\Some.sde\FeatureClass"
for dirpath, dirnames, filenames in walk:
for filename in filenames:
fcs.append(os.path.join(dirpath, filename))

for fc in fcs:
geometryType = arcpy.Describe(fc).shapeType
print geometryType
if geometryType == 'Point':

#Get feature class name
fcsname = os.path.basename(fc)
name = os.path.splitext(fcsname)
y = name[1].lstrip('.')
print y

#For fields in some feature classes
fields = ["OID@", "LANDDISTRICT", "LANDLOT"]

#Make feature layers
arcpy.MakeFeatureLayer(fc, 'fc')
arcpy.MakeFeatureLayer(Dist_LL, 'Dist_LL')

for row in arcpy.da.SearchCursor(fc, fields):
if row[1] is None:
arcpy.SelectLayerByLocation_management (fc_layer, 'INTERSECT', Dist_LL_lyr)
select_records = arcpy.da.SearchCursor(Dist_LL_lyr)
print select_records
print row
elif row[2] is None:
print row
else:
continue‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Robert

0 Kudos
13 Replies
RobertPhillips
Occasional Contributor III

Thanks Micah,

Transferring the field values form the Dist_LL feature class to the other feature classes is what I am trying to accomplish. I wasn't quite sure if a spatial join would work in this instance, because I tried that method using modelbuilder and unfortunately I could not get any ID fields to match. The other method that I tried was to do an intersect on the features and then use the add join method to match the IDs which works, but I think another issue is that some of the features may overlap one another (long story as to why) or there may be another reason. I just also noticed that we have 0 for some values in the fields so I will need to make some slight modifications to the script to update those values as well.

Just another quick question(still corresponding to this thread). What is the difference between using the SearchCursor and specifying a query to get certain values vs. using the SearchCursor and running if/else statements to identify certain values. I am not quite sure which one is used over the other, and which one do would work best in this situation or does it not matter.

Robert

0 Kudos
RobertPhillips
Occasional Contributor III

Hi Micah,

Here is the updated portion of the script. I am trying to figure out a solution to just return the selected records in the Dist_LL layer based on the number of selected records in fc.

for fc in fcs:
geometryType = arcpy.Describe(fc).shapeType
print geometryType
if geometryType == 'Point':

#Get feature class name
fcsname = os.path.basename(fc)
name = os.path.splitext(fcsname)
y = name[1].lstrip('.')
print y

#Get these fields from feature classes
fields = ["OID@", "LANDDISTRICT", "LANDLOT"]

for row in arcpy.da.SearchCursor(fc, fields):
if row[1] is None:
arcpy.MakeFeatureLayer_management(fc, 'fc_layer')
arcpy.SelectLayerByLocation_management('fc_layer', 'INTERSECT', 'DistLL_layer')
for DLrow in arcpy.da.SearchCursor('DistLL_layer', ["LAND_DIST", "LAND_LOT"]):
print DLrow
print row
elif row[2] is None:
print row
else:
continue‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I am not sure if the problem lies with the way the search cursor is ran or if it was even set up to return the selected features. I will try to modify this to see if I can get the correct result to return, but I am not sure how to get there.

Also, I keep receiving this error message for some reason and I don't know how to offset this error. It occurs every time and I don't know if it is the way it is nested or if it is due to not being deleted. I will try a couple of things to see if I can correct this but this error is new to me. Any assistance on this would also be greatly appreciated.

Traceback (most recent call last):
File "U:\Models_Tools\Scripts related to Landlot and District\Populate Landlot and District.py", line 31, in <module>
arcpy.MakeFeatureLayer_management(fc, 'fc_layer')
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", line 6993, in MakeFeatureLayer
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output Layer: Dataset fc_layer already exists.
Failed to execute (MakeFeatureLayer).

Thank you very much for your help Micah.

Robert

0 Kudos
RobertPhillips
Occasional Contributor III

Never mind about the error. I came up with this solution and it seems to work.

feature_layer = arcpy.MakeFeatureLayer_management(fc, str(y + '_layer'))

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

You had a couple different issues come up in this thread, but please mark the one that helped the most as correct to close the thread out.

0 Kudos