Skip to content

Instantly share code, notes, and snippets.

from datetime import datetime
# https://register.geonorge.no/data/documents/sosi-standard-del-1-generell-del_Realisering%20i%20SOSI-format%20og%20GML_v1_del1-2-realiseringsosigml-4_0_.pdf
# De følgende parameterene må kunne settes utenifra
dataeier = 'NORSAR'
dataprodusent = 'NORSAR'
oppdateringsdato = '20240815'
objtype = 'Kabelgrøft'
objektkatalog = ''
datauttaksdato = datetime.now().strftime("%Y%m%d%H%M%S")
@sickel
sickel / qgistimefields.py
Last active February 23, 2025 14:17
A script to make fields with year, day of year and decimal hour from a epoch field in QGIS
# From a layer with a timestamp as an epoch number,
# adds in fields for year, day of year and decimal hour if they do not exist yet
# goes through the dataset, calculate the year, doy and hour and populates the fields
import datetime
epochfield = 'acqtime'
layer = qgis.utils.iface.activeLayer()
if layer.dataProvider().fieldNameIndex(epochfield) == -1:
# Do not want to do anything on a layer without an epoch field
print(f'Data layer does not have an "{epochfield}" field')
else:
@sickel
sickel / status2json.py
Last active February 14, 2025 12:29 — forked from ravnx/status2json.py
Parse the Nagios status.dat and print out lines to be read by postgresql file_fdw
#!/usr/bin/python3
"""
This script reads the nagios status.dat file and exports it as a csv-file
the output can be read directly by postgresql file_fdw foreign data wrapper.
To set this up, make sure fdw_file is ready to use and define the following table:
create foreign table nagiosstatus(
statustype text,
hostname text,
@sickel
sickel / johnstoncsv.py
Last active October 28, 2024 21:02
Import of data from johnstonarchive nuclear tests
import sys
from html.parser import HTMLParser
"""Download the specific lists from https://www.johnstonsarchive.net/nuclear/tests/index.html.
Save them with the same file name as the header (e.g.
"Database of nuclear tests, United States: part 1, 1945-1963")
run the script with the filename as argument and it will make a tab delimited text file."""
filename=sys.argv[1]
outfile = filename+'.dat'
@sickel
sickel / rendercontrol.py
Last active October 7, 2024 10:42
Turn on or off rendering of categories in a QGIS categorized layer
def rendercats(searchstring):
layer = iface.activeLayer()
renderer = layer.renderer()
categories = renderer.categories()
new_renderer = QgsCategorizedSymbolRenderer(attrName=renderer.classAttribute())
for cat in categories:
value = cat.value()
if value is None:
cat.setRenderState(False)
else:
@sickel
sickel / importforecast.py
Last active March 17, 2024 08:58
Script to store forecasts from met.no
#!/usr/bin/python3
"""
Data are downloaded hourly from api.met.no using
/usr/bin/curl -s -A "<email>" "https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=59&lon=10" > /var/www/html/homelog2/weather.json 2>/dev/null
check https://api.met.no/ for terms and usage
After being downloaded, the data are available on a local http-server. The reason I am not reading the file directly in
this script is that I am also using it for other purposes that need the http
schema for table: https://gist.github.com/sickel/9ae88a47f665c0962120078ed93e103a
@sickel
sickel / forecast.sql
Created March 15, 2024 06:25
Database table to store forecasts from met.no
create table forecast(
parameter varchar not null,
value numeric not null,
unit varchar,
timestamp timestamp not null);
alter table forecast add primary key(parameter,timestamp);
def measureline(search,reverse=False,offset=0):
"""
To be run in the QGIS python console. Select a line, run measureline(<search>) and it will put a marker at the point
<search> mapunits along the line from the start of the line and print out the lat,lon of the point.
By either setting a negative search length or setting reverse to True, it will measure from the end of the line instead.
A number given as <offset> will be subtracted from <search>. If an offset is given, search should be a positive value.
Developed in QGIS 3.20, will probably run in other 3.X version, will probably not work in QGIS 2.X
"""
@sickel
sickel / readfrost.py
Last active September 10, 2021 10:31
Read lightning data from frost,met.no
#!/usr/bin/python3
"""Reads lightning data from frost.met.no. Inserts all points and auxilliary data into a PostGIS database. After allt
the points are inserted, uncertainty ellipses are calculated for each point and stored in the same record. In QGIS it is
then possible to use a calculated geometry for either the point or the ellipse"""
# Using format strings, so needs python >= 3.6
# If this file is stored in a version control system, rewrite the next two lines to
# read clientid and connection parameters from a file that is not checked in in the VCS
client_id="FROSTCLIENTID"
@sickel
sickel / dataleser.py
Last active November 17, 2019 17:30
For å lese krysstabeller
#!/usr/bin/python3
from openpyxl import load_workbook
import sys
filenames=[]
for i in range(1,len(sys.argv)):
print(sys.argv[i])
filenames.append(sys.argv[i])