Skip to content

Commit

Permalink
GH-214: Simplify controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-ueding committed Jan 4, 2025
1 parent 60089d8 commit 9d21096
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 96 deletions.
56 changes: 26 additions & 30 deletions geo_activity_playground/webui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,31 @@
from flask import Flask
from flask import render_template

from geo_activity_playground.core.activities import ActivityRepository
from geo_activity_playground.core.config import Config
from geo_activity_playground.core.config import ConfigAccessor
from geo_activity_playground.explorer.tile_visits import TileVisitAccessor
from geo_activity_playground.webui.activity.blueprint import make_activity_blueprint
from geo_activity_playground.webui.activity.controller import ActivityController
from geo_activity_playground.webui.auth.blueprint import make_auth_blueprint
from geo_activity_playground.webui.authenticator import Authenticator
from geo_activity_playground.webui.calendar.blueprint import make_calendar_blueprint
from geo_activity_playground.webui.calendar.controller import CalendarController
from geo_activity_playground.webui.eddington.blueprint import make_eddington_blueprint
from geo_activity_playground.webui.eddington.controller import EddingtonController
from geo_activity_playground.webui.entry_controller import EntryController
from geo_activity_playground.webui.equipment.blueprint import make_equipment_blueprint
from geo_activity_playground.webui.equipment.controller import EquipmentController
from geo_activity_playground.webui.explorer.blueprint import make_explorer_blueprint
from geo_activity_playground.webui.explorer.controller import ExplorerController
from geo_activity_playground.webui.heatmap.blueprint import make_heatmap_blueprint
from geo_activity_playground.webui.search.blueprint import make_search_blueprint
from geo_activity_playground.webui.settings.blueprint import make_settings_blueprint
from geo_activity_playground.webui.square_planner.blueprint import (
make_square_planner_blueprint,
)
from geo_activity_playground.webui.summary.blueprint import make_summary_blueprint
from geo_activity_playground.webui.summary.controller import SummaryController
from geo_activity_playground.webui.tile.blueprint import make_tile_blueprint
from geo_activity_playground.webui.tile.controller import TileController
from geo_activity_playground.webui.upload_blueprint import make_upload_blueprint
from ..core.activities import ActivityRepository
from ..core.config import Config
from ..core.config import ConfigAccessor
from ..explorer.tile_visits import TileVisitAccessor
from .activity.blueprint import make_activity_blueprint
from .activity.controller import ActivityController
from .auth_blueprint import make_auth_blueprint
from .authenticator import Authenticator
from .calendar.blueprint import make_calendar_blueprint
from .calendar.controller import CalendarController
from .eddington.blueprint import make_eddington_blueprint
from .eddington.controller import EddingtonController
from .entry_controller import EntryController
from .equipment.blueprint import make_equipment_blueprint
from .equipment.controller import EquipmentController
from .explorer.blueprint import make_explorer_blueprint
from .explorer.controller import ExplorerController
from .heatmap.blueprint import make_heatmap_blueprint
from .search.blueprint import make_search_blueprint
from .settings.blueprint import make_settings_blueprint
from .square_planner.blueprint import make_square_planner_blueprint
from .summary.blueprint import make_summary_blueprint
from .summary.controller import SummaryController
from .tile_blueprint import make_tile_blueprint
from .upload_blueprint import make_upload_blueprint


def route_start(app: Flask, repository: ActivityRepository, config: Config) -> None:
Expand Down Expand Up @@ -88,7 +85,6 @@ def td(v: datetime.timedelta):

config = config_accessor()
summary_controller = SummaryController(repository, config)
tile_controller = TileController(config)
activity_controller = ActivityController(repository, tile_visit_accessor, config)
calendar_controller = CalendarController(repository)
equipment_controller = EquipmentController(repository, config)
Expand Down Expand Up @@ -136,7 +132,7 @@ def td(v: datetime.timedelta):
make_summary_blueprint(summary_controller), url_prefix="/summary"
)

app.register_blueprint(make_tile_blueprint(tile_controller), url_prefix="/tile")
app.register_blueprint(make_tile_blueprint(config), url_prefix="/tile")
app.register_blueprint(
make_upload_blueprint(
repository, tile_visit_accessor, config_accessor(), authenticator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask import request
from flask import url_for

from geo_activity_playground.webui.authenticator import Authenticator
from .authenticator import Authenticator


def make_auth_blueprint(authenticator: Authenticator) -> Blueprint:
Expand Down
Empty file.
29 changes: 0 additions & 29 deletions geo_activity_playground/webui/tile/blueprint.py

This file was deleted.

36 changes: 0 additions & 36 deletions geo_activity_playground/webui/tile/controller.py

This file was deleted.

42 changes: 42 additions & 0 deletions geo_activity_playground/webui/tile_blueprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import io

import matplotlib.pyplot as pl
import numpy as np
from flask import Blueprint
from flask import Response

from geo_activity_playground.core.config import Config
from geo_activity_playground.core.raster_map import get_tile


def make_tile_blueprint(config: Config) -> Blueprint:
blueprint = Blueprint("tiles", __name__, template_folder="templates")

@blueprint.route("/color/<int:z>/<int:x>/<int:y>.png")
def tile_color(x: int, y: int, z: int):
map_tile = np.array(get_tile(z, x, y, config.map_tile_url)) / 255
f = io.BytesIO()
pl.imsave(f, map_tile, format="png")
return Response(bytes(f.getbuffer()), mimetype="image/png")

@blueprint.route("/grayscale/<int:z>/<int:x>/<int:y>.png")
def tile_grayscale(x: int, y: int, z: int):
map_tile = np.array(get_tile(z, x, y, config.map_tile_url)) / 255
map_tile = np.sum(map_tile * [0.2126, 0.7152, 0.0722], axis=2) # to grayscale
map_tile = np.dstack((map_tile, map_tile, map_tile)) # to rgb
f = io.BytesIO()
pl.imsave(f, map_tile, format="png")
return Response(bytes(f.getbuffer()), mimetype="image/png")

@blueprint.route("/pastel/<int:z>/<int:x>/<int:y>.png")
def tile_pastel(x: int, y: int, z: int):
map_tile = np.array(get_tile(z, x, y, config.map_tile_url)) / 255
averaged_tile = np.sum(map_tile * [0.2126, 0.7152, 0.0722], axis=2)
grayscale_tile = np.dstack((averaged_tile, averaged_tile, averaged_tile))
factor = 0.7
pastel_tile = factor * grayscale_tile + (1 - factor) * map_tile
f = io.BytesIO()
pl.imsave(f, pastel_tile, format="png")
return Response(bytes(f.getbuffer()), mimetype="image/png")

return blueprint

0 comments on commit 9d21096

Please sign in to comment.