Looking for python scripts

06-12-2018 12:09 PM
New Contributor III

Trying to find a couple scripts... 

I want to use my windows task manager to run them at a given time, so I am wanting to have the parameters pre-filled with inputs and outputs. Would like to generate token, but other I've set up were fine without. 

 #1) Want to pull a feature service from AGOL, includes point layer and three related tables, all with attributes enabled. I would ideally like all of this to remain intact. If not possible then a script to download replicas would suffice. The one I found throws error-  ModuleNotFoundError: No module named 'urllib2' 

#2) A script to export data to an access .mdb 

--If one can be made to maintain relationships & attachments and then imported into access .mdb as such, that would be brilliant, but not thinking this one is possible. So if I could just get something that will overwrite the existing table through an export, I'd be super grateful! 

I've gotten so far as downloading multiple at once, but it didn't maintain attachments. I was also able to append the .mdb, but it created loads of duplicates, and a simple CopyFeatures was not working for me. 

Tried working with the Download Attachments created by Jake Skinner, I believe, but do not know enough script to modify it to allow both features and tables to be grabbed, along with pre-filled parameters. 

I am using ArcMap 10.6 and ArcPro 2.1.3, although .mdb does not seem to connect in Pro

Thank you for any and all assistance!! 

0 Kudos
9 Replies
MVP Esteemed Contributor

Personal geodatabases (AKA access mdb) are not supported in ArcGIS PRO.  See 001324: Personal Geodatabases are not supported in 64-bit versions of ArcGIS.—Help | ArcGIS Desktop If you absolutely need your data in a .mdb, you'll need to stick with ArcGIS/ArcMap/ArcCatalog.

Can you provide the code you currently are using?

That should just about do it....
New Contributor III

Yeah, I've been connecting to it in ArcMap & Catalog

0 Kudos
MVP Esteemed Contributor

This is a big ask of community members.  It appears you have tried some things on your own, which is good, but what is asked here doesn't give any of those details and asks for quite a bit.

Looking at #1, what script have you tried, exactly?

0 Kudos
New Contributor III

Download with attachments used (user is prompted to enter variables) by Jake Skinner Download ArcGIS Online Feature Service or ArcGIS Server Feature/Map Service 

This works for one service at a time, and does not overwrite an existing, as I would hope to. If not possible, just downloading the replica or something similar into a zip file will do. 

For exporting the .mdb, the table is going from one database to another. So I tried this one (also from Jake Skinner) but it duplicates instead of updating (all fields are a perfect match). When I change "Append_management" to CopyFeatures, it doesn't do anything, although not running any errors, but not completing the task- 

  1. import arcpy, os  
  2. from arcpy import env  
  4. GDB1 = r"C:\temp\fileGDB1.gdb"  
  5. GDB2 = r"C:\temp\fileGDB2.gdb"  
  7. env.workspace = GDB1  
  9. #Append tables  
  10. for table in arcpy.ListTables("*"😞          
  11.     env.workspace = GDB2  
  12.     for tableGDB in arcpy.ListTables(table):  
  13.         try:  
  14.                 if tableGDB.lower() == table.lower():  
  15.                     arcpy.AddMessage("\tAppending " + table)  
  16.                     try:                              
  17.                             arcpy.Append_management(GDB1 + os.sep + table, GDB2 + os.sep + tableGDB, "NO_TEST")                                                                  
  18.                     except:  
  19.                             arcpy.AddMessage("Could not append " + table)  
  20.                             pass  
  21.         except:  
  22.                 pass  
  23.     env.workspace = GDB1  
0 Kudos
New Contributor III

I've also tried others and multiple iterations of each before reaching out to see if someone can guide me in the right direction here

0 Kudos
New Contributor III

Found a workable one for replicas (although would still prefer one to overwrite existing if anyone has one): 

from __future__ import print_function
from arcrest.security import AGOLTokenSecurityHandler
from arcrest.agol import FeatureService
from arcrest.common.filters import LayerDefinitionFilter

if __name__ == "__main__":
    username = "[username]"
    password = "[passwprd]"
    url = "[feature service url]"
    proxy_port = None
    proxy_url = None
    agolSH = AGOLTokenSecurityHandler(username=username,
    fs = FeatureService(
    result = fs.createReplica(replicaName='Demo',

    print( result)

Then I am using this one to rename the attachments as something more meaningful, otherwise they come as Photo1.jpeg, Photo2.jpeg...

import arcpy, os
from collections import defaultdict

inFC = r'D:\test.gdb\INLET' # Feature Class 
inTable = r'D:\test.gdb\INLET__ATTACH' # Attachment table
fileLocation = r'D:\test\TestOutput' # Output location

# Get dictionary of ObjectID and associated field value
myFeatures = dict()
with arcpy.da.SearchCursor(inFC, ['GLOBALID', 'ID']) as cursor:
for row in cursor:
myFeatures[row[0]] = row[1]

# Create dictionary to count usage of the field value (to increment files)
valueUsage = defaultdict(int)

# Loop through attachments, incrementing field value usage, and using that
# increment value in the filename
with arcpy.da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'REL_GLOBALID']) as cursor:
for row in cursor:
if row[3] in myFeatures:
attachment = row[0]
fieldValue = myFeatures[row[3]] # Value of specified field in feature class
valueUsage[fieldValue] += 1 # Increment value
filename = "{0}_{1}.jpeg".format(fieldValue, valueUsage[fieldValue]) # filename = FieldValue_1
output = os.path.join(fileLocation, filename) # Create output ilepath
open(output, 'wb').write(attachment.tobytes()) # Output attachment to file‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍


Is there a way to link the two? I think I would need to do an unzip prior to the second. 

0 Kudos
MVP Legendary Contributor

Brita... /blogs/dan_patterson/2016/08/14/script-formatting to make it readable 

New Contributor III

Thank you!

0 Kudos
MVP Esteemed Contributor

Brita- watch your indents (or lack there of) in the second script at line 10 and then again at 18...

Is there a way to link the two?

You could put both scripts into one, calling each with their own def():

That should just about do it....
0 Kudos