def download_area(self, srtm_model=1): if self.interactive: input_information = self._get_input_data() else: input_information = [self.data_dict[srtm_model], self.destination_points, self.destination_folder] elevation.clip(product=input_information[0], bounds=tuple(input_information[1]), output=input_information[2]) # clean up stale temporary files and fix the cache in the event of a server error elevation.clean()
def download(self, cleanup=True): """Download the SRTM data in GeoTIFF format""" dpath = os.path.dirname(self.tiffdata) if not os.path.isdir(dpath): print('Creating path', dpath) os.makedirs(dpath) elevation.clip(self.bounds, product=self.product, output=self.tiffdata) if cleanup: elevation.clean()
def getTif(minX, maxX, minY, maxY, fname): if minX > maxX or minY > maxY: print('') print('**Error: Check Your latitude and longitude bounds!**') print('') elif round(maxX - minX, 3) != round(maxY - minY, 3): print('') print('**Error: Your bounds must be a square region!**') print('') else: dem_path = fname output = os.getcwd() + dem_path elevation.clip(bounds=(minX, minY, maxX, maxY), output=output) elevation.clean() return (output, minX, maxX, minY, maxY)
def export( self, region_name: str = "kenya", product: str = "SRTM3", max_download_tiles: int = 15, ) -> None: """ Export SRTm topography data Arguments ---------- region_name: str = 'kenya' The region to download. Must be one of the regions in the region_lookup dictionary product: {'SRTM1', 'SRTM3'} = 'SRTM3' The product to download the data from max_download_tiles: int = 15 By default, the elevation package doesn't allow more than 9 tiles to be downloaded. Kenya is 12 tiles - this increases the limit to allow Kenya to be downloaded """ region = region_lookup[region_name] output_tif = self.output_folder / f"{region_name}.tif" if not output_tif.exists(): print(f"Downloading tiles. Saving as tif to {output_tif}") try: elevation.clip( # type: ignore bounds=self._region_to_tuple(region), output=output_tif.resolve().as_posix(), product=product, max_download_tiles=max_download_tiles, margin="1", ) except Exception as e: print(e) elevation.clean() # type: ignore output_nc = self.output_folder / f"{region_name}.nc" if not output_nc.exists(): print(f"Converting {output_tif} to NetCDF format ({output_nc})") self._tif_to_nc(output_tif, output_nc)
def export( self, region_name: str = "kenya", product: str = "SRTM3", max_download_tiles: int = 15, ) -> None: r"""Export SRTm topography data :param region_name: Defines a geographical subset of the downloaded data to be used. Should be one of the regions defined in src.utils.region_lookup. Default = ``"kenya"``. :param product: One of ``{"SRTM1", "SRTM3"}``, the product to download the data from. Default = ``"SRTM3"``. :param max_download_tiles: By default, the elevation package doesn't allow more than 9 tiles to be downloaded. Kenya is 12 tiles - this increases the limit to allow Kenya to be downloaded. Default = ``15``. """ region = region_lookup[region_name] output_tif = self.output_folder / f"{region_name}.tif" if not output_tif.exists(): print(f"Downloading tiles. Saving as tif to {output_tif}") try: elevation.clip( # type: ignore bounds=self._region_to_tuple(region), output=output_tif.resolve().as_posix(), product=product, max_download_tiles=max_download_tiles, margin="1", ) except Exception as e: print(e) elevation.clean() # type: ignore output_nc = self.output_folder / f"{region_name}.nc" if not output_nc.exists(): print(f"Converting {output_tif} to NetCDF format ({output_nc})") self._tif_to_nc(output_tif, output_nc)
def get_ned_elevation_raster(extent, res=250, **kwargs): """ Wrapper for elevation.clip that will generate a GeoTIFF file for a given project extent. :param output: Path to output file. Existing files will be overwritten. :param cache_dir: Root of the DEM cache folder. :param product: DEM product choice. """ PRODUCT = {"30": elevation.PRODUCTS[0], "250": elevation.PRODUCTS[1]} logger.debug("Fetching NED raster data for project region extent") if kwargs.get("output") is None: raise AttributeError("output= argument is required by " + "elevation.clip() and cannot be None") if os.path.isfile(kwargs.get("output")): logger.warning("Warning : It looks like output=" + kwargs.get("output") + " already exists... skipping.") else: elevation.clip(bounds=extent, product=PRODUCT[str(res)], **kwargs) elevation.clean()
import os import elevation # create out folder out_path = os.path.join(os.getcwd(), "tmp") os.makedirs(out_path, exist_ok=True) # Because we can't request large areas all at once, # loop through 1 degree tiles along 49th parallel for i, xmin in enumerate(range(-123, -113)): output = os.path.join(out_path, 'mapzen_{}.tif'.format(i)) elevation.clip(bounds=(xmin, 48, xmin + 1, 49.1), output=output) # clean up stale temporary files and fix the cache in the event of a ver error elevation.clean()
def elevation_generator(site_lon, site_lat, res): bbox = np.array( [site_lon - res, site_lat - res, site_lon + res, site_lat + res]) ######################################################################## ### Build latitude and longitude grid for GOES-16 ######################################################################## t = time.time() goes_grid_file = os.getcwd() + '/aux/GOESR_ABI_CONUS_East.nc' grid_dataset = Dataset(goes_grid_file) lats, lons = grid_dataset.variables[ 'Latitude'][:].data, grid_dataset.variables['Longitude'][:].data # city bounds indices based on lat/lon bbox nc_indx_corners = np.array([ np.unravel_index( np.argmin( np.abs(np.subtract(lons, bbox[0])) + np.abs(np.subtract(lats, bbox[1]))), np.shape(lats)), np.unravel_index( np.argmin( np.abs(np.subtract(lons, bbox[0])) + np.abs(np.subtract(lats, bbox[3]))), np.shape(lats)), np.unravel_index( np.argmin( np.abs(np.subtract(lons, bbox[2])) + np.abs(np.subtract(lats, bbox[1]))), np.shape(lats)), np.unravel_index( np.argmin( np.abs(np.subtract(lons, bbox[2])) + np.abs(np.subtract(lats, bbox[3]))), np.shape(lats)) ]) # clip indices for lat/lon of city and GOES-16 data nc_indx_bounds = [ np.min([ nc_indx_corners[0][0], nc_indx_corners[1][0], nc_indx_corners[2][0], nc_indx_corners[3][0] ]), np.max([ nc_indx_corners[0][0], nc_indx_corners[1][0], nc_indx_corners[2][0], nc_indx_corners[3][0] ]), np.min([ nc_indx_corners[0][1], nc_indx_corners[1][1], nc_indx_corners[2][1], nc_indx_corners[3][1] ]), np.max([ nc_indx_corners[0][1], nc_indx_corners[1][1], nc_indx_corners[2][1], nc_indx_corners[3][1] ]) ] # Actual GOES-16 coordinate grid lon_plot = lons[nc_indx_bounds[0]:nc_indx_bounds[1], nc_indx_bounds[2]:nc_indx_bounds[3]] lat_plot = lats[nc_indx_bounds[0]:nc_indx_bounds[1], nc_indx_bounds[2]:nc_indx_bounds[3]] print('--------------------------------------------------') print("Lat/lon data pull and clip elapsed time: %.2f s" % (time.time() - t)) print('--------------------------------------------------') ######################################################################## # Get elevation data # From STM DEM # From elevation api STRM3 = 90m resolution elevation DEM ######################################################################## t = time.time() elevation.clip(bounds=bbox, output=(os.getcwd().replace(' ', '\ ')) + '/elevation_tifs/' + site_name.lower().replace(' ', '_') + '.tif', product='SRTM1') elevation.clean() dem_raster = rasterio.open('./elevation_tifs/' + site_name.lower().replace(' ', '_') + '.tif') src_crs = dem_raster.crs # get projection info utm = pyproj.Proj(src_crs) # Pass CRS of image from rasterio lonlat = pyproj.Proj(init='epsg:4326') src_shape = src_height, src_width = dem_raster.shape T0 = rasterio.transform.from_bounds(bbox[0], bbox[1], bbox[2], bbox[3], src_width, src_height) x_ll, y_ll = T0 * ((pyproj.transform(lonlat, utm, bbox[0], bbox[1]))) x_ul, y_ul = T0 * ((pyproj.transform(lonlat, utm, bbox[0], bbox[3]))) x_lr, y_lr = T0 * ((pyproj.transform(lonlat, utm, bbox[2], bbox[1]))) x_ur, y_ur = T0 * ((pyproj.transform(lonlat, utm, bbox[2], bbox[3]))) x_span = [x_ll, x_ul, x_lr, x_ur] y_span = [y_ll, y_ul, y_lr, y_ur] cols, rows = np.meshgrid(np.arange(src_width), np.arange(src_height)) eastings, northings = T0 * (cols, rows) cols, rows = [], [] # transformation from raw projection to WGS84 lat/lon values xvals, yvals = (eastings, northings) elev = dem_raster.read(1) # actual elevation data from DEM lats_elev, lons_elev = (pyproj.transform(utm, lonlat, xvals, yvals) ) # actual lats/lons for elevation data elev = elev.ravel() # ravel the elevation for processing lats_elev = lats_elev.ravel() # ravel for processing lons_elev = lons_elev.ravel() # ravel for processing # this is somewhat of a computationally expensive process, but it finds the elevation nearest to each GOES-16 pixel goes_elev = [elev[np.argmin(np.abs(np.subtract(ii,lons_elev))+np.abs(np.subtract(jj,\ lats_elev)))] for ii,jj in zip(lon_plot.ravel(),lat_plot.ravel())] goes_elev = np.reshape(goes_elev, np.shape(lat_plot)) # reshape to match GOES-16 grid os.remove('./elevation_tifs/' + site_name.lower().replace(' ', '_') + '.tif') # remove the .tif file csv_fname = site_name + '_' + str(site_lat) + '_' + str( site_lon) + '_elevation.csv' np.savetxt(os.getcwd() + '/elevation_data/' + csv_fname, goes_elev, delimiter=",") print('--------------------------------------------------') print("Elevation data access time: %.2f s" % (time.time() - t)) print('--------------------------------------------------')
def elevation_generator(lats, lons, domain): t = time.time() # Define custom file name segment with bounding coordinates (NW and SE corners) domain_name = str(domain[1]) + 'N_' + str(domain[2]) + 'W_' + str( domain[0]) + 'N_' + str(domain[3]) + 'W' # Define names to dependent directories # (one for rasterio .tifs, the other for corresponding CSV data) tif_dir, csv_dir = 'elevation_tifs/', 'elevation_csvs/' tif_path = os.path.join(os.path.dirname(__file__), tif_dir) csv_path = os.path.join(os.path.dirname(__file__), csv_dir) # Create the file name for the output .csv file csv_fname = domain_name + '_elevation.csv' # Create new elevation .tif and .csv if the file doesn't already exist for this domain if not os.path.isfile(os.path.join(csv_path, csv_fname)): # If directories don't exist, create them if not os.path.isdir(tif_path): os.mkdir(tif_path) if not os.path.isdir(csv_path): os.mkdir(csv_path) # Customized domain list to match elevation package convention elev_domain = [domain[2], domain[0], domain[3], domain[1]] # Retrieve elevation data from SRTM and return a .tif of the spatial domain. # If SRTM3 (90m resolution) doesn't work, try SRTM1 (30m resolution) try: print('\t Using SRTM3...') elevation.clip(bounds=elev_domain, output=(tif_path + domain_name + '.tif'), product='SRTM3') except: print('\t Using SRTM1, will take longer than SRTM3...') elevation.clip(bounds=elev_domain, output=(tif_path + domain_name + '.tif'), product='SRTM1') elevation.clean() # Raster image processing dem_raster = rasterio.open(tif_path + domain_name + '.tif') src_crs = dem_raster.crs # Get projection info utm = pyproj.Proj(src_crs) # Pass CRS of image from rasterio lonlat = pyproj.Proj(init='epsg:4326') src_height, src_width = dem_raster.shape T0 = rasterio.transform.from_bounds(domain[2], domain[0], domain[3], domain[1], src_width, src_height) x_ll, y_ll = T0 * ( (pyproj.transform(lonlat, utm, domain[0], domain[1]))) x_ul, y_ul = T0 * ( (pyproj.transform(lonlat, utm, domain[0], domain[3]))) x_lr, y_lr = T0 * ( (pyproj.transform(lonlat, utm, domain[2], domain[1]))) x_ur, y_ur = T0 * ( (pyproj.transform(lonlat, utm, domain[2], domain[3]))) cols, rows = np.meshgrid(np.arange(src_width), np.arange(src_height)) eastings, northings = T0 * (cols, rows) cols, rows = [], [] # Transformation from raw projection to WGS84 lat/lon values xvals, yvals = (eastings, northings) # Retrieve actual elevation data and corresponding coordinates from DEM elev = dem_raster.read(1) lats_elev, lons_elev = (pyproj.transform(utm, lonlat, xvals, yvals)) # Ravel each dataset for processing elev = elev.ravel() lats_elev = lats_elev.ravel() lons_elev = lons_elev.ravel() # Find the elevation value closes to each GOES-16 pixel goes_elev = [ elev[np.argmin( np.abs(np.subtract(ii, lons_elev)) + np.abs(np.subtract(jj, lats_elev)))] for ii, jj in zip(lons.ravel(), lats.ravel()) ] # Reshape data to match GOES-16 grid goes_elev = np.reshape(goes_elev, np.shape(lats)) # Remove the .tif file os.remove(tif_path + domain_name + '.tif') # Save the .csv to the corresponding directory np.savetxt(os.path.join(csv_path, csv_fname), goes_elev, delimiter=",") return goes_elev runtime = time.time() - t
def clean(**kwargs): elevation.clean(**kwargs)
def clean(**kwargs): elevation.clean(**kwargs)
def clean(ctx, **kwargs): if ctx.parent and ctx.parent.params: kwargs.update(ctx.parent.params) elevation.clean(**kwargs)
def landsat_dl(name, output, xmin, ymin, xmax, ymax): # clip the SRTM1 30m DEM of Rome and save it to Rome-DEM.tif elevation.clip(bounds=(xmin, ymin, xmax, ymax), output=output) # clean up stale temporary files and fix the cache in the event of a server error elevation.clean() return 'download/%s' % name