Skip to content

Wrong reprojection for EPSG:3857 #313

Closed

Description

@AbelVM

I get this shift in latitude when rendering a COG in 3857

Image

It's rendered OK in QGIS

Activity

  1. kylebarron commented on Mar 5, 2026

    @kylebarron
    Member

    Can you check what proj4.js version you're using. cc proj4js/proj4js#545 and proj4js/proj4js#546

    Also, as it is your issue is not reproducible at all

  2. kylebarron commented on Mar 5, 2026

    @kylebarron
    Member

    Ideally we wouldn't perform any CPU reprojection for 3857 inputs, see #153 and #41.

    Let me know if you'd like to work on that.

    It should be similar to how the MVT layer renders from local web mercator coordinates instead of doing CPU work to convert to screen space.

    https://github.com/visgl/deck.gl/blob/2edcc0dde1bbc9a93fdfc4b52dfea0daf1f3c68b/modules/geo-layers/src/mvt-layer/mvt-layer.ts#L265-L270

  3. AbelVM commented on Mar 6, 2026

    @AbelVM
    Author

    I've rewritten my project from scratch, and I can say I'm using proj4 v2.20.3 (this is the version supposed to fix this issue) everywhere. Unless there's an old version hidden somewhere...

    Image

    I'd been searching for a way to force a reprojection to 4326 using RasterReprojector for this very specific case, but no success. Even forcing the wkt2 definition of the CRS. I always get the same vertical shift regardless of the approach. I just can't find what makes this specific raster fail with deck.gl-raster.

    I've been running through the issues in this repo, proj4, and deck.gl Mapbox overlay, just in case. No clue.

    The raster info looks ok, and it renders flawlessly in QGIS or in this maplibre plugin example https://labs.geomatico.es/maplibre-cog-protocol/custom-color.html#10.28/42.2571/2.9413

    ➜  gdalinfo sentinel2.tif                           
    Driver: GTiff/GeoTIFF
    Files: sentinel2.tif
           sentinel2.tif.aux.xml
    Size is 2560, 1280
    Coordinate System is:
    PROJCRS["WGS 84 / Pseudo-Mercator",
        BASEGEOGCRS["WGS 84",
            ENSEMBLE["World Geodetic System 1984 ensemble",
                MEMBER["World Geodetic System 1984 (Transit)"],
                MEMBER["World Geodetic System 1984 (G730)"],
                MEMBER["World Geodetic System 1984 (G873)"],
                MEMBER["World Geodetic System 1984 (G1150)"],
                MEMBER["World Geodetic System 1984 (G1674)"],
                MEMBER["World Geodetic System 1984 (G1762)"],
                MEMBER["World Geodetic System 1984 (G2139)"],
                MEMBER["World Geodetic System 1984 (G2296)"],
                ELLIPSOID["WGS 84",6378137,298.257223563,
                    LENGTHUNIT["metre",1]],
                ENSEMBLEACCURACY[2.0]],
            PRIMEM["Greenwich",0,
                ANGLEUNIT["degree",0.0174532925199433]],
            ID["EPSG",4326]],
        CONVERSION["Popular Visualisation Pseudo-Mercator",
            METHOD["Popular Visualisation Pseudo Mercator",
                ID["EPSG",1024]],
            PARAMETER["Latitude of natural origin",0,
                ANGLEUNIT["degree",0.0174532925199433],
                ID["EPSG",8801]],
            PARAMETER["Longitude of natural origin",0,
                ANGLEUNIT["degree",0.0174532925199433],
                ID["EPSG",8802]],
            PARAMETER["False easting",0,
                LENGTHUNIT["metre",1],
                ID["EPSG",8806]],
            PARAMETER["False northing",0,
                LENGTHUNIT["metre",1],
                ID["EPSG",8807]]],
        CS[Cartesian,2],
            AXIS["easting (X)",east,
                ORDER[1],
                LENGTHUNIT["metre",1]],
            AXIS["northing (Y)",north,
                ORDER[2],
                LENGTHUNIT["metre",1]],
        USAGE[
            SCOPE["Web mapping and visualisation."],
            AREA["World between 85.06°S and 85.06°N."],
            BBOX[-85.06,-180,85.06,180]],
        ID["EPSG",3857]]
    Data axis to CRS axis mapping: 1,2
    Origin = (283734.248994573950768,5224623.757348367944360)
    Pixel Size = (38.218514142588127,-38.218514142588127)
    Metadata:
      AREA_OR_POINT=Area
    Image Structure Metadata:
      LAYOUT=COG
      COMPRESSION=DEFLATE
      INTERLEAVE=PIXEL
    Tiling Scheme:
      NAME=GoogleMapsCompatible
      ZOOM_LEVEL=12
    Corner Coordinates:
    Upper Left  (  283734.249, 5224623.757) (  2d32'55.78"E, 42d25'24.44"N)
    Lower Left  (  283734.249, 5175704.059) (  2d32'55.78"E, 42d 5'53.60"N)
    Upper Right (  381573.645, 5224623.757) (  3d25'39.84"E, 42d25'24.44"N)
    Lower Right (  381573.645, 5175704.059) (  3d25'39.84"E, 42d 5'53.60"N)
    Center      (  332653.947, 5200163.908) (  2d59'17.81"E, 42d15'39.78"N)
    Band 1 Block=256x256 Type=UInt16, ColorInterp=Gray
      Min=0.000 Max=9586.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=9586
        STATISTICS_MEAN=2073.031171875
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=1419.0319875205
        STATISTICS_VALID_PERCENT=100
    Band 2 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=11685.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=11685
        STATISTICS_MEAN=2756.07625
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=1875.547791721
        STATISTICS_VALID_PERCENT=100
    Band 3 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=14719.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=14719
        STATISTICS_MEAN=3698.118984375
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=2687.6226017062
        STATISTICS_VALID_PERCENT=100
    Band 4 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=17762.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=17762
        STATISTICS_MEAN=3962.06484375
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=3459.0320173716
        STATISTICS_VALID_PERCENT=100
    Band 5 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=20322.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=20322
        STATISTICS_MEAN=5654.497421875
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=4501.7019889176
        STATISTICS_VALID_PERCENT=100
    Band 6 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=23545.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=23545
        STATISTICS_MEAN=9435.824609375
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=7208.0362097044
        STATISTICS_VALID_PERCENT=100
    Band 7 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=30205.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=30205
        STATISTICS_MEAN=11041.038046875
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=8556.8100620138
        STATISTICS_VALID_PERCENT=100
    Band 8 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=30162.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=30162
        STATISTICS_MEAN=11178.05140625
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=8726.8071197683
        STATISTICS_VALID_PERCENT=100
    Band 9 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=30846.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=30846
        STATISTICS_MEAN=11792.508984375
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=9231.8738627418
        STATISTICS_VALID_PERCENT=100
    Band 10 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=28367.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=28367
        STATISTICS_MEAN=9832.676015625
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=8131.3724134878
        STATISTICS_VALID_PERCENT=100
    Band 11 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=23444.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=23444
        STATISTICS_MEAN=6536.791484375
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=5775.7604591468
        STATISTICS_VALID_PERCENT=100
    Band 12 Block=256x256 Type=UInt16, ColorInterp=Undefined
      Min=0.000 Max=32156.000 
      Overviews: 1280x640, 640x320, 320x160, 160x80
      Metadata:
        STATISTICS_APPROXIMATE=YES
        STATISTICS_MAXIMUM=32156
        STATISTICS_MEAN=11915.650625
        STATISTICS_MINIMUM=0
        STATISTICS_STDDEV=9331.8555716746
        STATISTICS_VALID_PERCENT=100         
  4. AbelVM commented on Mar 6, 2026

    @AbelVM
    Author

    If I reproject the raster to some random (but valid) CRS, it works as expected. Only 3857 fails

    Image
    gdalwarp \
    -t_srs EPSG:25831 \
    -multi \
    -wo NUM_THREADS=ALL_CPUS \
    -wm 256 \
    --config GDAL_CACHEMAX 256 \
    -co TILED=YES \
    -co COMPRESS=ZSTD \
    -co PREDICTOR=2 \
    sentinel2.tif \
    sentinel_test.tif
  5. kylebarron commented on Mar 6, 2026

    @kylebarron
    Member

    @AbelVM you still haven't provided any reproducible example or data file

  6. kylebarron commented on Mar 6, 2026

    @kylebarron
    Member

    Ok I found the file from your upstream example.

    I converted to a 3-band RGB in COG format with

    gdal_translate -of COG sentinel2.tif sentinel2_rgb.tif -b 4 -b 3 -b 2

    Checking in Python

    import rasterio
    from pyproj import CRS
    
    path = "/Users/kyle/Downloads"
    name = "sentinel2_rgb.tif"
    
    src = rasterio.open(f"{path}/{name}")
    assert CRS(src.crs) == CRS.from_epsg(3857)
    
    print(src.bounds)
    # BoundingBox(left=283734.24899457395, bottom=5175704.059245856, right=381573.64519959956, top=5224623.757348368)

    Now first to validate our TypeScript geotiff parser is inferring the bounding box correct, we can add some console.logs in our onGeoTIFFLoad callback.

    console.log("geotiff.bbox", geotiff.bbox);
    // geotiff.bbox (4) [283734.24899457395, 5175704.059245856, 381573.64519959956, 5224623.757348368]
    
    console.log("geotiff.crs", geotiff.crs);
    // geotiff.crs 3857

    Ok, so we're parsing the GeoTIFF tags correctly. Now lets check the WGS84 bounding box inferred by getGeographicBounds

    console.log("geographicBounds", geographicBounds);
    // geographicBounds {west: 2.5488281249999973, south: 42.289627708833116, east: 3.4277343749999973, north: 42.615061851884434}

    Now let's check against Python

    from pyproj import Transformer
    
    transformer = Transformer.from_crs(
        src.crs,
        CRS.from_epsg(4326),
        always_xy=True,
    )
    transformer.transform(src.bounds.left, src.bounds.bottom)
    # (2.5488281249999973, 42.098222411189745)

    Let's put a console.log into getGeographicBounds

    console.log(converter.forward([minX, minY]));
    // [2.5488281249999973, 42.289627708833116]

    See, this is off by .2 degrees of latitude. This points to a projection error in proj4js. Now we can move on to creating an issue in proj4js.

    ^^ This is the kind of analysis that's helpful for reproducing an issue. Uploading screenshots (alone) isn't sufficient to help diagnose a problem.

  7. kylebarron commented on Mar 6, 2026

    @kylebarron
    Member

    I've been running through the issues in this repo, proj4, and deck.gl Mapbox overlay, just in case. No clue.

    There was an issue created in proj4js a few days ago: proj4js/proj4js#548

    The maintainer commented there

    would you be willing to create a pull request AND add test cases for the failing cases to make sure they are fixed?

    @AbelVM since you're currently hitting this, would you be able to make a PR to proj4js to fix this bug? That would be very helpful.

  8. AbelVM commented on Mar 7, 2026

    @AbelVM
    Author

    @AbelVM you still haven't provided any reproducible example or data file

    My fault! I've been deep in #215, working on a plugin, and I took a lot of things for granted when posting the issue. For example, providing the offending raster file 😅 . Thank you for the research!!

    As proj4js v2.20.3 was, in theory, blameless, I was looking into deck.gl Mapbox Overlay, as there are some old issues there about overlay position regarding interleaving.

    I'm moving to the proj4js issue to check the info there and go try a PR

    EDIT Ok, it looks like I'm late, and it's already fixed with proj4js/proj4js#553

  9. kylebarron commented on Mar 7, 2026

    @kylebarron
    Member

    https://github.com/proj4js/proj4js/releases/tag/v2.20.4 is released so it looks like this should be fixed now

    I've been deep in #215, working on a plugin

    Is your work on this open source? We'll have first-party custom visualization in this library, but I might start work on Zarr first.

  10. AbelVM commented on Mar 7, 2026

    @AbelVM
    Author

    Yes! I'm fine tuning it right now and will publish it later 🤓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions