Skip to content

Instantly share code, notes, and snippets.

@cindygis
cindygis / buildDefQryFromXLS.py
Last active August 19, 2016 19:48
Loops over an existing spreadsheet and builds up a definition query to apply to a layer in ArcMap
#
# @author Cindy Williams
# @date 25/06/2013
#
# Loops over an existing spreadsheet with attribute data
# and builds up a definition query to apply to the dataset.
#
# For use in the Python window in ArcMap.
#
@cindygis
cindygis / changeDelimiterInCSV.py
Created July 19, 2016 07:57
Replaces the delimiter in a csv file
'''
@date 19/07/2016
@author Cindy Williams-Jayakumar
Replaces the delimiter in a csv file
using the pathlib library in Python 3
'''
import csv
@cindygis
cindygis / createTableFieldsFromXLS.py
Created May 5, 2016 05:50
Converts a spreadsheet containing field names and aliases into a file geodatabase table.
'''
@date 09/07/2013
@author Cindy Williams
Converts a spreadsheet containing field names and aliases
into a file geodatabase table.
'''
import arcpy
xls = r"C:\Some\Arb\Folder\test.xlsx\Fields$"
@cindygis
cindygis / reverseGeocodeXLScoords.py
Created April 21, 2016 08:05
Quickly reverse geocode coordinates in a spreadsheet
import geocoder
import pandas as pd
xls = r'C:\Some\Arb\Folder\coords.xls'
out_xls = r'C:\Some\Arb\Folder\geocoded.xls'
df = pd.read_excel(xls)
for index, row in df.iterrows():
g = geocoder.google([row[3], row[2]], method='reverse')
df.set_value(index, 'Street Address', g.address)
@cindygis
cindygis / addFieldWithExtendTable.py
Last active March 31, 2016 06:45
Adds a new field using a numpy array to layers in a map document.
'''
@date 12/10/2015
@author Cindy Williams
Adds a new field to layers in a map document, based
on a current field.
For use in the Python window in ArcMap.
'''
@cindygis
cindygis / sortArcMapTOC.py
Created July 3, 2015 06:32
Sorts the TOC in ArcMap alphabetically by using a temporary group layer for reference.
#
# @date 03/07/2015
# @author Cindy Williams
#
# Sorts the TOC in ArcMap alphabetically using
# a temporary group layer for reference. This group
# layer should be empty, and can be created anywhere
# in the TOC.
#
# For use in the Python window in ArcMap.
@cindygis
cindygis / filterDataFrameUsingMask.py
Created December 17, 2015 10:09
Filters a pandas data frame using a mask
import pandas as pd
in_xls = r"C:\Some\Arb\Folder\test.xlsx"
columns = [0, 2, 3, 4, 6, 8]
# Use a function to define the mask
# to create a subset of the data frame
def mask(df, key, value):
return df[df[key] == value]
@cindygis
cindygis / updateRowsWithNestedLoop.py
Created December 4, 2015 06:22
Updates a field using a nested loop in an arcpy update cursor
import arcpy
lyr = arcpy.mapping.Layer(r'C:\Some\Arb\Folder\work.gdb\ftr_test')
cursor = arcpy.da.UpdateCursor(lyr, 'Name')
for i in xrange(10):
for e in ['X', 'Y']:
upd.next() # Increment the cursor
upd.updateRow(['Zone ' + str(i) + e])
@cindygis
cindygis / createRandomPointsInPoly.py
Last active October 14, 2015 06:29
Creates random points in each polygon feature. Bypasses the Advanced licence needed to run the Create Random Points tool in ArcMap.
#
# @date 06/08/2015
# @author Cindy Williams
#
# Creates random points in each polygon feature
# without the need for an Advanced licence to run
# the Create Random Points tool.
#
# For use in the Python window in ArcCatalog.
#
@cindygis
cindygis / extractDOCtablesToXLS.py
Created September 29, 2015 08:53
Writes the contents of all rows from multiple tables in a Word document to a spreadsheet.
import docx
import xlwt
doc = r"C:\Some\Arb\Folder\input.docx"
xls = r"C:\Some\Arb\Folder\output.xls"
document = docx.Document(doc)
book = xlwt.Workbook()
cur_sheet = book.add_sheet("Tables")
row_num = 0