Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a shapefile of US Cities and I want to get the X and Y coordinate of EACH city in that shapefile. I've tried this:

for city in city_cursor:
    geom = city.Shape
    point = geom.getPart()
    citylist_City_Name.append(city.CITY_NAME)
    citylist_Country.append(city.CNTRY_NAME)
    citylist_Admin.append(city.ADMIN_NAME)
    citylist_Population.append(city.Population)
    citylist_X_Coor.append(geom.point.X)
    citylist_Y_Coor.append(geom.point.Y)

but I get the error:

Traceback (most recent call last): File "C:/Users/workd.py", line 43, in citylist_X_Coor.append(geom.point.X) AttributeError: 'PointGeometry' object has no attribute 'point'

I don't really understand the error message? How can I fix it? Thanks!

share|improve this question
    
I notice that this has also been asked on Stack Overflow with same answer given there by someone else. –  PolyGeo May 4 '13 at 11:12
add comment

1 Answer

You just need point.X and point.Y not geom.point.X and geom.point.Y.

Alternatively, if you are using ArcGIS for Desktop 10.1 or later then code like that below using the Data Access module should do the same thing but run faster.

shpFile = r"C:\temp\cities.shp"
fields = ['CITY_NAME', 'CNTRY_NAME', 'ADMIN_NAME', 'Population', 'SHAPE@XY']

with arcpy.da.SearchCursor(shpFile, fields) as city_cursor:

   for city in city_cursor:
       citylist_City_Name.append(city[0])
       citylist_Country.append(city[1])
       citylist_Admin.append(city[2])
       citylist_Population.append(city[3])
       citylist_X_Coor.append(city[4][0])
       citylist_Y_Coor.append(city[4][1])
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.