-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpygeonet_vectorio.py
211 lines (204 loc) · 9.18 KB
/
pygeonet_vectorio.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
import os
import numpy as np
from osgeo import ogr
from osgeo import osr
from osgeo import gdal
import pygeonet_prepare as Parameters
# Writing drainage network node (head/junction) shapefiles
def write_drainage_nodes(xx,yy, node_type, fileName, shapeName):
print "Writing", node_type, "shapefile"
# set up the shapefile driver
# driver = ogr.GetDriverByName(Parameters.driverName)
driver = ogr.GetDriverByName('ESRI Shapefile')
# This will delete and assist in overwrite of the shape files
if os.path.exists(fileName):
driver.DeleteDataSource(fileName)
# create the data source
data_source = driver.CreateDataSource(fileName)
# create the spatial reference, same as the input dataset
if not hasattr(Parameters, 'geotransform'):
fullFilePath = os.path.join(Parameters.demDataFilePath,
Parameters.demFileName)
ds = gdal.Open(fullFilePath, gdal.GA_ReadOnly)
geotransform = ds.GetGeoTransform()
Parameters.geotransform = geotransform
Parameters.inputwktInfo = ds.GetProjection()
srs = osr.SpatialReference()
gtf = Parameters.geotransform
georef = Parameters.inputwktInfo
srs.ImportFromWkt(georef)
# Project the xx, and yy points
xxProj = float(gtf[0])+ \
float(gtf[1]) * np.array(xx)
yyProj = float(gtf[3])+ \
float(gtf[5])*np.array(yy)
# create the layer
layer = data_source.CreateLayer(shapeName,\
srs, ogr.wkbPoint)
# Add the fields we're interested in
field_name = ogr.FieldDefn("Type", ogr.OFTString)
field_name.SetWidth(24)
layer.CreateField(field_name)
layer.CreateField(ogr.FieldDefn("Latitude", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("Longitude", ogr.OFTReal))
# Now add the channel heads as features to the layer
for i in xrange(0,len(xxProj)):
# create the feature
feature = ogr.Feature(layer.GetLayerDefn())
# Set the attributes using the values
feature.SetField("Type", node_type)
feature.SetField("Latitude", xxProj[i])
feature.SetField("Longitude", yyProj[i])
# create the WKT for the feature using Python string formatting
wkt = "POINT(%f %f)" % (float(xxProj[i]) , float(yyProj[i]))
# Create the point from the Well Known Txt
point = ogr.CreateGeometryFromWkt(wkt)
# Set the feature geometry using the point
feature.SetGeometry(point)
# Create the feature in the layer (shapefile)
layer.CreateFeature(feature)
# Destroy the feature to free resources
feature.Destroy()
# Destroy the data source to free resources
data_source.Destroy()
# Writing drainage paths as shapefile
def write_drainage_paths(geodesicPathsCellList):
print 'Writing drainage paths'
driver = ogr.GetDriverByName(Parameters.driverName)
if os.path.exists(Parameters.drainagelineFileName):
driver.DeleteDataSource(Parameters.drainagelineFileName)
data_source = driver.CreateDataSource(Parameters.drainagelineFileName)
if not hasattr(Parameters, 'geotransform'):
fullFilePath = os.path.join(Parameters.demDataFilePath,
Parameters.demFileName)
ds = gdal.Open(fullFilePath, gdal.GA_ReadOnly)
geotransform = ds.GetGeoTransform()
Parameters.geotransform = geotransform
Parameters.inputwktInfo = ds.GetProjection()
srs = osr.SpatialReference()
gtf = Parameters.geotransform
georef = Parameters.inputwktInfo
srs.ImportFromWkt(georef)
layer = data_source.CreateLayer(Parameters.drainagelinefileName,\
srs, ogr.wkbLineString)
field_name = ogr.FieldDefn("Type", ogr.OFTString)
field_name.SetWidth(24)
layer.CreateField(field_name)
for i in xrange(0,len(geodesicPathsCellList)):
# Project the linepoints to appropriate projection
xx = geodesicPathsCellList[i][1]
yy = geodesicPathsCellList[i][0]
# Project the xx, and yy points
xxProj = float(gtf[0])+ \
float(gtf[1]) * np.array(xx)
yyProj = float(gtf[3])+ \
float(gtf[5])*np.array(yy)
# create the feature
feature = ogr.Feature(layer.GetLayerDefn())
# Set the attributes using the values
feature.SetField("Type", 'ChannelNetwork')
# create the WKT for the feature using Python string formatting
line = ogr.Geometry(ogr.wkbLineString)
for j in xrange(0,len(xxProj)):
line.AddPoint(xxProj[j],yyProj[j])
# Create the point from the Well Known Txt
#lineobject = line.ExportToWkt()
# Set the feature geometry using the point
feature.SetGeometryDirectly(line)
# Create the feature in the layer (shapefile)
layer.CreateFeature(feature)
# Destroy the feature to free resources
feature.Destroy()
# Destroy the data source to free resources
data_source.Destroy()
# Writing cross section shapefiles
def write_cross_sections(TotalcrossSectionsXYArray,XSIDArray):
print "Writing Cross Sections shapefile"
driver = ogr.GetDriverByName(Parameters.driverName)
if os.path.exists(Parameters.xsFileName):
driver.DeleteDataSource(Parameters.xsFileName)
data_source = driver.CreateDataSource(Parameters.xsFileName)
if not hasattr(Parameters, 'geotransform'):
fullFilePath = os.path.join(Parameters.demDataFilePath,
Parameters.demFileName)
ds = gdal.Open(fullFilePath, gdal.GA_ReadOnly)
geotransform = ds.GetGeoTransform()
Parameters.geotransform = geotransform
Parameters.inputwktInfo = ds.GetProjection()
srs = osr.SpatialReference()
gtf = Parameters.geotransform
georef = Parameters.inputwktInfo
srs.ImportFromWkt(georef)
layer = data_source.CreateLayer(Parameters.xsshapefileName,\
srs, ogr.wkbLineString)
field_name = ogr.FieldDefn("Type", ogr.OFTString)
field_name.SetWidth(24)
layer.CreateField(field_name)
layer.CreateField(ogr.FieldDefn("ID", ogr.OFTInteger))
for i in xrange(0,len(TotalcrossSectionsXYArray)):
xx = TotalcrossSectionsXYArray[i][1]
yy = TotalcrossSectionsXYArray[i][0]
xxProj = float(gtf[0])+ \
float(gtf[1]) * np.array(xx)
yyProj = float(gtf[3])+ \
float(gtf[5])*np.array(yy)
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetField("Type", 'CrossSection')
feature.SetField("ID", XSIDArray[i])
line = ogr.Geometry(ogr.wkbLineString)
for j in xrange(0,len(xxProj)):
line.AddPoint(xxProj[j],yyProj[j])
feature.SetGeometryDirectly(line)
layer.CreateFeature(feature)
feature.Destroy()
data_source.Destroy()
# Writing drainage paths as shapefile
def write_bank_lines(leftBankCellList,rightBankCellList):
print 'Writing bank lines'
driver = ogr.GetDriverByName(Parameters.driverName)
if os.path.exists(Parameters.banklineFileName):
driver.DeleteDataSource(Parameters.banklineFileName)
data_source = driver.CreateDataSource(Parameters.banklineFileName)
if not hasattr(Parameters, 'geotransform'):
fullFilePath = os.path.join(Parameters.demDataFilePath,
Parameters.demFileName)
ds = gdal.Open(fullFilePath, gdal.GA_ReadOnly)
geotransform = ds.GetGeoTransform()
Parameters.geotransform = geotransform
Parameters.inputwktInfo = ds.GetProjection()
srs = osr.SpatialReference()
gtf = Parameters.geotransform
georef = Parameters.inputwktInfo
srs.ImportFromWkt(georef)
layer = data_source.CreateLayer(Parameters.banklinefileName,
srs, ogr.wkbLineString)
field_name = ogr.FieldDefn("Type", ogr.OFTString)
field_name.SetWidth(24)
layer.CreateField(field_name)
field_name = ogr.FieldDefn("Side", ogr.OFTString)
field_name.SetWidth(24)
layer.CreateField(field_name)
for n in range(2):
if n == 0:
BankCellList = leftBankCellList
Bank = 'Left'
else:
BankCellList = rightBankCellList
Bank = 'Right'
for i in xrange(0,len(leftBankCellList)):
xx = BankCellList[i][1]
yy = BankCellList[i][0]
xxProj = float(gtf[0])+ \
float(gtf[1]) * np.array(xx)
yyProj = float(gtf[3])+ \
float(gtf[5])*np.array(yy)
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetField("Type", "Bank")
feature.SetField("Type", Bank)
line = ogr.Geometry(ogr.wkbLineString)
for j in xrange(0,len(xxProj)):
line.AddPoint(xxProj[j],yyProj[j])
feature.SetGeometryDirectly(line)
layer.CreateFeature(feature)
feature.Destroy()
data_source.Destroy()