mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-02 12:56:24 -05:00
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically. Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config. PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
This commit is contained in:
parent
4072ac30ed
commit
adb0adbd94
27 changed files with 2115 additions and 170 deletions
|
|
@ -1,8 +1,14 @@
|
|||
import json
|
||||
from http import HTTPStatus
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from modules.map.config import get_maps_config
|
||||
from modules.map.conftest import vector_metadata
|
||||
from wrolpi.api_utils import api_app
|
||||
from wrolpi.common import get_all_configs
|
||||
from wrolpi.conftest import production_like_sessions, write_lock_held_briefly
|
||||
|
||||
|
||||
|
|
@ -180,3 +186,104 @@ async def test_subscription_writes_wait_for_a_concurrent_writer(async_client, te
|
|||
_, response = await async_client.get('/api/map/subscribe')
|
||||
regions = [i['region'] for i in response.json['subscriptions']]
|
||||
assert regions == (['us-alaska'] if step == 'subscribe' else [])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_files_describes_files(async_client, test_directory, make_pmtiles):
|
||||
"""A file is described by its own metadata, and by how the map draws it."""
|
||||
make_pmtiles('map/usa.pmtiles')
|
||||
make_pmtiles('map/extras/features.pmtiles',
|
||||
vector_metadata('towers', geometries=dict(towers='Point'), name='Features', description='Things'))
|
||||
|
||||
request, response = await async_client.get('/api/map/files')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
files = {i['map_path']: i for i in response.json['files']}
|
||||
assert files['usa.pmtiles']['kind'] == 'basemap'
|
||||
assert files['usa.pmtiles']['title'] == 'Protomaps Basemap'
|
||||
features = files['extras/features.pmtiles']
|
||||
assert features['kind'] == 'overlay'
|
||||
assert features['title'] == 'Features'
|
||||
assert features['description'] == 'Things'
|
||||
assert features['path'] == 'map/extras/features.pmtiles'
|
||||
assert features['visible'] is True
|
||||
assert features['vector_layers'] == [
|
||||
dict(id='towers', minzoom=0, maxzoom=8, description=None, geometry='Point')]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_file_sub_directory(async_client, test_directory, make_pmtiles):
|
||||
"""A file in a sub-directory can be deleted."""
|
||||
pmtiles_file = make_pmtiles('map/extras/deeper/features.pmtiles')
|
||||
|
||||
request, response = await async_client.delete('/api/map/files/extras/deeper/features.pmtiles')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
assert not pmtiles_file.exists()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rebuild_search_index_sub_directory(async_client, test_directory, make_pmtiles):
|
||||
"""The search index of a file in a sub-directory can be built."""
|
||||
pmtiles_file = make_pmtiles('map/regions/oregon.pmtiles')
|
||||
|
||||
with mock.patch('modules.map.search._check_tippecanoe', return_value=True), \
|
||||
mock.patch('modules.map.search._run_build', new_callable=mock.AsyncMock) as mock_run_build:
|
||||
request, response = await async_client.post('/api/map/search/rebuild/regions/oregon.pmtiles')
|
||||
assert response.status_code == HTTPStatus.ACCEPTED
|
||||
cmd, description = mock_run_build.call_args.args
|
||||
assert str(pmtiles_file) in cmd
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_visible(async_client, test_directory, make_pmtiles):
|
||||
"""A map file can be ignored and shown again; the choice is saved to the maps config."""
|
||||
make_pmtiles('map/extras/api-ignored.pmtiles')
|
||||
config_file = test_directory / 'config/maps.yaml'
|
||||
|
||||
body = dict(map_path='extras/api-ignored.pmtiles', visible=False)
|
||||
request, response = await async_client.post('/api/map/files/visible', json=body)
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
request, response = await async_client.get('/api/map/files')
|
||||
assert [i['visible'] for i in response.json['files']] == [False]
|
||||
assert yaml.safe_load(config_file.read_text())['ignored_files'] == ['extras/api-ignored.pmtiles']
|
||||
|
||||
body = dict(map_path='extras/api-ignored.pmtiles', visible=True)
|
||||
request, response = await async_client.post('/api/map/files/visible', json=body)
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
request, response = await async_client.get('/api/map/files')
|
||||
assert [i['visible'] for i in response.json['files']] == [True]
|
||||
assert yaml.safe_load(config_file.read_text())['ignored_files'] == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_visible_unknown_file(async_client, test_directory):
|
||||
"""A file which does not exist cannot be ignored."""
|
||||
body = dict(map_path='missing.pmtiles', visible=False)
|
||||
request, response = await async_client.post('/api/map/files/visible', json=body)
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
|
||||
body = dict(map_path='../../etc/passwd', visible=False)
|
||||
request, response = await async_client.post('/api/map/files/visible', json=body)
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_maps_config_import(async_client, test_directory, make_pmtiles):
|
||||
"""A maps config a person edited is applied when it is imported."""
|
||||
make_pmtiles('map/hand-edited.pmtiles')
|
||||
make_pmtiles('map/other.pmtiles')
|
||||
config = get_maps_config()
|
||||
config.get_file().parent.mkdir(parents=True, exist_ok=True)
|
||||
config.get_file().write_text(yaml.safe_dump(dict(version=1, ignored_files=['hand-edited.pmtiles'])))
|
||||
|
||||
config.import_config()
|
||||
|
||||
request, response = await async_client.get('/api/map/files')
|
||||
visible = {i['map_path']: i['visible'] for i in response.json['files']}
|
||||
assert visible == {'hand-edited.pmtiles': False, 'other.pmtiles': True}
|
||||
|
||||
|
||||
def test_maps_config_is_shared(async_client):
|
||||
"""The maps config lives in the context the Sanic workers share (a config which does not is saved by one
|
||||
worker and overwritten by another), and is among the configs WROLPi manages."""
|
||||
assert get_maps_config()._config is api_app.shared_ctx.maps_config
|
||||
assert get_all_configs()['maps.yaml'] is get_maps_config()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue