Skip to content

Instantly share code, notes, and snippets.

/* https://twitter.com/tkardi/status/1521366132371902465 */
drop table if exists tc;
create table tc as
select gid, path,
st_lineinterpolatepoint(
st_scale(
st_rotate(
st_exteriorring(
st_buffer(
@tkardi
tkardi / lt-update.sh
Created December 21, 2020 15:23
download and prepare LT address data
echo 'Apskritys geometries (from geojson) to lt.adr_gra_apskritys'
ogr2ogr -f PostgreSQL "PG:host=localhost user=postgres dbname=postgres" -lco overwrite=yes -lco schema=lt -lco launder=yes -lco geometry_name=geometry -lco fid=oid -nln adr_gra_apskritys -a_srs epsg:3346 -t_srs epsg:3346 https://www.registrucentras.lt/aduomenys/?byla=adr_gra_apskritys.json -progress
echo 'Apskritys props (from csv) to lt.adr_apskritys'
ogr2ogr -f PostgreSQL "PG:host=localhost user=postgres dbname=postgres" -lco overwrite=yes -lco schema=lt -lco launder=yes -lco fid=oid -nln adr_apskritys https://www.registrucentras.lt/aduomenys/?byla=adr_apskritys.csv
echo 'Savivaldybės geometries (from geojson) to lt.adr_gra_savivaldybes'
@tkardi
tkardi / flightradar.html
Last active April 11, 2023 08:09
Code companion to 'Building a flightradar in Leaflet (pt II)'
<!DOCTYPE html>
<html>
<!-- The following HTML page together with the accompanying
javascript is published under the Unlicense.
SPDX-License-Identifier: Unlicense
-->
<head>
<meta charset="utf-8">
<title>Air traffic map</title>
@tkardi
tkardi / flaskapp.py
Last active May 20, 2022 11:51
Code companion to 'Building a flightradar in Leaflet (pt I)'
# Flask app to run the `proxy.py` script.
# Run it on the path where both of the files are saved with
# (on Linux, for other systems plese see
# http://flask.pocoo.org/docs/1.0/quickstart/#a-minimal-application)
#
# $ export FLASK_APP=flaskapp.py
# $ flask run
#
# This will make the API available at http://127.0.0.1:5000/flightradar
#
@tkardi
tkardi / expanding-polygons.sql
Last active July 30, 2018 10:33
subdividing "empty" space by expanding polygons in PostGIS
/** Subdividing "emtpy" space by expanding polygons
* -----------------------------------------------
* This is the code-companion that is used in
* https://tkardi.ee/writeup/post/2018/07/21/subdividing-space/
*
* Input data includes shapefiles from the Estonian Land Board's homepage
* (downloaded May 2018):
* - Estonian third level administrative units (settlements) with coastline
* borders: "asustusüksus", available for download from
* https://geoportaal.maaamet.ee/docs/haldus_asustus/asustusyksus_shp.zip
@tkardi
tkardi / meshing-up-polygons.sql
Last active July 30, 2018 10:27
Calculate a polygon "mesh" with left/right side properties in PostGIS
/** Calculate a polygon "mesh" with left/right side properties in PostGIS
* ----------------------------------------------------------------------
* Based on Estonian third level administrative units data downloaded
* from the Estonian Land Board's homepage (May 2018) @
* https://geoportaal.maaamet.ee/eng/Maps-and-Data/Administrative-and-Settlement-Division-p312.html
* Direct link to the shapefile - https://geoportaal.maaamet.ee/docs/haldus_asustus/asustusyksus_shp.zip
*
* The following SQL code is published under the Unlicense.
*/
@tkardi
tkardi / benchmarking.py
Created May 9, 2018 09:13
Benchmarking GeoServer WMS 1.3.0/1.1.1 version differences using the same dataset in different native SRS
import argparse
import requests
from PIL import Image
from io import BytesIO
URL = 'http://localhost:8080/geoserver/test/ows?'
params = {
'service':'wms',
'request':'getmap',
@tkardi
tkardi / array_format_as_range.sql
Last active January 2, 2018 05:53
Formats input PostgreSQL int[] to a "human-readable" range representation. Originally meant for making up year-ranges from a comma-separated list of years.
create or replace function array_format_as_range(
arr int[], step int default 1)
returns varchar
as
$$
declare
l int=array_upper(arr,1);
hyphen varchar='-';
comma varchar=', ';
prv int;
@tkardi
tkardi / gwc_restructure_tiledir.py
Created June 19, 2017 11:11
A quick and dirty script for restructing GeServer GWC created tilesets from internal directory structure to TMS-like z/x/y structure. Optionally discarding "empty" files and dividing files into separate subdirectories (e.g based on admin-units) based on an index file Raw
import json
import os
import shutil
from time import time
# absolute path to gwc dir, e.g '/opt/geoserver/data/gwc' or 'c:/geoserver/data_dir/gwc'
TILEPATH = '<path_to_geoserver_gwc_dir>'
# All zoomfolders in os.path.join(TILPATH, TILELAYER) path that should be scanned,
ZOOMFOLDERS = [
@tkardi
tkardi / gs_fix_config.py
Created April 20, 2017 06:05
Fix GeoServer workspace config xml files by replacing certain values (e.g workspace id).
import os
from lxml import etree
def iter_path(path, ext):
"""Returns an iterator over files on path with a certain fileextension."""
for dirname, paths, files in os.walk(path):
for _file in files:
_fname, _ext = os.path.splitext(_file)
if _ext.lower() == '.%s' % ext.lower():