From 4c54b7002a5006927e8d6493f67fff656b79393b Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Mon, 10 Aug 2020 22:55:08 +0200 Subject: [PATCH] Add DB functions to save raw and final data from P1/11 --- bin/datastore.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/bin/datastore.py b/bin/datastore.py index b64d16e..7afc26a 100644 --- a/bin/datastore.py +++ b/bin/datastore.py @@ -3,6 +3,7 @@ import json import psycopg2 import configuration import preplots +import p111 """ Interface to the PostgreSQL database. @@ -334,6 +335,88 @@ class Datastore: self.maybe_commit() + def save_raw_p111 (self, records, fileinfo, filepath, epsg = 0, filedata = None): + + with self.conn.cursor() as cursor: + hash = self.add_file(filepath, cursor) + incr = p111.point_number(records[0]) <= p111.point_number(records[-1]) + + qry = """ + INSERT INTO raw_lines (sequence, line, remarks, ntbp, incr) + VALUES (%s, %s, '', %s, %s) + ON CONFLICT DO NOTHING; + """ + + cursor.execute(qry, (fileinfo["sequence"], fileinfo["line"], False, incr)) + + qry = """ + INSERT INTO raw_lines_files (sequence, hash) + VALUES (%s, %s) + ON CONFLICT DO NOTHING; + """ + + cursor.execute(qry, (fileinfo["sequence"], hash)) + + + shots = [ (fileinfo["sequence"], p111.line(r), p111.point_number(r), r["Object Ref. Number"], r["tstamp"], hash, p111.easting(r), p111.northing(r), epsg) for r in records ] + + qry = """ + INSERT INTO raw_shots (sequence, line, point, objref, tstamp, hash, geometry) + VALUES (%s, %s, %s, %s, %s, %s, ST_SetSRID(ST_MakePoint(%s, %s), %s)) + ON CONFLICT (sequence, point) DO UPDATE + SET + objref = EXCLUDED.objref, tstamp = EXCLUDED.tstamp, + hash = EXCLUDED.hash, geometry = EXCLUDED.geometry; + """ + + cursor.executemany(qry, shots) + + if filedata is not None: + self.save_file_data(filepath, json.dumps(filedata), cursor) + + self.maybe_commit() + + def save_final_p111 (self, records, fileinfo, filepath, epsg = 0, filedata = None): + + with self.conn.cursor() as cursor: + hash = self.add_file(filepath, cursor) + + qry = """ + INSERT INTO final_lines (sequence, line, remarks) + VALUES (%s, %s, '') + ON CONFLICT DO NOTHING; + """ + + cursor.execute(qry, (fileinfo["sequence"], fileinfo["line"])) + + qry = """ + INSERT INTO final_lines_files (sequence, hash) + VALUES (%s, %s) + ON CONFLICT DO NOTHING; + """ + + cursor.execute(qry, (fileinfo["sequence"], hash)) + + + shots = [ (fileinfo["sequence"], p111.line(r), p111.point_number(r), r["Object Ref. Number"], r["tstamp"], hash, p111.easting(r), p111.northing(r), epsg) for r in records ] + + qry = """ + INSERT INTO final_shots (sequence, line, point, objref, tstamp, hash, geometry) + VALUES (%s, %s, %s, %s, %s, %s, ST_SetSRID(ST_MakePoint(%s, %s), %s)) + ON CONFLICT (sequence, point) DO UPDATE + SET + objref = EXCLUDED.objref, tstamp = EXCLUDED.tstamp, + hash = EXCLUDED.hash, geometry = EXCLUDED.geometry; + """ + + cursor.executemany(qry, shots) + + if filedata is not None: + self.save_file_data(filepath, json.dumps(filedata), cursor) + + self.maybe_commit() + + def save_file_data(self, path, filedata, cursor = None): """ Save arbitrary data associated with a file.