Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_from_bounds_two():
width = 80
height = 80
left = -120
top = 70
right = -80.5
bottom = 30.5
tr = transform.from_bounds(left, bottom, right, top, width, height)
# pixelwidth, rotation, ULX, rotation, pixelheight, ULY
expected = Affine(0.49375, 0.0, -120.0, 0.0, -0.49375, 70.0)
assert [round(v, 7) for v in tr] == [round(v, 7) for v in expected]
# Round right and bottom
right = -80
bottom = 30
tr = transform.from_bounds(left, bottom, right, top, width, height)
# pixelwidth, rotation, ULX, rotation, pixelheight, ULY
expected = Affine(0.5, 0.0, -120.0, 0.0, -0.5, 70.0)
assert [round(v, 7) for v in tr] == [round(v, 7) for v in expected]
# #Check Only if ROW is the same (same date)
# all_pr = ['{:03d},{:03d}'.format(int(i['path']),int(i['row'])) for i in proc_images]
# all_row = [i['row'] for i in proc_images]
# if len(list(set(all_row))) > 1:
# print '''AOI covering more than one Row :
# Please choose one of the following: {}
# Using --path_row option'''.format(' | '.join(list(set(all_pr))))
# sys.exit(1)
#Output transform
aoi_bounds = utils.feat_to_bounds(aoi_wgs84) # (minx, miny, maxx, maxy)
width = int((aoi_bounds[2] - aoi_bounds[0]) / float(res))
height = int((aoi_bounds[3] - aoi_bounds[1]) / float(res))
dst_affine = transform.from_bounds(*aoi_bounds, width, height)
l8_images = []
for scene in scenes_params:
landsat_address = 's3://landsat-pds/{}'.format(scene['key'])
if ndvi:
meta_data = utils.landsat_get_mtl(scene['scene'])
band_address = '{}_B4.TIF'.format(landsat_address)
with VirtualWarpedFile(band_address,
dst_crs='EPSG:3857',
resampling=Resampling.bilinear).open() as src:
window = src.window(*aoi_bounds, boundless=True)
def get_resolution(bounds, dims):
height, width = dims
t = transform.from_bounds(*bounds.bounds, width=width, height=height)
return abs(t.a), abs(t.e)
window_data = read_window(src, bounds_4326)
# reproject data into Skadi's CRS
projected_data = reproject(window_data, WEB_MERCATOR_CRS)
# paste the resulting data into a common array
data = paste(projected_data, data, bounds_4326)
((left, right), (bottom, top)) = warp.transform(SKADI_CRS, WEB_MERCATOR_CRS, bounds_4326[::2], bounds_4326[1::2])
bounds_3857 = (left, bottom, right, top)
profile = src.profile
profile.update({
'crs': WEB_MERCATOR_CRS,
'dtype': data.dtype,
'transform': transform.from_bounds(*bounds_4326, width=TARGET_WIDTH, height=TARGET_HEIGHT),
'width': TARGET_WIDTH,
'height': TARGET_HEIGHT,
'nodata': data.fill_value,
})
with rasterio.open('tmp/3857.tif', 'w', **profile) as out:
out.write(data.filled())
nw_merc_x, nw_merc_y = lnglat2merc(min_lng, max_lat)
left_pix_offset, top_pix_offset = merc2pixel(xmin, ymin, zoom, nw_merc_x,
nw_merc_y)
se_merc_x, se_merc_y = lnglat2merc(max_lng, min_lat)
se_left_pix_offset, se_top_pix_offset = merc2pixel(xmax, ymax, zoom,
se_merc_x, se_merc_y)
right_pix_offset = tile_sz - se_left_pix_offset
bottom_pix_offset = tile_sz - se_top_pix_offset
uncropped_height = tile_sz * (ymax - ymin + 1)
uncropped_width = tile_sz * (xmax - xmin + 1)
height = uncropped_height - top_pix_offset - bottom_pix_offset
width = uncropped_width - left_pix_offset - right_pix_offset
transform = rasterio.transform.from_bounds(nw_merc_x, se_merc_y, se_merc_x,
nw_merc_y, width, height)
with rasterio.open(
output_path,
'w',
driver='GTiff',
height=height,
width=width,
count=3,
crs='epsg:3857',
transform=transform,
dtype=rasterio.uint8) as dataset:
out_x = 0
for xi, x in enumerate(range(xmin, xmax + 1)):
tile_xmin, tile_xmax = 0, tile_sz - 1
if x == xmin:
tile_xmin += left_pix_offset
self.lon_spacing = lon_spacing
self.lat_spacing = lat_spacing
self.lon = lon
self.lat = lat
mtx = np.empty((n_lat, n_lon), dtype=np.float32)
rasterio.warp.reproject(
rasterio.band(args.raster, 1),
mtx,
src_transform = args.raster.affine,
src_crs = args.raster.crs,
dst_crs = rasterio.crs.CRS({
'proj': 'longlat', 'ellps': 'WGS84', 'datum': 'WGS84',
'no_defs': True}),
dst_transform = rasterio.transform.from_bounds(
west, south, east, north,
n_lon, n_lat),
dst_nodata = 0,
resampling = rasterio.warp.Resampling.cubic)
mtx[mtx < 0] = 0
mtx = np.log1p(mtx)
mtx -= np.amin(mtx)
mtx /= np.amax(mtx)
# For no reason that I can find, 'mtx' will come out upside down.
self.mtx = np.flipud(mtx)
(count, height, width) = data.shape
# HGT only supports byte, int16, and uint16
data = data.astype(np.int16)
data.fill_value = _nodata(np.int16)
meta = {
"count": count,
"crs": data_crs,
"dtype": data.dtype,
"driver": "SRTMHGT",
"nodata": data.fill_value,
"height": height,
"width": width,
"transform": transform.from_bounds(
*data_bounds,
width=width,
height=height),
}
# GDAL's SRTMHGT driver requires that filenames be correct
(lon, lat) = map(int, map(round, data_bounds[:2]))
x = "W" if lon < 0 else "E"
y = "S" if lat < 0 else "N"
filename = "{}{}{}{}.hgt".format(y, abs(lat), x, abs(lon))
filename = "{}{:02d}{}{:03d}.hgt".format(y, abs(lat), x, abs(lon))
with MemoryFile(filename=filename) as memfile:
with memfile.open(**meta) as dataset:
dataset.write(data.filled())
array_sub = self._get_bands_subset(array)
if feature_type.is_time_dependent():
array_sub = self._get_dates_subset(array_sub, eopatch.timestamp)
else:
# add temporal dimension
array_sub = np.expand_dims(array_sub, axis=0)
if not feature_type.is_spatial():
# add height and width dimensions
array_sub = np.expand_dims(np.expand_dims(array_sub, axis=1), axis=1)
time_dim, height, width, band_dim = array_sub.shape
index = time_dim * band_dim
dst_transform = rasterio.transform.from_bounds(*eopatch.bbox, width=width, height=height)
dst_crs = {'init': CRS.ogc_string(eopatch.bbox.crs)}
image_dtype = array_sub.dtype if self.image_dtype is None else self.image_dtype
if image_dtype == np.int64:
image_dtype = np.int32
warnings.warn('Data from feature {} cannot be exported to tiff with dtype numpy.int64. Will export as '
'numpy.int32 instead'.format((feature_type, feature_name)))
# Write it out to a file
with rasterio.open(self._get_file_path(filename, create_dir=True), 'w', driver='GTiff',
width=width, height=height,
count=index,
dtype=image_dtype, nodata=self.no_data_value,
transform=dst_transform, crs=dst_crs) as dst:
output_array = array_sub.astype(image_dtype)
output_array = np.moveaxis(output_array, -1, 1).reshape(index, height, width)