Skip to content

Instantly share code, notes, and snippets.

View justinlewis's full-sized avatar

Justin Lewis justinlewis

  • TerraFrame
  • Marble, Colorado
View GitHub Profile
@justinlewis
justinlewis / postgis_randomise_points.sql
Last active September 13, 2019 09:16
Create a random point geometry within the boundaries of a specified polygon. Implemented as a PostGIS function. Inputs include the_geom (name of input polygon geometry field) and maxiter (max # of iterations to try placing point within the polygon).
-- Function: babysim_random_jobs_explode(geometry, integer)
-- DROP FUNCTION babysim_random_jobs_explode(geometry, integer);
CREATE OR REPLACE FUNCTION babysim_random_jobs_explode(the_geom geometry, maxiter integer DEFAULT 10000)
RETURNS geometry AS
$BODY$
DECLARE
i INTEGER := 0;
x0 DOUBLE PRECISION;
@justinlewis
justinlewis / postgis_cluster_boundary_create.sql
Created January 30, 2013 18:03
Creates polygon boundaries around clusters of polygons. Implemented as a PostGIS function. Inputs include data table, geometry field (built for polygon), unique id, and radius (distance between clustered polygons).
-- Function: public.polygon_clusters(character varying, character varying, character varying, numeric)
-- DROP FUNCTION public.polygon_clusters(character varying, character varying, character varying, numeric);
CREATE OR REPLACE FUNCTION public.polygon_clusters(parcels character varying, geom character varying, gid character varying, radius numeric)
RETURNS SETOF record AS
$BODY$
DECLARE
lid_new integer;
dmn_number integer := 1;
@justinlewis
justinlewis / cleanse_postgis_geometry_columns_table.py
Last active December 11, 2015 00:19
Script for truncating and repopulating the geometry_columns table in a pre PostGIS 2.0 database.
##
## Script for truncating and repopulating the geometry_columns table in a pre PostGIS 2.0 database.
#### This is important because the geometry_columns table often has legacy records where tables have been deleted
#### but the corresponding geom record was not removed.
##
## This could be done as a PostgreSQL function with a trigger as well.
##
## Author = Justin Lewis
##
@justinlewis
justinlewis / google_postgis_geocode.py
Created January 11, 2013 19:26
Script that geocodes address data stored in a PostgreSQL (PostGIS) database using Google.
##
## Mike Tafel and Justin Lewis
## 2010/07/26 : updated Dec. 2012
##
## Script that geocodes address data stored in a PostgreSQL (PostGIS) database using Google.
### Preps the db table, reads address data, builds a request url, returns lat/long/precision valuse to the same table,
### transforms coordinates to 4326 then 2232.
##
## Dependancies:
#### PostGIS database, Google Private Key and Client ID, all the libraries listed in the import below
@justinlewis
justinlewis / postgis2shp.py
Last active October 10, 2022 09:02
A simple script for converting PostGIS tables to shapefiles with ogr2ogr.
#### Author: Justin Lewis
#### This script can be used to export a single pg table to shapefile.
####
#### Developed in a linux environment but should require minimal modification to run on other platforms.
####
#### Dependancies:
###### fwtools (gdal > ogr2ogr), PostGIS database, Python, PyGreSQL
import os, _pg