-
Notifications
You must be signed in to change notification settings - Fork 18
/
LSD_GeologyTools.py
388 lines (275 loc) · 12.6 KB
/
LSD_GeologyTools.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 07 13:34:02 2017
@author: smudd
"""
# This script modified from
# http://geoinformaticstutorial.blogspot.co.uk/2012/11/convert-shapefile-to-raster-with-gdal.html
# Importing needed modules
import os
from os.path import exists
from osgeo import ogr, osr
import LSDPlottingTools as LSDPT
import gdal as gdal
import numpy as np
from osgeo.gdalconst import GA_ReadOnly
def readFile(filename):
print("Hey buddy, Reading the file: "+filename)
filehandle = gdal.Open(filename, GA_ReadOnly )
if filehandle == None:
raise Exception("Unable to read the data file")
band1 = filehandle.GetRasterBand(1)
geotransform = filehandle.GetGeoTransform()
geoproj = filehandle.GetProjection()
Z = band1.ReadAsArray()
xsize = filehandle.RasterXSize
ysize = filehandle.RasterYSize
return xsize,ysize,geotransform,geoproj,Z
def writeFile(filename,geotransform,geoprojection,data):
(x,y) = data.shape
format = "GTiff"
noDataValue = -9999
driver = gdal.GetDriverByName(format)
# you can change the dataformat but be sure to be able to store negative values including -9999
dst_datatype = gdal.GDT_Float32
#print(data)
dst_ds = driver.Create(filename,y,x,1,dst_datatype)
dst_ds.GetRasterBand(1).WriteArray(data)
dst_ds.GetRasterBand(1).SetNoDataValue( noDataValue )
dst_ds.SetGeoTransform(geotransform)
dst_ds.SetProjection(geoprojection)
return 1
def Rasterize_BGS_geologic_maps(shapefile_name):
# The shapefile to be rasterized:
print('Rasterize ' + shapefile_name)
#get path and filename seperately
shapefilefilepath = LSDPT.GetPath(shapefile_name)
shapefilename = LSDPT.GetFileNameNoPath(shapefile_name)
shapefileshortname = LSDPT.GetFilePrefix(shapefile_name)
print("Shapefile name is: "+shapefilename)
# now get the the fields from the shapefile
daShapefile = shapefile_name
dataSource = ogr.Open(daShapefile)
daLayer = dataSource.GetLayer(0)
# lets see what the layers are
print("Let me tell you what the names of the fields are!")
layerDefinition = daLayer.GetLayerDefn()
for i in range(layerDefinition.GetFieldCount()):
print(layerDefinition.GetFieldDefn(i).GetName())
# The raster file to be created and receive the rasterized shapefile
outrastername = shapefileshortname + '.tif'
outraster = shapefilefilepath+os.sep+ outrastername
outcsv = shapefilefilepath+os.sep+shapefileshortname+'_lithokey.csv'
print("Full name of out raster is: "+outraster)
# Rasterize!!
system_call = 'gdal_rasterize -a BGSREF -l ' + shapefileshortname +' -tr 90 -90 -a_nodata -9999 ' + shapefile_name + ' ' + outraster
print("System call is: ")
print(system_call)
os.system(system_call)
# now convert the raster to UTM, as well as delete the stupid TIF
# The raster file to be created and receive the rasterized shapefile
outrastername_bil = shapefileshortname + '.bil'
outraster_bil = shapefilefilepath+os.sep+ outrastername_bil
print("Full name of out raster is: "+outraster_bil)
# This assumes UTM zone 30, because why would we do any work in East Anglia?
system_call2 = 'gdalwarp -t_srs EPSG:32630 -of ENVI -dstnodata -9999 ' + outraster + ' ' + outraster_bil
os.system(system_call2)
# Now get rid of the tif
system_call3 = 'rm '+ outraster
os.system(system_call3)
# Make a key for the bedrock
geol_dict = dict()
for feature in daLayer:
ID = feature.GetField("BGSREF")
GEOL = feature.GetField("RCS_D")
if ID not in geol_dict:
print("I found a new rock type, ID: "+ str(ID)+ " and rock type: " + str(GEOL))
geol_dict[ID] = GEOL
print("The rocks are: ")
print(geol_dict)
with open(outcsv, 'wb') as f:
f.write('ID,rocktype\n')
for key in geol_dict:
f.write(str(key)+','+ str(geol_dict[key])+'\n')
print("All done")
def Rasterize_geologic_maps_pythonic(shapefile_name, raster_resolution = 400, geol_field = "xx"):
# The shapefile to be rasterized:
print('Rasterize ' + shapefile_name)
#get path and filename seperately
shapefilefilepath = LSDPT.GetPath(shapefile_name)
shapefilename = LSDPT.GetFileNameNoPath(shapefile_name)
shapefileshortname = LSDPT.GetFilePrefix(shapefile_name)
print("Shapefile name is: "+shapefilename)
# now get the the fields from the shapefile
daShapefile = shapefile_name
dataSource = ogr.Open(daShapefile)
daLayer = dataSource.GetLayer(0)
# lets see what the layers are
print("Let me tell you what the names of the fields are!")
layerDefinition = daLayer.GetLayerDefn()
for i in range(layerDefinition.GetFieldCount()):
print(layerDefinition.GetFieldDefn(i).GetName())
# The raster file to be created and receive the rasterized shapefile
outrastername = shapefileshortname + '.tif'
outraster = shapefilefilepath+os.sep+ outrastername
outcsv = shapefilefilepath+os.sep+shapefileshortname+'_lithokey.csv'
print("Full name of out raster is: "+outraster)
# Create the destination data source
inGridSize=float(raster_resolution)
xMin, xMax, yMin, yMax = daLayer.GetExtent()
xRes = int((xMax - xMin) / inGridSize)
yRes = int((yMax - yMin) / inGridSize)
rasterDS = gdal.GetDriverByName('GTiff').Create(outraster, xRes, yRes, 1, gdal.GDT_Byte)
# Define spatial reference
NoDataVal = -9999
rasterDS.SetProjection(daLayer.GetSpatialRef().ExportToWkt())
rasterDS.SetGeoTransform((xMin, inGridSize, 0, yMax, 0, -inGridSize))
rBand = rasterDS.GetRasterBand(1)
rBand.SetNoDataValue(NoDataVal)
rBand.Fill(NoDataVal)
# Rasterize
gdal.RasterizeLayer(rasterDS, [1], daLayer, options = ["ATTRIBUTE=GEOL_CODE"])
# Make a key for the bedrock
geol_dict = dict()
for feature in daLayer:
ID = feature.GetField(geol_field)
GEOL = feature.GetField("GEOL_CODE")
if ID not in geol_dict:
print("I found a new rock type, ID: "+ str(ID)+ " and rock type: " + str(GEOL))
geol_dict[ID] = GEOL
print("The rocks are: ")
print(geol_dict)
with open(outcsv, 'wb') as f:
f.write('ID,rocktype\n')
for key in geol_dict:
f.write(str(key)+','+ str(geol_dict[key])+'\n')
print("Done rasterizing!")
return outraster
def Correct_Raterized_GLIM_map(tifname):
# And now for a hack that converts to
print("The raster name is: "+tifname)
[xsize,ysize,geotransform,geoproj,Z] = readFile(tifname)
print("Before data check")
print(Z)
print("Data type is: "+ str(Z.dtype))
X = Z.astype(int)
# Set large negative values to -9999
X[X<=0] = -9999
#Z[np.isnan(Z)]= -9999
print("After_data_check")
print(X)
#get path and filename seperately
filepath = LSDPT.GetPath(tifname)
#filename = LSDPT.GetFileNameNoPath(tifname)
fileshortname = LSDPT.GetFilePrefix(tifname)
outraster2 = filepath+fileshortname + '2.tif'
writeFile(outraster2,geotransform,geoproj,X)
def geologic_maps_modify_shapefile(shapefile_name, geol_field = "xx"):
# The shapefile to be rasterized:
print('Rasterize ' + shapefile_name)
#get path and filename seperately
shapefilefilepath = LSDPT.GetPath(shapefile_name)
#shapefilename = LSDPT.GetFileNameNoPath(shapefile_name)
shapefileshortname = LSDPT.GetFilePrefix(shapefile_name)
# get the new shapefile name
new_shapefile_name = shapefilefilepath+os.sep+shapefileshortname+"_new.shp"
# copy the shapefile into the new shapefile--we don't wwant to mess up the original data
print("The New Shapefile name is: "+new_shapefile_name)
Copy_Shapefile(shapefile_name,new_shapefile_name)
# New shapefile is opened for writing.
dataSource = ogr.Open(new_shapefile_name,1)
daLayer = dataSource.GetLayer(0)
# add a new field
new_field = ogr.FieldDefn("GEOL_CODE", ogr.OFTInteger)
daLayer.CreateField(new_field)
# lets see what the layers are
print("Let me tell you what the names of the fields are after I added one!")
layerDefinition = daLayer.GetLayerDefn()
for i in range(layerDefinition.GetFieldCount()):
print(layerDefinition.GetFieldDefn(i).GetName())
# Make a key for the bedrock
geol_dict = dict()
geol_iterator = 0
for feature in daLayer:
GEOL = feature.GetField(geol_field)
if GEOL not in geol_dict:
geol_iterator = geol_iterator+1
print("I found a new rock type, GEOL: "+ str(GEOL)+ " and rock type: " + str(geol_iterator))
geol_dict[GEOL] = geol_iterator
# now get the geol code
this_geol_code = geol_dict[GEOL]
# set the feature
feature.SetField("GEOL_CODE", this_geol_code)
# need to update the layer
daLayer.SetFeature(feature)
print("The rocks are: ")
print(geol_dict)
print("All done")
return new_shapefile_name, geol_dict
def Copy_Shapefile(shapefile_name,new_shapefile_name):
"""
Sweet Jesus why is this so difficult?
"""
if exists(shapefile_name) is False:
raise Exception('[Errno 2] No such file or directory: \'' + shapefile_name + '\'')
# get the short name of the new shapefile
shapefileshortname = LSDPT.GetFilePrefix(new_shapefile_name)
print("The shortname is: "+shapefileshortname)
# read in the data
src = ogr.Open(shapefile_name)
daLayer = src.GetLayer(0)
# lets see what the layers are
print("Let me tell you what the names of the fields are!")
layerDefinition = daLayer.GetLayerDefn()
for i in range(layerDefinition.GetFieldCount()):
print(layerDefinition.GetFieldDefn(i).GetName())
geom_type = layerDefinition.GetGeomType()
# get rid of previous copies
if exists(new_shapefile_name):
os.remove(new_shapefile_name)
# get the driver and create a new data source
driver = ogr.GetDriverByName('ESRI Shapefile')
#src.Destroy()
# Now write to the the outfile
out_ds = driver.CreateDataSource(new_shapefile_name)
# create the output layer
#out_lyr = out_ds.CreateLayer("yo",srs = daLayer.GetSpatialRef(),geom_type=ogr.wkbPolygon)
out_lyr = out_ds.CreateLayer("yo",srs = daLayer.GetSpatialRef(),geom_type=geom_type)
# Add input Layer Fields to the output Layer if it is the one we want
for i in range(0, layerDefinition.GetFieldCount()):
fieldDefn = layerDefinition.GetFieldDefn(i)
#fieldName = fieldDefn.GetName()
out_lyr.CreateField(fieldDefn)
# Get the output Layer's Feature Definition
outLayerDefn = out_lyr.GetLayerDefn()
# Add features to the ouput Layer
for inFeature in daLayer:
# Create output Feature
outFeature = ogr.Feature(outLayerDefn)
# add in geometries
geom = inFeature.GetGeometryRef()
outFeature.SetGeometry(geom.Clone())
# Add new feature to output Layer
# Add field values from input Layer
for i in range(0, outLayerDefn.GetFieldCount()):
fieldDefn = outLayerDefn.GetFieldDefn(i)
#fieldName = fieldDefn.GetName()
outFeature.SetField(outLayerDefn.GetFieldDefn(i).GetNameRef(),
inFeature.GetField(i))
out_lyr.CreateFeature(outFeature)
#out_ds.Destroy()
if __name__ == "__main__":
print("WARNING: THIS SCRIPT IS DEPRECATED, AN EASIER VERSION OF IT IS NOW USABLE THROUGH Rasterization.py ACCESSIBLE FROM COMMAND LINE.")
#shapefile_name = '/home/smudd/SMMDataStore/analysis_for_papers/Geology_raster/bgs-50k_1726879/sc034/sc034_eyemouth_bedrock.shp'
#shapefile_name = 'T:\\analysis_for_papers\\Geology_raster\\bgs-50k_1726879\\sc034\\sc034_eyemouth_bedrock.shp'
#shapefile_name = '/home/smudd/SMMDataStore/analysis_for_papers/Iberia_geology/SouthernSpain_geology.shp'
#tifname = '/home/smudd/SMMDataStore/analysis_for_papers/Iberia_geology/SouthernSpain_geology_new2.tif'
shapefile_name = '/home/s1675537/PhD/LSDTopoData/Maxime/geographic_delimitation/geographic.shp'
#shapefile_name = 'C:\\VagrantBoxes\\LSDTopoTools\\Topographic_projects\\Iberia\\TipOfSpain.shp'
#new_shapefile_name = 'C:\\VagrantBoxes\\LSDTopoTools\\Topographic_projects\\Iberia\\New_TipOfSpain.shp'
#tifname = 'C:\\VagrantBoxes\\LSDTopoTools\\Topographic_projects\\Iberia\\TipOfSpain_new.tif'
new_shapefile_name, geol_dict = geologic_maps_modify_shapefile(shapefile_name, geol_field = "zone")
tifname = Rasterize_geologic_maps_pythonic(new_shapefile_name,raster_resolution = 30, geol_field = "zone")
Correct_Raterized_GLIM_map(tifname)
print("WARNING: THIS SCRIPT IS DEPRECATED, AN EASIER VERSION OF IT IS NOW USABLE THROUGH Rasterization.py ACCESSIBLE FROM COMMAND LINE.")
#