pythonpython-2.7gisarcpyarcmap

How to add a point with XY coordinates inside a Feature Class in arcpy?


I am fairly new to arcpy and I've literally spent the day trying to figure out how to add a point with XY coordinates inside a Feature Class in arcpy. Here's the code I have right now:

coordinates = raw_input("Please enter longitude and latitude as floats split by a space: ")
c_split = coordinates.split()
lng = float(c_split[0])
lat = float(c_split[1])

spatial_reference = arcpy.SpatialReference(3857)
arcpy.CreateFeatureclass_management('D:\Documents\GIS_DATA\buildings_sample_8', "center.shp", "POINT", "", "DISABLED", "DISABLED", spatial_reference)
center = "center.shp"
cursor = arcpy.da.InsertCursor('D:\Documents\GIS_DATA\buildings_sample_8\center.shp', ["SHAPE@XY"])
xy = (lng, lat)

cursor.insertRow([xy])

This manages to create the shapefile center.shp within the appropriate directory, but I am not able to add the user-entered longitude and latitude values to the point, making the point appear at the default 0, 0.

This is probably a super easy question, but I haven't been able to find the documentation online.

Thank you!


Solution

  • try changing this:

    xy = (lng, lat)
    

    to this:

    xy = arcpy.Point(lng, lat)
    

    I'm guessing your coordinate floating point is indeed a point (.) and not a comma (,) otherwise you'll get an error trying to create the Point (Input value is not numeric).