-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGEO6_Infrastructure_grid.py
219 lines (192 loc) · 7.55 KB
/
GEO6_Infrastructure_grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#Python code for ArcMap 10.2, Python v 2.7
# Code development by Brian O' Connor June 2015
import sys, os
import os
import arcpy
from arcpy import env
import time
beginTime = time.clock()
#set to overwrite existing outputs
arcpy.env.overwriteOutput = True
#set workspace for inputs and outputs
tempFolder="C:\Data\GEO6\Extractives_Map"
#myWorkspace="C:\Data\GEO6\Extractives_Map"
#arcpy.env.workspace = myWorkspace
# Set local variables
West_Asia = 'WA'
Asia_Pacific = 'AP'
Europe = 'EU'
Latin_America_Caribbean = 'LAC'
Africa = 'AFR'
Region_List=['WA','AP','EU','LAC','AFR']
for region in Region_List:
out_name = region + "_geodatabase"
newDB = tempFolder+"/"+out_name
if os.path.isdir(newDB):
print "Output database exists - skipping database creation"
else:
print "Executing CreateFileGDB: "+newDB
arcpy.CreateFileGDB_management(tempFolder,out_name)
#Set environment extent
arcpy.env.extent = 'C:\Data\GEO6\Extractives_Map\GEO6_WA_Extractives.gdb\WA_boundary'
#Point_feature_type='Point'
fclist = arcpy.ListFeatureClasses()
#Point_fclist = arcpy.ListFeatureClasses("trans_*",Point_feature_type,"")
#Intersect transport layers with the grid
#Note - the grid should be in an equal area projection, e.g. Mercator for later calcs
# Set up loop for points and polygons
countLine=0
countPoint=0
countArea=0
countOther=0
Parser="_intersect"
for feature in fclist:
lastchar = feature[-1]
if lastchar=='p':
countPoint=countPoint+1
inFeatures = [feature, "wgs84_grid_half_degree"]
arcpy.Intersect_analysis(inFeatures, feature + Parser,"ONLY_FID","#","POINT")
if lastchar=='l':
countLine=countLine+1
inFeatures = [feature, "wgs84_grid_half_degree"]
arcpy.Intersect_analysis(inFeatures, feature + Parser, "ONLY_FID", "","LINE")
elif lastchar=='a':
countArea=countLine+1
inFeatures = [feature, "wgs84_grid_half_degree"]
arcpy.Intersect_analysis(inFeatures, feature + Parser, "ONLY_FID", "","INPUT")
#else:
#countOther=countOther+1
print fclist
print countLine,"line features have been intersected,",countPoint,"point features have been intersected and", countArea,"polygon features have been intersected" #countOther "features remain in the workspace"
#Make a separate list of the intersected point and line layers
feature_type_line='Polyline'
feature_type_point='Point'
feature_type_area='Polygon'
fclistLine = arcpy.ListFeatureClasses("*_intersect",feature_type_line,"")
fclistPoint = arcpy.ListFeatureClasses("*_intersect",feature_type_point,"")
fclistArea = arcpy.ListFeatureClasses("*_intersect",feature_type_area,"")
# Project layers to an equidistant projection for correct calculation of length (note - only need to do this for line files):
#NOTE- this step has been omitted and replaced with a geodesic length calculation
for feature in fclistLine:
arcpy.AddGeometryAttributes_management(feature,Geometry_Properties="LENGTH_GEODESIC",Length_Unit="KILOMETERS",Area_Unit="#",Coordinate_System="#")
print "Geodesic length calculated for", feature
for feature in fclistArea:
arcpy.AddGeometryAttributes_management(feature,Geometry_Properties="AREA_GEODESIC",Length_Unit="",Area_Unit="SQUARE_KILOMETERS",Coordinate_System="#")
print "Geodesic area calculated for", feature
# calculate summary stats and output a table
# for lines
count=0
for feature in fclistLine:
count=count+1
#Generate stats table
arcpy.Statistics_analysis(feature,feature+"_sum_table_l","LENGTH_GEO SUM","FID_wgs84_grid_half_degree")
print count, " sum line tables written to file"
# for areas
count=0
for feature in fclistArea:
count=count+1
#Generate stats table
arcpy.Statistics_analysis(feature,feature+"_sum_table_a","AREA_GEO SUM","FID_wgs84_grid_half_degree")
print count, " sum area tables written to file"
#For points - need to count the number of points in the grid cell, i.e. frequency
count=0
FieldNameList=[]
for feature in fclistPoint:
#parsing the field name for stats calc from the file name
count=count+1
FieldName=fclistPoint[count-1:count]
for i in FieldName:
x=i.find('_intersect')
statsField = "FID_"+i[0:x]
#statsFieldStr= statsField.encode('utf8')
print statsField
FieldNameList.append(statsField)
print FieldNameList
#Generate 2-way matrix of file name and field name
zipped = zip (fclistPoint,FieldNameList)
for i,j in zip(fclistPoint,FieldNameList):
statsFields = [[j, "COUNT"]]
arcpy.Statistics_analysis(i,i+ "_count_table", statsFields,"FID_wgs84_grid_half_degree")
count=count+1
print count, " count point tables written to file"
# Join the count and sum fields back to the original grid cells (using FID) via a table join
#for lines
Tablelist = arcpy.ListTables("*_sum_table_l")
# Set the local parameters:
#for lines
indata = "wgs84_grid_half_degree"
in_field="OBJECTID"
joinField = "FID_wgs84_grid_half_degree"
fieldList = ["SUM_LENGTH_GEO"]
print"Joining line distance tables to the original grid"
for table in Tablelist:
try:
arcpy.JoinField_management (indata, in_field, table, joinField, fieldList)
print "joined %s successfully" % table
except:
print arcpy.GetMessages()
break
#for areas
Tablelist = arcpy.ListTables("*_sum_table_a")
indata = "wgs84_grid_half_degree"
in_field="OBJECTID"
joinField = "FID_wgs84_grid_half_degree"
fieldList = ["SUM_AREA_GEO"]
print "Joining area tables to the original grid"
for table in Tablelist:
try:
arcpy.JoinField_management (indata, in_field, table, joinField, fieldList)
print "joined %s successfully" % table
except:
print arcpy.GetMessages()
break
#for points
Tablelist = arcpy.ListTables("*_count_table")
# Set the local parameters
indata = "wgs84_grid_half_degree"
in_field="OBJECTID"
joinField = "FID_wgs84_grid_half_degree"
fieldList = ["FREQUENCY"]
print "Joining point frequency tables to the original grid"
for table in Tablelist:
try:
arcpy.JoinField_management (indata, in_field, table, joinField, fieldList)
print "joined %s successfully" % table
except:
print arcpy.GetMessages()
break
#Create list of file names and field names
FileNameList= fclistLine + fclistArea + fclistPoint
#FieldNames = arcpy.ListFields("wgs84_grid_half_degree")
FieldNameList = [f.name for f in arcpy.ListFields("wgs84_grid_half_degree")]
#for field in FieldNames:
# FieldNameList = field.name
FieldNameList= FieldNameList[6:]
z = zip(FileNameList,FieldNameList)
count = 0
for i,j in z:
count = count+1
i=i[:-len(Parser)]
arcpy.PolygonToRaster_conversion("wgs84_grid_half_degree",j,i,"#","#",0.5)
print "Exported field "+ j + " to file: " + i + ",raster: " + str(count)
print count, "rasters exported"
#delete unnecessary feature classes
dataType="#"
#Create safe list of files for non-deletion
SaveFiles=["wgs84_grid_half_degree","WA_boundary", "Mines_all_prospect_p", "Mines_all_future_p","Mines_all_current_p","Fields_all_overlap_futureincurrent_diss_a","Fields_all_future_dissolve_a","Fields_all_current_dissolve_a","all_oil_gas_pipes_l"]
#Create list of files to delete
DeleteList=[]
for feature in fclist:
if not feature in SaveFiles:
DeleteList.append (feature)
else:
pass
for item in DeleteList:
arcpy.Delete_management(item, dataType)
print "%s deleted" % (item)
#delete unnecessary tables
tables = arcpy.ListTables()
for table in tables:
arcpy.Delete_management(table, dataType)
print "%s deleted" % (table)
print ("Total elapsed time (seconds): " + str(time.clock() - beginTime))