I'm a programming-beginner and I am currently writing my first Python-Script. That's why the question is more a general (or perhaps a semantic) one. I want to write a specific ID to the attribute table of an existing shapefile. This ID is amongst others composed of the coordinates (centroid) of the shape. So I tried the following:
The problem is, that after processing there is the same ID in every polygon of the shapefile. I'm using only a test-shapefile to try it and it consists of 3 polygons. They all get the same ID in their attribut-field "GLIMS_ID". But the print-statement above on the other hand returns all the different coordinates of the different polygons. What's the mistake?
I know, the code requires a lot of revision and it is not yet beautiful. I would therefore be open-minded for every other suggestion for improvement. I also tried to vary the "position" of the last 4 lines, without success.
Thank you for your help!
Code:
import arcpy
arcpy.env.workspace = "**************"
outline = "***********testoutline7.shp"
fields = ['SHAPE@XY']
with arcpy.da.SearchCursor(outline, fields) as cursor:
for row in cursor:
coordX = row[0][0]
if coordX < 0:
coordX = coordX + 360
coordY = row[0][1]
print "X: {}, Y: {}".format(coordX,coordY)
#----transformation of the centroid-coordinates---#
Xrnd = round(coordX, 3)
longitude = str(Xrnd)
glimsLongitude = longitude.split('.')
Yrnd = round(coordY, 3)
latitude = str(Yrnd)
glimsLatitude = latitude.split('.')
#-----------------------------------------#
with arcpy.da.UpdateCursor(outline, "GLIMS_ID") as crsr:
for glims in crsr:
glims[0] = "G" + glimsLongitude[0] + glimsLongitude[1] + "E" + glimsLatitude[0] + glimsLatitude[1] + "N"
crsr.updateRow(glims)
I know, the code requires a lot of revision and it is not yet beautiful. I would therefore be open-minded for every other suggestion for improvement. I also tried to vary the "position" of the last 4 lines, without success.
Thank you for your help!