Skip to content

Instantly share code, notes, and snippets.

View edigiacomo's full-sized avatar

Emanuele Di Giacomo edigiacomo

  • ARPAE-SIMC
  • Italy
View GitHub Profile
@edigiacomo
edigiacomo / tornado-web-subprocess.py
Last active August 29, 2015 14:09
Un processo viene lanciato alla connessione del client, il quale ne riceve lo stdout e può interromperlo chiudendo la connessione.
# encoding: utf-8
import tornado.httpserver
import tornado.iostream
import tornado.ioloop
import tornado.web
import tornado.process
import tornado.concurrent
import concurrent.futures
import subprocess
@edigiacomo
edigiacomo / leaflet-arpa-wsf-wms.html
Last active August 29, 2015 14:10
This example show how to load some data from ARPA Emilia-Romagna WFS and WMS using Leaflet, OpenLayers (as a GML parser) and jQuery.
<html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers.js"></script>
<script>
var map = null;
var macroarea = null;
@edigiacomo
edigiacomo / settings.devel.py
Created November 21, 2014 07:44
Automatic admin creation after `migrate` command in Django
from django.dispatch import receiver
from django.db import models
from django.contrib.auth.models import User
@receiver(models.signals.post_migrate)
def create_superuser(sender, **kwargs):
if not User.objects.filter(username='admin').first():
User.objects.create_superuser('admin', 'admin@localhost', 'admin')
@edigiacomo
edigiacomo / python-dballe-json-report.py
Last active August 29, 2015 14:26
JSON generic report for Python DB-All.e
#!/usr/bin/env python
import dballe
from itertools import groupby
import sys
def do_report(db, station, date, records):
"""Create a report from a list of records of the same station,date."""
keyfoo = lambda r: (
r.get("timerange"), r.get("level")
@edigiacomo
edigiacomo / ckan-datastore-upsert.py
Last active September 13, 2021 11:36
Simple example of upserting records in a CKAN Datastore
import requests # http://docs.python-requests.org/en/latest/
import json
def upsert_records(records, ckan_url, resource_id, ckan_api_key=None):
"""Send a list of records to CKAN Datastore and return a requests.Response
object (see http://docs.python-requests.org/en/latest/api/#requests.Response).
Example:
url = "http://mysite.org/ckan"
resource_id = "7d33cafe-0118-4b9a-8d8d-e82d1935e2ea"
@edigiacomo
edigiacomo / calcolo-giorni-lavorati-as-2015-16.py
Last active January 29, 2016 19:12
Calcolo dei giorni lavorati durante l'A.S. 2015-16 per il Comune di Pesaro
from datetime import date, timedelta
import calendar
def daterange(start, end, step=timedelta(days=1)):
d = start
while d <= end:
yield d
d += step
@edigiacomo
edigiacomo / arkimet-rescan-archived-section.sh
Last active October 8, 2015 13:43
Rescan archived section in arkimet dataset
#!/bin/bash
trap cleanup EXIT
cleanup()
{
[[ -n "$tmpdir" ]] && rm -rf $tmpdir
}
show_help()
@edigiacomo
edigiacomo / geodjango-postgis-raster-to-png.py
Last active May 9, 2022 04:31
Django view that converts a RasterField to PNG
from django.http import HttpResponse
from django.db.models import BinaryField, Func, F, Value, CharField
from .models import MyModel
def view_png(request):
"""Return a PostGIS raster as a PNG image."""
# MyModel is a django model with a RasterField named "rast".
# Note: ST_AsGdalRaster can reproject the data,
@edigiacomo
edigiacomo / allerta-comuni.py
Last active January 28, 2016 14:17
Allerta comuni
# encoding: utf-8
#
# Crea un GeoJSON dei comuni a partire dal raster di precipitazione in cui
# aggiunge i seguenti attributi alle feature:
# - preci_count: numero di celle nel comune
# - preci_sum: somma della precipitazione nel comune
# - preci_max: massima precipitazione nel comune
# - preci_threshold_50mm_count: numero di celle nel comune che superano i 50mm
# - preci_threshold_50mm_alert: "green" se nessuna cella supera i 50mm,
# "yellow" se la superano meno di dieci, "red" se la superano 10 o più
@edigiacomo
edigiacomo / img2tiff.sh
Last active February 26, 2016 09:37
Convert ENVI file with 52 bands in 52 GeoTIFF with color table
#!/bin/bash
print_help()
{
echo "Usage: $0 ENVI_FILE OUTPUTDIR"
echo ""
echo "Convert a 52 band ENVI file in 52 GeoTiff files with colortable"
}
if [[ "$1" == "-h" || "$1" == "--help" ]]