View difference between Paste ID: SkV1dYhM and VJYSZt0T
SHOW: | | - or go back to the newest paste.
1
# ---------------------------------------------------------------------------
2
# Final.py
3
# Created on: 5/12/2014
4
# Created by: Noah C. Huntington
5
# Copyright: Furman Land Surveyors, Inc. 2014 - (806)374-4246
6
# Description:  Calculates Multiple Least Cost Paths Between Substations
7
# ---------------------------------------------------------------------------
8
9
# Import modules
10
import arcpy
11
import os
12
from arcpy import env
13
arcpy.env.overwriteOutput = 1
14
from arcpy.sa import *
15
import math
16
17
# Check out any necessary licenses
18
arcpy.CheckOutExtension("Spatial")
19
20
# Declare variables for tool parameters
21
subs  = arcpy.GetParameterAsText(0)
22
Field = arcpy.GetParameterAsText(1)
23
originValue = arcpy.GetParameterAsText(2)
24
destValue   = arcpy.GetParameterAsText(3)
25
lines = arcpy.GetParameterAsText(4)
26
Dem = arcpy.GetParameterAsText(5)
27
landuse = arcpy.GetParameterAsText(6)
28
wells = arcpy.GetParameterAsText(7)
29
savepath = arcpy.GetParameterAsText(8)
30
31
# Weights applied to criteria
32
slope_Weight = 10
33
landuse_weight = 90
34
35
count = 0
36
37
# Set Geoprocessing environments
38
arcpy.env.cellSize = 90
39
ws = arcpy.env.workspace = savepath + '\\Scratch.gdb'
40
sr = arcpy.SpatialReference(2275)
41
42
'''
43
subs  = r"G:\Xcel\Route Tool\Southwest\Data.gdb\Subs_4201"
44
Field = "Name"
45
originValue = "Hillside"
46
destValue   = "Coulter"
47
lines = r"G:\Xcel\Route Tool\Southwest\Data.gdb\OLS_Lines_Proj"
48
Dem =r"G:\Xcel\Route Tool\Southwest\Data.gdb\Dem_4201"
49
landuse = r"G:\Xcel\Route Tool\Southwest\Data.gdb\lu_4201"
50
wells = r"G:\Xcel\Route Tool\Southwest\Data.gdb\wells_4201"
51
savepath = r"G:\Xcel\Route Tool\Southwest\Hillside"
52
'''
53
54
55
56
#   --------------------------------------------------------------------------------------
57
#   Functions and variables for savepaths are declared in this section
58
#   Dynamic path for out polylines
59
#   --------------------------------------------------------------------------------------
60
Output_polyline =  os.path.join(savepath  + "\\Results.gdb"  + "\\LCP_" + chr(count + 65))
61
62
def get_backlink(count):
63
    return os.path.join(savepath  + "\\Scratch.gdb"  + "\\backlink_" + str(count))
64
65
def get_LCP(count):
66
    return os.path.join(savepath  + "\\Scratch.gdb"  + "\\LCP_" + chr(count + 65))
67
68
def get_costRaster(count):
69
    return os.path.join(savepath  + "\\Scratch.gdb"  + "\\cost_Raster_" + str(count))
70
71
def get_weighted_Raster(count):
72
    return os.path.join(savepath  + "\\Scratch.gdb"  + "\\weighted_Raster_" + str(count))
73
74
75
76
77
78
79
#   -----------------------------------------------------------------------------------
80
#   This function retrieves the coordinates of the endpoints
81
#       It is called for during the while loop while running extractbypoints
82
#   -----------------------------------------------------------------------------------
83
84
def get_Coordinates():
85
86
    #Create SQL statement
87
    where_clause_envelope =  "\"{}\" = '{}'" "or " "\"{}\" = '{}'".format(Field,originValue,Field,destValue) #produces: "Name" = 'Amarillo 34th St'or "Name" = 'Coulter
88
89
90
    #Create temp layer for origin and destination
91
    Ends =  os.path.join(savepath  + "\\Scratch.gdb"  + "\\Ends_" + str(count))
92
    Ends_fl = arcpy.MakeFeatureLayer_management(subs, Ends, where_clause_envelope)
93
94
    fields = ["SHAPE@X","SHAPE@Y"]
95
    cursor = arcpy.da.SearchCursor(Ends_fl, fields)
96
    record1 = next(cursor)
97
    record2 = next(cursor)
98
    x1 = record1[0]
99
    x2 = record2[0]
100
    y1 = record1[1]
101
    y2 = record2[1]
102
103
    pointList = [arcpy.Point(x1, y1),
104
         arcpy.Point(x2,y2)]
105
106
    return pointList
107
108
def get_buffer_Distance():
109
    # This function determines the buffer distance
110
    #   which is performed in order to determine study area before undergoing raster processing
111
112
    #Create SQL Statement
113
    where_clause_envelope =  "\"{}\" = '{}'" "or " "\"{}\" = '{}'".format(Field,originValue,Field,destValue)#produces: "Name" = 'Amarillo 34th St'or "Name" = 'Coulter
114
115
    #Create temp layer for origin and destination
116
    Points = arcpy.MakeFeatureLayer_management(subs, "Subs_Selection", where_clause_envelope)
117
118
    Line = arcpy.PointsToLine_management(Points, arcpy.Geometry(),"","","NO_CLOSE")
119
    length = 0
120
    for geometry in Line:
121
        length += geometry.length
122
123
    miles = length/5280
124
    if miles > 10:
125
        Buffer = 20*5280
126
    else:
127
        Buffer = length*2
128
129
    return Buffer
130
131
132
133
#   --------------------------------------------------------------------------------------
134
#   This is the main section of the tool
135
#
136
#   --------------------------------------------------------------------------------------
137
138
def main():
139
    count = 0
140
    arcpy.CreateFileGDB_management(savepath, "Scratch.gdb")
141
    arcpy.CreateFileGDB_management(savepath, "Results.gdb")
142
143
    arcpy.AddMessage("Data will be saved to: {0}" .format(ws))
144
145
    #Create SQL statement
146
    where_clause_envelope =  "\"{}\" = '{}'" "or " "\"{}\" = '{}'".format(Field,originValue,Field,destValue)#produces: "Name" = 'Amarillo 34th St'or "Name" = 'Coulter
147
148
    #Create temp layer for origin and destination
149
    subset = arcpy.MakeFeatureLayer_management(subs, "Subs_Selection", where_clause_envelope)
150
151
    #Create extent polygon from selected subs
152
    arcpy.AddMessage("Computing Extents...")
153
    arcpy.MinimumBoundingGeometry_management(subset, "Subs_Env", "", "ALL")
154
155
    #Buffer the envelope
156
    arcpy.AddMessage("Buffering Extent Polygon...")
157
    Buffer = get_buffer_Distance()
158
    arcpy.Buffer_analysis("Subs_Env", "Envelope_Buffer", str(Buffer) + " Feet", "FULL", "ROUND", "ALL", "")
159
160
....