Skip to content

Instantly share code, notes, and snippets.

View JirkaChadima's full-sized avatar

Jiří Chadima JirkaChadima

View GitHub Profile
@JirkaChadima
JirkaChadima / refresh-data.sh
Last active January 30, 2018 15:10
cz-region-boundaries snippets
#!/bin/bash
# GPS data passed through https://github.com/tyrasd/osmtogeojson
curl -X POST 'https://lz4.overpass-api.de/api/interpreter' -d@overpass-query.txt | osmtogeojson > src.geojson
# utility script from https://github.com/JirkaChadima/cz-region-boundaries/blob/master/scripts/prepare-gps-data.js
node prepare-gps-data.js
# ZIP codes processing
PSCSOURCE='https://www.ceskaposta.cz/documents/10180/3738087/xls_pcobc.zip/50617e56-6e9a-4335-9608-96fec214e6ef'
# https://github.com/JirkaChadima/cz-region-boundaries/blob/master/data/zip/county-region.csv, collected from https://cs.wikipedia.org/wiki/Okresy_v_%C4%8Cesku#Okresy_podle_samospr%C3%A1vn%C3%BDch_kraj%C5%AF
OKRESFILE="../data/zip/county-region.csv"
//...
// java code
//...
/**
* https://stackoverflow.com/a/38675842
*/
protected boolean isInside(Point p, Polygon polygon) {
if (polygon == null) {
return false;
}
@JirkaChadima
JirkaChadima / backend.py
Last active June 30, 2017 18:59
listen-notify-backend
from eventlet.hubs import trampoline
def start_listening(queue):
"""Connects to PgSQL and listens to notifications, inspired by http://initd.org/psycopg/articles/2010/12/01/postgresql-notifications-psycopg2-eventlet/"""
if not queue:
return
connection = psycopg2.connect(DSN)
cursor = connection.cursor()
cursor.execute("LISTEN field_value_change")
@JirkaChadima
JirkaChadima / dbdef.sql
Last active June 30, 2017 18:54
listen-notify-database
-- simple table definition, sequence omitted
CREATE TABLE field_value (
id integer NOT NULL,
value character varying(255) NOT NULL,
field character varying(80)
);
CREATE FUNCTION field_value_notify_change() RETURNS trigger
LANGUAGE plpgsql
AS $$
@JirkaChadima
JirkaChadima / __init__.py
Last active June 30, 2017 18:49
listen-notify-flask-init
from flask import Flask
from flask_socketio import SocketIO
from flask_cors import CORS
app = Flask(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
CORS(app)
socketio = SocketIO(app)
@JirkaChadima
JirkaChadima / dashboard.ts
Last active June 30, 2017 18:49
listen-notify-dasboard-client
// omit imports
const SOCKET_URL = '/* @echo SOCKET_URL */';
@Component({
templateUrl: 'dashboard.component.html',
styleUrls: ['dashboard.component.less']
})
export class Dashboard implements OnInit, OnDestroy {
public mainSocket: WrappedSocket;
@JirkaChadima
JirkaChadima / websocket-service.ts
Last active June 30, 2017 18:46
listen-notify-socket-io-client
// .. omit some imports
@Injectable()
export class WebSocketService {
private sockets: {[name: string]: WrappedSocket} = {};
public connect(url: string, options?: any): WrappedSocket {
if (! this.sockets[url]) {
this.sockets[url] = new WrappedSocket({url: url, options: options});
}
return this.sockets[url];
@JirkaChadima
JirkaChadima / pgsql.py
Created June 27, 2017 18:23
listen-notify-database-handler
# http://initd.org/psycopg/docs/advanced.html#asynchronous-notifications
import select
import psycopg2
import psycopg2.extensions
conn = psycopg2.connect(DSN)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
curs = conn.cursor()
cursor.execute("LISTEN field_value_change")
navigator.geolocation.getCurrentPosition(function (position) {
xhrRequest('https://api.csas.cz/sandbox/webapi/api/v2/places?radius=500&country=CZ&types=ATM&limit=1&lat=' + position.coords.latitude + '&lng=' + position.coords.longitude,
'GET',
'7f29f5d5-c9d4-4266-8e6b-3733064da14x',
function (status, data) {
var response = {};
var json = JSON.parse(data).items[0];
response = {
'KEY_DISTANCE': json.distance,
'KEY_DIRECTION': computeBearing(position.coords, { // computes azimuth
static void detail_window_set_place(Place place) {
if (!detail_window) {
detail_window = detail_window_init();
}
text_layer_set_text(address_layer, place.address);
snprintf(distance_buffer, sizeof(distance_buffer), "%d m", place.distance);
place_direction = place.direction;
text_layer_set_text(distance_layer, distance_buffer);
text_layer_set_text(hours_layer, place.hours);
write_direction_to_ui(); // computes orientation of arrow