Compare commits
1 commit
main
...
switch-map
Author | SHA1 | Date | |
---|---|---|---|
4aa7e32273 |
5 changed files with 62 additions and 268 deletions
115
fonts/LICENSE.md
115
fonts/LICENSE.md
|
@ -1,115 +0,0 @@
|
|||
This repository is a fork of Libre Franklin: https://github.com/impallari/Libre-Franklin
|
||||
|
||||
## License for USWDS’s Modified Version
|
||||
|
||||
This repository contains both the original font software for Libre Franklin (the “Original Version”) and font software modifications made by the General Services Administration (GSA). This repository combines the Original Version and these GSA modifications into a piece of font software called Public Sans, which is a “Modified Version” of Libre Franklin.
|
||||
|
||||
As a work of the United States Government, the font software modifications made by GSA are not subject to copyright within the United States. Additionally, GSA waives copyright and related rights in its font software modifications worldwide through the [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/).
|
||||
|
||||
The Original Version (as defined in the SIL Open Font License, Version 1.1) remains subject to copyright under the SIL Open Font License, Version 1.1.
|
||||
|
||||
This Modified Version (Public Sans) contains both software under the SIL Open Font License, Version 1.1 and software modifications by GSA released as CC0. As a work of the United States Government, the software modifications made by GSA are not subject to copyright within the United States. Additionally, GSA waives copyright and related rights in its software modifications worldwide through the [CC0 1.0 Universal Public Domain Dedication](https://creativecommons.org/publicdomain/zero/1.0/). It is a “joint work” made of the original software and modifications combined into a single work.
|
||||
|
||||
**In practice, users of this Modified Version (Public Sans) should use Public Sans according to the terms of the SIL Open Font License, Version 1.1, below.** This is because this font is a combination of work subject to copyright and work not subject to copyright, so the more restrictive requirements apply to using the combined work.
|
||||
|
||||
## License of project USWDS’s Modified Version is based on
|
||||
|
||||
- Libre Franklin is licensed under the SIL Open Font License, Version 1.1 (<http://scripts.sil.org/OFL>)
|
||||
- To view the copyright and specific terms and conditions of Libre Franklin, please refer to [OFL.txt](https://github.com/impallari/Libre-Franklin/blob/master/OFL.txt)
|
||||
|
||||
## SIL Open Font License, Version 1.1
|
||||
|
||||
Copyright 2015 The Public Sans Project Authors (https://github.com/uswds/public-sans)
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||
|
||||
This license is copied below, and is also available with a FAQ at http://scripts.sil.org/OFL
|
||||
|
||||
```
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font creation
|
||||
efforts of academic and linguistic communities, and to provide a free and
|
||||
open framework in which fonts may be shared and improved in partnership
|
||||
with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply
|
||||
to any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components as
|
||||
distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting -- in part or in whole -- any of the components of the
|
||||
Original Version, by changing formats or by porting the Font Software to a
|
||||
new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,
|
||||
in Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the corresponding
|
||||
Copyright Holder. This restriction only applies to the primary font name as
|
||||
presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created
|
||||
using the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
||||
```
|
Binary file not shown.
|
@ -1,5 +1,4 @@
|
|||
from shapely import MultiPolygon, Polygon, Geometry, to_wkb, from_wkb
|
||||
from sqlalchemy.types import TypeDecorator, LargeBinary
|
||||
from shapely import MultiPolygon, Polygon
|
||||
|
||||
|
||||
def convert_outage_geometry(event) -> MultiPolygon:
|
||||
|
@ -10,21 +9,3 @@ def convert_outage_geometry(event) -> MultiPolygon:
|
|||
for ring in event["polygons"]["rings"]:
|
||||
polygon_list.append(Polygon(ring))
|
||||
return MultiPolygon(polygon_list)
|
||||
|
||||
|
||||
class DBGeometry(TypeDecorator):
|
||||
impl = LargeBinary
|
||||
cache_ok = True
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
if isinstance(value, Geometry):
|
||||
value = to_wkb(value)
|
||||
return value
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
if value is None:
|
||||
return value
|
||||
else:
|
||||
if not isinstance(value, Geometry):
|
||||
value = from_wkb(value)
|
||||
return value
|
||||
|
|
|
@ -1,20 +1,28 @@
|
|||
appdirs==1.4.4
|
||||
blurhash==1.1.4
|
||||
certifi==2024.8.30
|
||||
charset-normalizer==3.4.0
|
||||
certifi==2024.7.4
|
||||
charset-normalizer==3.3.2
|
||||
decorator==5.1.1
|
||||
greenlet==3.1.1
|
||||
idna==3.10
|
||||
pip-install==1.3.5
|
||||
future==1.0.0
|
||||
geographiclib==2.0
|
||||
greenlet==3.0.3
|
||||
idna==3.8
|
||||
Mastodon.py==1.8.1
|
||||
numpy==2.1.3
|
||||
pillow==11.0.0
|
||||
numpy==2.1.0
|
||||
pillow==10.4.0
|
||||
pip-install==1.3.5
|
||||
py-staticmaps==0.4.0
|
||||
python-dateutil==2.9.0.post0
|
||||
python-magic==0.4.27
|
||||
python-slugify==8.0.4
|
||||
PyYAML==6.0.2
|
||||
requests==2.32.3
|
||||
s2sphere==0.2.5
|
||||
shapely==2.0.6
|
||||
six==1.16.0
|
||||
SQLAlchemy==2.0.36
|
||||
SQLAlchemy==2.0.32
|
||||
staticmap==0.5.7
|
||||
svgwrite==1.4.3
|
||||
text-unidecode==1.3
|
||||
typing_extensions==4.12.2
|
||||
urllib3==2.2.3
|
||||
urllib3==2.2.2
|
||||
|
|
168
scl.py
168
scl.py
|
@ -5,16 +5,15 @@ from typing import Optional
|
|||
import mastodon
|
||||
import requests
|
||||
import shapely
|
||||
import staticmap
|
||||
import staticmaps
|
||||
import yaml
|
||||
from mastodon import Mastodon
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
from shapely import Geometry
|
||||
from PIL import Image, ImageDraw
|
||||
from sqlalchemy import create_engine, select
|
||||
from sqlalchemy.exc import NoResultFound
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column
|
||||
|
||||
from geospatial import DBGeometry, convert_outage_geometry
|
||||
from geospatial import convert_outage_geometry
|
||||
|
||||
post_datetime_format = "%b %e %l:%M %p"
|
||||
|
||||
|
@ -37,44 +36,6 @@ mastodon_client = Mastodon(
|
|||
)
|
||||
|
||||
|
||||
class AttribStaticMap(staticmap.StaticMap, object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.attribution = "© Stadia Maps © OpenMapTiles © OpenStreetMap"
|
||||
super(AttribStaticMap, self).__init__(*args, **kwargs)
|
||||
|
||||
def _draw_features(self, image):
|
||||
super(AttribStaticMap, self)._draw_features(image)
|
||||
|
||||
txt = Image.new("RGBA", image.size, (255, 255, 255, 0))
|
||||
fnt = ImageFont.truetype("fonts/PublicSans-Regular.otf", 24)
|
||||
d = ImageDraw.Draw(txt)
|
||||
|
||||
textSize = fnt.getbbox(self.attribution)
|
||||
textPosition = (image.size[0] - textSize[2], image.size[1] - textSize[3])
|
||||
offset = 2
|
||||
options = {"fill": (255, 255, 255, 180)}
|
||||
d.rectangle(
|
||||
[
|
||||
(textPosition[0] - (2 * offset), textPosition[1] - (2 * offset)),
|
||||
(
|
||||
textSize[2] + textPosition[0] + (2 * offset),
|
||||
textSize[3] + textPosition[1] + (2 * offset),
|
||||
),
|
||||
],
|
||||
**options,
|
||||
)
|
||||
|
||||
# draw text, full opacity
|
||||
d.text(
|
||||
(textPosition[0] - offset, textPosition[1] - offset),
|
||||
self.attribution,
|
||||
font=fnt,
|
||||
fill="black",
|
||||
)
|
||||
|
||||
image.paste(txt, (0, 0), txt)
|
||||
|
||||
|
||||
def classify_event_size(num_people: int) -> dict[str, str | bool]:
|
||||
if num_people < 250:
|
||||
return {
|
||||
|
@ -96,6 +57,15 @@ def classify_event_size(num_people: int) -> dict[str, str | bool]:
|
|||
}
|
||||
|
||||
|
||||
# Only needed for workaround for https://github.com/flopp/py-staticmaps/issues/39
|
||||
def textsize(self: ImageDraw.ImageDraw, *args, **kwargs):
|
||||
x, y, w, h = self.textbbox((0, 0), *args, **kwargs)
|
||||
return w, h
|
||||
|
||||
|
||||
ImageDraw.ImageDraw.textsize = textsize
|
||||
|
||||
|
||||
def get_hashtag_string(event) -> str:
|
||||
city = str()
|
||||
try:
|
||||
|
@ -129,23 +99,28 @@ def do_initial_post(
|
|||
# Fallback location from the SCL API in case one couldn't be reverse geocoded
|
||||
area_text = event["city"]
|
||||
try:
|
||||
map = AttribStaticMap(
|
||||
1024,
|
||||
1024,
|
||||
url_template="https://tiles.stadiamaps.com/tiles/outdoors/{z}/{x}/{y}@2x.png?api_key="
|
||||
+ stadiamaps_api_key,
|
||||
tile_size=512,
|
||||
context = staticmaps.Context()
|
||||
tile_provider = staticmaps.TileProvider(
|
||||
"stadia-outdoors",
|
||||
"https://tiles.stadiamaps.com/tiles/outdoors/$z/$x/$y.png?api_key=$k",
|
||||
shards=["a", "b", "c", "d"],
|
||||
attribution="© Stadia Maps © OpenMapTiles © OpenStreetMap",
|
||||
api_key=stadiamaps_api_key,
|
||||
)
|
||||
context.set_tile_provider(tile_provider)
|
||||
|
||||
assert event["polygons"]["type"] == "polygon"
|
||||
for ring in event["polygons"]["rings"]:
|
||||
polygon = staticmap.Polygon(
|
||||
ring,
|
||||
# Appending 7F to the fill_color makes it 50% transparent
|
||||
fill_color="{}7F".format(event_class["outage_color"]),
|
||||
outline_color=event_class["outage_color"],
|
||||
simplify=True,
|
||||
context.add_object(
|
||||
staticmaps.Area(
|
||||
[staticmaps.create_latlng(lat, lon) for lon, lat in ring],
|
||||
fill_color=staticmaps.parse_color(
|
||||
"{}7F".format(event_class["outage_color"])
|
||||
),
|
||||
width=2,
|
||||
color=staticmaps.parse_color(event_class["outage_color"]),
|
||||
)
|
||||
)
|
||||
map.add_polygon(polygon)
|
||||
|
||||
try:
|
||||
outage_center: shapely.Point = outage_geometries.centroid
|
||||
|
@ -216,13 +191,13 @@ def do_initial_post(
|
|||
except Exception:
|
||||
alt_text = "A map showing the location of the outage."
|
||||
|
||||
map_image = map.render()
|
||||
map_image: Image = context.render_pillow(512, 512)
|
||||
|
||||
with io.BytesIO() as map_image_file:
|
||||
map_image.save(map_image_file, format="WebP", method=6)
|
||||
map_image.save(map_image_file, format="PNG")
|
||||
map_media_post = mastodon_client.media_post(
|
||||
map_image_file.getvalue(),
|
||||
mime_type="image/webp",
|
||||
mime_type="image/png",
|
||||
description=alt_text,
|
||||
)
|
||||
map_media_post_id = map_media_post["id"]
|
||||
|
@ -273,9 +248,7 @@ Cause: {}
|
|||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
type_annotation_map = {
|
||||
Geometry: DBGeometry,
|
||||
}
|
||||
pass
|
||||
|
||||
|
||||
class SclOutage(Base):
|
||||
|
@ -293,13 +266,9 @@ class SclOutage(Base):
|
|||
start_time: Mapped[datetime] = mapped_column()
|
||||
num_people: Mapped[int] = mapped_column()
|
||||
max_num_people: Mapped[int] = mapped_column()
|
||||
neighborhood: Mapped[Optional[str]] = mapped_column()
|
||||
city: Mapped[Optional[str]] = mapped_column()
|
||||
outage_geometries: Mapped[Optional[Geometry]] = mapped_column()
|
||||
geometries_modified: Mapped[Optional[bool]] = mapped_column()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"SclOutage(scl_outage_id={self.scl_outage_id!r}, most_recent_post_id={self.most_recent_post_id!r}, initial_post_id={self.initial_post_id!r}, map_media_post_id={self.map_media_post_id!r}, last_updated_time={self.last_updated_time!r}, no_longer_in_response_time={self.no_longer_in_response_time!r}, start_time={self.start_time!r}, num_people={self.num_people!r}, max_num_people={self.max_num_people!r}, neighborhood={self.neighborhood!r}, city={self.city!r}, outage_geometries={self.outage_geometries!r}, geometries_modified={self.geometries_modified!r})"
|
||||
return f"SclOutage(scl_outage_id={self.scl_outage_id!r}, most_recent_post_id={self.most_recent_post_id!r}, initial_post_id={self.initial_post_id!r}, map_media_post_id={self.map_media_post_id!r}, last_updated_time={self.last_updated_time!r}, no_longer_in_response_time={self.no_longer_in_response_time!r}), start_time={self.start_time!r}), num_people={self.num_people!r}), max_num_people={self.max_num_people!r})"
|
||||
|
||||
|
||||
engine = create_engine("sqlite:///scl.db")
|
||||
|
@ -331,18 +300,7 @@ with Session(engine) as session:
|
|||
existing_record = lookup_result.one()
|
||||
updated_properties = []
|
||||
updated_entries = []
|
||||
|
||||
est_restoration_diff_mins = (
|
||||
abs(
|
||||
(
|
||||
estimated_restoration_time
|
||||
- existing_record.estimated_restoration_time
|
||||
).total_seconds()
|
||||
)
|
||||
/ 60
|
||||
)
|
||||
# Only post if estimated restoration time has changed by 60m or more
|
||||
if est_restoration_diff_mins >= 60:
|
||||
if estimated_restoration_time != existing_record.estimated_restoration_time:
|
||||
existing_record.estimated_restoration_time = estimated_restoration_time
|
||||
if estimated_restoration_time > datetime.now():
|
||||
# New estimated restoration time is in the future, so likely to be a real time
|
||||
|
@ -371,10 +329,6 @@ with Session(engine) as session:
|
|||
# Used to determine the maximum number of people affected by this outage, to determine if it's worth posting about
|
||||
existing_record.max_num_people = event["numPeople"]
|
||||
max_event_class = classify_event_size(existing_record.max_num_people)
|
||||
if existing_record.outage_geometries != outage_geometries:
|
||||
print("Geometries modified")
|
||||
existing_record.outage_geometries = outage_geometries
|
||||
existing_record.geometries_modified = True
|
||||
|
||||
if updated_properties:
|
||||
updated_properties.sort()
|
||||
|
@ -395,18 +349,13 @@ with Session(engine) as session:
|
|||
),
|
||||
)
|
||||
if max_event_class["is_postable"] and existing_record.initial_post_id:
|
||||
try:
|
||||
post_result = mastodon_client.status_post(
|
||||
status="\n".join(updated_entries),
|
||||
in_reply_to_id=existing_record.most_recent_post_id,
|
||||
visibility="unlisted",
|
||||
language="en",
|
||||
)
|
||||
existing_record.most_recent_post_id = post_result["id"]
|
||||
except mastodon.MastodonNotFoundError:
|
||||
print(
|
||||
"Could not post a reply to the existing post, skip this update"
|
||||
)
|
||||
post_result = mastodon_client.status_post(
|
||||
status="\n".join(updated_entries),
|
||||
in_reply_to_id=existing_record.most_recent_post_id,
|
||||
visibility="unlisted",
|
||||
language="en",
|
||||
)
|
||||
existing_record.most_recent_post_id = post_result["id"]
|
||||
elif max_event_class["is_postable"]:
|
||||
print(
|
||||
"Posting an event that grew above the threshold required to post"
|
||||
|
@ -418,33 +367,17 @@ with Session(engine) as session:
|
|||
estimated_restoration_time,
|
||||
outage_geometries,
|
||||
)
|
||||
try:
|
||||
existing_record.neighborhood = initial_post_result[
|
||||
"neighborhood"
|
||||
]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
try:
|
||||
existing_record.city = initial_post_result["city"]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
existing_record.initial_post_id = initial_post_result["post_id"]
|
||||
existing_record.most_recent_post_id = initial_post_result["post_id"]
|
||||
existing_record.map_media_post_id = initial_post_result[
|
||||
"map_media_post_id"
|
||||
]
|
||||
else:
|
||||
print("Existing record was found, and no properties were updated.")
|
||||
session.commit()
|
||||
|
||||
except NoResultFound:
|
||||
print("Existing record not found")
|
||||
post_id = None
|
||||
map_media_post_id = None
|
||||
neighborhood = None
|
||||
city = None
|
||||
if not event_class["is_postable"]:
|
||||
print(
|
||||
"Outage is {} considered postable, will not post".format(
|
||||
|
@ -462,16 +395,6 @@ with Session(engine) as session:
|
|||
post_id = initial_post_result["post_id"]
|
||||
map_media_post_id = initial_post_result["map_media_post_id"]
|
||||
|
||||
try:
|
||||
neighborhood = initial_post_result["neighborhood"]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
try:
|
||||
city = initial_post_result["city"]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
new_outage_record = SclOutage(
|
||||
scl_outage_id=event["id"],
|
||||
outage_user_id=event["identifier"],
|
||||
|
@ -485,9 +408,6 @@ with Session(engine) as session:
|
|||
start_time=start_time,
|
||||
num_people=event["numPeople"],
|
||||
max_num_people=event["numPeople"],
|
||||
neighborhood=neighborhood,
|
||||
city=city,
|
||||
outage_geometries=outage_geometries,
|
||||
)
|
||||
session.add(new_outage_record)
|
||||
session.commit()
|
||||
|
@ -504,7 +424,7 @@ with Session(engine) as session:
|
|||
if active_outage.most_recent_post_id:
|
||||
try:
|
||||
post_result = mastodon_client.status_post(
|
||||
status="This outage is no longer in the SCL feed, which usually means it's either been resolved, or split into multiple smaller outages.\n\n#SeattleCityLightOutage #SCLOutage",
|
||||
status="This outage is reported to be resolved.\n\n#SeattleCityLightOutage #SCLOutage",
|
||||
in_reply_to_id=active_outage.most_recent_post_id,
|
||||
visibility="public",
|
||||
language="en",
|
||||
|
|
Loading…
Reference in a new issue