User Rating: 3 / 5

Star ActiveStar ActiveStar ActiveStar InactiveStar Inactive
I was asked how to build pdfs from a script that would export map items and different layers with a legend.

First we have in ArcGIS client a bunch of layers that are called District_1, District_2,District_x,etc. These are File Geodatabase feature classes with geometry type Polygon.
Basically it's the poly that defines a border for a Congressional District.

We have a database table called Health Center Clinics, which has longitude, latitude, the district in which they fall in, name, parent Health Center Name, and misc information.

We want to: show the district poly, all the health center clinics within that district, change the text label in the pdf, and set the name of the pdf.

Desired Outcome: show poly for district 1, include all health centers clinics, change the name to show the Congress persons name, Make a legend show the Health Center parent.

import arcpy 
def PDFIt(dist):

	tname = "Unrepresented"
	tdis = dist
	fname = "Congressional District " + dist + " - Unrepresented";
	rows = arcpy.SearchCursor("Health Center Clinics",fields="us_house_rep;us_house_number")
	for row in rows:
		if row.getValue("us_house_number") == tdis:
			tname = "The Honorable " + row.getValue("us_house_rep") + '\n' + "Congressional District " + row.getValue("us_house_number")
			tempname = row.getValue("us_house_rep").replace(" ","_")
			tempname = tempname.replace(".","")
			tempname = tempname.replace(".","")
			tempname = tempname.replace("(D)","")
			tempname = tempname.replace("(R)","")
			tempname = str(tempname.encode("utf-8").replace("é","e").replace("í","i").replace("á","a"))
			tempname = str(tempname.encode("utf-8").replace("'",""))
			tempname = tempname.replace("__","_")
			fname = row.getValue("us_house_number") + "_mapc_" + tempname + ".pdf"
	print tname	
	print fname		
	mxd = arcpy.mapping.MapDocument("CURRENT")
	titleItem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*")[0]
	titleItem.text = tname
	df = arcpy.mapping.ListDataFrames(mxd)[0]  
	lyr = arcpy.mapping.ListLayers(mxd, 'District_' + tdis, df)[0] 
	ext = lyr.getExtent()  
	df.extent = ext  
	lyr.visible = True
	lyrm = arcpy.mapping.ListLayers(mxd, 'Health Center Clinics', df)[0]
	lyrm.visible = True
	lyrm.definitionQuery= "[is_advocacy]='1' and [status] in ('Active','Reportable') and [us_house_number]='" + dist + "'"
	arcpy.mapping.ExportToPDF(mxd, "D:\\dlfs\\us_congress_clinic_sitename\\" + fname)
	lyr.visible = False
	lyrm.definitionQuery= "[is_advocacy]='1'and [status] in ('Active','Reportable')"
for x in range(1, 37):

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
I had an email asking me how to get rid of the red lines after a schema change to the database in MS SQL Management Studio .

One of my new favorite shortcuts is to reload the intellisense with in the query window. If you make a change to the schema of a database your intellisense won't pick it up and you will have red lines under things you know exists. Hit the keys Ctrl + Shift + R to fix it.

If you have a bunch of Query windows open and you have been disconnected from the server you can reconnect those query windows just by going to the left pane top item right click and reconnect