1

I'm running a script to create a buffer and find features from another shapefile that fall completely within that buffer. The script seems to work fine, it finds all the correct features (I checked using intersect in Desktop ArcMap) but then creates an empty shapefile. The resulting shapefile has the right schema from the input shapefile but no features.

EDIT: Strangely it seems to work fine if I do it to a GDB instead of a shapefile.

# this program buffers a file and finds what features are inside that buffer

# everything works except for writing to the new shapefile, and i can't figure out why

# import all the goodies
import ogr
import gdalconst
import sys
import os
import arcpy

# set the workspace for arcpy 
arcpy.env.workspace = "c:/work/lab8"

# set folders and files
folder = 'C:\\work\\lab8\\'
myFile = 'Parcels.shp'
myFile2 = 'Powerline.shp'
filename = folder + myFile

# get all the attributes using describe and count the number of features
desc = arcpy.Describe(myFile)
cm = arcpy.GetCount_management(myFile) 
nFeatures =int(cm[0])
print 'featureclass',myFile,'has',nFeatures,'features'
print 'running tool'

# make the new layers to select from
arcpy.MakeFeatureLayer_management(myFile, "parcels_lyr")
arcpy.MakeFeatureLayer_management(myFile2, "powerline_lyr")

# set the buffer destination
buffer_file = 'powerline_buffer.shp'
buffer_output = folder+buffer_file

# if the layer exists, delete it
if arcpy.Exists(buffer_output):
    arcpy.Delete_management(buffer_output)

#create the buffer
bufferDist = 250
sideType = "FULL"
endType = "ROUND"
arcpy.Buffer_analysis("powerline_lyr", buffer_output, bufferDist, sideType, endType)

# make the buffer result a layer
arcpy.MakeFeatureLayer_management(buffer_output, "buffer_lyr")

# run the intersect
arcpy.SelectLayerByLocation_management('parcels_lyr', 'COMPLETELY_WITHIN', 'buffer_lyr', "", "NEW_SELECTION")

# see how many features intersect. if 0, quit
matchcount = int(arcpy.GetCount_management('parcels_lyr')[0]) 
if matchcount == 0:
    print('no features intersect')
    sys.exit()
else:
    print matchcount, 'intersected'

# Set output file name
out_name = "parcels_within.shp"
out_location = folder+out_name
print out_location

# if the new featureclass exists, delete it
# i took this out in case it was causing problems, but it doesn't seem to be the cause
if arcpy.Exists(out_location):
    arcpy.Delete_management(out_location)
    print 'bye'

# showing all the features that interserct, to make sure it works
print '\nThese features of',myFile, 'intersect the buffer of',myFile2,': \n'
cursor = arcpy.SearchCursor('parcels_lyr')
for row in cursor:
    if row.getValue('SITUSADDR'):
        print row.getValue('SITUSADDR')

# Write the selected features to a new featureclass
# for some reason this creates an empty shapefile and I can't figure out why
arcpy.CopyFeatures_management('parcels_lyr',  out_location)
  • Strangely it seems to work fine if I do it to a GDB instead of a shapefile. – wisemana Dec 20 '16 at 6:27
  • 1
    That would seem to be important information for you to include in your question via using the edit button. Don't forget that posting a code snippet rather than your full code is more likely to result in attracting a potential answerer sooner. – PolyGeo Dec 20 '16 at 8:58
  • Will do. That is my full code – wisemana Dec 20 '16 at 16:28
  • Oh, I see. I've seen other posts where commenters ask for the whole code so that's what I did here. – wisemana Dec 20 '16 at 16:33
1

I've had a go at changing a few things in your code.

  1. The way you've constructed file paths. Using os.path.join is a cleaner way to join paths, rather than folder + file method.
  2. Using the raw file path method to identify paths. ie. when setting workspace or folder.
  3. I usually try and use variables when constructing code.
  4. I've set the workspace overwrite to True... This will overwrite the pre-created shapefile before adding the selection data to it.
  5. Ive added the "da" version of searchcursor. I assume "SITUSADDR" is a field within the table, therefore you are searching against this for values.

    # set the workspace for arcpy 
    folder = arcpy.env.workspace = r"c:/work/lab8"
    arcpy.env.overwriteOutput = True
    
    # set folders and files
    myFile = os.path.join(folder, 'Parcels.shp')
    myFile2 = os.path.join(folder, 'Powerline.shp')
    
    # get all the attributes using describe and count the number of features
    desc = arcpy.Describe(myFile)
    cm = arcpy.GetCount_management(myFile) 
    nFeatures =int(cm[0])
    print 'featureclass',myFile,'has',nFeatures,'features'
    print 'running tool'
    
    # make the new layers to select from
    parcels_layer = arcpy.MakeFeatureLayer_management(myFile, "parcels_lyr")
    powerline_layer = arcpy.MakeFeatureLayer_management(myFile2, "powerline_lyr")
    
    # set the buffer destination
    buffer_file = 'powerline_buffer.shp'
    buffer_output = os.path.join(folder, buffer_file)
    
    # if the layer exists, delete it
    if arcpy.Exists(buffer_output):
        arcpy.Delete_management(buffer_output)
    
    #create the buffer
    bufferDist = 250
    sideType = "FULL"
    endType = "ROUND"
    arcpy.Buffer_analysis(powerline_layer, buffer_output, bufferDist, sideType, endType)
    
    # make the buffer result a layer
    buffer_layer = arcpy.MakeFeatureLayer_management(buffer_output, "buffer_lyr")
    
    # run the intersect
    selection = arcpy.SelectLayerByLocation_management(parcel_layer, 'COMPLETELY_WITHIN', buffer_layer, "", "NEW_SELECTION")
    
    # see how many features intersect. if 0, quit
    matchcount = int(arcpy.GetCount_management(selection)[0]) 
    if matchcount == 0:
        print('no features intersect')
        sys.exit()
    else:
        print matchcount, 'intersected'
    
    # Set output file name
    out_name = "parcels_within.shp"
    out_location = os.path.join(folder, out_name)
    print out_location
    
    # if the new featureclass exists, delete it
    # i took this out in case it was causing problems, but it doesn't seem to be the cause
    if arcpy.Exists(out_location):
        arcpy.Delete_management(out_location)
        print 'bye'
    
    # showing all the features that interserct, to make sure it works
    print '\nThese features of',myFile, 'intersect the buffer of',myFile2,': \n'
    with arcpy.da.SearchCursor(selection, ['SITUSADDR']) as scursor:
        for srow in scursor:
            print srow[0]
    
    
    # Write the selected features to a new featureclass
    # for some reason this creates an empty shapefile and I can't figure out why
    arcpy.CopyFeatures_management(selection,  out_location)
    
| improve this answer | |
  • Thanks. it gave me a syntax error on this line 'cursor = arcpy.da.SearchCursor(selection, ['SITUSADDR']) as scursor: – wisemana Dec 20 '16 at 16:37
  • cursor = arcpy.da.SearchCursor(selection, ['SITUSADDR']) as scursor: – wisemana Dec 20 '16 at 16:37
  • ive changed it. it should have been "with". Check the above code for the change – MacroZED Dec 20 '16 at 16:38
  • Thanks. That still runs fine but the .shp is still empty. If I do it to a GDB instead that works fine, oddly. – wisemana Dec 20 '16 at 16:48
  • That is strange. I don't understand why that's happening. When you write to a GDB, are there any features? – MacroZED Dec 20 '16 at 17:12

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.