From 3758ac21cb8d3a4de6428a3ee9f1735bd9a591aa Mon Sep 17 00:00:00 2001 From: W1CDN Date: Mon, 15 Jan 2024 17:04:13 -0600 Subject: [PATCH] Plot recent packets on map. --- .gitignore | 2 ++ api_app.py | 78 ++++++++++++++++++++++++++++++++------------ templates/index.html | 50 +++++++++++++++++++++++++--- templates/map.html | 18 +++++++++- test_db.py | 36 -------------------- 5 files changed, 123 insertions(+), 61 deletions(-) delete mode 100644 test_db.py diff --git a/.gitignore b/.gitignore index ab303c9..e121693 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ config.ini *.db *.log +/temp/* +*.pyc diff --git a/api_app.py b/api_app.py index ed483d5..fb35a98 100644 --- a/api_app.py +++ b/api_app.py @@ -113,12 +113,31 @@ def index(): station['time_ago'] = timeago.format(station['last_heard_unix'], datetime.datetime.now()) + # Map stuff + frames_locs = list(filter(lambda x: x['latitude'] != None, frames)) + # Make a GeoJSON + geojs = json.dumps({ + "type": "FeatureCollection", + "features":[ + { + "type":"Feature", + "geometry": { + "type":"Point", + "coordinates":[frame['longitude'], frame['latitude']], + }, + "properties":frame, + + } for frame in frames_locs + ] + }) + return render_template('index.html', station_call = config['Settings']['station_call'], station_lat = config['Settings']['station_lat'], station_lon = config['Settings']['station_lon'], frames = frames, - stations = stations) + stations = stations, + geojs = geojs) @api_app.route('/map') def map(): @@ -126,31 +145,50 @@ def map(): # Get the default list of frames from the API frames = json.loads(requests.get(config['Settings']['base_url']+"/packets").text)['data'] - # Make markers for all the frames - id_counter = 0 - markers = '' - marker_ids = [] - for frame in frames: - if frame['latitude'] != None: - # Create unique ID for each marker - idd = 'frame' + str(id_counter) - id_counter += 1 + frames_locs = list(filter(lambda x: x['latitude'] != None, frames)) - # Create each marker - markers += "var {idd} = L.marker([{latitude}, {longitude}]);\ - {idd}.addTo(map).bindTooltip('{from_ssid}', permanent=true).openTooltip();".format(idd=idd, latitude=frame['latitude'],\ - longitude=frame['longitude'], - from_ssid=frame['from'], - created=frame['created']) - # Try to make a list of markers for Leaflet, but not working - marker_ids.append(idd) + # Make a GeoJSON + geojs = json.dumps({ + "type": "FeatureCollection", + "features":[ + { + "type":"Feature", + "geometry": { + "type":"Point", + "coordinates":[frame['longitude'], frame['latitude']], + }, + "properties":frame, + + } for frame in frames_locs + ] + }) + + # Make markers for all the frames + # id_counter = 0 + # markers = '' + # marker_ids = [] + # for frame in frames: + # if frame['latitude'] != None: + # # Create unique ID for each marker + # idd = 'frame' + str(id_counter) + # id_counter += 1 + + # # Create each marker + # markers += "var {idd} = L.marker([{latitude}, {longitude}]);\ + # {idd}.addTo(map).bindTooltip('{from_ssid}', permanent=true).openTooltip();".format(idd=idd, latitude=frame['latitude'],\ + # longitude=frame['longitude'], + # from_ssid=frame['from'], + # created=frame['created']) + # # Try to make a list of markers for Leaflet, but not working + # marker_ids.append(idd) return render_template('map.html', station_lat = config['Settings']['station_lat'], station_lon = config['Settings']['station_lon'], - markers = markers, - marker_ids = marker_ids) + station_call = config['Settings']['station_call'], + #markers = markers, + geojs = geojs) class Packets(Resource): def get(self): diff --git a/templates/index.html b/templates/index.html index 3b864fd..493a1e2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,18 +4,58 @@ {{station_call}} Status + + + + + -

{{station_call}} Status

-Station location: {{station_lat}}, {{station_lon}} +
+
-

About

-This is a work in progress. See https://amiok.net/gitea/W1CDN/aprs_tool for usage. +

{{station_call}} Status

+ Station location: {{station_lat}}, {{station_lon}} + +

About

+ This is a work in progress. See https://amiok.net/gitea/W1CDN/aprs_tool for usage. +
+
+
+ +
+

Recent RF Packets

@@ -40,6 +80,8 @@ This is a work in progress. See OpenStreetMap contributors'}).addTo(map); - {{markers|safe}} + //{{markers|safe}} + + // Show station location + var station = L.marker([{{station_lat}}, {{station_lon}}]).addTo(map).bindTooltip('{{station_call}}', permanent=true).openTooltip(); + + // Show GeoJSON of markers + var group = L.geoJSON({{geojs|safe}}, + { + style: function (feature) { + return {color: feature.properties.color}; + } + }).bindTooltip(function (layer) { + return 'Object '+layer.feature.properties.object_name+' from '+layer.feature.properties.from; + }, permanent=true).addTo(map); + + // Zoom to show all + map.fitBounds(group.getBounds().pad(0.2)); diff --git a/test_db.py b/test_db.py deleted file mode 100644 index e0959e5..0000000 --- a/test_db.py +++ /dev/null @@ -1,36 +0,0 @@ - -# Learn how to update database - -import sqlite3 - -def get_db_connection(): - conn = sqlite3.connect('database.db') - conn.row_factory = sqlite3.Row - return conn - -conn = get_db_connection() - -# Grab a random row from frames table and pretend it is new -cur = conn.cursor() -cur.execute("SELECT [from], id, created_unix FROM frames ORDER BY RANDOM() LIMIT 1;") -rows = cur.fetchall() -results = dict(rows[0]) -values = ', '.join('"%s"' % w for w in results.values()) - - -# Build query -# "from" is wrappedin [] because it is a reserved word and using '' doesn't work. -query3 = "INSERT INTO stations ([from], frames_id, last_heard_unix, count) \ -VALUES("+values+", 1) \ -ON CONFLICT([from]) \ -DO UPDATE SET count = count + 1;" - -# example https://stackoverflow.com/a/50718957/2152245 -# query2 = "INSERT INTO stations ([from], frames_id, last_heard_unix, count) \ -# VALUES('KC9TZN-8', 4068, 1687623864, 1) \ -# ON CONFLICT([from]) \ -# DO UPDATE SET count = count + 1;" - -conn.execute(query3) -conn.commit() -conn.close()