0

I'm curious if there is a way to reproject a raster to have 0 as the center longitude rather than 180. I am trying to convert this raster. I've been able to do this in gdal, but I'd like the solution to be in rasterio as it's more pythonic and I also don't think you're supposed to mix the two within one script. I've tried a couple different ways, but I believe the following to be the closest:

import rasterio
from rasterio.warp import reproject


def change_center_long_from_180_to_0(input_fp, output_fp):
    with rasterio.open(input_fp) as src:
        kwargs = src.meta.copy()
        kwargs.update({'CENTER_LONG': 0, 'SOURCE_EXTRA':1000})
        with rasterio.open(output_fp, 'w', **kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(
                    source=rasterio.band(src, i),
                    destination=rasterio.band(dst, i),
                    src_transform=src.transform,
                    src_crs=src.crs)

    return

However, this isn't doing anything to the raster.

2 Answers 2

3

You could try rioxarray: https://gis.stackexchange.com/a/357810

import rioxarray

rds = rioxarray.open_rasterio(input_fp)
rds = rds.assign_coords(x=(((rds.x + 180) % 360) - 180)).sortby('x')
rds.rio.to_raster(output_fp)
2
  • Very easy method and much faster than the "R" alternative. Got to change ds.x to rds.x for this to work though. Commented Dec 21, 2022 at 15:18
  • Glad to hear that it worked & thanks for the tip.
    – snowman2
    Commented Dec 22, 2022 at 13:58
0

I have worked with 0-360 data and the easiest method that I have found is in R using rotate from the raster library. I just transformed a geoid file from the NGS that was in 0-360 to 0 centered at Greenwich. See below with function calls and dumps of the CRS info. Note:

  • the shift worked despite the warning thrown in R
  • there are other output types than .asc which generated a larger file than the .bin

'''

    library(raster)
    > dir()[2]
    [1] "g2012bu0.bin"
    > r<-raster(dir()[2])
    > r
    class      : RasterLayer 
    dimensions : 2041, 4201, 8574241  (nrow, ncol, ncell)
    resolution : 0.01666667, 0.01666667  (x, y)
    extent     : 229.9917, 300.0083, 23.99167, 58.00833  (xmin, xmax, ymin, 
    ymax)
    crs        : +proj=longlat +ellps=GRS80 +no_defs 
    source     : g2012bu0.bin 
    names      : g2012bu0 

    > r2<-rotate(r)
    Warning message:
    In .local(x, ...) :
      this does not look like an appropriate object for this function
    
    > r2
    class      : RasterLayer 
    dimensions : 2041, 4201, 8574241  (nrow, ncol, ncell)
    resolution : 0.01666667, 0.01666667  (x, y)
    extent     : -130.0083, -59.99167, 23.99167, 58.00833  (xmin, xmax, ymin, 
    ymax)
    crs        : +proj=longlat +ellps=GRS80 +no_defs 
    source     : g2012bu0.bin 
    names      : g2012bu0 
    > writeRaster(r2,'g2012bu0.asc')
    
    '''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.