Skip to content

Instantly share code, notes, and snippets.

@knu2xs
knu2xs / get-dom-using-xpath
Last active January 4, 2016 00:19
Locate a DOM element using XPath and return as JS DOM object instance for further mainpulation
function xpathObject (xpathString) {
return document.evaluate(
xpathString,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
};
@knu2xs
knu2xs / military-date-string.js
Last active January 4, 2016 20:00
Modify date prototype to include a method to return the date the way I like it.
Date.prototype.toMilitaryString = function () {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return this.getDate() + ' ' + months[this.getMonth()] + ' ' + this.getFullYear();
};
@knu2xs
knu2xs / bootstrap-nav-list-item.hbs
Last active August 29, 2015 13:55
bootstrap active class in <li> instead of <a href> for navigation unordered lists
{{#link-to "reach.index" tagName="li" href=false}}
<a {{bind-attr href="view.href"}}>
Overview
</a>
{{/link-to}}
@knu2xs
knu2xs / ember-view-afterrender.js
Last active August 29, 2015 13:55
add afterRender event hook to ember views primitive
// create after render view event hook to detect when view is finished loading
// http://mavilein.github.io/javascript/2013/08/01/Ember-JS-After-Render-Event/
// https://github.com/emberjs/ember.js/commit/512327c0128f53d4a0d6828ab38f27c6ccf686ec
Ember.View.reopen({
didInsertElement: function () {
this._super();
Ember.run.scheduleOnce('afterRender', this, this.afterRenderEvent);
},
afterRenderEvent: function () {}
});
@knu2xs
knu2xs / arcpy-unique-table-field-values.py
Last active August 29, 2015 13:55
This is a Python function returning unique values from a field in an ArcGIS table, either a stand alone table or a feature class. It was adapted from code sample on ArcGIS resource center Help documentation on arcpy.da.SearchCursor http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000011000000
# Depends on first importing arcpy.da at top of script
def unique_values(input_table, field):
# search cursor wrapped in list generator creating list of all values
values = (row[0] for row in arcpy.da.SearchCursor(input_table, (field)))
# pass list into set to get only unique values and return the result
return sorted(set(values))
@knu2xs
knu2xs / fix-invalid-characters.py
Last active August 29, 2015 13:55
Replaces or drops invalid characters from names according to rules for naming feature classes
def fix_invalid_characters(self, string):
"""
Drops invalid characters from names according to rules for naming feature classes, notably must
start with a letter and remaining characters must be numbers, letters or underscores.
"""
# drop invalid characters at beginning of string
string = re.sub(r'^[^a-zA-Z]+', '', string)
# find any invalid characters in remainder of string and replace with underscores
return re.sub(r'[^a-z0-9A-Z_]', '_', string)
@knu2xs
knu2xs / python-addin-button-call-tool.py
Last active August 29, 2015 13:56
Small modifications to the default file created by the Python Add-In assistant to be able to include and use a custom toolbox and tool as part of an add-in.
import arcpy
import pythonaddins
import os
class addDefinitionQueryLayers(object):
"""Implementation for arcpyMappingAddin_addin.button (Button)"""
def __init__(self):
self.enabled = True
@knu2xs
knu2xs / add-layers-with-definition-query-from-attribute.py
Last active August 29, 2015 13:56
For every unique value permutation in a feature class attribute field, create a unique layer displaying only these values utilizing a definition query.
# import arcpy...if already imported in your script, this is not necessary
import arcpy
def _uniqueValues(input_fc, field):
# search cursor wrapped in list comprehension creating list of all values
values = [row[0] for row in arcpy.da.SearchCursor(input_fc, (field))]
# pass list into set to get only unique values and return the result reverse sorted
return sorted(set(values), reverse=True)
@knu2xs
knu2xs / arcpy-get-attribute-name-list.py
Last active August 29, 2015 13:56
list comprehension to collect all attribute field names in a variable
# get parameter
feature_class = arcpy.getParameterAsText(0)
# use list comprehension to list all field objects and access the name of the field
attribute_names = [field.name for field in arcpy.ListFields(feature_class)]
@knu2xs
knu2xs / arcgis-python-addin-toolbox.py
Last active August 29, 2015 13:56
Example onclick event to streamline the process of including a toolbox and tool as part of a Python add-in
def onClick(self):
# name of toolbox without tbx extension
toolboxName = "Mapping"
# name of tool to be executed
toolName = "AddDefinitionQueryLayers"
# create string with path to toolbox
toolboxPath = os.path.join(os.path.dirname(__file__), toolboxName + ".tbx")