Select to view content in your preferred language

Import multiple rasters into map document, then print to pdf...

3056
2
Jump to solution
01-21-2013 01:37 PM
SamCoggins1
Occasional Contributor
Hello:

I have about 191 .ecw files to bring into an map document. I've tried to load them with the Add Data button but it crashes ArcGIS. So, I thought I would script it with arcpy.mapping. I have got to a point where I can print a pdf of the first .ecw in the list.

The basic idea is that I want to add all the .ecw's and then print one big pdf of the whole thing.

Now I'm unsure of how to progress. I'd appreciate some advice on the script, I'm fairly new to Python, and very new to arcpy.mapping, so please keep it simple! Thanks... Here's my script so far...

import arcpy, os from arcpy import env  # Workspace inDir = (r'F:\Ortho_prelim\TEST')  # Specify map document mxd = arcpy.mapping.MapDocument(r'F:\Imagery.mxd')  # Specify data frame df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]  # Import file  dirList = os.listdir(r'F:\Ortho_prelim\TEST') for fname in dirList:     if fname.endswith('.ecw'):         print inDir + os.sep + fname         lyrFile = arcpy.mapping.Layer(inDir + os.sep + fname)         arcpy.mapping.AddLayer(df, lyrFile)           # refresh view and table of contents, save changes to mxd arcpy.RefreshActiveView() arcpy.RefreshTOC() mxd.save() arcpy.mapping.ExportToPDF(mxd, r"F:\Imagery.pdf")
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
I believe what is happening is each time a raster is added, the df extent is being set only to that raster.  You will need to set the df extent prior to export to PDF.  The following worked on my 10.1 machine. (note the paths are different).

import arcpy, os from arcpy import env  # Workspace inDir = (r'C:\Temp\Images')  # Specify map document mxd = arcpy.mapping.MapDocument(r'C:\Temp\Images\Imagery.mxd')  # Specify data frame df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]  # Import file  dirList = os.listdir(r'C:\Temp\Images') for fname in dirList:     if fname.endswith('.TIF'):         print inDir + os.sep + fname         lyrFile = arcpy.mapping.Layer(inDir + os.sep + fname)         arcpy.mapping.AddLayer(df, lyrFile)          #set df extent newExtent = df.extent newExtent.XMin, newExtent.YMin = 1017500, 629600 newExtent.XMax, newExtent.YMax = 1023000, 639200 df.extent = newExtent  arcpy.mapping.ExportToPDF(mxd, r'C:\Temp\Images\Imagery.pdf'


Note you found a loophole in using arcpy.mapping.Layer.  It is intended to add layer files, not source data.  The rasters may not render the way that you want them.

Jeff

View solution in original post

0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
I believe what is happening is each time a raster is added, the df extent is being set only to that raster.  You will need to set the df extent prior to export to PDF.  The following worked on my 10.1 machine. (note the paths are different).

import arcpy, os from arcpy import env  # Workspace inDir = (r'C:\Temp\Images')  # Specify map document mxd = arcpy.mapping.MapDocument(r'C:\Temp\Images\Imagery.mxd')  # Specify data frame df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]  # Import file  dirList = os.listdir(r'C:\Temp\Images') for fname in dirList:     if fname.endswith('.TIF'):         print inDir + os.sep + fname         lyrFile = arcpy.mapping.Layer(inDir + os.sep + fname)         arcpy.mapping.AddLayer(df, lyrFile)          #set df extent newExtent = df.extent newExtent.XMin, newExtent.YMin = 1017500, 629600 newExtent.XMax, newExtent.YMax = 1023000, 639200 df.extent = newExtent  arcpy.mapping.ExportToPDF(mxd, r'C:\Temp\Images\Imagery.pdf'


Note you found a loophole in using arcpy.mapping.Layer.  It is intended to add layer files, not source data.  The rasters may not render the way that you want them.

Jeff
0 Kudos
SamCoggins1
Occasional Contributor
Thanks for the response it was very helpful. However, changing the extent will be an issue down the road here as I'm hoping to eventually add the imagery to a map document that uses data driven pages. Plus, you're correct, by adding the source data rather than a layer I have run into the issue that it does not display properly.

So... I thought I could use Python to make the layer and then add the .ecw's to a layer and then use the layer file in my script. Which would would all work well and would mean I don't have to open a map document.

So I will need two scripts

1) To make a layer file containing the .ecw files.
2) To add the layer file to the map (with data driven pages) and then print all to pdf.

At the moment I'm further along the second script and am working on the first. If I have any more issues I'll post up later... Thanks again
0 Kudos