Skip to content

Instantly share code, notes, and snippets.

@cindygis
cindygis / transferSTsAndCVDs.md
Last active August 29, 2015 14:17
Transfer descriptions of subtypes and coded value domains from feature class to shapefile

######Change this setting when exporting a feature class with subtypes and attribute domains to shapefile

####Transfer Field Domain Descriptions (Environment setting)

#####This geoprocessing environment controls whether output shapefiles and dBASE (.dbf) tables will have fields added containing domain and subtype descriptions, in addition to fields containing domain and subtype codes.

@cindygis
cindygis / listCodeValuesInCVD.py
Last active August 29, 2015 14:17
Prints out a list of code-value pairs for a given attribute domain in a workspace.
import arcpy
arcpy.env.workspace = "Database Connections\arb.sde"
cvd = "cvd_District"
for dom in arcpy.da.ListDomains():
if dom.name == cvd:
for i in dom.codedValues.iteritems():
print i
# One liner
cvd_pairs = [i for i in dom.codedValues.iteritems() for dom in arcpy.da.ListDomains() if dom.name == cvd]
@cindygis
cindygis / viewAGSDataStoreDatabases.py
Created March 23, 2015 09:19
Prints the list of databases registered with the given ArcGIS Server, along with the connection properties (excluding ENCRYPTED_PASSWORD).
#
# @date 23/03/2015
# @author Cindy Williams
#
# Prints the list of databases registered with the
# given ArcGIS Server, along with the connection
# properties (excluding ENCRYPTED_PASSWORD).
#
# For use in the Python window in ArcCatalog.
#
@cindygis
cindygis / generateChainageAlongLineFeatures.py
Created March 25, 2015 08:59
Generates chainage at a set distance along line features and saves it to an existing, empty point feature class.
#
# @date 27/11/2014
# @author Cindy Williams
#
# Generates chainage at a set distance along line features
# and saves it to an existing, empty point feature class.
# The point feature class should have a predefined schema
# in case other attributes need to be transferred.
#
# For use as a standalone script.
@cindygis
cindygis / exportDDPtoJPEG.py
Created March 25, 2015 09:11
Basic export of data driven pages to multiple jpegs
import arcpy
import os
mxd = arcpy.mapping.MapDocument(r"C:\Some\Arb\Folder\Test.mxd")
fld = r"C:\Some\Arb\Folder\jpegs"
ddp = mxd.dataDrivenPages
for i in range(1, ddp.pageCount + 1):
ddp.currentPageId = i
name = os.path.join(fld, ddp.pageRow.getValue(ddp.pageNameField) + ".jpg")
@cindygis
cindygis / getDiffApplySelect.py
Last active August 29, 2015 14:17
Gets the difference between two lists of SGs and applies a feature selection containing the difference
drak = [row[0] for row in arcpy.da.SearchCursor("DSG", "SG21Code")]
sga = [row[0] for row in arcpy.da.SearchCursor("SGA", "SGA.ID")]
lr_diff = lambda l, r: list(set(l).difference(r))
rest = lr_diff(sga, drak)
qry = """ SGA.ID IN (\'""" + "\', \'".join(rest) + "\')"
arcpy.management.SelectLayerByAttribute("SGA","NEW_SELECTION", qry)
@cindygis
cindygis / projectFtrsForWeb.py
Last active August 29, 2015 14:17
Projects feature classes to Web Mercator (Auxiliary Sphere) if they are not in it already.
#
# @date 24/03/2015
# @author Cindy Williams
#
# Projects feature classes to Web Mercator (Auxiliary Sphere)
# if they are not in it already.
#
# For use as a standalone script.
#
@cindygis
cindygis / replaceLayerSources.py
Created March 25, 2015 18:16
Replace data sources of layers in a map document using two different methods.
#
# @date 25/03/2015
# @author Cindy Williams
#
# Replace data sources of layers in a mxd using
# two different methods.
#
# For use in the Python window in ArcMap.
#
@cindygis
cindygis / Adding outputs to map.md
Last active August 29, 2015 14:18
Preventing intermediate geoprocessing results from being added to the data frame in ArcMap

When forced to run a script in the Python window in ArcMap, or when it's easier to do so, change the environmental setting to prevent the intermediate layers from being added to the TOC.

######arcpy.env.addOutputsToMap = False

Change it back to True when you want the result to be added e.g

arcpy.env.workspace = r"C:\Some\Arb\Folder\work.gdb"
arcpy.env.addOutputsToMap = False
ftr_list = [arcpy.management.MakeFeatureLayer(ftr) for ftr in arcpy.ListFeatureClasses()]
@cindygis
cindygis / convertVarcharToGeometry.sql
Last active August 29, 2015 14:18
Spatially enables a table in SQL that contains valid coordinates, to create a spatial view for use in ArcMap.
SELECT GIS_ID, GEOMETRY::STPointFromText('POINT(' + CONVERT(varchar(50), Longitude) + ' ' + CONVERT(varchar(50), Latitude) + ' )', 4326) AS Location
FROM tbl_random
WHERE Longitude > 0 # Checks if there are valid coordinates for Eastern Hemisphere