[GIS] How to get point x and y geometry from a shapefile

arcgis-desktopgeometrypython

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!

Best 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])
Related Question