Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in the Tiles class. #8

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
black formatter
  • Loading branch information
bbonenfant committed Mar 2, 2023
commit b2b791a62e03e6b1c9b133210c4266d2e7ea3033
47 changes: 22 additions & 25 deletions dted/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
from .latlon import LatLon


class Tiles():
'''
class Tiles:
"""
Holds a dictionary of Tile objects

Allows the user to specify a directory to
load files from and access tiles.
'''
def __init__(self, in_memory: bool=True, dted_level: int=None):
'''
"""

def __init__(self, in_memory: bool = True, dted_level: int = None):
"""
Initializes the Tiles class

Args:
Expand All @@ -25,39 +26,37 @@ def __init__(self, in_memory: bool=True, dted_level: int=None):
to which level of dted you will search for in your
directory. if this is not set it will load both level 1
and 2.
'''
"""
self.in_memory = in_memory
self.dted_level = dted_level
self.tile_map = {}


def load_tiles_from_dir(self, folder: str) -> bool:
'''
"""
makes tile objects from passed in folder of dted
and stores into a dictionary. This also works recursively.

Args:
folder: the string containing a directory of dted
files that you would like to load.
'''
"""
if not os.path.isdir(folder):
print('passed in folder does not exist')
print("passed in folder does not exist")
return False

if self.dted_level:
search = f'{folder}/**/*.dt{self.dted_level}'
search = f"{folder}/**/*.dt{self.dted_level}"
else:
search = f'{folder}/**/*.dt*'
search = f"{folder}/**/*.dt*"

for filepath in glob.glob(search, recursive=True):
tile = Tile(Path(filepath), in_memory=self.in_memory)
key = self._get_dict_key_from_ll(tile.dsi.south_west_corner)
self.tile_map[f'{key}'] = tile
self.tile_map[f"{key}"] = tile
return True


def get_tile(self, latlon: LatLon) -> Tile:
'''
"""
Gets a Tile object from the tile dictionary.

Args:
Expand All @@ -66,15 +65,14 @@ def get_tile(self, latlon: LatLon) -> Tile:
Returns:
Tile object if tile is present
None if no tile present
'''
"""
key = self._get_dict_key_from_ll(latlon)
if key in self.tile_map:
return self.tile_map[f'{key}']
return self.tile_map[f"{key}"]
return None


def get_elevation(self, latlon: LatLon) -> float:
'''
"""
Lookup the terrain elevation at the specified location.

This will return the elevation of the explicitly defined DTED point
Expand All @@ -87,21 +85,20 @@ def get_elevation(self, latlon: LatLon) -> float:
Raises:
NoElevationDataError: If the specified location is not contained within the
DTED file.
'''
"""
key = self._get_dict_key_from_ll(latlon)
if key in self.tile_map:
tile = self.tile_map[f'{key}']
tile = self.tile_map[f"{key}"]
if tile:
return tile.get_elevation(latlon)
return None


def _get_dict_key_from_ll(self, latlon: LatLon) -> str:
'''
"""
Create the key for the dictionary based on a LatLon
object.

Args:
latlon: the lat lon which lies within the dted file
'''
return f'{int(math.floor(latlon.latitude))}{int(math.floor(latlon.longitude))}'
"""
return f"{int(math.floor(latlon.latitude))}{int(math.floor(latlon.longitude))}"