Reputation: 43
Im trying to do some basic image filtering. Ive included a snippet verbatim from the rasterio cookbook (I removed .astype() from the median filter output). The issue is that my input and output rasters should have the same extent but dont. The transform and affine are different for the input and output. Is this the expected behavior? Do I need to do something to the affine and transform to get the output to be the same as the input?
Python 2.7.11 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)] on win32
rasterio==0.36.0
import rasterio
from scipy.signal import medfilt
path = "map.tif"
output = "map2.tif"
with rasterio.open(path) as src:
array = src.read()
profile = src.profile
# apply a 5x5 median filter to each band
filtered = medfilt(array, (1, 5, 5))
# Write to tif, using the same profile as the source
with rasterio.open(output, 'w', **profile) as dst:
dst.write(filtered)
print profile
print dst.profile
>>> {'count': 1, 'crs': CRS({'init': u'epsg:3857'}), 'interleave': 'band', 'dtype': 'float64', 'affine': Affine(100.0, 0.0, -13250000.0, 0.0, 100.0, 3980000.0), 'driver': u'GTiff', 'transform': (-13250000.0, 100.0, 0.0, 3980000.0, 0.0, 100.0), 'height': 1700, 'width': 1700, 'tiled': False, 'nodata': None}
>>> {'count': 1, 'crs': CRS({'init': u'epsg:3857'}), u'interleave': 'band', 'dtype': 'float64', 'affine': Affine(-13250000.0, 100.0, 0.0, 3980000.0, 0.0, 100.0), 'driver': u'GTiff', 'transform': (0.0, -13250000.0, 100.0, 100.0, 3980000.0, 0.0), 'height': 1700, 'width': 1700, u'tiled': False, 'nodata': None}
Upvotes: 4
Views: 7583
Reputation: 6406
The rasterio docs include a history affine/transform usage that you may find useful. I used to have a couple lines like the following to handle this:
out_profile = src.profile.copy()
out_affine = out_profile.pop("affine")
out_profile["transform"] = out_affine
# then, write the output raster
with rasterio.open(output, 'w', **out_profile) as dst:
dst.write(filtered)
I think that is what is necessary here.
Upvotes: 1