Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweaks to zoom level calculation #207

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions app/javascript/projects/modelling/bounding_box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//TODO: Make this customisable

import { Extent, getArea } from "ol/extent"
import { createXYZ } from "ol/tilegrid"

const westHorsely = [-49469.089243, 6669018.450996]
const bexhill = [55641.379277, 6570068.329224]
Expand All @@ -26,15 +27,20 @@ export const currentBbox = `${currentExtent.join(",")},EPSG:3857`
export const zoomLevel = 20

// WIP: Sets global zoom level from extent, requires tweaking and tests
export function zoomFromExtent(extent: Extent): number {
const area = getArea(extent) / 100

if(area < 5000000) return 23
if(area < 10000000) return 22
if(area < 100000000) return 21
if(area < 100000000000) return 20
export function zoomFromExtent(extent: Extent, maxtiles: number): number {
const tileGrid = createXYZ()
const zoomLevels = Array.from({ length: 30 }, (_, index) => index + 1).reverse()

return 18
for (const zoom of zoomLevels){
const tiles = tileGrid.getTileRangeForExtentAndZ(extent, zoom)
const tileCount = tiles.getWidth() * tiles.getHeight()
if(tileCount <= maxtiles){
return zoom
}
}

return 0

}

// Required format for some requests
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/projects/project_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export function ProjectEditor({ projectId, projectSource, backButtonPath, dbMode
})

// Retrieve the project extent from the project source, or use the current extent if not present. Calculate the zoom level from the extent.
const maxTiles = 10000000
const projectExtent: Extent = projectSource.extent ? projectSource.extent : currentExtent
const projectZoom: number = zoomFromExtent(projectExtent)
const projectZoom: number = zoomFromExtent(projectExtent, maxTiles)

const [sidebarVisible, setSidebarVisible] = React.useState(true)
const [layerPaletteVisible, setLayerPaletteVisible] = React.useState(false)
Expand Down
25 changes: 16 additions & 9 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,32 @@

<script>

function zoomLevel(area){

if(area < 5000000) return 23
if(area < 10000000) return 22
if(area < 100000000) return 21
if(area < 100000000000) return 20
function zoomLevel(extent, maxtiles){

const tileGrid = ol.tilegrid.createXYZ()
const zoomLevels = Array.from({ length: 30 }, (_, index) => index + 1).reverse()

for (const zoom of zoomLevels){
const tiles = tileGrid.getTileRangeForExtentAndZ(extent, zoom)
const tileCount = tiles.getWidth() * tiles.getHeight()
if(tileCount <= maxtiles){
console.log('zoom', zoom, 'tiles', tileCount)
return zoom
}
}

return 18
}

function showZoom(){
const extent = document.getElementById('project_extent').value.split(',').map(Number)
const area = ol.extent.getArea(extent) / 100
const area = (extent[2]-extent[0])*(extent[3]-extent[1])

if(isNaN(area)){
document.getElementById('zoom').innerHTML = 'No valid extent selected'
document.getElementById('zoom').style.backgroundColor = 'red'
}else{
const zoom = zoomLevel(area)

const zoom = zoomLevel(extent, 10000000)
const zoomCategory = zoom > 20 ? ['green', 'high'] : zoom < 20 ? ['orange', 'low'] : ['yellow', 'medium']


Expand Down