Skip to content

Instantly share code, notes, and snippets.

View mdiener21's full-sized avatar

Michael Diener mdiener21

View GitHub Profile
@mdiener21
mdiener21 / map_2_lists_to_dict.py
Created November 4, 2017 06:59
Map two lists into a dictionary ordered by keys
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
#result looks like this {'a': 1, 'b': 2, 'c': 3}
# ordered dictionary
import collections
keys = ['a', 'b', 'c']
values = [1, 2, 3]
@mdiener21
mdiener21 / create_fishnet.sql
Created February 11, 2018 09:13
create a survey grid as vector with Postgis as a function
CREATE OR REPLACE FUNCTION ST_CreateFishnet(
nrow integer, ncol integer,
xsize float8, ysize float8,
x0 float8 DEFAULT 0, y0 float8 DEFAULT 0,
OUT "row" integer, OUT col integer,
OUT geom geometry)
RETURNS SETOF record AS
$$
SELECT i + 1 AS row, j + 1 AS col, ST_Translate(cell, j * $3 + $5, i * $4 + $6) AS geom
FROM generate_series(0, $1 - 1) AS i,
@mdiener21
mdiener21 / join_pts_polys.py
Created April 4, 2018 06:38
shapely spatial join points and polygons
import shapefile
import shapely
#Load the shapefile of polygons and convert it to shapely polygon objects
polygons_sf = shapefile.Reader("C:/PolygonShapeFile.shp")
polygon_shapes = polygons_sf.shapes()
polygon_points = [q.points for q in polygon_shapes ]
from shapely.geometry import Polygon
polygons = [Polygon(q) for q in polygon_points]
@mdiener21
mdiener21 / backup_postgresql2ftp.py
Created April 10, 2018 08:59
Backup Postgresql DB to FTP with python
import ftplib
import os
import sys
import subprocess
from optparse import OptionParser
from datetime import datetime
DB_USER = 'databaseuser'
DB_NAME = 'databasename'
@mdiener21
mdiener21 / move_files.py
Last active April 17, 2018 07:48
Python move files within folders recursively into a new single folder
import os
from os.path import isfile, join
import shutil
my_path = "/home/username/Pictures"
file_types = ('.JPG', '.jpg', '.AVI')
year = '2010'
destination_folder = "/home/username/Pictures/final_2010"
for root, dirs, files in os.walk(my_path):
@mdiener21
mdiener21 / bbox.py
Last active May 8, 2018 07:31
Find the center of a set of points using this bounding box class in python
class BoundingBox(coordinate_list):
"""
Return a 2D bounding box from a set of points
Example input: mypoints = [(0,2),(3,4),(5,6)]
Example Usage: BoundingBox(mypoints)
Example Usage: bbox = BoundingBox(mypoints)
bbox.width
bbox.height
bbox.centroid
"""
@mdiener21
mdiener21 / install-qgis3-ubuntu1604.sh
Last active July 12, 2018 12:56
Install QGIS 3 on Ubuntu 16.04
--QGIS 3 Ubuntu 16.04
sudo sh -c 'echo "deb https://qgis.org/ubuntugis xenial main" >> /etc/apt/sources.list'
sudo sh -c 'echo "deb-src https://qgis.org/ubuntugis xenial main" >> /etc/apt/sources.list'
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
wget -O - https://qgis.org/downloads/qgis-2017.gpg.key | gpg --import
gpg --fingerprint CAEB3DC3BDF7FB45
gpg --export --armor CAEB3DC3BDF7FB45 | sudo apt-key add -
@mdiener21
mdiener21 / dikstra_graph.py
Created July 13, 2018 06:00
Python Dijkstra
# source: https://dev.to/mariamxl
# https://dev.to/mariamxl/dijkstras-algorithm-in-python-algorithms-for-beginners-dkc
from collections import deque, namedtuple
# we'll use infinity as a default distance to nodes.
inf = float('inf')
Edge = namedtuple('Edge', 'start, end, cost')
@mdiener21
mdiener21 / openlayers_stacked_style.js
Last active October 3, 2018 14:41
Openlayers stacked font awesome styles for points
// using font awesome 5 free version
var faFlagSolidStyle = new ol.style.Style({
text: new ol.style.Text({
text: '\uf024', // fas flag solid
scale: 1,
font: 'normal 18px FontAwesome',
offsety: -30,
offsetx: -10,
fill: new ol.style.Fill({color: 'black'}),
})
@mdiener21
mdiener21 / geoserver-cors.xml
Created October 22, 2018 06:47
Geoserver Cors settings for Tomcat
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>