From 03e4383c5bee2f706925811d0f73002ba5764ead Mon Sep 17 00:00:00 2001 From: dpslwk Date: Thu, 14 Apr 2022 12:02:20 +0100 Subject: [PATCH] GpsexportPlugin update --- buildmap/plugins/gpsexport/__init__.py | 79 ++++++----- buildmap/plugins/gpsexport/exportsql.py | 132 +++++++++--------- .../gpsexport/templates}/export-html.jinja | 4 +- .../plugins/gpsexport/templates}/kml.jinja | 0 4 files changed, 106 insertions(+), 109 deletions(-) rename {templates => buildmap/plugins/gpsexport/templates}/export-html.jinja (69%) rename {templates => buildmap/plugins/gpsexport/templates}/kml.jinja (100%) diff --git a/buildmap/plugins/gpsexport/__init__.py b/buildmap/plugins/gpsexport/__init__.py index ba54456..7114768 100644 --- a/buildmap/plugins/gpsexport/__init__.py +++ b/buildmap/plugins/gpsexport/__init__.py @@ -1,26 +1,33 @@ +from __future__ import absolute_import import csv import datetime import logging import os import shutil import time -from jinja2 import Environment, PackageLoader +from jinja2 import Environment, PackageLoader, select_autoescape from sqlalchemy import text -from util import write_file +# from util import write_file +def write_file(name, data): + with open(name, "w") as fp: + fp.write(data) + + from . import exportsql -class GPSExport(object): - def __init__(self, _, config, db): +class GpsexportPlugin(object): + def __init__(self, buildmap, _config, opts, db): self.log = logging.getLogger(__name__) self.queries = exportsql.queries - self.config = config - self.base_path = os.path.dirname(os.path.abspath(__file__)) - self.temp_dir = os.path.join(self.base_path, "temp") self.db = db - shutil.rmtree(self.temp_dir, True) - os.makedirs(self.temp_dir) + self.buildmap = buildmap + self.opts = opts + self.env = Environment( + loader=PackageLoader("buildmap.plugins.gpsexport", "templates"), + autoescape=select_autoescape(["html", "xml"]), + ) def run_query(self, query): return self.db.execute(text(query)).fetchall() @@ -28,8 +35,7 @@ def run_query(self, query): def generate_kml(self, dir, name, places): if "kml" not in places[0].keys(): return - env = Environment(loader=PackageLoader("buildmap", "templates")) - template = env.get_template("kml.jinja") + template = self.env.get_template("kml.jinja") write_file(os.path.join(dir, name + ".kml"), template.render(places=places)) if name in self.filesToList: self.filesToList[name].append("kml") @@ -38,7 +44,7 @@ def generate_kml(self, dir, name, places): def generate_csv(self, dir, name, places): with open(os.path.join(dir, name + ".csv"), "w") as csvfile: - fieldnames = places[0].keys() + fieldnames = list(places[0].keys()) fieldnames.sort() if "name" in fieldnames: fieldnames.insert(0, fieldnames.pop(fieldnames.index("name"))) @@ -52,48 +58,41 @@ def generate_csv(self, dir, name, places): self.filesToList[name] = ["csv"] def generate_html(self, dir, files): - env = Environment(loader=PackageLoader("buildmap", "templates")) - template = env.get_template("export-html.jinja") - write_file(os.path.join(dir, "export.html"), template.render(files=files)) + template = self.env.get_template("export-html.jinja") + write_file( + os.path.join(dir, "export.html"), + template.render(files=files, name=self.opts.get("name")), + ) def run(self): start_time = time.time() self.log.info("Generating map GPS exports...") - kmlDir = self.config.output_directory + "/kml" - tempKmlDir = kmlDir + "-" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - oldKmlDir = kmlDir + "-old" - csvDir = self.config.output_directory + "/csv" - tempCsvDir = csvDir + "-" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - oldCsvDir = csvDir + "-old" + + out_path = self.buildmap.resolve_path(self.buildmap.config["web_directory"]) + + kmlDir = out_path + "/kml" + csvDir = out_path + "/csv" results = {} self.log.info("Getting map data") - for name, query in self.queries.iteritems(): + for name, query in self.queries.items(): self.log.info("Fetching %s" % (name)) results[name] = self.run_query(query) self.log.info("Generating kml/csv files...") - os.mkdir(tempKmlDir) - os.mkdir(tempCsvDir) + + if not os.path.isdir(kmlDir): + os.makedirs(kmlDir) + if not os.path.isdir(csvDir): + os.makedirs(csvDir) + self.filesToList = {} - for name, places in results.iteritems(): - self.generate_kml(tempKmlDir, name, places) - self.generate_csv(tempCsvDir, name, places) - - self.log.info("Moving new kml into place...") - shutil.rmtree(oldKmlDir, True) - if os.path.exists(kmlDir): - shutil.move(kmlDir, oldKmlDir) - shutil.move(tempKmlDir, kmlDir) - - self.log.info("Moving new csv into place...") - shutil.rmtree(oldCsvDir, True) - if os.path.exists(csvDir): - shutil.move(csvDir, oldCsvDir) - shutil.move(tempCsvDir, csvDir) + for name, places in results.items(): + self.generate_kml(kmlDir, name, places) + self.generate_csv(csvDir, name, places) self.log.info("Generaring html links page") - self.generate_html(self.config.output_directory, self.filesToList) + self.generate_html(out_path, self.filesToList) self.log.info("Generation complete in %.2f seconds", time.time() - start_time) diff --git a/buildmap/plugins/gpsexport/exportsql.py b/buildmap/plugins/gpsexport/exportsql.py index 9f00f60..f45a3e3 100644 --- a/buildmap/plugins/gpsexport/exportsql.py +++ b/buildmap/plugins/gpsexport/exportsql.py @@ -1,73 +1,71 @@ # flake8: noqa queries = { - "TentCentres": """SELECT tent as name, - tent_size, - split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as lat, - split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as long, - ST_AsKML(ST_Centroid(wkb_geometry)) AS kml - FROM site_plan - WHERE tent IS NOT NULL - ORDER by name ASC""", + # "TentCentres": """SELECT tent AS name, + # tent_size, + # split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS lat, + # split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS long, + # ST_AsKML(ST_Centroid(wkb_geometry)) AS kml + # FROM site_plan + # WHERE tent IS NOT NULL + # ORDER BY name ASC""", "TentCorners": """ - SELECT name, corner_lat, corner_long, ST_AsKML(ST_SetSRID(ST_MakePoint(corner_long, corner_lat), 4326)) AS kml FROM (SELECT DISTINCT tent as name, -ST_Y(ST_Transform((ST_DumpPoints(wkb_geometry)).geom, 4326)) as corner_lat, -ST_X(ST_Transform((ST_DumpPoints(wkb_geometry)).geom, 4326)) as corner_long + SELECT name, corner_lat, corner_long, ST_AsKML(ST_SetSRID(ST_MakePoint(corner_long, corner_lat), 4326)) AS kml FROM (SELECT DISTINCT tent AS name, +ST_Y(ST_Transform((ST_DumpPoints(wkb_geometry)).geom, 4326)) AS corner_lat, +ST_X(ST_Transform((ST_DumpPoints(wkb_geometry)).geom, 4326)) AS corner_long FROM site_plan WHERE tent IS NOT NULL - ORDER by name ASC) a; - """, - "Datenklo": """SELECT dk_name as name, - split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as lat, - split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as long, - ST_AsKML(ST_Centroid(wkb_geometry)) AS kml - FROM site_plan - WHERE dk_name is not NULL - ORDER by name ASC""", - "NOC": """SELECT emfnet as name, - split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as lat, - split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as long, - ST_AsKML(ST_Centroid(wkb_geometry)) AS kml - FROM site_plan - WHERE emfnet is not NULL - ORDER by name ASC""", - "Power": """SELECT distro_name as name, - power_distro as type, - split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as lat, - split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as long, - ST_AsKML(ST_Centroid(wkb_geometry)) AS kml - FROM site_plan - WHERE distro_name is not NULL - ORDER by name ASC""", - "Festoons": """SELECT - split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as start_lat, - split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as start_long, - split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as end_lat, - split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as end_long, - ST_AsKML(wkb_geometry) as kml - FROM site_plan - WHERE layer = 'Lighting - Festoon'""", - "PathsEuromat": """SELECT - split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as start_lat, - split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as start_long, - split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as end_lat, - split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as end_long, - ST_AsKML(wkb_geometry) as kml - FROM site_plan - WHERE layer = 'Paths - Euromat'""", - "PathsFireLane": """SELECT - split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as start_lat, - split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as start_long, - split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as end_lat, - split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as end_long, - ST_AsKML(wkb_geometry) as kml - FROM site_plan - WHERE layer = 'Paths - Fire Lane'""", - "FenceHeras": """SELECT - split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as start_lat, - split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as start_long, - split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) as end_lat, - split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) as end_long, - ST_AsKML(wkb_geometry) as kml - FROM site_plan - WHERE layer = 'Fence (Heras)'""", + ORDER BY name ASC) a;""", + "Datenklo": """SELECT dk_name AS name, + split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS lat, + split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS long, + ST_AsKML(ST_Centroid(wkb_geometry)) AS kml + FROM site_plan + WHERE layer = 'NOC ... DK'""", + # "NOC": """SELECT emfnet AS name, + # split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS lat, + # split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS long, + # ST_AsKML(ST_Centroid(wkb_geometry)) AS kml + # FROM site_plan + # WHERE emfnet is not NULL + # ORDER BY name ASC""", + "Power": """SELECT name AS name, + distro AS type, + split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS lat, + split_part(ST_AsLatLonText(ST_Transform(ST_Centroid(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS long, + ST_AsKML(ST_Centroid(wkb_geometry)) AS kml + FROM site_plan + WHERE distro IS NOT NULL + ORDER BY name ASC""", + "Festoons": """SELECT + split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS start_lat, + split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS start_long, + split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS end_lat, + split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS end_long, + ST_AsKML(wkb_geometry) AS kml + FROM site_plan + WHERE layer = 'Lighting ... Festoon'""", + "PathsTrackway": """SELECT + split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS start_lat, + split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS start_long, + split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS end_lat, + split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS end_long, + ST_AsKML(wkb_geometry) AS kml + FROM site_plan + WHERE layer = 'Paths ... Trackway'""", + "PathsFire": """SELECT + split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS start_lat, + split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS start_long, + split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS end_lat, + split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS end_long, + ST_AsKML(wkb_geometry) AS kml + FROM site_plan + WHERE layer = 'Paths ... Fire'""", + "HerasInternal": """SELECT + split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS start_lat, + split_part(ST_AsLatLonText(ST_Transform(ST_StartPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS start_long, + split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 1) AS end_lat, + split_part(ST_AsLatLonText(ST_Transform(ST_EndPoint(wkb_geometry), 4326), 'DD.DDDDDD'), ' ', 2) AS end_long, + ST_AsKML(wkb_geometry) AS kml + FROM site_plan + WHERE layer = 'Heras (internal)'""", } diff --git a/templates/export-html.jinja b/buildmap/plugins/gpsexport/templates/export-html.jinja similarity index 69% rename from templates/export-html.jinja rename to buildmap/plugins/gpsexport/templates/export-html.jinja index 09fc353..093a74a 100644 --- a/templates/export-html.jinja +++ b/buildmap/plugins/gpsexport/templates/export-html.jinja @@ -1,11 +1,11 @@ - EMF 2016 Map exports + {{name}} Map exports diff --git a/templates/kml.jinja b/buildmap/plugins/gpsexport/templates/kml.jinja similarity index 100% rename from templates/kml.jinja rename to buildmap/plugins/gpsexport/templates/kml.jinja