Trouble with selecting features by location using arcpy?

2857
13
Jump to solution
10-03-2019 09:52 AM
RPGIS
by
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
1 Solution

Accepted Solutions
RPGIS
by
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

13 Replies
RandyBurton
MVP Alum

And the error message is?

0 Kudos
RPGIS
by
Occasional Contributor III

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'

MicahBabinski
Occasional Contributor III

Gotta include the '_management' bit to get the proper syntax. See the examples: 

Make Feature Layer—Help | ArcGIS for Desktop 

RPGIS
by
Occasional Contributor III

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.

0 Kudos
MicahBabinski
Occasional Contributor III

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.

RPGIS
by
Occasional Contributor III

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:

  1. Identify the intersecting features in the layer(using the select layer by location)
  2. Get the selected records for the fields in the feature layer
  3. Using the selected records in that layer, populate the null fields in the other existing layer.

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

0 Kudos
MicahBabinski
Occasional Contributor III

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?

  1. I think I understand this step. You want to select points that fall within your Dist_LL feature class.
  2. You lose me a bit here. Are all the selected points going to have the same values for a set of fields?
  3. What other layer are you trying to populate? Do the fields match between the layer where you've selected some points and the layer you want to update with the values of the selected points?
0 Kudos
RPGIS
by
Occasional Contributor III

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.

0 Kudos
MicahBabinski
Occasional Contributor III

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:

  1. After line 4 below, use the where clause parameter of the Make Feature Layer tool to create a feature layer only where the relevant attribute ("LANDDISTRICT" is it?) is null
  2. Run a Spatial Join with your point feature layer as the target features, your polygon feature layer as the join features, and an output feature class in the in_memory workspace
  3. Use the Add Join tool to join your output point features back to your original point feature layer - the OIDs should be a correct match.
  4. Use the Calculate Field tool to update the null values with the values from the polygon layer. 

Micah