Skip to content

Commit

Permalink
move fn getGeoFileFromString and getGeoFileFromUser from getTarget.py…
Browse files Browse the repository at this point in the history
… to parseGeoTIFF.py
  • Loading branch information
mkrupczak3 committed Aug 16, 2022
1 parent 3a3936d commit fb5615d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/find_me_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from PIL import ImageTk

import parseImage
from parseGeoTIFF import getAltFromLatLon, binarySearchNearest
from parseGeoTIFF import getAltFromLatLon, binarySearchNearest, getGeoFileFromUser, getGeoFileFromString
from getTarget import *

def find_me_mode():
Expand Down
123 changes: 1 addition & 122 deletions src/getTarget.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
def getTarget():
print("Hello World!")
print("I'm getTarget.py")
elevationData, (x0, dx, dxdy, y0, dydx, dy) = getGeoFileFromUser()
elevationData, (x0, dx, dxdy, y0, dydx, dy) = parseGeoTIFF.getGeoFileFromUser()

print("The shape of the elevation data is: ", elevationData.shape)
print("The raw Elevation data is: ")
Expand Down Expand Up @@ -119,127 +119,6 @@ def getTarget():
# ANSI escape sequences \033[ for underlining: stackabuse.com/how-to-print-colored-text-in-python
print(f' Gauss-Krüger (meters): ZONE: {GK_zone} X: {int((targetSK42_N_GK - SK42_N_GK_10k_Grid)/100000)} \033[4m{SK42_N_GK_10k_Grid}\033[0;0m Y: {int((targetSK42_E_GK - SK42_E_GK_10k_Grid)/100000)} \033[4m{SK42_E_GK_10k_Grid}\033[0;0m Alt: \033[4m{targetSK42Alt}\033[0;0m')

"""get and open a geoFile named by a string
e.g. from a command line argument
if the name is invalid, exit with error
"""
def getGeoFileFromString(geofilename):
geofilename.strip()
# geoFile = gdal.Open(geofilename)
geoFile = GeoTiff(geofilename)
if geoFile is None:
outstr = f'FATAL ERROR: can\'t find file with name \'{geofilename}\''
sys.exit(outstr)

# band = geoFile.GetRasterBand(1)
# elevationData = band.ReadAsArray()
elevationData = geoFile.read()

try:
# convert to numpy array for drastic in-memory perf increase
elevationData = np.array(elevationData)
except MemoryError:
# it is possible, though highly unlikely,
# ...that a very large geotiff may exceed memory bounds
# this should only happen on 32-bit Python runtime
# or computers w/ very little RAM
#
# performance will be severely impacted
elevationData = None
elevationData = geodata.read()

x0 = geoFile.tifTrans.get_x(0,0)
dx = geoFile.tifTrans.get_x(1,0) - x0
y0 = geoFile.tifTrans.get_y(0,0)
dy = geoFile.tifTrans.get_y(0,1) - y0
# dxdy, dydx will be != 0 if image is rotated or skewed
# unfortunately, I can't figure out how to check this after switching from
# ...library 'gdal' to 'geotiff'
dxdy = dydx = 0
geoTransform = (x0, dx, dxdy, y0, dydx, dy)

return elevationData, geoTransform

"""prompt the user for the entry of a GeoTIFF filename
if filename is invalid, will re-prompt
until a valid file name is entered
returns 2D array elevationData and x and y parameters
"""
def getGeoFileFromUser():
print("Which GeoTiff file would you like to read?")
geoFile = None
while geoFile is None:
geofilename = str(input("Enter the GeoTIFF filename: "))
geofilename.strip()
if geofilename.isdecimal() or geofilename.isnumeric():
print(f'ERROR: filename {geofilename} does not contain at least 1 non-digit character')
print('Please try again')
continue
else:
try:
# geoFile = gdal.Open(geofilename) # old 'gdal' invocation
geoFile = GeoTiff(geofilename) # new 'geotiff' invocation
except:
print(f'ERROR: can\'t find file with name \'{geofilename}\'')
geoFile = None
print('Please try again')
continue
#

# band = geoFile.GetRasterBand(1)
# elevationData = band.ReadAsArray()

elevationData = geoFile.read()

try:
# convert to numpy array for drastic in-memory perf increase
elevationData = np.array(elevationData)
except MemoryError:
# it is possible, though highly unlikely,
# ...that a very large geotiff may exceed memory bounds
# this should only happen on 32-bit Python runtime
# or computers w/ very little RAM
#
# performance will be severely impacted
elevationData = None
elevationData = geodata.read()

x0 = geoFile.tifTrans.get_x(0,0)
dx = geoFile.tifTrans.get_x(1,0) - x0
y0 = geoFile.tifTrans.get_y(0,0)
dy = geoFile.tifTrans.get_y(0,1) - y0
# dxdy, dydx will be != 0 if image is rotated or skewed
# unfortunately, I can't figure out how to check this after switching from
# ...library 'gdal' to 'geotiff'
dxdy = dydx = 0
geoTransform = (x0, dx, dxdy, y0, dydx, dy)

return elevationData, geoTransform

