2

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!

1
  • I notice that this has also been asked on Stack Overflow with same answer given there by someone else.
    – PolyGeo
    Commented May 4, 2013 at 11:12

1 Answer 1

3

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])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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