I am writing a script that populates a field full of Lat/Long coordinates and most likely should be a tuple. Originally I just created a Lat/Long tuple pair nested within a list and set equal to a variable. Ex: coords =[(34.456548, -119.710025)]
.
Now I am trying to create a for loop that will be able to take a users Lat,Long input as an argument and then populate a field named Lat_Long with that user input.
Here is the code so far Code:
import os, arcpy
coords = input([(),()])
new_shp = arcpy.CreateFeatureclass_management(r"C:/geodata","test_4.shp", "POINT", spatial_reference=4326)
arcpy.AddField_management(new_shp,'Lat_long', 'LONG')
with arcpy.da.InsertCursor(new_shp,['SHAPE@XY']) as icur:
for coord in coords:
print("Inserted {} into {}\n".format(coords,new_shp))
icur.insertRow([coord]
print(new_shp)
print("Script_Complete!)
This code creates an empty shapefile along with the other files eg. shx. and are put into a the specified directory. Would there need to be another for loop to iterate over the shapefile and produce new rows of Lat/Long user input?
-
If you have a -119 latitude, there's going to be trouble (coords are lon/lat). You also have an indent issue and a missing closing paren.Vince– Vince2022年07月08日 02:13:00 +00:00Commented Jul 8, 2022 at 2:13
1 Answer 1
This uses a function to create a feature class in a file geodatabase, and insert a point from the x, y coordinates entered as the functions' parameters. If the feature class already exists, it will just insert the row based on the functions' parameters
import arcpy
arcpy.env.workspace = r'C:\Path\to\InsertXYRow.gdb'
fc = r'C:\Path\to\InsertXYRow.gdb\XYFeatureClass'
if arcpy.Exists(fc):
print ('The feature class already exits')
else:
fc = arcpy.CreateFeatureclass_management(arcpy.env.workspace,'XYFeatureClass','Point')
arcpy.AddField_management(fc,'Lat', 'DOUBLE')
arcpy.AddField_management(fc, 'Long', 'DOUBLE')
fieldNames = [field.name for field in arcpy.ListFields(fc)]
def insertFeature(lat,long):
inCur = arcpy.da.InsertCursor(fc, fieldNames)
newShape =(lat, long)
newRow = (None, newShape, lat, long)
inCur.insertRow(newRow)
del inCur
insertFeature(-100.008976, 40.123456)
Explore related questions
See similar questions with these tags.