# """check if a geoTiff is invalid, i.e. rotated or skewed
# Parameters
# ----------
# dxdy : float
# might be the rate of x change per unit y
# if this is not 0, we have a problem!
# dydx : float
# might be the rate of y change per unit x
# if this is not 0, we have a problem!
# """
# def ensureValidGeotiff(dxdy, dydx):
# # I'm making the assumption that the image isn't rotated/skewed/etc.
# # This is not the correct method in general, but let's ignore that for now
# # If dxdy or dydx aren't 0, then this will be incorrect
# # we cannot deal with rotated or skewed images in current version
# if dxdy != 0 or dydx != 0:
# outstr = "FATAL ERROR: GeoTIFF is rotated or skewed!"
# outstr += "\ncannot proceed with file: "
# outstr += geofilename
# print(outstr, file=sys.stderr)
# sys.exit(outstr)

"""handle user input of data, using message for prompt
guaranteed to return a float in range
Expand Down
123 changes: 123 additions & 0 deletions src/parseGeoTIFF.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,129 @@ def main():

# end main func

"""get and open a geoFile named by a string
e.g. from a command line argument
if the name is invalid, exit with error
"""
def getGeoFileFromString(geofilename):
geofilename.strip()
# geoFile = gdal.Open(geofilename)
geoFile = GeoTiff(geofilename)
if geoFile is None:
outstr = f'FATAL ERROR: can\'t find file with name \'{geofilename}\''
sys.exit(outstr)

# band = geoFile.GetRasterBand(1)
# elevationData = band.ReadAsArray()
elevationData = geoFile.read()

try:
# convert to numpy array for drastic in-memory perf increase
elevationData = np.array(elevationData)
except MemoryError:
# it is possible, though highly unlikely,
# ...that a very large geotiff may exceed memory bounds
# this should only happen on 32-bit Python runtime
# or computers w/ very little RAM
#
# performance will be severely impacted
elevationData = None
elevationData = geodata.read()

x0 = geoFile.tifTrans.get_x(0,0)
dx = geoFile.tifTrans.get_x(1,0) - x0
y0 = geoFile.tifTrans.get_y(0,0)
dy = geoFile.tifTrans.get_y(0,1) - y0
# dxdy, dydx will be != 0 if image is rotated or skewed
# unfortunately, I can't figure out how to check this after switching from
# ...library 'gdal' to 'geotiff'
dxdy = dydx = 0
geoTransform = (x0, dx, dxdy, y0, dydx, dy)

return elevationData, geoTransform

"""prompt the user for the entry of a GeoTIFF filename
if filename is invalid, will re-prompt
until a valid file name is entered
returns 2D array elevationData and x and y parameters
"""
def getGeoFileFromUser():
print("Which GeoTiff file would you like to read?")
geoFile = None
while geoFile is None:
geofilename = str(input("Enter the GeoTIFF filename: "))
geofilename.strip()
if geofilename.isdecimal() or geofilename.isnumeric():
print(f'ERROR: filename {geofilename} does not contain at least 1 non-digit character')
print('Please try again')
continue
else:
try:
# geoFile = gdal.Open(geofilename) # old 'gdal' invocation
geoFile = GeoTiff(geofilename) # new 'geotiff' invocation
except:
print(f'ERROR: can\'t find file with name \'{geofilename}\'')
geoFile = None
print('Please try again')
continue
#

# band = geoFile.GetRasterBand(1)
# elevationData = band.ReadAsArray()

elevationData = geoFile.read()

try:
# convert to numpy array for drastic in-memory perf increase
elevationData = np.array(elevationData)
except MemoryError:
# it is possible, though highly unlikely,
# ...that a very large geotiff may exceed memory bounds
# this should only happen on 32-bit Python runtime
# or computers w/ very little RAM
#
# performance will be severely impacted
elevationData = None
elevationData = geodata.read()

x0 = geoFile.tifTrans.get_x(0,0)
dx = geoFile.tifTrans.get_x(1,0) - x0
y0 = geoFile.tifTrans.get_y(0,0)
dy = geoFile.tifTrans.get_y(0,1) - y0
# dxdy, dydx will be != 0 if image is rotated or skewed
# unfortunately, I can't figure out how to check this after switching from
# ...library 'gdal' to 'geotiff'
dxdy = dydx = 0
geoTransform = (x0, dx, dxdy, y0, dydx, dy)

return elevationData, geoTransform

# """check if a geoTiff is invalid, i.e. rotated or skewed
# Parameters
# ----------
# dxdy : float
# might be the rate of x change per unit y
# if this is not 0, we have a problem!
# dydx : float
# might be the rate of y change per unit x
# if this is not 0, we have a problem!
# """
# def ensureValidGeotiff(dxdy, dydx):
# # I'm making the assumption that the image isn't rotated/skewed/etc.
# # This is not the correct method in general, but let's ignore that for now
# # If dxdy or dydx aren't 0, then this will be incorrect
# # we cannot deal with rotated or skewed images in current version
# if dxdy != 0 or dydx != 0:
# outstr = "FATAL ERROR: GeoTIFF is rotated or skewed!"
# outstr += "\ncannot proceed with file: "
# outstr += geofilename
# print(outstr, file=sys.stderr)
# sys.exit(outstr)


"""given a latitude and longitude, obtain the altitude (elevation)
from the nearest point
Expand Down
2 changes: 1 addition & 1 deletion src/parseImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
# except ImportError:
# import xml.etree.ElementTree as ET

from parseGeoTIFF import getAltFromLatLon, binarySearchNearest
from parseGeoTIFF import getAltFromLatLon, binarySearchNearest, getGeoFileFromUser, getGeoFileFromString
from getTarget import *

from WGS84_SK42_Translator import Translator as converter # rafasaurus' SK42 coord translator
Expand Down

0 comments on commit fb5615d

Please sign in to comment.