question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Broadcast error with mosaic_reader past the bounds of a dataset

See original GitHub issue

Hi 👋

It seems like attempting to build a mosaic with a bbox that extends beyond the bounds of a dataset creates an inconsistent data shape between datasets. I think this is happening because WarpedVRT reader doesn’t support boundless reads, so the shape of the data is clipped to the dataset’s bounds? I’ve worked around this by using a custom reader that uses rasterio.wrap.reproject after reading from the COG directly (instead of reprojecting on the fly).

I put together a short script demonstrating this issue with a couple of NAIP COGs from Planetary Computer:

import rio_tiler

cog_urls = [
    'https://naipeuwest.blob.core.windows.net/naip/v002/ca/2020/ca_060cm_2020/35116/m_3511663_se_11_060_20200523.tif',
    'https://naipeuwest.blob.core.windows.net/naip/v002/ca/2020/ca_060cm_2020/35116/m_3511663_ne_11_060_20200523.tif',
]

bbox = [-116.15799958899584, 35.059243863202646, -116.15220014855299, 35.06651998829758]

def _reader(asset, *args, **kwargs):
  with rio_tiler.io.COGReader(asset) as cog:
    return cog.part(*args, **kwargs)

image_data, _assets = rio_tiler.mosaic.mosaic_reader(
    cog_urls,
    _reader,
    bbox,
)
COG & BBox Geometry

Screenshot from 2022-07-12 10-55-44

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -116.1522,
              35.059244
            ],
            [
              -116.158,
              35.059244
            ],
            [
              -116.158,
              35.06652
            ],
            [
              -116.1522,
              35.06652
            ],
            [
              -116.1522,
              35.059244
            ]
          ]
        ]
      },
      "properties": {
        "name": "read bounds",
        "fill": "green"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -116.122847,
              35.060229
            ],
            [
              -116.122135,
              35.126769
            ],
            [
              -116.189694,
              35.12724
            ],
            [
              -116.190352,
              35.060699
            ],
            [
              -116.122847,
              35.060229
            ]
          ]
        ]
      },
      "properties": {
        "cog url": "https://naipeuwest.blob.core.windows.net/naip/v002/ca/2020/ca_060cm_2020/35116/m_3511663_ne_11_060_20200523.tif"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -116.122792,
              34.997741
            ],
            [
              -116.12208,
              35.064281
            ],
            [
              -116.189654,
              35.064752
            ],
            [
              -116.19031,
              34.99821
            ],
            [
              -116.122792,
              34.997741
            ]
          ]
        ]
      },
      "properties": {
        "cog url": "https://naipeuwest.blob.core.windows.net/naip/v002/ca/2020/ca_060cm_2020/35116/m_3511663_se_11_060_20200523.tif"
      }
    }
  ]
}
</details

Traceback:

Traceback (most recent call last):
  File "rio-tiler-boundless.py", line 15, in <module>
    image_data, _assets = rio_tiler.mosaic.mosaic_reader(
  File ".local/lib/python3.9/site-packages/rio_tiler/mosaic/reader.py", line 94, in mosaic_reader
    pixel_selection.feed(img.as_masked())
  File ".local/lib/python3.9/site-packages/rio_tiler/mosaic/methods/defaults.py", line 20, in feed
    pidex = self.tile.mask & ~tile.mask
ValueError: operands could not be broadcast together with shapes (4,1232,982) (4,1231,981) 

Not totally sure if I’ve understood the cause here correctly, or of how to fix this in rio-tiler, but I’m happy to contribute a fix with guidance if you’re interested. Thanks!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
geospatial-jeffcommented, Jul 15, 2022

because looking at the geo info from the 2 COG their resolution is the same!

The resolution in native CRS is the same but are slightly different after being projected to match the CRS of the bounding box (4326) when calculating height/width etc. for the VRT. Both images are from the same quadrangle (ne/se) and there is latitudinal distortion projecting from NAD83 UTM to WGS84. If the two images you’ve selected were east/west oriented (ex. ne/nw or se/sw) you probably wouldn’t see this issue.

The resolutions of the two images (in degrees) after reprojection are:

5.909249718773602e-06
5.907606274181144e-06

Setting width and height should fix this; another workaround is to reproject the bbox to the native CRS of the image which will avoid the rounding error when reprojecting to WGS84:

import rio_tiler
from rasterio.crs import CRS
from rasterio.warp import transform_bounds

cog_urls = [
    'https://naipeuwest.blob.core.windows.net/naip/v002/ca/2020/ca_060cm_2020/35116/m_3511663_se_11_060_20200523.tif',
    'https://naipeuwest.blob.core.windows.net/naip/v002/ca/2020/ca_060cm_2020/35116/m_3511663_ne_11_060_20200523.tif',
]

bbox = [-116.15799958899584, 35.059243863202646, -116.15220014855299, 35.06651998829758]
bbox26911 = transform_bounds(
    CRS.from_epsg(4326), CRS.from_epsg(26911), *bbox
)

def _reader(asset, *args, **kwargs):
  with rio_tiler.io.COGReader(asset) as cog:
    return cog.part(*args, **kwargs)

image_data, _assets = rio_tiler.mosaic.mosaic_reader(
    cog_urls,
    _reader,
    bbox26911,
    bounds_crs=CRS.from_epsg(26911),
)
1reaction
lorenzoricommented, Jul 22, 2022

you are right, when I cropped the the tiles to share I messed with the resolution. Indeed if I project the originals to 3857 and assert they have the same resolution, the error from mosaic_reader disappears. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Broadcast error when dataset is recombined after a stack ...
I have code which performs the split-apply-combine pattern on a dataset, and it appears to work as expected until it reaches a point...
Read more >
Python h5py - Why do I get a broadcast error? - Stack Overflow
I am trying to read a .h5 file data.h5 , which has 2 datasets, 'Data ...
Read more >
Out of bounds" error returned when importing data or editing a ...
Importing data to a geodatabase feature dataset or feature class, or creating features in an ArcMap editing session, fails with one of the ......
Read more >
Running reindex for data source fails with "Bounds exception"
Problem. Attempt to run reindex for a data source in IBM Lifecycle Query Engine (LQE) fails with "BlockAccessBase: Bounds exception: C:\indices\ ...
Read more >
Generalization Error Bounds on Deep Learning with Markov ...
In this paper, we derive upper bounds on generalization errors for deep neural networks with Markov datasets. These bounds are developed based on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found