Added openstreetmap maps from coord

pull/18/head
Sami Mokaddem 2017-10-11 10:47:11 +02:00
parent 3d79103cd5
commit 41644425fb
6 changed files with 165 additions and 38 deletions

View File

@ -5,9 +5,16 @@ graph_log_refresh_rate = 1
[Log]
fieldname_order=["time", "level", "name", "message"]
[Redis]
[RedisLog]
host=localhost
port=6250
db=0
channel=1
zmq_url="tcp://crf.circl.lu:5556"
[RedisMap]
host=localhost
port=6250
db=1
channelProc=CoordToProcess
channelDisp=PicToDisplay

67
retreive_map_pic.py Executable file
View File

@ -0,0 +1,67 @@
#!/usr/bin/env python3.5
import redis
import requests
import shutil
import json
import math
import sys, os
import time
from subprocess import PIPE, Popen
URL_OPEN_MAP = "http://tile.openstreetmap.org/{zoom}/{x}/{y}.png"
MAP_DIR = "static/maps/"
ZOOM = 16
dlat = 2
dlon = 2
serv = redis.StrictRedis('localhost', 6250, 2)
channel_proc = "CoordToProcess"
channel_disp = "PicToDisplay"
def lon2tile(lon, zoom):
return (math.floor((lon+180)/360*math.pow(2,zoom)))
def lat2tile(lat, zoom):
return (math.floor((1-math.log(math.tan(lat*math.pi/180) + 1/math.cos(lat*math.pi/180))/math.pi)/2 *math.pow(2,zoom)))
def main():
pubsub = serv.pubsub(ignore_subscribe_messages=True)
pubsub.subscribe(channel_proc)
while True:
for d in pubsub.listen():
#data = json.loads(d.decode('utf8'))
download_maps(d['data'])
def create_box_around_coord(lon,lat):
pics = []
for dx in range(-1,2,1):
for dy in range(-1,2,1):
pics.append(URL_OPEN_MAP.format(x=lon+dx, y=lat+dy, zoom=ZOOM))
return pics
def download_and_merge(url_list, coord):
with open('in.txt', 'w') as f:
for i, url in enumerate(url_list):
f.write(str(i+1)+' '+url+'\n')
map_name = "map_{lon}-{lat}.png".format(lon=coord['lon'], lat=coord['lat'])
path = os.path.join(MAP_DIR, map_name)
FULL_COMMAND = "cat in.txt | parallel --colsep ' ' wget -O maps/{1} {2} && montage -geometry +0+0 maps/1 maps/4 maps/7 maps/2 maps/5 maps/8 maps/3 maps/6 maps/9 "+path
os.system(FULL_COMMAND)
return map_name
def download_maps(coord):
coord = json.loads(coord.decode('utf8'))
print(str(coord))
lat = lat2tile(coord['lat'], ZOOM)
lon = lon2tile(coord['lon'], ZOOM)
urls = create_box_around_coord(lon, lat)
map_name = download_and_merge(urls, coord)
print(map_name)
serv.publish(channel_disp, map_name)
if __name__ == '__main__':
main()

View File

