Skip to content

Instantly share code, notes, and snippets.

View ka7eh's full-sized avatar

Kaveh Karimi-Asli ka7eh

View GitHub Profile
@ka7eh
ka7eh / useElementRect.ts
Created August 12, 2020 18:28
A custom React hook that returns an object containing an element's position and size attributes. The object gets updated on window resize.
type ElementRect = {
width: number | null;
height: number | null;
top: number | null;
right: number | null;
bottom: number | null;
left: number | null;
marginTop: number | null;
marginRight: number | null;
marginBottom: number | null;
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ka7eh
ka7eh / swap-settings.md
Last active November 18, 2020 21:15
Modify swap files

Add a swap file:

Create a file (you can replace /swapfile with another location and filename): sudo fallocate -l 1G /swapfile
Change its permissions: sudo chmod 600 /swapfile
Mark the new file as swap space: sudo mkswap /swapfile
Enable the swap file: sudo swapon /swapfile
Check and see if the file is enabled: swapon --show
Update fstab to make your changes permanent (you might want to create a backup of the existing fstab): echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Adjust swappiness, if needed:
@ka7eh
ka7eh / psycopg2.md
Last active November 18, 2020 21:15
Python packages install hints

psycopg2:

LDFLAGS="-I<path-to-openssl>/include -L<path-to-openssl>/lib" pip install psycopg2

CREATE OR REPLACE FUNCTION ST_GridFromBbox(
IN x0 double precision,
IN y0 double precision,
IN x1 double precision,
IN y1 double precision,
IN xsize double precision,
IN ysize double precision,
OUT "row" integer,
OUT col integer,
OUT geom geometry)
@ka7eh
ka7eh / qgis_model.py
Created June 15, 2017 22:01
An example of how to load and run multiple QGIS algorithms outside of QGIS GUI
import os
import sys
from qgis.core import *
from PyQt4.QtGui import *
home = os.path.expanduser('~')
sys.path.extend([
'/usr/share/qgis/python/plugins/processing',
@ka7eh
ka7eh / st_createfishnet.sql
Created June 12, 2017 23:22
Creates a grid of `xsize*ysize` cells in a given extent
CREATE OR REPLACE FUNCTION ST_CreateFishNet(
IN xsize double precision,
IN ysize double precision,
IN north double precision DEFAULT 0,
IN east double precision DEFAULT 0,
IN south double precision DEFAULT 1,
IN west double precision DEFAULT 1)
RETURNS SETOF geometry AS
$BODY$
SELECT ST_Translate(cell, $6 + (i * $1), $5 + (j * $2)) AS geom
@ka7eh
ka7eh / geom_factory.py
Created June 2, 2017 00:04
A factory for creating random polygons in a given extent
# requires pyproj and shapely
import collections
import functools
import math
import random
import pyproj
from shapely.geometry import box, point, polygon
from shapely.ops import transform
@ka7eh
ka7eh / cssInliner.js
Created December 8, 2016 00:00
Convert styling rules of an element into inline styling
function cssInliner(el) {
var cssProperties = getComputedStyle(el, null);
var cssText = '';
for (var i = 0; i < cssProperties.length; i++) {
cssText += cssProperties[i] + ':' + cssProperties.getPropertyValue(cssProperties[i]) + ';'
}
el.style.cssText = cssText;
for (var j = 0; j < el.childElementCount; j++) {
@ka7eh
ka7eh / getStyle.js
Created December 7, 2016 23:57
Returns all styling rules of the passed doc as a string
function getStyles(doc) {
/** idea from https://github.com/NYTimes/svg-crowbar **/
var styles = '',
styleSheets = doc.styleSheets;
if (styleSheets) {
for (var i = 0; i < styleSheets.length; i++) {
processStyleSheet(styleSheets[i]);
}
}