def test_srtm3_height_map_collection_load_area(): collection = Srtm3HeightMapCollection() collection.build_file_index() collection.load_area( RasterBaseCoordinates.from_file_name("N38W006"), RasterBaseCoordinates.from_file_name("N40W008"), ) loaded_height_maps = [ hm for hm in collection.height_maps.values() if hm.raster ] assert len(loaded_height_maps) == 9
def __init__(self, path: Path, base_coordinates: RasterBaseCoordinates = None): self.path = path self.base_coordinates = (base_coordinates or RasterBaseCoordinates.from_file_path(path)) # We subtract one as each row overlaps the neighbouring raster by 1 pixel self.pixel_width = 1 / (self.values_per_row - 1)
def get_height_map_for_latitude_and_longitude( self, latitude: float, longitude: float) -> HeightMap: """Get the HeightMap for the given latitude and longitude""" base = RasterBaseCoordinates.from_float(latitude, longitude) try: return self.height_maps[base] except KeyError: raise Exception( f"Height map for {base} not found. Have you called " f"build_file_index() on your heightmap collection?")
def build_file_index(self): """Load an index of all available files This reads file names, but does not load the contained data. This is lazy-loaded on demand """ self.height_maps = {} for hgt_path in self.hgt_dir.glob("**/*.hgt*"): hgt_name = hgt_path.name.split(".")[0] self.height_maps[RasterBaseCoordinates.from_file_name( hgt_name)] = self.height_map_class(path=hgt_path)
def get_height_map_for_latitude_and_longitude( self, latitude: float, longitude: float) -> HeightMap: """Get the HeightMap for the given latitude and longitude""" base = RasterBaseCoordinates.from_float(latitude, longitude) try: return self.height_maps[base] except KeyError: raise NoHeightMapDataException( f"Height map for {base} not found. Either your the SRTM directory " f"'{self.hgt_dir}' is missing files, or you have set auto_build_index=False " f"and therefore need to manually call build_file_index(). It is probably " f"the former.")
def test_raster_base_coordinates_from_file_name_sw(): assert RasterBaseCoordinates.from_file_name("S40W005") == (-40, -5)
def test_raster_base_coordinates_from_file_name_ne(): assert RasterBaseCoordinates.from_file_name("N40E005") == (40, 5)
def test_raster_base_coordinates_zero(): assert (RasterBaseCoordinates.from_float( latitude=0.1, longitude=0.1, ).file_name == "N00E000")
def test_raster_base_coordinates_ne(): assert (RasterBaseCoordinates.from_float( latitude=40.1, longitude=5.1, ).file_name == "N40E005")
def test_raster_base_coordinates_sw(): assert (RasterBaseCoordinates.from_float( latitude=-7.956037, longitude=-14.357948, ).file_name == "S08W015")
def test_raster_base_coordinates_se(): assert (RasterBaseCoordinates.from_float( latitude=-33.768549, longitude=18.504695, ).file_name == "S34E018")
def test_raster_base_coordinates_nw(): assert (RasterBaseCoordinates.from_float( latitude=40.1, longitude=-7.1, ).file_name == "N40W008")