Skip to content

Commit 62b4ea4

Browse files
committed
Make the silly maps look nicer
1 parent 7f651c7 commit 62b4ea4

File tree

16 files changed

+457
-205
lines changed

16 files changed

+457
-205
lines changed

every_election/apps/elections/templates/elections/election_summary.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ <h3>{{ object.election_subtype }}</h3>
1818
{% include "./division_map.html" %}
1919
{% endif %}
2020

21+
{{ svg|safe }}
22+
23+
2124
<dl class="ds-descriptions">
2225
<div>
2326
<dt>ID</dt>

every_election/apps/elections/views/general.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from elections.constants import ELECTION_TYPES
99
from elections.forms import NoticeOfElectionForm
1010
from elections.models import ElectionType, Election, Document
11+
from og_images.svg_maker import SVGGenerator
1112

1213

1314
class ElectionTypesView(ListView):
@@ -117,6 +118,8 @@ def get_context_data(self, **kwargs):
117118
)
118119
context["form"] = form
119120
context["user_can_upload_docs"] = user_is_moderator(self.request.user)
121+
if not self.object.group_type:
122+
context["svg"] = SVGGenerator(self.object).svg()
120123
return context
121124

122125
def post(self, *args, **kwargs):

every_election/apps/og_images/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class OgImagesConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "og_images"

every_election/apps/og_images/management/__init__.py

Whitespace-only changes.

every_election/apps/og_images/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.core.management import BaseCommand
2+
3+
from og_images.svg_maker import SVGGenerator
4+
5+
6+
class Command(BaseCommand):
7+
def handle(self, *args, **options):
8+
svg = SVGGenerator(21888)
9+
svg.write_svg()
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import subprocess
2+
from pathlib import Path
3+
4+
from django.conf import settings
5+
from django.core.management.base import BaseCommand
6+
from django.db import connection
7+
8+
layers = {
9+
"openmap-local": {
10+
"buildings": {"file_glob": "*_Building.shp"},
11+
"roads": {"file_glob": "*_Road.shp"},
12+
"surface_water": {"file_glob": "*_SurfaceWater_Area.shp"},
13+
"tidal": {"file_glob": "*_TidalWater.shp"},
14+
"stations": {"file_glob": "*_RailwayStation.shp"},
15+
"railway_track": {"file_glob": "*_RailwayTrack.shp"},
16+
"railway_tunnel": {"file_glob": "*_RailwayTunnel.shp"},
17+
"roundabout": {"file_glob": "*_Roundabout.shp"},
18+
},
19+
"greenspaces": {"greenspaces": {"file_glob": "*_GreenspaceSite.shp"}},
20+
}
21+
22+
23+
class Command(BaseCommand):
24+
help = "My shiny new management command."
25+
26+
def add_arguments(self, parser):
27+
parser.add_argument(
28+
"data_dir",
29+
action="store",
30+
help="Path to OS Open Map Local data directory",
31+
type=Path,
32+
)
33+
parser.add_argument(
34+
"--product", default="openmap-local", action="store", choices=layers.keys()
35+
)
36+
37+
def handle(self, *args, **options):
38+
self.cursor = connection.cursor()
39+
40+
self.data_dir = options["data_dir"]
41+
42+
for layer, layer_data in layers[options["product"]].items():
43+
self.create_table(layer, layer_data)
44+
45+
def table_name_from_layer_name(self, layer):
46+
return f"og_images_layer_{layer}".replace("-", "_")
47+
48+
def layer_files(self, layer, layer_data):
49+
return list(self.data_dir.glob(f"**/{layer_data['file_glob']}"))
50+
51+
def create_table(self, layer, layer_data):
52+
"""
53+
Drop the old table
54+
Generate layer SQL
55+
Create the new table
56+
"""
57+
self.cursor.execute(
58+
f"""
59+
DROP TABLE IF EXISTS {self.table_name_from_layer_name(layer)}
60+
""",
61+
)
62+
layer_files = self.layer_files(layer, layer_data)
63+
result = subprocess.run(
64+
[
65+
"shp2pgsql",
66+
"-p",
67+
"-I",
68+
layer_files[0],
69+
self.table_name_from_layer_name(layer),
70+
],
71+
stdout=subprocess.PIPE,
72+
)
73+
self.cursor.execute(result.stdout)
74+
75+
import tempfile
76+
77+
with tempfile.TemporaryDirectory() as tmpdir:
78+
for i, filename in enumerate(layer_files):
79+
result = subprocess.run(
80+
[
81+
"shp2pgsql",
82+
"-D", # Dump format
83+
"-s", # Transform the SRID
84+
"27700:4326", # From:to
85+
"-a", # Append data, don't create table
86+
filename,
87+
self.table_name_from_layer_name(layer),
88+
],
89+
stdout=subprocess.PIPE,
90+
)
91+
temp_path = Path(tmpdir) / f"{i}.dump"
92+
with open(temp_path, "wb") as f:
93+
f.write(result.stdout)
94+
database = settings.DATABASES["default"]
95+
connection_string = f"postgresql://{database['USER']}:{database['PASSWORD']}@{database['HOST']}/{database['NAME']}"
96+
subprocess.run(
97+
[
98+
"psql",
99+
connection_string,
100+
"-f", # File
101+
temp_path,
102+
],
103+
stdout=subprocess.DEVNULL,
104+
)

every_election/apps/og_images/migrations/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)