Select to view content in your preferred language

Grabbing the field value of a shapefile

705
4
07-15-2010 01:17 PM
CharlieRoberts
Emerging Contributor
I have a list of point shapefiles that I would like to test in order to see if they are located within a large shapefile of many polygons (areas).  I am using the Select Layer By Location tool to do this.  I would like to discard the points that are not in these areas.  For the points that are inside an area, I would like to grab the name of the area, which is in an attribute field of the large shapefile.

I am doing this with python code and I think I need to have a cursor, but I am still unsure about this.


Any help would be greatly appreciated.  Thanks!
0 Kudos
4 Replies
JamesHood
Frequent Contributor
I think if I was you, I would do something like this...

(this can be done outside python )
Select by location: polygons that contain points
-->  create new layer  Polys



##(Then inside python)

#OPEN PREVIOUSLY CREATED TEXT FILE
text_file = open("c:/AREAS.txt", "w")

#create python loop to grab the name, output the name to the text file

polys = c:/newpolygonlayer

Rows = gp.updatecursor(Polys)
Row = Rows.next()
for Row:
     areaName = Row.TableField
     print "We have selected " + str(areaName)
     lines = [str(areaName) + "\n",]
     text_file.writelines(lines)

text_file.close()

For the record I have never used the write to text function but I would like to one day.  Also my syntax probably has a few errors but I hope this helps get the ball rolling.
0 Kudos
PaulBrandt
Regular Contributor
Have you tried looking at the Intersect tool in the Analysis/Overlay toolbox. I believe this is what you are trying to do. If your point features don't intersect each other, I think you could throw everything in at once, otherwise you will have to run the tool in batch for each of the point feature classes with your polygon feature class, either through python, or in the GUI.
0 Kudos
CharlieRoberts
Emerging Contributor
Paul & Jason,

Thanks for the responses!

Paul, that tool is exactly what I am looking for I think.  If it is, then you just made my script a ton simpler and easier to follow.

I'll let you know if it ended up working.


Thanks again!
0 Kudos
JeffWard
Honored Contributor
You could also do a spatial join.
Jeff Ward
Summit County, Utah
0 Kudos