Python: Insert Photographs into ArcMap Data View Programattically

1903
2
11-22-2013 07:51 AM
PeterWilson
Occasional Contributor III
I have hundreds of photographs that have been taken as part of a survey. I've created point features that represent the location of each photograph as well as added a field to the point features with the full path to the photographs. I'd like to insert the photographs into the data view of ArcMap, specify the width and height to display the photograph and have them centered at the point feature associated  with the photograph. I wouldn't like to do this manually as there are a couple hundred photographs. The photographs need to be inserted not hyperlinked as I need to print the various sections of the surveyed area and display the photographs on the printed map.

Regards
Tags (2)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Peter,

Here is some code that zooms to each point, creates a polygon feature class from the extent, then converts the polygon to a raster in a new directory.  The code then copies the original photo to the new directory (overwriting the raster previously created), so that it can use the coordinate information, and then defines the projection of the rasters.  If you need to change the size of the raster, you can try adding in the Rescale function.

import arcpy
from arcpy import env
from arcpy import mapping
env.overwriteOutput = 1

mxd = mapping.MapDocument(r"C:\temp\python\Points.mxd")
df = mapping.ListDataFrames(mxd)[0]

lyr = arcpy.mapping.ListLayers(mxd)[0]

# Create new photo directory
newPhoto = r"C:\Temp\Python\Photos\New Photos"

dict = {}

# Create dictionary of OBJECTIDs and the path to the photo
with arcpy.da.SearchCursor(lyr, ["OBJECTID", "PHOTO"]) as cursor:
   for row in cursor:
      rasterName = row[1].split("\\")[-1]
      dict[row[0]] = rasterName

firstTime = "True"

# Select each point, zoom/pan to the selected point, create a polygon feature class from extent,
# convert polygon to raster dataset
for key in dict:
   if firstTime == 'True':
      arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "OBJECTID = " + str(key))
      df.extent = lyr.getSelectedExtent()
      XMAX = df.extent.XMax
      XMIN = df.extent.XMin
      YMAX = df.extent.YMax
      YMIN = df.extent.YMin
      pnt1 = arcpy.Point(XMIN, YMIN)
      pnt2 = arcpy.Point(XMIN, YMAX)
      pnt3 = arcpy.Point(XMAX, YMAX)
      pnt4 = arcpy.Point(XMAX, YMIN)
      array = arcpy.Array()
      array.add(pnt1)
      array.add(pnt2)
      array.add(pnt3)
      array.add(pnt4)
      array.add(pnt1)
      polygon = arcpy.Polygon(array)
      arcpy.CopyFeatures_management(polygon, r"IN_MEMORY\polygon")
      arcpy.PolygonToRaster_conversion(r"IN_MEMORY\polygon", "OID", r"IN_MEMORY\raster")
      arcpy.CopyRaster_management(r"IN_MEMORY\raster", newPhoto + "\\" + dict[key])
      firstTime = "False"
   else:
      arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "OBJECTID = " + str(key))
      df.panToExtent(lyr.getSelectedExtent())
      XMAX = df.extent.XMax
      XMIN = df.extent.XMin
      YMAX = df.extent.YMax
      YMIN = df.extent.YMin
      pnt1 = arcpy.Point(XMIN, YMIN)
      pnt2 = arcpy.Point(XMIN, YMAX)
      pnt3 = arcpy.Point(XMAX, YMAX)
      pnt4 = arcpy.Point(XMAX, YMIN)
      array = arcpy.Array()
      array.add(pnt1)
      array.add(pnt2)
      array.add(pnt3)
      array.add(pnt4)
      array.add(pnt1)
      polygon = arcpy.Polygon(array)
      arcpy.CopyFeatures_management(polygon, r"IN_MEMORY\polygon")
      arcpy.PolygonToRaster_conversion(r"IN_MEMORY\polygon", "OID", r"IN_MEMORY\raster")
      arcpy.CopyRaster_management(r"IN_MEMORY\raster", newPhoto + "\\" + dict[key])

del mxd

# Clear the selection
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")

list = []

with arcpy.da.SearchCursor(lyr, ["PHOTO"]) as cursor:
   for row in cursor:
      list.append(row[0])

# Copy original photo to the new photo directory, replacing the raster created previously
for photo in list:
   shutil.copy(photo, newPhoto)

spatial_ref = arcpy.Describe(lyr).spatialReference

# Project each raster
env.workspace = newPhoto
for raster in arcpy.ListRasters("*"):
   arcpy.DefineProjection_management(raster, spatial_ref)
0 Kudos
PeterWilson
Occasional Contributor III
Hi Jake

Thanks for the following, great idea n generating the rasters from the current spatial extent. I'll post my final script once complete.

Regards
0 Kudos