POST
|
Great! I used your latter suggestion (but without the next()-method) and it worked! Just using the UpdateCursor without SearchCursor. It's actually that simple. Many thanks!
... View more
06-20-2014
03:01 PM
|
0
|
0
|
539
|
POST
|
At a quick glance, you're looping through your SearchCursor, and within that SearchCursor you're looping through the UpdateCursor. So each time through the Search loop, you're updating all your data with the same set of data, the one row, of search results. If three loops through your SearchCursor returned a, b, c, respectively, your UpdateCursor then populates your field with all a's, then all b's, then all c's, respectively. Ok, thank you, I guess i see what you mean. But do you therefore also have a solution? I tried to set the UpdateCursor-Method out of the SearchCursor-loop, but with the same effect. Is UpdateCursor probably the wrong approach for this purpose?
... View more
06-20-2014
10:50 AM
|
0
|
0
|
539
|
POST
|
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: 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) 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!
... View more
06-20-2014
04:32 AM
|
0
|
4
|
832
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|