I have a python code :

import gdal
import numpy
from skimage.filters import threshold_otsu
ds = gdal.Open('A:\\algo\\f2.tif')
band = ds.GetRasterBand(1)
arr = band.ReadAsArray()
thresh = threshold_otsu(arr,16)
binary = arr > thresh
driver = gdal.GetDriverByName("GTiff")
outdata = driver.Create("A:\\algo\\test11.tif", 14823, 9985, 1, gdal.GDT_UInt16)
outdata.SetGeoTransform(ds.GetGeoTransform())
outdata.SetProjection(ds.GetProjection())
outdata.GetRasterBand(1).WriteArray(binary)
outdata.GetRasterBand(1).SetNoDataValue(10000)
outdata.FlushCache() ##saves to disk!!
outdata = None
band=None
ds=None

In this code, line 4 gives the location/path of "input file" and line 10 gives the location of "output file".

I want to create a user interface to give this location in the user interface itself and run the code from user interface itself.

I have tried making a user interface using "tkinter" module :

from tkinter import *
from tkinter import filedialog
def input():
    file1 = filedialog.askopenfile()
    label = Label(text=file1).pack()
def input2():
    file2 = filedialog.asksaveasfile(mode="w", defaultextension=".tif")
    label = Label(text=file2).pack()    
w = Tk()
w.geometry("500x500")
w.title("FLOOD_MAPPER")
h = Label(text = "S1A FLOOD MAPPER", bg = "yellow", fg = "black", height = "3", width = "500")
h.pack()
i1 = Label(text = "Input*")
i1.place(x=10, y=70)
i1b = Button(w, text = "Select File", command =input)
i1b.place(x=250, y=70)
i2 = Label(text = "Intermediate Product*")
i2.place(x=10, y=140)
i2b = Button(w, text = "Save as", command =input2)
i2b.place(x=250, y=140)
button = Button(w, text="Generate Map", bg = "red", fg = "black", height = "2", width="30")
button.place(x=150, y=400)
w.mainloop()

But I didn't understand how to link these two codes.

The moment I click on button "generate map" in the user interface I want the location/path of Input and output given in the user interface box to move to their respective places in the 1st code and then run the same code aumoatically.

Kindly, help me to achieve my requirement.

1 Answers

1
furas On Best Solutions

It can look like this. I removed keep only important elements in tkinter.

I put code in your_code and it can get filenames as paramaters. So this code looks similar as before.

I create function gen_map which get run your_code with filenames which are assigned to global variables input_filename, `output_filename.

I assing gen_map to button Button( command=gen_map) so it will run it when you press button.

Other buttons open dialog to get file names and assign to global variables input_filename, output_filename.

from tkinter import *
from tkinter import filedialog

import gdal
import numpy
from skimage.filters import threshold_otsu

def your_code(input_file, output_file):

    #ds = gdal.Open('A:\\algo\\f2.tif')

    ds = gdal.Open(input_file)

    band = ds.GetRasterBand(1)
    arr = band.ReadAsArray()
    thresh = threshold_otsu(arr,16)
    binary = arr > thresh
    driver = gdal.GetDriverByName("GTiff")

    #outdata = driver.Create("A:\\algo\\test11.tif", 14823, 9985, 1, gdal.GDT_UInt16)
    outdata = driver.Create(output_file, 14823, 9985, 1, gdal.GDT_UInt16)

    outdata.SetGeoTransform(ds.GetGeoTransform())
    outdata.SetProjection(ds.GetProjection())
    outdata.GetRasterBand(1).WriteArray(binary)
    outdata.GetRasterBand(1).SetNoDataValue(10000)
    outdata.FlushCache() ##saves to disk!!

    #outdata = None
    #band = None
    #ds = None

def get_input_filename():
    global input_filename

    # `askopenfilename` instead of `askopenfile` to get filename instead of object file
    input_filename = filedialog.askopenfilename()
    input_label['text'] = input_filename

def get_output_filename():
    global output_filename

    # `asksaveasfilename` instead of `asksaveasfile` to get filename instead of object file
    output_filename = filedialog.asksaveasfilename(defaultextension=".tif")
    output_label['text'] = output_filename

def gen_map():
    #global input_filename
    #global output_filename
    print('input:', input_filename)
    print('output:', output_filename)

    your_code(input_filename, output_filename)

#---------------------------------------------
# global variables with default values at start

input_filename = 'A:\\algo\\f2.tif'
output_filename = "A:\\algo\\test11.tif"

root = Tk()

#input_label = Label(root, text=input_filename)
input_label = Label(root, text="Input*")
input_label.pack()

input_button = Button(root, text="Select File", command=get_input_filename)
input_button.pack()

#output_label = Label(root, text=output_filename)
output_label = Label(root, text="Intermediate Product*")
output_label.pack()

output_button = Button(root, text="Save as", command=get_output_filename)
output_button.pack()

gen_map_button = Button(root, text="Generate Map", command=gen_map)
gen_map_button.pack()

root.mainloop()