user88484
user88484

Reputation: 1557

Match raster cell size to another raster

I've been using the code posted here to match a rasters' cell size to another raster. More specifically, I have sentinel-2 band (jp2 format) band with 20m cell\pixel size and I would like to split\resample\resize it to 10m. What I did so far, which worked great is to use the script from the link I added, where I input two raster: the 20m band, and another 10m band. They both have the same spatial extent, same crs. The weird problem I encounter now, is that this script still work, but the output raster size is about 500mb, where the original size is about 1000kb. I don't understand why this could happen. Any ideas? I'm using python 3 on win 10 machine. If you have another way of splitting the cell size using gdal or rasterio or other python libraies, it could also help. I just want to split every 20m pixel into 4 pixels of 10m with the same value, i.e., each new 10m cell will have the same value of the 20m cell. I'm posting the code I'm using anyways:

from osgeo import gdal, gdalconst

# Source
src_filename = r'C:\SN2 sr\T36SXB_20180406T081601_B05_20m.jp2'
src = gdal.Open(src_filename, gdalconst.GA_ReadOnly)
src_proj = src.GetProjection()
src_geotrans = src.GetGeoTransform()

# We want a section of source that matches this:
match_filename = r"C:\SN2 sr\T36SXB_20180406T081601_B04_10m.jp2"
match_ds = gdal.Open(match_filename, gdalconst.GA_ReadOnly)
match_proj = match_ds.GetProjection()
match_geotrans = match_ds.GetGeoTransform()
wide = match_ds.RasterXSize
high = match_ds.RasterYSize

# Output / destination
dst_filename = r'C:\SN2 sr\try5.jp2'
dst = gdal.GetDriverByName('Gtiff').Create(dst_filename, wide, high, 1, gdalconst.GDT_Float32)
dst.SetGeoTransform( match_geotrans )
dst.SetProjection( match_proj)

# Do the work
gdal.ReprojectImage(src, dst, src_proj, match_proj, gdalconst.GRA_NearestNeighbour)

del dst # Flush

print ("finish")

Upvotes: 1

Views: 2541

Answers (2)

Hamid Jafarbiglu
Hamid Jafarbiglu

Reputation: 1

I believe this issue is related to the compression method that is used in the file generation process. Using 'compress':'lzw' might be helpful in reducing the file size. When I tried your code with lzw compression I got a file size similar to the bigger input file (high res file). However, the gigh-res file it self was around 700 mb.

Upvotes: 0

Jascha Muller
Jascha Muller

Reputation: 243

Strange problem, because the code looks good. I guess both the original sentinel-2 files are also Float32, so that might no be the problem. One thing I did pick up is when you are creating you new file you are specifying a '.jp2' extension with a 'Gtiff' driver. The driver for jp2 is 'JPEG2000'. This might be your problem? Hope it helps

Upvotes: 0

Related Questions