Skip to content

Instantly share code, notes, and snippets.

@danabauer
Created May 18, 2013 05:57
Show Gist options
  • Save danabauer/249ba987c182259d1e37 to your computer and use it in GitHub Desktop.
Save danabauer/249ba987c182259d1e37 to your computer and use it in GitHub Desktop.
Aaron Ogle's walkshed nyc script
# ---------------------------------------------------------------------------
# walkshed_friction_nyc.py
# ---------------------------------------------------------------------------
# Import system modules
import ConfigParser, walkshed, sys, string, os, datetime, arcgisscripting
temp_path = "C:\projects\dbauer_walkshed\NYC\data\temp"
shape_path = "C:\projects\dbauer_walkshed\NYC\data\shape"
raster_path = "C:\projects\dbauer_walkshed\NYC\data\raster"
default_friction = 5
buf_dist = "5 Meters"
cell_size = 5
# Friction
def make_friction(gp, paths, planes, barriers):
# Delete Files
gp.AddMessage("Deleting %s" % temp_path)
os.system("del /Q %s*.*" % temp_path)
layers = []
layers.extend(paths)
layers.extend(planes)
layers.extend(barriers)
shp_field = "FID"
max_friction = 999
min_friction = -999
for layer in layers :
polygon_layer = walkshed.shp_name(shape_path, layer["name"])
# Buffer source shapes as defined
if layer["buffer"] == True:
gp.AddMessage("Buffering %s... [%s]" % (layer["name"], datetime.datetime.now().isoformat()))
gp.Buffer_analysis(walkshed.shp_name(shape_path, layer["name"]), walkshed.buf_name(temp_path, layer["name"]), buf_dist, "FULL", "ROUND", "NONE", "")
polygon_layer = walkshed.buf_name(temp_path, layer["name"])
# Convert all buffered shapes to rasters
seed1 = walkshed.get_seed()
gp.AddMessage("Converting %s to raster... [%s]" % (layer["name"], datetime.datetime.now().isoformat()))
gp.FeatureToRaster_conversion(polygon_layer, shp_field, walkshed.temp_ras_name(temp_path, layer["name"], seed1), cell_size)
# Reclassify
gp.AddMessage("Reclassifying %s... [%s]" % (layer["name"], datetime.datetime.now().isoformat()))
remap = "0 1000000 %d;NoData %d" % (layer["friction"], max_friction if layer["group"] == "paths" else min_friction)
gp.Reclassify_sa(walkshed.temp_ras_name(temp_path, layer["name"], seed1), "VALUE", remap, walkshed.temp_ras_name(temp_path, layer["name"], "final"))
# Combine layers
gp.AddMessage("Assembling all layers... [%s]" % (datetime.datetime.now().isoformat()))
max = walkshed.make_max_map_algebra(barriers, temp_path, "final", None)
gp.AddMessage(max)
gp.SingleOutputMapAlgebra_sa(max, walkshed.temp_ras_name(temp_path, "friction", "barriers"))
max = walkshed.make_max_map_algebra(planes, temp_path, "final", walkshed.temp_ras_name(temp_path, "friction", "barriers"))
gp.AddMessage(max)
gp.SingleOutputMapAlgebra_sa(max, walkshed.temp_ras_name(temp_path, "friction", "planes"))
remap = "%d %d" % (min_friction, max_friction)
gp.Reclassify_sa(walkshed.temp_ras_name(temp_path, "friction", "planes"), "VALUE", remap, walkshed.temp_ras_name(temp_path, "friction", "planes2"))
min = walkshed.make_min_map_algebra(paths, temp_path, "final", walkshed.temp_ras_name(temp_path, "friction", "planes2"))
gp.AddMessage(min)
gp.SingleOutputMapAlgebra_sa(min, walkshed.temp_ras_name(temp_path, "friction", "final"))
remap = "%d %d" % (max_friction, default_friction)
gp.Reclassify_sa(walkshed.temp_ras_name(temp_path, "friction", "final"), "VALUE", remap, walkshed.ras_name(raster_path, "friction"))
gp.AddMessage("%s complete! [%s]" % (walkshed.ras_name(raster_path, "friction"), datetime.datetime.now().isoformat()))
gp = arcgisscripting.create(9.3)
gp.OutputCoordinateSystem = "C:/Program Files/ArcGIS/Coordinate Systems/Projected Coordinate Systems/World/WGS 1984 Web Mercator (Auxiliary Sphere).prj"
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")
gp.CheckOutExtension("spatial")
for i in range(len(sys.argv)):
if i > 0:
arg = sys.argv[i]
print "Processing " + arg
config = ConfigParser.RawConfigParser()
config.read(arg)
temp_path = config.get("common", "temp_path")
shape_path = config.get("common", "shape_path")
raster_path = config.get("common", "raster_path")
default_friction = config.getint("common", "default_friction")
buf_dist = config.get("common", "buf_dist")
cell_size = config.getint("common", "cell_size")
gp.CellSize = cell_size
gp.Mask = config.get("common", "mask")
gp.Extent = config.get("common", "extent")
gp.OverWriteOutput = 1
# Sources
layerSections = config.get("common", "friction_sections").split(",")
layers = { "paths": [], "planes": [], "barriers": [] }
for ls in layerSections:
group = config.get(ls, "group")
layers[config.get(ls, "group")].append({ "name": config.get(ls, "name"), "friction": config.getint(ls, "friction"), "buffer": config.getboolean(ls, "buffer"), "group": group })
gp.AddMessage("Calculating friction [%s]" % (datetime.datetime.now().isoformat()))
make_friction(gp, layers["paths"], layers["planes"], layers["barriers"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment