I want to write a scale value that is changing for every Data Driven Page (since using map extent best-fit so that each polygon in the driving layer would be whole in "its" exported map) to the table of the driving layer, field "SCALE".
I found out I cannot use
mxd.dataDrivenPages.pageRow.setValue ()
(returning run time error: ERROR 999999)
because Data Driven Pages pageRow cannot be used to write a value in (but might be in the future) See pageRow - read only or can be modified?,
and so arcpy.da.UpdateCursor needs to be used instead (as arcpy.UpdateCursor is older version and way slower!).
THIS IS CURRENT CODE (you may have a look at what I tried below the two horizontal lines):
import arcpy, os
mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
rad= mxd.dataDrivenPages.pageRow.getValue("KOO") #KOO is NameField
#arcpy.mapping.ExportToJPEG(mxd, r"D:\PS" + os.sep + str(rad) + ".jpeg", resolution=400)
sc=int(mxd.dataDrivenPages.dataFrame.scale)
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE") as cursor:
for row in cursor:
row[0]=sc
cursor.updateRow(row)
This one fills all rows (field SCALE) with the scale number of the current Data Driven Page. But what I want is to have the right value in SCALE field for every row (with current scale)
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
As suggested by artwork21 I tried to use pageNum in cursor's where-clause.
It did not worked
("por" in codes is a new field in the table that contains the number same as pageNum
- I would like to avoid adding fields in atr. table, but at least tried):
strf=mxd.dataDrivenPages.pageRow.getValue("por")
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE","""pageNum=strf""") as cursor:
for row in cursor:
row[0]=sc
cursor.updateRow(row)
-> This gave RuntimeError: A column was specified that does not exist. for row "for row in cursor:"
I tried to change the syntax in where-clause in "cursor line" but with no success
playing with str(pageNum), str(strf), """, " and ' using the code above
and also with writing where-clause outside the cursor and changing the syntax, examples:
strf=mxd.dataDrivenPages.pageRow.getValue("por")
whr= str(pageNum) + "=" + str(strf)
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE",whr) as cursor:
for row in cursor:
row[0]=sc
cursor.updateRow(row)
#RuntimeError: ERROR 999999: Error executing function.
whr= "str(pageNum) + "=" + str(strf)"
#Runtime error SyntaxError: can't assign to literal
also tried
strf=mxd.dataDrivenPages.pageRow.getValue("por")
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE") as cursor:
for row in cursor:
if pageNum==strf:
row[0]=sc
cursor.updateRow(row)
or changing the 4th line to
if str(pageNum)==str(strf)
both ended up with: script ran, no cell in atr. table filled
I already knew those talking about query expressions/SQL in ArcGIS help: 1, 2, 3, but none of them talking about variables...
Trying to find a way searching in pages concerning variables specially with cursors at https://gis.stackexchange.com/ or https://community.esri.com/ meantioning different ways in answers, still none of them worked for me...
2 Answers 2
You may use the pageNum value to create a where clause expression within the for pageNum loop to filter the update cursor to only update the current page scale field attribute:
UpdateCursor (in_table, field_names, {where_clause})
where_clause
An optional expression that limits the records returned. For more information on WHERE clauses and SQL statements, see Building a query expression.
-
have not found a way how to make it work, more specific answer?Abc1– Abc12017年11月01日 19:03:46 +00:00Commented Nov 1, 2017 at 19:03
-
If you tried this method please update the question including code you used and provide any error info or where you are stuck.artwork21– artwork212017年11月01日 19:07:47 +00:00Commented Nov 1, 2017 at 19:07
-
edited the code + question (with where clause it finally helped but still not working the way it should)Abc1– Abc12017年11月18日 12:11:01 +00:00Commented Nov 18, 2017 at 12:11
So after a lot of trying and searching... finally I found a way that should work
(assumming that the fact this gives numbers not being the current scale but some numbers close to it, may be connected with bugs appeared recently and being in proccess of solving with ESRI Tech. support, knowing that before that appeared the numbers where ok geting sc value
the same way)
import arcpy, os
mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
rad= mxd.dataDrivenPages.pageRow.getValue("KOO")
#arcpy.mapping.ExportToJPEG(mxd, r"D:\PS" + os.sep + str(rad) + ".jpeg", resolution=400)
sc=int(mxd.dataDrivenPages.dataFrame.scale)
pork=arcpy.AddFieldDelimiters(mxd.dataDrivenPages.indexLayer, "KOO")
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE","{0} = '{1}'".format(pork, rad)) as cursor:
for row in cursor:
row[0]=sc
cursor.updateRow(row)
any way(syntax/formatting) of trying
pageNum=strf
could not help as those are equal when it gets to thewith cursor
line )using fields it is helpful and recommended to use
arcpy.AddFieldDelimiters
(see arcpy.AddFieldDelimiters in ArcGIS help)using this
"{0} = {1}".format(x, y))
way of syntax writing appears to be useful and working, do not forget to bound the strings with'
Explore related questions
See similar questions with these tags.
None
for what ever reason. Whilst looking at the help file is does actually explicitly state that pageRow is read only but then the explanation says it can be used to modify fields! Wonder if that is the issue?