Improve map image generation #50

Merged
buckbanzai merged 2 commits from image-improvements into main 2024-08-25 12:04:52 -07:00
Showing only changes of commit 004373034a - Show all commits

26
scl.py
View file

@ -11,7 +11,7 @@ from PIL import Image, ImageDraw, ImageFont
from sqlalchemy import create_engine, select
from sqlalchemy.exc import NoResultFound
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column
from staticmap import CircleMarker, Polygon, StaticMap
from staticmap import Polygon, StaticMap
from geospatial import convert_outage_geometry
@ -45,10 +45,7 @@ class AttribStaticMap(StaticMap, object):
super(AttribStaticMap, self)._draw_features(image)
txt = Image.new("RGBA", image.size, (255, 255, 255, 0))
# get a font
# fnt = ImageFont.truetype('FreeMono.ttf', 12)
fnt = ImageFont.load_default()
# get a drawing context
fnt = ImageFont.truetype("fonts/PublicSans-Regular.otf", 24)
d = ImageDraw.Draw(txt)
textSize = fnt.getbbox(self.attribution)
@ -132,10 +129,11 @@ def do_initial_post(
area_text = event["city"]
try:
map = AttribStaticMap(
512,
512,
url_template="https://tiles.stadiamaps.com/tiles/outdoors/{z}/{x}/{y}.png?api_key="
1024,
1024,
url_template="https://tiles.stadiamaps.com/tiles/outdoors/{z}/{x}/{y}@2x.png?api_key="
+ stadiamaps_api_key,
tile_size=512,
)
assert event["polygons"]["type"] == "polygon"
for ring in event["polygons"]["rings"]:
@ -158,16 +156,6 @@ def do_initial_post(
# SE Corner
assert outage_center.y > 47.2 and outage_center.x < -122
marker_outline = CircleMarker(
(outage_center.x, outage_center.y), "white", 18
)
marker = CircleMarker(
(outage_center.x, outage_center.y), event_class["outage_color"], 12
)
map.add_marker(marker_outline)
map.add_marker(marker)
# Zoom level 17 ensures that we won't get any building/POI names, just street names
geocode_url = "{nominatim_url}/reverse?lat={lat}&lon={lon}&format=geocodejson&zoom=17".format(
nominatim_url=nominatim_url,
@ -230,7 +218,7 @@ def do_initial_post(
map_image = map.render()
with io.BytesIO() as map_image_file:
map_image.save(map_image_file, format="PNG", optimize=True)
map_image.save(map_image_file, format="WebP", method=6)
map_media_post = mastodon_client.media_post(
map_image_file.getvalue(),
mime_type="image/png",