Skip to content

Instantly share code, notes, and snippets.

@rabbleNaut
Last active August 29, 2015 13:57
Show Gist options
  • Save rabbleNaut/9691826 to your computer and use it in GitHub Desktop.
Save rabbleNaut/9691826 to your computer and use it in GitHub Desktop.
For use with ESRI model builder. Downloads images based on URLs, creates new fields, populates new fields with links to local images for reporting.
'''
DoForms_Downloader.py
v1.10
3/21/2014
This script will accomplish the following:
- Allows user to select featureclass generated from DoForms
- Create new field(s) to store local image URL
- Download images to local drive ( /Images directory )
- Populates new field(s) w/ links to local image URL
****
NOTE:
Images are named, in this case, on the "LABEL" field
see: currentID = str(row.LABEL) . This seemed to be the only appropriate unique identifier
****
'''
import arcpy, os
import urllib
from arcpy import *
# Input parameters from model
inputFC = GetParameterAsText(0)
outputFolder = GetParameterAsText(1)
fieldList = ListFields(inputFC, "Image*")
x = 0 # Counter
# Adds new fields to featureclass depending on number of image fields (ex. IMAGE_4)
for field in fieldList:
x += 1
localField = "LOCALIMG_" + str(x)
AddField_management(inputFC, localField, "TEXT","","","","Local Image " + str(x),"")
x = 0 # Reset counter
#Iterates through each feature in shapefile, retrieves photograph from cloud and downloads to local drive
for field in fieldList:
rows = SearchCursor(inputFC, "", "", "", "")
x += 1
imgField = field.name
for row in rows:
currentID = str(row.LABEL)
currentURL = row.getValue(imgField)
if len(currentURL) > 1:
AddWarning ("Downloading site: %s" % currentID)
outputPhotos = outputFolder + os.sep + "Image" + "_" +str(x) + "_" + currentID + ".jpg"
urllib.urlretrieve (currentURL, outputPhotos)
else: AddMessage ("Site: %s does not contain an %s URL" % (currentID, imgField))
del row, rows
x = 0 # Reset counter
#Iterates through each feature in shapefile and writes the local URL to the field, if applicable
for field in fieldList:
rowz = UpdateCursor(inputFC)
x += 1
localField = "LOCALIMG_" + str(x)
imgField = field.name
for row in rowz:
currentID = str(row.LABEL)
currentURL = row.getValue(imgField)
if len(currentURL) > 1:
outputPhotos = outputFolder + os.sep + "Image" + "_" +str(x) + "_" + currentID + ".jpg"
AddWarning ("Writing site: %s" % currentID)
row.setValue(localField, outputPhotos)
rowz.updateRow(row)
else: AddMessage ("Site: %s does not contain an %s URL" % (currentID, imgField))
del row, rowz
# Just a helpful reminder
AddWarning ("""
Your images have been downloaded to the following location:
%s
""" % outputFolder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment