Skip to content

Instantly share code, notes, and snippets.

@TimSC
TimSC / splitquoted.py
Last active September 27, 2019 22:57
Split a quoted string by delimiter using python
from __future__ import unicode_literals
from __future__ import print_function
def SplitQuoted(inStr, delim=','):
if '"' in delim or '\\' in delim:
raise ValueError("Delimiter not supported")
l = len(inStr)
@TimSC
TimSC / shapelib_example.cpp
Last active February 11, 2019 14:05
Basic usage of shapelib
//By Tim Sheerman-Chase, released under CC0
//Compile: g++ shapelib_example.cpp -lshp -o shapelib_example
#include <iostream>
#include <vector>
#include <string>
#include "shapefil.h"
using namespace std;
int main(int argc, const char **argv)
@TimSC
TimSC / checkosc.py
Created November 10, 2018 23:53
Find non existent referenced objects in osm osc change file
from pyo5m import OsmData
import requests
if __name__=="__main__":
#
osc = OsmData.OsmChange()
osc.LoadFromOscXml(open("bad_changeset.osc", "rb"))
refNodes, refWays, refRelations = set(), set(), set()
@TimSC
TimSC / pycrocosm-diffs.py
Last active November 10, 2018 06:51
Download osc diffs using custom pycrocosm api method
#Download osc diffs using custom pycrocosm api method
#Released under CC0 license
import requests
import datetime
import os
import gzip
def DownloadDiffs(epoch = datetime.date(2016, 9, 27),
dt=datetime.timedelta(1),
@TimSC
TimSC / syncfsm.py
Last active September 23, 2018 02:09
Sync freestreetmap replication data
import os
import requests
if __name__=="__main__":
interval = "day"
pth = "/home/tim/replicatefsm"
#Check what already exists
fiList = os.listdir(pth)
@TimSC
TimSC / equalize_histogram.py
Last active April 10, 2020 21:34
Equalize histogram using numpy/python
import numpy as np
def EqualizeHistogram(a, bins):
a = np.array(a)
hist, bins2 = np.histogram(a, bins=bins)
#Compute CDF from histogram
cdf = np.cumsum(hist, dtype=np.float64)
cdf = np.hstack(([0], cdf))
cdf = cdf / cdf[-1]
@TimSC
TimSC / natstats-postcode.py
Created June 17, 2018 07:46
Filter National Statistics Office Postcode data set
import csv
# Filtering file https://data.gov.uk/dataset/7ec10db7-c8f4-4a40-8d82-8921935b4865/national-statistics-postcode-lookup-uk
if __name__=="__main__":
reader = csv.DictReader(open("/home/tim/Downloads/National_Statistics_Postcode_Lookup_UK.csv"))
out = csv.DictWriter(open("natstats-po-postcodes.csv", "wt"), reader.fieldnames)
out.writeheader()
@TimSC
TimSC / codepoint.py
Last active May 13, 2018 12:46
OS Codepoint file reader, Released under the CC0 license.
#OS Codepoint file reader, by Tim Sheerman-Chase 2018
#Released under the CC0 license.
import zipfile
import csv
import os
import io
#Get Code point open from https://www.ordnancesurvey.co.uk/business-and-government/products/code-point-open.html
def ReadCodePoint(fi, callback):
@TimSC
TimSC / fileserv.py
Last active May 9, 2018 09:57
Simple python web server to control access to a single file (GET/PUT)
#To write: curl http://localhost:8000/ --upload-file test.xml
#and to read: curl http://localhost:8000/
from __future__ import print_function
from __future__ import unicode_literals
import sys
if sys.version_info[0] >= 3:
import http.server as httpserver
import socketserver
else:
import BaseHTTPServer as httpserver
@TimSC
TimSC / fileexist.php
Created May 5, 2018 03:54
File exist testing
<?php
$fiLi = glob("/home/tim/Desktop/test/*.pdf");
while(true)
{
$i = array_rand($fiLi);
if(!file_exists($fiLi[$i]))
throw RuntimeError("File said not to exist");
}