Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GpsexportPlugin update #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 39 additions & 40 deletions buildmap/plugins/gpsexport/__init__.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
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()

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")
Expand All @@ -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")))
Expand All @@ -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)
132 changes: 65 additions & 67 deletions buildmap/plugins/gpsexport/exportsql.py
Original file line number Diff line number Diff line change
@@ -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)'""",
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>EMF 2016 Map exports</title>
<title>{{name}} Map exports</title>
</head>
<body>
<ul>
{% for name, types in files.iteritems() %}
{% for name, types in files.items() %}
<li>{{name}} {% for type in types%}<a href="/{{type}}/{{name}}.{{type}}">{{type}}</a> {% endfor %}</li>
{% endfor %}
</ul>
Expand Down
File renamed without changes.