Select to view content in your preferred language

Python - Create duplicate polygons based on number of intersects with points fc

467
2
08-02-2010 08:05 AM
KevinCressy
Emerging Contributor
Hello all,

Does anyone know

I have been trying to write a script that will create duplicate polygons based on the number of points that intersect each polygon.

The catch is that I want the output polygon feature class to include the "name" of the point layer intersecting it.

So if I have three points: ID1, ID1 and ID3 intersecting polygon A I would like a polygon feature class with an attribute table showing:

FID, PolygonName, PointName
0, A, ID1
1, A, ID2
2, A, ID3
3, B, etc, etc, etc


Is this possible in python? I have been struggling with getting this to work with the following code....

Any help would be appreciated!

Regards,

Kevin


import arcgisscripting
gp = arcgisscripting.create(9.3)
print "Step1"
# Load required toolboxes...
gp.AddToolbox(r"C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx")

Output_Layer_Name = "Polygons_Layer"
Polygons_shp = r"C:\Temp\ES106506\Polygons.shp"
Polygons_Layer = "Polygons_Layer"
Output_Feature_Class = "C:\\Temp\\ES106506\\Polygons_CopyFeatures.shp"

gp.Workspace = r"C:\Temp\ES106506\output.gdb"
gp.OverWriteOutput = 1
try:
#Define the points layer
gp.MakeFeatureLayer(r"C:\Temp\ES106506\Points.shp", "Points_Layer")
#Define the polygons layer
gp.MakeFeatureLayer(Polygons_shp, Polygons_Layer)
print "Step5"
#create search cursor to loop through points
points = gp.searchCursor("Points_Layer")
print "Step6"
#For each point select the polygon intersecting it

point = points.next()
while points:
gp.selectLayerByLocation(Polygons_Layer, "INTERSECT", point.shape)
#Create a new Polygon feature class based on the original Point
#feature class and named based on a field within the the polygon field
name = Polygons_Layer.TempID
gp.CopyFeatures(Polygons_Layer, Polygons_Layer.name)

#move on to next point
point = points.next()
print "Finished"
del gp, points, point

except:
print gp.getmessages()
print "Errored"
del gp
0 Kudos
2 Replies
ChrisSnyder
Honored Contributor
Check out the SpatialJoin tool. This should do exatly what you want.

Make the TargetFeatures your polygons, the JoinFeatures your points. Be sure to specify the join operation as a 'JOIN_ONE_TO_MANY'.
0 Kudos
KevinCressy
Emerging Contributor
Thanks for that - that does solve the problem - however I am still keen to crack with python.

Les Smith from ESRI UK kindly pointed out an error in my code i.e.

"I think this bit of code:

point = points.next()
while points:

should be

point = points.next()
while point:"


I have modified the code below but I still seem to be having an issue.

The code seems to be working up to the first selection - however then I want to take this selected feature and add the value of the field from the point layer to this polygon feature class. perhaps I need to do a join on this selected feature and the point in the feature class????



import arcgisscripting
gp = arcgisscripting.create(9.3)

# Load required toolboxes...
gp.AddToolbox(r"C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx")

Output_Layer_Name = "Polygons_Layer"
Polygons_shp = r"C:\Temp\ES106506\Polygons.shp"
Polygons_Layer = "Polygons_Layer"
Output_Feature_Class = r"C:\Temp\ES106506\Polygons_CopyFeatures.shp"

gp.Workspace = r"C:\Temp\ES106506\output.gdb"
gp.OverWriteOutput = 1

try:
#Define the points layer
gp.MakeFeatureLayer(r"C:\Temp\ES106506\Points.shp", "Points_Layer")

#Define the polygons layer
gp.MakeFeatureLayer(Polygons_shp, Polygons_Layer)

#create search cursor to loop through points
points = gp.searchCursor("Points_Layer")

#For each point select the polygon intersecting it

point = points.next()
while point:
gp.selectLayerByLocation(Polygons_Layer, "INTERSECT", point.shape)

#Create a new Polygon feature class based on the original Point
#feature class and named based on a field within the the polygon field
name = Polygons_Layer.TempID
gp.CopyFeatures(Polygons_Layer, Polygons_Layer.name)

#move on to next point
point = points.next()
print "Finished"
del gp, points, point

except:
print gp.getmessages()
print "Errored"
del gp
0 Kudos