@ -2,6 +2,7 @@
from flask import Flask, render_template, Response
import json
import redis
import random
import configparser
from time import gmtime as now
from time import sleep, strftime
@ -13,13 +14,19 @@ cfg.read(configfile)
app = Flask(__name__)
redis_server = redis.StrictRedis(
host=cfg.get('Redis', 'host'),
port=cfg.getint('Redis', 'port'),
db=cfg.getint('Redis', 'db'))
redis_server_log = redis.StrictRedis(
host=cfg.get('RedisLog', 'host'),
port=cfg.getint('RedisLog', 'port'),
db=cfg.getint('RedisLog', 'db'))
redis_server_map = redis.StrictRedis(
host=cfg.get('RedisMap', 'host'),
port=cfg.getint('RedisMap', 'port'),
db=cfg.getint('RedisMap', 'db'))
subscriber = redis_server.pubsub(ignore_subscribe_messages=True)
subscriber.psubscribe(cfg.get('Redis', 'channel'))
subscriber_log = redis_server_log.pubsub(ignore_subscribe_messages=True)
subscriber_log.psubscribe(cfg.get('RedisLog', 'channel'))
subscriber_map = redis_server_map.pubsub(ignore_subscribe_messages=True)
subscriber_map.psubscribe(cfg.get('RedisMap', 'channelDisp'))
eventNumber = 0
class LogItem():
@ -80,16 +87,26 @@ def index():
@app.route("/_logs")
def logs():
return Response(event_stream(), mimetype="text/event-stream")
return Response(event_stream_log(), mimetype="text/event-stream")
@app.route("/_maps")
def maps():
return Response(event_stream_maps(), mimetype="text/event-stream")
@app.route("/_get_log_head")
def getLogHead():
return json.dumps(LogItem('').get_head_row())
def event_stream():
for msg in subscriber.listen():
def event_stream_log():
for msg in subscriber_log.listen():
content = msg['data']
print('sending', content)
yield EventMessage(content).to_json()
def event_stream_maps():
for msg in subscriber_map.listen():
content = msg['data'].decode('utf8')
yield 'data: {}\n\n'.format(json.dumps({ 'path': content }))
if __name__ == '__main__':
app.run(host='localhost', port=8000)
app.run(host='localhost', port=8000, threaded=True)

View File

