-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshapeFileDifference.py
More file actions
executable file
·255 lines (172 loc) · 6.74 KB
/
shapeFileDifference.py
File metadata and controls
executable file
·255 lines (172 loc) · 6.74 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
#
# take the difference of stage and depth from two shapefiles
#
import os
import sys
from osgeo import ogr
from osgeo import osr
import numpy
import datetime
import time
import Scientific.IO.NetCDF
from Scientific.IO.NetCDF import NetCDFFile
from scipy import stats
#
# sort out argv
#
if len(sys.argv) <> 3:
sys.exit("\nUsage: %s shapeFilePositive shapeFileNegative\n" % sys.argv[0])
shapeFileNamePos = sys.argv[ 1 ]
shapeFileNameNeg = sys.argv[ 2 ]
deBug = True
# no. of rows ans columns in the eden grid
nrow = 405
ncol = 287
# create arrrays
# one for the positive values and one for the negative values
stagePos = numpy.zeros( ( 405, 287 ), 'f' )
depthPos = numpy.zeros( ( 405, 287 ), 'f' )
depthNeg = numpy.zeros( ( 405, 287 ), 'f' )
stageNeg = numpy.zeros( ( 405, 287 ), 'f' )
depthDiff = numpy.zeros( ( 405, 287 ), 'f' )
stageDiff = numpy.zeros( ( 405, 287 ), 'f' )
rowListPos = []
colListPos = []
rowListNeg = []
colListNeg = []
print "Opening\n%s\n%s" % ( shapeFileNamePos, shapeFileNameNeg )
driver = ogr.GetDriverByName( "ESRI Shapefile" )
dataSourcePos = driver.Open( shapeFileNamePos, 0 )
dataSourceNeg = driver.Open( shapeFileNameNeg, 0 )
print type( dataSourcePos )
print type( dataSourceNeg )
layerPos = dataSourcePos.GetLayer()
layerNeg = dataSourceNeg.GetLayer()
for feature in layerPos:
currentRow = feature.GetFieldAsInteger( "row" )
currentCol = feature.GetFieldAsInteger( "col" )
currentWaterDepth = feature.GetFieldAsDouble( "WaterDepth" )
currentStage = feature.GetFieldAsDouble( "Stage" )
rowListPos.append( currentRow )
colListPos.append( currentCol )
if deBug: print "Row: %d" % currentRow
if deBug: print "Cow: %d" % currentCol
if deBug: print "Depth: %f" % currentWaterDepth
if deBug: print "Stage: %f" % currentStage
depthPos[ currentRow, currentCol ] = currentWaterDepth
stagePos[ currentRow, currentCol ] = currentStage
for feature in layerNeg:
currentRow = feature.GetFieldAsInteger( "row" )
currentCol = feature.GetFieldAsInteger( "col" )
currentWaterDepth = feature.GetFieldAsDouble( "WaterDepth" )
currentStage = feature.GetFieldAsDouble( "Stage" )
rowListNeg.append( currentRow )
colListNeg.append( currentCol )
if deBug: print "Row: %d" % currentRow
if deBug: print "Cow: %d" % currentCol
if deBug: print "Depth: %f" % currentWaterDepth
if deBug: print "Stage: %f" % currentStage
depthNeg[ currentRow, currentCol ] = currentWaterDepth
stageNeg[ currentRow, currentCol ] = currentStage
# Populate array of average stage and average depth from all the shape files
# processed
#
print "no. rows: %d" % ( len( rowListPos ) )
print "no. cols: %d" % ( len( colListPos ) )
print "no. rows: %d" % ( len( rowListNeg ) )
print "no. cols: %d" % ( len( colListNeg ) )
for i in range( 0, 46818 ):
stageDiff[ rowListPos[ i ], colListPos[ i ] ] = ( stagePos[ rowListPos[ i ], colListPos[ i ] ] - stageNeg[ rowListPos[ i ], colListPos[ i ] ] )
depthDiff[ rowListPos[ i ], colListPos[ i ] ] = ( depthPos[ rowListPos[ i ], colListPos[ i ] ] - depthNeg[ rowListPos[ i ], colListPos[ i ] ] )
## if deBug: print "Avg stage: %f" % ( stageAvg[ i, j ] )
## if deBug: print "Avg depth: %f" % ( depthAvg[ i, j ] )
## if deBug: print "row: %d\ncol: %d" % ( i, j )
# Open lsel file to use as a mask
#
netcdf_lsel_filename = "/opt/physical/agency_data/eden/dem/eden_dem_cm_oc11.nc"
lsel_input = NetCDFFile( netcdf_lsel_filename, 'r' )
lsel = lsel_input.variables[ 'dem' ][ : ]
# Begin creating shapefile
#
# Create Spatial Reference
#
spatialReference = osr.SpatialReference()
spatialReference.ImportFromProj4( '+proj=utm +zone=17 +ellps=WGS84 +datum=WGS84 +units=m')
# Create Driver
#
outDriver = ogr.GetDriverByName( "ESRI Shapefile" )
# Create Layer
#
if deBug: print "Creating Final Layer"
outLayerName = "edenEpaDiff"
outPathName = "/physical/gis/eden/" + outLayerName + ".shp"
# Delete shapefile if it exists
#
if os.path.isfile( outPathName ):
print "File exists, must be deleted: %s" % outPathName
systring = "rm " + outPathName
if ( os.system( systring ) ) == 0:
print "file deleted"
else: print "could not delete file"
# Create Data Source: the directory where the shapefile goes (depends on driver)
#
if deBug: print "Creating outDataSource"
outDataSource = outDriver.CreateDataSource( "/physical/gis/eden" )
# Create Layer: names the shapefie (depends on spatial reference)
#
if deBug: print "Creating gridLayer"
outGrid_layer = outDataSource.CreateLayer( outLayerName, spatialReference, ogr.wkbMultiPolygon )
# Define Feature attributes
#
if deBug: print "Defining Feature Attributes"
# Define the fields objects
#
field_row = ogr.FieldDefn( "row", ogr.OFTInteger )
field_col = ogr.FieldDefn( "col", ogr.OFTInteger )
field_Stage = ogr.FieldDefn( "Stage", ogr.OFTReal )
field_WaterDepth = ogr.FieldDefn( "WaterDepth", ogr.OFTReal )
# Add Feature Attributes to Layer (i.e. to the shapefile)
#
if deBug: print "Creating Fields"
outGrid_layer.CreateField( field_row )
outGrid_layer.CreateField( field_col )
outGrid_layer.CreateField( field_Stage )
outGrid_layer.CreateField( field_WaterDepth )
# Create Layer Definition: This is needed to create each individual feature
# in the layer
#
outGrid_layer_defn = outGrid_layer.GetLayerDefn()
if deBug: print "creating Geometry"
left_edge_x = 463200
bottom_edge_y = 2790000
start_y = bottom_edge_y
for i in range( 0, nrow):
start_x = left_edge_x - 400
start_y = bottom_edge_y + ( i * 400 )
for j in range( 0, ncol ):
start_x += 400
# the entire rectagular domain is not populated with data, so we use a mask
# from the lsel file and skip the record
if numpy.isnan( lsel[ i, j ]):
continue
feature = ogr.Feature( outGrid_layer_defn )
feature.SetField( "WaterDepth", float( depthDiff[ i, j ] ) )
feature.SetField( "Stage", float( stageDiff[ i, j ] ) )
#
# Fields of the feature are set individually, then destroyed
# the new one is created above with the same outGrid_layer_defn...
# i think?
feature.SetField( "row", i )
feature.SetField( "col", j )
ring = ogr.Geometry( ogr.wkbLinearRing )
ring.AddPoint( start_x, start_y )
ring.AddPoint( start_x + 400, start_y )
ring.AddPoint( start_x + 400, start_y + 400 )
ring.AddPoint( start_x, start_y + 400 )
ring.AddPoint( start_x, start_y )
cell = ogr.Geometry( ogr.wkbPolygon )
cell.AddGeometry( ring )
feature.SetGeometry( cell )
outGrid_layer.CreateFeature( feature )
feature.Destroy()