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
Solved! Go to Solution.
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'))
And the error message is?
Hi Randy,
Here is the error message that I receive.
Traceback (most recent call last):
File "U:\Models_Tools\Scripts related to Landlot and District\Populate Landlot and District.py", line 17, in <module>
arcpy.MakeFeatureLayer(fc, 'fc_layer')
AttributeError: 'module' object has no attribute 'MakeFeatureLayer'
Gotta include the '_management' bit to get the proper syntax. See the examples:
Thanks Micah,
I can't believe something that simple had slipped past me. I will make those changes and see if that fixes the issue.
No worries - I've made that error many times (especially when I haven't got adequate sleep, which is often). Pro tip: use the Python window in ArcMap/Pro or an IDE that offers some code completion to check your syntax if you are unsure.
Thanks again Micah,
I made the necessary changes but now I have an issue where the number of returned features exceeds that of the queried features. I am trying to figure out what is the best approach to this:
So what I am trying to do is basically extract values spatially from one feature and using those values to populate the null fields in another layer. It seemed simple enough, but for some reason it returns an inordinate amount of records, which exceeds that of the queried records.
What would be the best way to script this? I am fairly close but I am having trouble figuring out how to get a matching number of records from another layer.
Thanks,
Robert
I'm not 100% clear on what you're trying to accomplish. It might help if you post the latest version of your script. Can you help me understand your desired steps?
Hi Micah,
The points do fall inside the Dist_LL layer, and the Dist_LL layer has
populated values in the fields. The points that fall inside have the same
fields(though named differently) but are null. The points are a set of
different feature classes, and I'm trying to see if I can get the feature
classes that have null values in them get populated using values in the
fields in the Dist_LL layer. I don't have access to my updated script but I
will post it once I get back to the office tomorrow. I hoped this
explanation is a bit clearer.
Thanks Robert,
I understand your workflow much better now. It sounds like you want to transfer attributes from a polygon layer to the point layers that fall within it, where the corresponding attributes (although they are named differently) are null. Using search cursors may be feasible, but I think using a spatial join will be easier, especially if you have lots and lots of points in each layer. Nested cursors are pretty slow to run, sadly. Here's what I'd recommend:
Micah