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 geol_field = geol_field # 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 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 = str(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) print("HELLO") 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() geol_field = geol_field 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, 'w') 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 rasterisation(shapefile_name, shapefile_name_b, raster_resolution=400): # 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) # now get the the fields from the shapefile #opening shapefile_a daShapefile = shapefile_name dataSource = ogr.Open(daShapefile) print(daShapefile) daLayer = dataSource.GetLayer(0) #opening shapefile_b daShapefile_b = shapefile_name_b dataSource_b = ogr.Open(daShapefile_b) print(daShapefile_b) daLayer_b = dataSource_b.GetLayer(0) # The raster file to be created and receive the rasterized shapefile # Creating output raster outrastername = shapefileshortname + '.tif' outraster = shapefilefilepath + os.sep + outrastername print("Full name of out raster is: " + outraster) # Create the destination data source #21/08 This datasource size is based on the two input shapefiles inGridSize = float(raster_resolution) xMin_a, xMax_a, yMin_a, yMax_a = daLayer.GetExtent() xMin_b, xMax_b, yMin_b, yMax_b = daLayer_b.GetExtent() ### getting extents to accomodate larger raster than extents of both input shapefiles if xMin_a < xMin_b: xMin = xMin_a else: xMin = xMin_b if yMin_a < yMin_b: yMin = yMin_a else: yMin = yMin_b if xMax_a > xMax_b: xMax = xMax_a else: xMax = xMax_b if yMax_a > yMax_b: yMax = yMax_a else: yMax = yMax_b print daLayer.GetExtent() print daLayer_b.GetExtent() ##new shapefile #21/08 This shapefile provides and unrasterised record of the output basins driver = ogr.GetDriverByName("ESRI Shapefile") data_source = driver.CreateDataSource(directory + "output.shp") #Getting srs from input shapefile_a srs = daLayer.GetSpatialRef() layer = data_source.CreateLayer("output", srs, ogr.wkbPolygon) definitions = [] layerDefinition_b = daLayer.GetLayerDefn() for i in range(layerDefinition_b.GetFieldCount()): definition = layerDefinition_b.GetFieldDefn(i) definitions.append(definition) for definition in definitions: layer.CreateField(definition) features, area_list = featuresToList(directory) print "length", len(features) for feature in features: layer.CreateFeature(feature) layerDefinition = layer.GetLayerDefn() ### getting extents to accomodate larger raster xMin, xMax, yMin, yMax = layer.GetExtent() ###extent is correct #Calculating x and y resolution xRes = int((xMax - xMin) / inGridSize) yRes = int((yMax - yMin) / inGridSize) #21/08 creating raster output rasterDS = gdal.GetDriverByName('GTiff'.encode('utf-8')).Create( directory + "output6.tif", xRes, yRes, 1, gdal.GDT_Float32) # Define spatial reference # OK as all srs have been confirmed equal in projection-debug.py 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 x_i = 0 area_list.sort(reverse=True) for area in area_list: if area != -9999.99: #Execute queries(SQL) testSQL = "SELECT * FROM output WHERE AREA='%s'" % (area) print "area", area temp = data_source.ExecuteSQL(testSQL) x_i = x_i + 1 gdal.RasterizeLayer(rasterDS, [1], temp, options=["ATTRIBUTE=EBE_MMKYR"]) #closing raster object data_source = None print("done")