Created
March 15, 2018 12:07
-
-
Save AlexArcPy/766dff593ae520c26f2bab0a1efd19ca to your computer and use it in GitHub Desktop.
Python start up script for ArcGIS Desktop users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Python start up file that supports both Python 2 and 3. | |
Helpful set of preimported modules and predefined | |
variables for ArcMap and ArcGIS Pro users. | |
''' | |
# pretty printing instead of regular print | |
# https://docs.python.org/2/library/pprint.html | |
from pprint import pprint | |
import os | |
import sys | |
import imp | |
import inspect | |
print('-- {} -- \n'.format(inspect.stack()[0][1])) | |
try: | |
imp.find_module('tabulate') | |
tabulate_found = True | |
from tabulate import tabulate | |
except ImportError: | |
tabulate_found = False | |
# ---------------------------------------------------------------------- | |
def main(): | |
PYTHON = sys.version_info[0] | |
SYSAPP = sys.executable | |
OTHER = 'Other' | |
APPS_MAPPER = { | |
'ArcCatalog.exe': 'ArcCatalog', | |
'ArcMap.exe': 'ArcMap', | |
'ArcGlobe.exe': 'ArcGlobe', | |
'ArcScene.exe': 'ArcScene', | |
'ArcGISPro.exe': 'ArcGISPro' | |
} | |
# use pprint by default when evaluating the | |
# contents of variables to get a nice view | |
if PYTHON == 2: | |
import __builtin__ | |
bltin = __builtin__ | |
else: | |
import builtins | |
bltin = builtins | |
def myhook(value, show=pprint, bltin=bltin): | |
if value is not None: | |
bltin._ = value | |
show(value) | |
sys.displayhook = myhook | |
app = APPS_MAPPER.get(os.path.basename(SYSAPP), OTHER) | |
if app == OTHER: | |
return | |
import arcpy | |
# if in ArcMap, then Python 2.7 is used and you probably | |
# would like to have `mxd` object pointing to the `current` | |
if app == 'ArcMap': | |
# add more relevant imports | |
global mp | |
import arcpy.mapping as mp | |
global mxd | |
mxd = mp.MapDocument("CURRENT") | |
# handy function to execute and see the results of | |
# a SQL query against an enterprise geodatabase | |
global sql | |
global conn | |
conn = None | |
def sql(query=''): | |
con_obj = arcpy.ArcSDESQLExecute(conn) | |
res = con_obj.execute(query) | |
if tabulate_found: | |
print(tabulate(res)) | |
return | |
# if ArcGIS Pro, then Python 3.5 is used and you need to get | |
# the `project` and then its `maps` | |
# cannot create create reference to the map at the initialization | |
# time because the Python window is loaded before the map. | |
# You need to call `mapinit()` in the Python window to get access | |
# to the `mapp` variable | |
if app == 'ArcGISPro': | |
global mapinit | |
def mapinit(): | |
global mapp | |
mapp = arcpy.mp.ArcGISProject("CURRENT").listMaps()[0] | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment