0

I have a point features and polygon features. I need to:

  1. Select points that intersect the polygons
  2. Copy the points to a new shapefile
  3. Name this new point shapefile using an attribute of the polygon it intersects

Some of these polygons overlap each other. If one point intersects 2 polygons, this point would need to be included in two new shapefiles.

I'm very new to Python, and therefore I'm trying to achieve this by modifying the code below. I believe most of the basic pieces are there.

#import libraries
import arcpy, os, sys

#set current workspace
arcpy.env.workspace = r"C:\data"
arcpy.env.overwriteOutput = True

#assign variables
rawSurvey = "rawSurvey" #rawSurvey is the point file from the survey company
surface= "surface"       # surface is the approach surface polygon
output = r"C:\Users\data\airspace_Working2.gdb"

#create cursor to cycle through each "Type" of surface
cursor = arcpy.da.UpdateCursor ('surface', "Type")
for row in cursor:
    # selects the survey features intersecting (under) the surfaces
    # Make a layer and select rawSurvey points which overlap each polygon
    arcpy.MakeFeatureLayer_management ('rawSurvey', 'Tes_lyr')
    arcpy.SelectLayerByLocation_management ('Tes_lyr', 'intersect', 'surface')

# If features matched criteria write them to new feature classes appSurvey_"type" based on which type of surface they ovelrap
matchcount = int(arcpy.GetCount_management ('Tes_lyr').getOutput(0))
if matchcount == 0:
    print ('no features matched spatial and attribute criteria')
else:
    arcpy.CopyFeatures_management ('Tes_Lyr', 'appSurvey')
#print the number of features matched
print (str(matchcount) + ' points were under the surfaces and written as appSurvey')
  • 1
    What's the problem? – phloem Feb 11 '15 at 20:41
  • If you add a single question mark to indicate your precise question, and also describe what happens (error, etc) when you run your code, then this may get answered more quickly. – PolyGeo Feb 11 '15 at 22:07
0

Piece of cake. Use a cursor to iterate and selection to determine intersection. You need feature layers to allow selection. Check out the below code with comments that explain each step.

#Input polygon feature class
inPolyfc = r"in\polygon\full\path"
#Name of ID field to be used to name new shapefiles
PolyIDfld = "ID_FIELD_NAME"

#input point feature class
inPointfc = r"in\point\full\path"

#Out folder path for new shape file
outPath = r"Out\full\path"

from arcpy import *
import os

#Make feature layer of feature classes to allow selection
MakeFeatureLayer_management (inPolyfc, "Polylyr")
MakeFeatureLayer_management (inPointfc, "Pointlyr")

#Get name of OID field for sql expression
polyOIDfld = Describe ("Polylyr").OIDFieldName

#iterate through polygons with cursor
cursor = da.SearchCursor ("Polylyr", ["OID@", PolyIDfld])
for row in cursor:
    #sql to select individual polygon
    sql = '"{0}" = {1}'.format (polyOIDfld, str(row[0]))
    #select individual polygon
    SelectLayerByAttribute_management ("Polylyr", "", sql)

    #select intersecting points
    SelectLayerByLocation_management ("Pointlyr", "", "Polylyr")

    #Check to make sure points are selected, ie at least one point actually intersects polygon
    if Describe ("pointlyr").FIDSet:
        FeatureClassToFeatureClass_conversion ("Pointlyr", outPath, str(row[1]) + ".shp")
    #Optional: Create empty feature class if no points intersect
    else:
        CreateFeatureclass_management (outPath, str(row[1]) + ".shp", "POINT", inPointfc)
del row
del cursor

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.