Referencing attributes in a script

871
4
10-17-2013 07:37 AM
JenniferAbbott
New Contributor
Good morning,

I am trying to automate my map so that every time I make a new map the property number changes according to the attribute table. I have figured out how to do this (thanks to this forum), but I can only make it happen with one shapefile. The dataset I'm working out of has many shapefiles which I need to reference some of these. My script is as follows;

import arcpy
mxd=arcpy.mapping.MapDocument("Current")
lyr=arcpy.mapping.ListLayers(mxd,"ECLS")[0]
elemlist=arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENTS","Sub Title")[0]
rows=arcpy.SearchCursor(lyr.dataSource)
row=rows.next()
elemlist.text="Recreational Lease" + "\n" + "Property No:" + row.getValue("PROPERTY")

I know it has something to do with the [0] as the object being referenced, but I'm not sure how to reference other items in the attribute table? I've tried creating a list, but that doesn't seem to be working....Can someone please help me???
Tags (2)
0 Kudos
4 Replies
ChrisPedrezuela
Occasional Contributor III
Do you mind sharing what other shapefiles you need to use and reference and what info you need from them in order complete your input for your "subtitle" text element? It sounds like DDP would be ideal for your property mapping, but you'll require a single parcel layer to use as an ddp index layer. Then you can add dynamic text element based on the attributes of your index layer to use for dynamic titles. But as you said you have several shapefiles, which we are not sure what they are or are they individual pproperty shapefiles? Perhaps you can clarify further.

Cheers
0 Kudos
JenniferAbbott
New Contributor
I am using a large dataset out of SDE which has many shapefiles within it. It contains parcels of land for which I need to make a map for each parcel. I think datadriven pages would be adequate and am going to give that a try. Thank you.
0 Kudos
GeoffOlson
Occasional Contributor
You may not want to use "lyr.datasource" for your search cursor.  I originally did something similar with a DDP map and it disregards any definition queries or selections because it's using the actual source of the data instead of the layer - unless that's what you want.  Here's base code I used for updating the text elements in my map.  This code did separate the labels into 2 text elements, though, so that way the first half of the features would be in one column, and the second half would be in the second column.  I think this might help you.  I got some of this from the South Kingston, RI example available from ESRI.

import arcpy, os

#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr = arcpy.mapping.ListLayers(mxd, "IA_Detail_2010")[0]

#Get page number from data driven page - specified in the tool parameter dialogue box
pageNum = arcpy.GetParameterAsText(0)
ddp = mxd.dataDrivenPages
arcpy.AddMessage(pageNum)
pageID = mxd.dataDrivenPages.getPageIDFromName(pageNum)
mxd.dataDrivenPages.currentPageID = pageID

#Set layer definition query, this contols the rowcount variable
pageFieldValue = pageNum
mapLyr.definitionQuery = '"pageNum" = %s' % pageNum

#Listing the text elements on the page
concatElem1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat1")[0]
concatElem2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat2")[0]

#Finds the number of features in the map and sets up the lists to evenly distribute into the two text elements/columns
rowcount = int(arcpy.GetCount_management("IA_Detail_2010").getOutput(0))
percolumn = round(rowcount / 2.0)
count1 = 1
count2 = rowcount

#Specifies the features being used for the SearchCursor
sortfield = "crash"
rows = arcpy.SearchCursor(mapLyr, "", "", "CONCAT")
fieldrow  = arcpy.SearchCursor(mapLyr, "", "", "pageNum", sortfield + " A")
currentpage = ""

text_var1 = str()
text_var2 = str()

#The first for and if block limits the searched rows to the definition query
#The seconded/indented for and if block adds the text fields to the text elements
for row in fieldrow:
    if currentpage != row.pageNum:
        currentpage = row.pageNum
        for row in rows:
            if count1 <= percolumn:
                text_var1 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep)
                concatElem1.text = text_var1
                count1 += 1
            elif count2 > percolumn:
                text_var2 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep)
                concatElem2.text = text_var2
                count2 - 1
            else:
                pass
    else:
        pass

#Removed the definition query so all page numbers appear when the script is run next and refresh he layout view
mapLyr.definitionQuery = ""
arcpy.RefreshActiveView()
del mxd, row, rows, rowcount, percolumn, count1, count2
0 Kudos
ChrisPedrezuela
Occasional Contributor III
Yup Jen. I think this saves you the time of creating and maintaining a code just for this task. You could work off from the SDE feature class of the parcels so that if that data is being updated, your ddp enabled mxd will get refreshed as well. But if you want to use the shapefiles for some valid reason, it would be best to merge them all in one dataset assuming their table schema are the same, before using them as a ddp index layer. And if all information you need to for your titles, subtitles etc are within the parcel data themselves then just simply use Data Driven Page Attribute (assuming your using 10.1).

Cheers & Goodluck.


I am using a large dataset out of SDE which has many shapefiles within it. It contains parcels of land for which I need to make a map for each parcel. I think datadriven pages would be adequate and am going to give that a try. Thank you.
0 Kudos