ARiisgaard / Thesis

0 stars 0 forks source link

Currently displayed tiles #24

Open ARiisgaard opened 4 years ago

ARiisgaard commented 4 years ago

To be able to adjust the tile-coloring to the currently visible tiles I need to know which tiles are being displayed.

I could achieve this by tinkering more with gdal2tiles and make it produce a table with data about each tile. Knowing the latitude and longitude of each corner would allow me to find the tiles by comparing with the current map extent (map.getView().calculateExtent(map.getSize())). Any tile with a corner within the map extent must be visible on the map.

I haven't started with this tinkering yet, because it sounds like a feature Openlayers already would have. No need to tinker with a script myself if a oneline command already exist.

So far I have been unable to find such a command. The closest I have come is tileSource.tileCache.entries_ , which would be able to lead me to the path to the relevant files: image So, in this case I would know that the file "g2tTiles/3/1/4.tiff", that I would have to get data from.

However this issue with the command above is that the tilecache also keeps data, that is no longer being displayed, because they are still stored in the cache - which would mess up the coloring.

Does Openlayers have a current displayed tiles command or should I take the gdal2tiles-tinkering route?

crstn commented 4 years ago

I would try to solve this in OL. A quick search in the API docs led me to the TileGrid class, which has a method called forEachTileCoord. If I understand this method correctly, it should let you loop over the tiles in the current extent.

ARiisgaard commented 4 years ago

I have tried using forEachTileCoord with the code:

        mapExtent = map.getView().calculateExtent(map.getSize())
        mapZoom = map.getView().getZoom();
        var tileUrlFunction = tileSource.getTileUrlFunction()
        tileGrid.forEachTileCoord(mapExtent, mapZoom, function (tileCoord) {
          console.log(tileUrlFunction(tileCoord, 1, ol.proj.get('EPSG:4326')));
        })

This returns "g2tTiles/8/185/40.tiff" or similar, which format-wise is what I'm looking for. Issue is that this command always return tiles from a layer 5 zoom levels too high. So when the code returned the response above, the actual file being loaded was "g2tTiles/3/2/3.tiff". This seem to be an issue with my get-zoom-command or the zoom of the tilelaying

crstn commented 4 years ago

Does that piece of code give you the correct tiles for other layers that you didn't create yourself (e.g. the OSM base map)?

ARiisgaard commented 4 years ago

I couldn't get the code to work with the osm layer, but if I run it with the same layer as the original websitewas based on, where I get the same issue also with a difference of 5.

I did also notice that some of the expected tiles have negative values. Fx. "g2tTiles/7/125/-5.tiff", which is not a possible value with the current setup. A bit of research have lead me to believe that it is the result of my map running an Openlayers older that 5th edition. This might also explain the other issue.

I have tried to replace my maps version (4.6.4) with the newest one with the code below, but that results in a olgeotiff-error; "Uncaught TypeError: Cannot read property 'ERROR' of undefined". I don't know if the issue is that Geotiff only have been written for the older version.

<!-- In the header -->
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/build/ol.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/css/ol.css" type="text/css">
    <!-- <script src="./maps from geotiff_files/ol-debug.js.download"></script>
    <link rel="stylesheet" href="./maps from geotiff_files/ol-debug.css"> -->