0

I'm working on a project that utilizes ArcPy to join a shapefile and a excel file. This join takes information from the excel file and creates a map that highlights certain areas different shades of a color to display the data in a more visually concise format.

I start with a blank map file, and place the shapefile and the excel file into the file as layers. The join works perfectly with no errors; however, when I go to save the map file as a copy, and then subsequently export a pdf of the output, I get blank files both times.

I call arcpy.RefreshTOC() and arcpy.RefreshActiveView() to attempt to make my changes refresh onto the map, but nothing happens.

Here is my code:

import arcpy

# setup workspace
env.workspace = r"C:\Users\Class\Desktop\MapCreation\Resources"
env.qualifiedFieldNames = False

# start work on blank map
mapFile = r'C:\Users\Class\Desktop\MapCreation\Resources\blankMap.mxd'
mxd = arcpy.mapping.MapDocument(mapFile)

# create a table view from the xls
table = arcpy.mapping.TableView(r'C:\Users\Class\Desktop\MapCreation\Resources\2012tpreg.xls\teens$')

# create a new layer using shapefile
shape_file_layer = arcpy.MakeFeatureLayer_management(r'C:\Users\Class\Desktop\MapCreation\Resources\MSshapefiles\MScounties.shp')

# AddJoin
arcpy.AddJoin_management(shape_file_layer, "CNTY_FIPS", table, "FiPS")

# refresh the TOC and ActiveView in order to propagate the changes
arcpy.RefreshTOC()
arcpy.RefreshActiveView()

# export PDF
mxd.saveACopy(r'C:\Users\Class\Desktop\MapCreation\Resources\newMap.mxd')
arcpy.mapping.ExportToPDF(mxd, "test.pdf")

I should note that I know the join is working correctly, because when I run that piece code inside the ArcGIS environment (rather than in IDLE or Command Prompt) it creates the join as a layer in the map environment.

I have tried a lot of different ideas to try to get this problem fixed and I couldn't figure out anything that helped. If anyone has any ideas on what could be wrong I would love to hear them.

migrated from stackoverflow.com Apr 28 '15 at 21:23

This question came from our site for professional and enthusiast programmers.

2

I think it might be because you do not add a layer to Mxd in code, because if it is in accordance with your code, the final layer will only exist in memory, and will not be automatically added to your Mxd, so when you keep Mxd when the layer will not be saved. Therefore, you need to add this section of code.

# AddJoin
arcpy.AddJoin_management(shape_file_layer, "CNTY_FIPS", table, "FiPS")

# New code
layerfile=r"c:\temp\temp.lyr"
arcpy.SaveToLayerFile_management(shape_file_layer, layerfile, "ABSOLUTE")
addlayer = arcpy.mapping.Layer(layerfile)
dataFrame=arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.mapping.AddLayer(dataFrame, addlayer)

You can refer to the contents here. Use python to add layers to TOC

As an alternative, you can also write to the following code. So you may not need to use a temporary layer files.

# AddJoin
result=arcpy.AddJoin_management(shape_file_layer, "CNTY_FIPS", table, "FiPS")

# New code
addlayer = result.getOutput(0)
dataFrame=arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.mapping.AddLayer(dataFrame, addlayer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.