From 2f68a7cab94758c9f3e49d86ce64babe0c1fe6bf Mon Sep 17 00:00:00 2001 From: Lev Dragunov Date: Fri, 6 Nov 2015 16:11:32 +0300 Subject: [PATCH] Generator log parsing script. --- scripts/import_points.py | 54 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 scripts/import_points.py diff --git a/scripts/import_points.py b/scripts/import_points.py new file mode 100644 index 0000000..e0a1d43 --- /dev/null +++ b/scripts/import_points.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +import psycopg2 +import os, argparse + +def parse_double_points(line): + if "Double" in line: + words = line.split() + lat = words[9].split("(")[1][:-1] + lon = words[10].split(")")[0] + return float(lat), float(lon), 1 + +def parse_unknown_outgoing(line): + if "Unknowing" in line: + words = line.split() + lat = words[9] + lon = words[10] + return float(lat), float(lon), 2 + +filters = (parse_double_points, parse_unknown_outgoing) + +parser = argparse.ArgumentParser(description='Extract borders warning points from generator log files to databse.') +parser.add_argument('-s', '--source', help='Generator log file path.') +parser.add_argument('-c', '--connection', help='Database connection string.') +parser.add_argument('-v', dest='verbose', action='store_true', help='Print status messages') +options = parser.parse_args() + +# Check log file for existance. +if not os.path.exists(options.source): + print "Generator log file", options.source, "does not exists." + exit(1) + +# Process the log. +points = [] +with open(options.source) as logfile: + for line in logfile.readlines(): + for f in filters: + result = f(line) + if result: + points.append(result) + break + +# Print stats. +print "Found {} points".format(len(points)) +print "There are {} points with no external mwm, and {} points if features that have many border intersections". format( + len([a for a in points if a[2] == 2]), len([a for a in points if a[2] == 1]) + ) + +# Commit to the database +conn = psycopg2.connect(options.connection) +cursor = conn.cursor() +cursor.execute("INSERT into points (geom, type) VALUES {}".format(",". + join(["(ST_GeomFromText('POINT({} {})', 4326), {})".format(*p) for p in points]))) +conn.commit()