@ -3,6 +3,7 @@ var updateInterval = 1000*graph_log_refresh_rate; // 1s
var maxNumPoint = 60;
var maxNumCoord = 100;
var img_to_change = 1;
var emptyArray = [];
var mapCoord = [];
var mapVal = [];
@ -142,25 +143,40 @@ var optionsPieChart = {
$(document).ready(function () {
createHead(function() {
if (!!window.EventSource) {
var source = new EventSource(urlForLogs);
var source_log = new EventSource(urlForLogs);
source.onopen = function(){
//console.log('connection is opened. '+source.readyState);
source_log.onopen = function(){
//console.log('connection is opened. '+source_log.readyState);
};
source.onerror = function(){
//console.log('error: '+source.readyState);
source_log.onerror = function(){
//console.log('error: '+source_log.readyState);
};
source.onmessage = function(event) {
source_log.onmessage = function(event) {
var json = jQuery.parseJSON( event.data );
updateLogTable(json.feedName, json.log);
};
} else {
console.log("No event source");
console.log("No event source_log");
}
});
var source_map = new EventSource(urlForMaps);
source_map.onmessage = function(event) {
var json = jQuery.parseJSON( event.data );
var img2 = linkForDefaultMap.replace(/\/[^\/]+$/, "/"+json.path);
$("#img"+img_to_change).fadeOut(400, function(){ $(this).attr('src', img2); }).fadeIn(400);
img_to_change = img_to_change == 4 ? 0 : img_to_change+1;
};
source_map.onopen = function(){
console.log('connection is opened. '+source_map.readyState);
};
source_map.onerror = function(){
console.log('error: '+source_map.readyState);
};
});
var rData = [
@ -298,7 +314,7 @@ function marker_animation(x, y, curNumMarker) {
.css({'left': x-15 + 'px'}) /* HACK to center the effect */
.css({'top': y-15 + 'px'})
.css({ 'background-color': markerColor })
.animate({ opacity: 0, scale: 1, height: '80px', width:'80px', margin: '-25px' }, 1000, 'linear', function(){$(this).remove(); })
.animate({ opacity: 0, scale: 1, height: '80px', width:'80px', margin: '-25px' }, 700, 'linear', function(){$(this).remove(); })
);
}

View File

@ -73,10 +73,22 @@ img {
<div id="feedDiv1A" style="width:50%; height: calc(100% - 30px); position: relative; float: left;"></div>
<div id="feedDiv1B" style="width:50%; height: calc(100% - 30px); position: relative; float: left;"></div>-->
<div id="panelbody" class="panel-body" style="height: 100%;">
<div id="feedDiv1A" style="width:50%; height: 50%; position: relative; float: left;"><img id="img1" src="{{ url_for('static', filename='maps/map.png') }}" style="width:98%;height:97%;"></div>
<div id="feedDiv1B" style="width:50%; height: 50%; position: relative; float: left;"><img id="img2" src="{{ url_for('static', filename='maps/map.png') }}" style="width:98%;height:97%;"></div>
<div id="feedDiv2A" style="width:50%; height: 50%; position: relative; float: left;"><img id="img3" src="{{ url_for('static', filename='maps/map.png') }}" style="width:98%;height:97%;"></div>
<div id="feedDiv2B" style="width:50%; height: 50%; position: relative; float: left;"><img id="img4" src="{{ url_for('static', filename='maps/map.png') }}" style="width:98%;height:97%;"></div>
<div id="feedDivMap1" style="width:50%; height: 50%; position: relative; float: left;">
<small id="textMap1">text1</small>
<img id="img1" src="{{ url_for('static', filename='maps/default.png') }}" style="width:98%;height:97%;">
</div>
<div id="feedDivMap2" style="width:50%; height: 50%; position: relative; float: left;">
<small id="textMap1">text2</small>
<img id="img2" src="{{ url_for('static', filename='maps/default.png') }}" style="width:98%;height:97%;">
</div>
<div id="feedDivMap3" style="width:50%; height: 50%; position: relative; float: left;">
<small style="height:10%;" id="textMap1">text3</small>
<img id="img3" src="{{ url_for('static', filename='maps/default.png') }}" style="width:98%;height:87%;">
</div>
<div id="feedDivMap4" style="width:50%; height: 50%; position: relative; float: left;">
<small style="height:10%;" id="textMap1">text4</small>
<img id="img4" src="{{ url_for('static', filename='maps/default.png') }}" style="width:98%;height:87%;">
</div>
</div>
<!-- /.panel-body -->
@ -166,14 +178,11 @@ img {
/* URL */
var urlForLogs = "{{ url_for('logs') }}";
var urlForHead = "{{ url_for('getLogHead') }}";
var urlForMaps = "{{ url_for('maps') }}";
var linkForDefaultMap = "{{ url_for('static', filename='maps/default.png') }}";
/* DATA */
var graph_log_refresh_rate = {{ graph_log_refresh_rate }};
setTimeout(function(){
var img2 = "{{ url_for('static', filename='maps/map2.png') }}";
$("#img1").fadeOut();
$('#img1').attr('src', img2);
$("#img1").delay(200).fadeIn();
}, 3000);
</script>
<script src="{{ url_for('static', filename='js/index.js') }}"></script>

View File

@ -13,34 +13,45 @@ configfile = os.path.join(os.environ['VIRTUAL_ENV'], '../config.cfg')
cfg = configparser.ConfigParser()
cfg.read(configfile)
zmq_url = cfg.get('Redis', 'zmq_url')
zmq_url = cfg.get('RedisLog', 'zmq_url')
zmq_url = "tcp://crf.circl.lu:5556"
channel = cfg.get('Redis', 'channel')
channel = cfg.get('RedisLog', 'channel')
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect(zmq_url)
socket.setsockopt_string(zmq.SUBSCRIBE, channel)
redis_server = redis.StrictRedis(
host=cfg.get('Redis', 'host'),
port=cfg.getint('Redis', 'port'),
db=cfg.getint('Redis', 'db'))
host=cfg.get('RedisLog', 'host'),
port=cfg.getint('RedisLog', 'port'),
db=cfg.getint('RedisLog', 'db'))
serv_coord = redis.StrictRedis(
host='localhost',
port=6250,
db=1)
channel_proc = "CoordToProcess"
channel_disp = "PicToDisplay"
# server side
pubsub = redis_server.pubsub(ignore_subscribe_messages=True)
while True:
rdm = random.randint(1,3)
time.sleep(float(rdm / 3))
lat = random.randint(-90,90)
lon = random.randint(-90,90)
rdm = random.randint(1,10)
time.sleep(float(rdm))
#lux
lon = random.uniform(5.7373, 6.4823)
lat = random.uniform(49.4061,49.7449)
#central eur
lon = random.uniform(3.936, 9.890)
lat = random.uniform(47.957, 50.999)
content = ["rdm "+str(rdm)]
content = [lat,lon]
jsonContent = json.dumps(content)
to_send = { 'name': 'feeder'+str(rdm), 'log': jsonContent }
redis_server.publish(channel, json.dumps(to_send))
serv_coord.publish(channel_proc, json.dumps({'lat': float(lat), 'lon': float(lon)}))
sys.exit(1)
while True: