Skip to content

Instantly share code, notes, and snippets.

View sshopov's full-sized avatar

Stoyan Shopov sshopov

View GitHub Profile
@sshopov
sshopov / arcpy_list_toolbox_tools.py
Created March 12, 2014 04:19
Listing all tools for every toolbox in ArcGIS
import re
import arcpy
for toolbox in arcpy.ListToolboxes():
wildcard = re.search(r"\((\w+)\)", toolbox).group(1)
for tool in arcpy.ListTools("*_%s"%wildcard):
print toolbox, tool, arcpy.Usage(tool)
@sshopov
sshopov / leaflet_add_locate_me_button.js
Created March 12, 2014 05:09
Adds a locate me button between the + and - zoom buttons. When clicked it zooms to the user's current location. Requires jquery, leaflet and font awesome CSS http://fortawesome.github.io/Font-Awesome/icons
$('.leaflet-control-zoom-in').after('<a onclick="map.locate({setView:true, zoomLevel:18})" href="#"><i class="fa fa-male"></i></a>');
@sshopov
sshopov / clip_and_zip.py
Created March 12, 2014 06:05
Clip desired features from the current ArcMap map document, export them to a file geodatabase and zip it up when done. Tags: arcpy, arcgis, arcmap, python, clip, zip.
'''
1. Open ArcMap and add the desired feature dataset to it.
2. Zoom to the extent that you want to export.
3. Open the Python prompt.
4. Paste the code below into it and hit Enter.
5. Go to where out_fgdb points to, then copy the export_fgdb.gdb.zip file and send it out.
'''
import shutil
import os
dict((section, dict(cfg_parser.items(section))) for section in cfg_parser.sections())
@sshopov
sshopov / arcpy_attribute_based_etl.py
Last active August 29, 2015 13:58
The Data Interoperability Extension for ArcGIS Desktop, also known as Safe FME, is great mainly thanks to its hunderds of transformers. However for simple use cases it's easier to perform such tasks in Python. The script below appends records from one feature class to another while applying transformers and mappings as defined in the config. Tag…
import sys
import os
import copy
import arcpy
def get_field_values(fc, field, where=""):
'''
@return: A list of the values for the given field in the given feature class
with the given optional where clause applied.
@sshopov
sshopov / run_in_new_thread_decorator.py
Last active February 24, 2018 21:34
A handy decorator to run a function in a new thread. It came from https://arcpy.wordpress.com/2013/10/25/using-os-startfile-and-webbrowser-open-in-arcgis-for-desktop/ Tags: arcpy python arcgis
import functools
import threading
# A decorator that will run its wrapped function in a new thread
def run_in_new_thread(function):
# functool.wraps will copy over the docstring and some other metadata
# from the original function
@functools.wraps(function)
def fn_(*args, **kwargs):
thread = threading.Thread(target=function, args=args, kwargs=kwargs)
@sshopov
sshopov / view_arcgis_domains.py
Created May 7, 2014 07:21
It's hard to easily go over 400+ geodatabase domains using ArcCatalog. This script has methods to: exports all domains and their values in alphabetical order to text file for easier browsing; find a specific domain; print domain usage for all fields in a given dataset; . Tags: ArcGIS arcpy GIS esri
import arcpy
def export_domains_to_txt():
lines = []
for domain in arcpy.da.ListDomains(arcpy.env.workspace):
if domain.codedValues:
values = ','.join(['%s:%s'%(key, domain.codedValues[key]) for key in sorted(domain.codedValues.keys())])
else:
values = domain.range
lines.append('%s : %s'%(domain.name, values))
@sshopov
sshopov / RegistryUpdateForIEScriptProblem
Created September 3, 2014 05:27
IE7 sometimes complains that a JavaScript script is running too slow and asks the user whether they want to terminate it or continue. To get rid of this annoying popup a few changes need to be made to the registry. ref: http://www.itwriting.com/blog/119-ie7-script-madness.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
namespace RegistryUpdateForIEScriptProblem
{
class Program
{
@sshopov
sshopov / DrawNumberOnImage
Created September 3, 2014 05:33
I had an icon representing a little blue dot and needed to create 100 copies each with a number in them. Python saves the day yet again. Tags: Python, PIL, Pillow
#requires http://www.pythonware.com/products/pil/ or https://github.com/python-pillow/Pillow
import Image, ImageFont, ImageDraw
def add_number(number):
'''Add the number to the center of an icon and save it to a new image
Useful for generating multiple icons'''
image = Image.open(r'C:\python\temp\bd.png')
font=ImageFont.truetype("arial.ttf", 55) #, encoding='utf-8')
draw=ImageDraw.Draw(image)
x=(image.size[0]-draw.textsize(number,font)[0])/2
@sshopov
sshopov / get_portal_security_token.py
Created June 3, 2016 06:20
Get a security token to access ArcGIS Online or Portal for ArcGIS services
import urllib
import urllib2
import json
def get_portal_security_token(portal_url, portal_admin_user, portal_admin_password, expiration=60):
'''
Returns a security token for AGOL or an ArcGIS portal.
Expiration period is specified in minutes and defaults to an hour.
The portal URL needs to be something like this:
https://www.arcgis.com/sharing/generateToken