2

I have a map with a background layer of Europe and another ( XYZ ) layer displaying a much smaller area.

How can I avoid the 404 error message from the XYZ layer for tiles that don't exist?

I have tried to pass:

extent: [-0.795668404302292,-0.7037491016945445,48.78018752203186,48.83999044180076 ]

But it doesn't work.

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
  • Use the extent option for the layer. The bounds must be in view projection units so you will need to transform the lon/lat extent to view projection if you are not using EPSG:4326 – Mike Feb 06 '20 at 15:02

1 Answers1

1

In most cases it is easier to set an extent on the layer.

To limit the extent of a source you must give it a custom tile grid, for example

var defaultTileGrid = createXYZ();

var source = new XYZ({
  url: .... ,
  tileGrid: new TileGrid({
    origin: defaultTileGrid.getOrigin(0),
    resolutions: defaultTileGrid.getResolutions(),
    extent: extent
  })
});

The bounds must be in the same projection as the source

So for EPSG:4326 you would need

var defaultTileGrid = createXYZ({extent: getProjection('EPSG:4326').getExtent()});

var source = new XYZ({
  url: .... ,
  tileGrid: new TileGrid({
    origin: defaultTileGrid.getOrigin(0),
    resolutions: defaultTileGrid.getResolutions(),
    extent: [-0.795668404302292, 48.78018752203186, -0.7037491016945445, 48.83999044180076] 
  })
});
Mike
  • 16,042
  • 2
  • 14
  • 30
  • what do you mean by 'createXYZ()' : thanks anyway : I'll try to figure out – Patrick Cailly Feb 07 '20 at 08:06
  • `import {createXYZ} from 'ol/tilegrid';` https://openlayers.org/en/latest/apidoc/module-ol_tilegrid.html – Mike Feb 07 '20 at 10:24
  • CreateXYZ has a default projection of EPSG:3857 and I need EPSG:4326. How do you change that ? – Patrick Cailly Feb 08 '20 at 11:04
  • I tried => var options = { minZoom: 6, maxZoom: 18, extent: getProjection("EPSG:4326").getExtent() } var defaultTileGrid = createXYZ( options ); – Patrick Cailly Feb 08 '20 at 11:12
  • Is the extent [-0.795668404302292, -0.7037491016945445, 48.78018752203186, 48.83999044180076] correct? That is a large extent covering parts of Africa and Europe. A "much smaller" extent in Europe would be [-0.795668404302292, 48.78018752203186, -0.7037491016945445, 48.83999044180076] – Mike Feb 08 '20 at 11:38
  • [-0.795668404302292, -0.7037491016945445, 48.78018752203186, 48.83999044180076] is EPSG:4326 and createXYZ by default uses EPSG:3857 ( from the online documentation. I can't see how to create a grid using CreateXYZ with EPSG:4326 extent – Patrick Cailly Feb 08 '20 at 12:46
  • createXYZ creates a tile grid to fit any extent (it defaults to the EPSG:3857 extent). It your tile coordinates are for that extent but you don't have full coverage you can use createXYZ to calculate the resolutions and origin and use those in a custom grid with a restricted extent. Extents should be [minX, minY, maxX, maxY] so [-0.795668404302292, -0.7037491016945445, 48.78018752203186, 48.83999044180076] is not a small extent within Europe in either projection. – Mike Feb 08 '20 at 13:23
  • I don't undestand if I have to provide an extent that fits the map or the layer, and if I have to pass it to the source definition or the CreateXYZ as option. What I did so far don't show the layer. And nothing from th console. so I assumed the CRS was wrong – Patrick Cailly Feb 08 '20 at 13:58
  • Apart the 404 errors where tiles don't exist did your original map display correctly? It would help to know how you had set it up, for example was already there already a custom grid. Also in most cases it is simpler to limit the extent on the layer, not the source. – Mike Feb 08 '20 at 19:13
  • apart from the from the 404 it is working. Imanage to get rid of the 404 error by using an url function tha returns a blank string when outside of extent but it is rather clumsy. Idon't know how to limit the extent of a layer . Do I have to pass extent: as layer option ? – Patrick Cailly Feb 09 '20 at 06:52
  • Yes, the layer also has an extent option. It must be specified in the view projection even if the source is in a different projection. – Mike Feb 09 '20 at 13:13