Skip to content

Commit

Permalink
Make the silly maps look nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Mar 26, 2023
1 parent 7f651c7 commit 62b4ea4
Show file tree
Hide file tree
Showing 16 changed files with 457 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ <h3>{{ object.election_subtype }}</h3>
{% include "./division_map.html" %}
{% endif %}

{{ svg|safe }}


<dl class="ds-descriptions">
<div>
<dt>ID</dt>
Expand Down
3 changes: 3 additions & 0 deletions every_election/apps/elections/views/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from elections.constants import ELECTION_TYPES
from elections.forms import NoticeOfElectionForm
from elections.models import ElectionType, Election, Document
from og_images.svg_maker import SVGGenerator


class ElectionTypesView(ListView):
Expand Down Expand Up @@ -117,6 +118,8 @@ def get_context_data(self, **kwargs):
)
context["form"] = form
context["user_can_upload_docs"] = user_is_moderator(self.request.user)
if not self.object.group_type:
context["svg"] = SVGGenerator(self.object).svg()
return context

def post(self, *args, **kwargs):
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions every_election/apps/og_images/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions every_election/apps/og_images/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class OgImagesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "og_images"
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.core.management import BaseCommand

from og_images.svg_maker import SVGGenerator


class Command(BaseCommand):
def handle(self, *args, **options):
svg = SVGGenerator(21888)
svg.write_svg()
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import subprocess
from pathlib import Path

from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connection

layers = {
"openmap-local": {
"buildings": {"file_glob": "*_Building.shp"},
"roads": {"file_glob": "*_Road.shp"},
"surface_water": {"file_glob": "*_SurfaceWater_Area.shp"},
"tidal": {"file_glob": "*_TidalWater.shp"},
"stations": {"file_glob": "*_RailwayStation.shp"},
"railway_track": {"file_glob": "*_RailwayTrack.shp"},
"railway_tunnel": {"file_glob": "*_RailwayTunnel.shp"},
"roundabout": {"file_glob": "*_Roundabout.shp"},
},
"greenspaces": {"greenspaces": {"file_glob": "*_GreenspaceSite.shp"}},
}


class Command(BaseCommand):
help = "My shiny new management command."

def add_arguments(self, parser):
parser.add_argument(
"data_dir",
action="store",
help="Path to OS Open Map Local data directory",
type=Path,
)
parser.add_argument(
"--product", default="openmap-local", action="store", choices=layers.keys()
)

def handle(self, *args, **options):
self.cursor = connection.cursor()

self.data_dir = options["data_dir"]

for layer, layer_data in layers[options["product"]].items():
self.create_table(layer, layer_data)

def table_name_from_layer_name(self, layer):
return f"og_images_layer_{layer}".replace("-", "_")

def layer_files(self, layer, layer_data):
return list(self.data_dir.glob(f"**/{layer_data['file_glob']}"))

def create_table(self, layer, layer_data):
"""
Drop the old table
Generate layer SQL
Create the new table
"""
self.cursor.execute(
f"""
DROP TABLE IF EXISTS {self.table_name_from_layer_name(layer)}
""",
)
layer_files = self.layer_files(layer, layer_data)
result = subprocess.run(
[
"shp2pgsql",
"-p",
"-I",
layer_files[0],
self.table_name_from_layer_name(layer),
],
stdout=subprocess.PIPE,
)
self.cursor.execute(result.stdout)

import tempfile

with tempfile.TemporaryDirectory() as tmpdir:
for i, filename in enumerate(layer_files):
result = subprocess.run(
[
"shp2pgsql",
"-D", # Dump format
"-s", # Transform the SRID
"27700:4326", # From:to
"-a", # Append data, don't create table
filename,
self.table_name_from_layer_name(layer),
],
stdout=subprocess.PIPE,
)
temp_path = Path(tmpdir) / f"{i}.dump"
with open(temp_path, "wb") as f:
f.write(result.stdout)
database = settings.DATABASES["default"]
connection_string = f"postgresql://{database['USER']}:{database['PASSWORD']}@{database['HOST']}/{database['NAME']}"
subprocess.run(
[
"psql",
connection_string,
"-f", # File
temp_path,
],
stdout=subprocess.DEVNULL,
)
Empty file.
3 changes: 3 additions & 0 deletions every_election/apps/og_images/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
Loading

0 comments on commit 62b4ea4

Please sign in to comment.