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 / postgis2geojson
Created March 4, 2013 18:28
Export a PostGIS table to GeoJSON format with ogr2ogr.
ogr2ogr -f "GeoJSON" /PATH/TO/NEWFILE.json PG:"host=YOUR_HOST dbname=YOUR_DB user=YOUR_USER password=YOUR_PASS port=5432" "YOUR_POSTGIS_TABLE(the_geom)"
@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
@justinlewis
justinlewis / shape2postgis.py
Created February 7, 2013 17:48
A simple script to load/replace a PostGIS table from Shapefile with the shp2pgsql utility. Completely deletes and rebuilds the table in the database.
# ---------------------------------------------------------------------------
#
# Script to copy data from an ESRI Shapefile to PostGIS database
# Requirements:
## shp2pgsql.exe
## psql.exe
## PostgreSQL DB with PostGIS installed
#
# ---------------------------------------------------------------------------
#
@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 / 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 / grant_table_privilege_on_all_table_w_psql
Last active January 10, 2018 22:22
A simple function and some commands to grant privileges to every table in a PostgreSQL database. * each file is a different method. You don't need need to use them all in conjunction.
Use the commands below to set privileges using the PSQL terminal commands.
TABLES:
for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" YOUR_DB` ; do psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done
SEQUENCES:
for tbl in `psql -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" YOUR_DB` ; do psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done
VIEWS:
for tbl in `psql -qAt -c "select table_name from information_schema.views where table_schema = 'public';" YOUR_DB` ; do psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done
@justinlewis
justinlewis / 1_build_spatial_index_all_postgis_tables.py
Created August 20, 2013 22:09
Script for building spatial indexes on tables that do not have them already in a PostGIS database. Attempts to build spatial indexes for all geometry fields in the db.
##
## Script for building spatial indexes on tables that do not have them already.
## Attempts to build spatial indexes for all geometry fields in the db.
##
## Uses the PyGreSQL module
##
## Future Improvements:
#### add some handling of existing indexes to possibly delete them and rebuild them with this naming convention
#### rather than just building a list of tables that have 1 or more indexes this should determine what fields for a specific table have an index (or dont) and handle them.
###### The current script would not catch a second geometry field in a table that has a single index because it would not be added to the index_list.
@justinlewis
justinlewis / size_all_tables_in_sql_server_db.sql
Created April 15, 2013 20:09
A query to list all the tables in a SQL Server database with their size in KB and the size of their Indexes.
-- Measures tables size (in kilobytes)
declare @t table (
name nvarchar(100), [rows] int, [reserved] nvarchar(100), [data] nvarchar(100), [index_size] nvarchar(100), [unused] nvarchar(100)
)
declare @name nvarchar(100)
declare tt cursor for
Select name from sys.tables
open tt
@justinlewis
justinlewis / add_geoserver_wfs_w_openlayers.js
Created March 5, 2013 23:00
Add a simple WFS layers to an OpenLayers map from a Geoserver WFS service. Standard approach to getting around the browsers same origin policy with JavaScript.
var tipProjects = new OpenLayers.Layer.Vector("TIP Projects 2012 - 2017", {
strategies: [new OpenLayers.Strategy.BBOX()],
protocol: new OpenLayers.Protocol.Script({
url: "http://gis.drcog.org/geoserver/wfs",
callbackKey: "format_options",
callbackPrefix: "callback:",
params: {
service: "WFS",
version: "1.1.0",
srsName: "EPSG:4326",
@justinlewis
justinlewis / search_column_name_from_all_tables_in_db.sql
Created February 13, 2013 21:57
Search for a specific column name from every table in a SQL Server database. Simply change the text after "LIKE" to the name your looking for.
USE YOUR_DB_NAME
GO
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%Level%' -- Change this to the column name your looking for.
ORDER BY schema_name, table_name;