Skip to content

Instantly share code, notes, and snippets.

@dbaston
dbaston / find_connected.sql
Created April 26, 2014 00:26
Find connected linestrings
-- Copyright Paul Ramsey, 2014
-- http://gis.stackexchange.com/questions/94203/how-to-group-connected-linestrings-in-postgis
drop table lines;
create table lines ( id integer primary key, geom geometry(linestring) );
insert into lines (id, geom) values ( 1, 'LINESTRING(0 0, 0 1)');
insert into lines (id, geom) values ( 2, 'LINESTRING(0 1, 1 1)');
insert into lines (id, geom) values ( 3, 'LINESTRING(1 1, 1 2)');
insert into lines (id, geom) values ( 4, 'LINESTRING(1 2, 2 2)');
insert into lines (id, geom) values ( 11, 'LINESTRING(10 10, 10 11)');
@dbaston
dbaston / gist:0969735b4a7b75a6a974
Created May 22, 2014 16:29
Compare structure of two tables
SELECT old.column_name as column_1141, old.data_type as type_1141, new.column_name as column_1142, new.data_type as type_1142
FROM
(SELECT * FROM information_schema.columns WHERE table_schema = 'old_schema' AND table_name = 'old_table') old
FULL OUTER JOIN
(SELECT * FROM information_schema.columns WHERE table_schema = 'new_schema' AND table_name = 'new_table') new
USING (column_name)
WHERE old.column_name IS DISTINCT FROM new.column_name
OR old.data_type != new.data_type
ORDER BY COALESCE(old.column_name, new.column_name) ASC;
package com.maponics.algorithm;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.operation.polygonize.Polygonizer;
import com.vividsolutions.jts.operation.union.UnaryUnionOp;
import java.util.*;
import java.util.concurrent.Callable;
@dbaston
dbaston / gist:f649b53c6ab338879778
Created August 13, 2014 14:05
convert 'text' fields into varchar fields of an appopriate length
-- convert 'text' fields into varchar fields of an appopriate length (useful for exporting to shp, dbf, etc)
CREATE OR REPLACE FUNCTION set_varchar_length (schema_name text, table_name text) RETURNS VOID AS $$
DECLARE rec record;
DECLARE len int;
DECLARE qry text;
DECLARE is_first boolean;
BEGIN
qry := 'ALTER TABLE ' || quote_ident(schema_name) || '.' || quote_ident(table_name);
is_first := true;
@dbaston
dbaston / ParallelCascadedPolygonUnion.java
Last active November 30, 2015 19:27
ParallelCascadedPolygonUnion
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.util.GeometryCombiner;
import com.vividsolutions.jts.index.strtree.STRtree;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class ParallelCascadedPolygonUnion {
public static Geometry union(Collection<Geometry> geoms) {
@dbaston
dbaston / cluster_impl.c
Last active December 19, 2015 20:40
ST_ClusterIntersecting as a window function
PG_FUNCTION_INFO_V1(cluster_intersecting_window);
Datum cluster_intersecting_window(PG_FUNCTION_ARGS)
{
WindowObject win_obj = PG_WINDOW_OBJECT();
uint32_t row = WinGetCurrentPosition(win_obj);
uint32_t ngeoms = WinGetPartitionRowCount(win_obj);
uint32_t* cluster_assignments = WinGetPartitionLocalMemory(win_obj, ngeoms * sizeof(uint32_t));
if (row == 0) /* beginning of the partition; do all of the work now */
{
@dbaston
dbaston / Dockerfile
Created March 29, 2016 14:51
Dockerfile to build pramsey's parallel PostGIS branch against Postgres/GEOS trunk
FROM phusion/baseimage
RUN apt-get update && apt-get install -y \
build-essential \
git \
bison \
flex \
zlib1g-dev \
autoconf \
libtool \
@dbaston
dbaston / Dockerfile
Created July 21, 2016 12:57
postgis build environment
FROM phusion/baseimage
RUN apt-get update && apt-get install -y build-essential git bison flex zlib1g-dev autoconf libtool libxml2-dev libproj-dev libjson0-dev libcunit1-dev libxml2-utils xsltproc imagemagick docbook-xsl docbook-mathml libgdal-dev wget cmake libgmp-dev libmpfi-dev libboost-thread-dev libboost-system-dev
RUN git clone --depth 1 https://github.com/postgres/postgres.git && cd postgres && ./configure --without-readline && make -j6 && make install && make clean && cd .. && rm -rf postgres
RUN git clone --depth 1 https://github.com/libgeos/libgeos.git && cd libgeos && ./autogen.sh && ./configure --libdir=/usr/lib && make -j6 && make install && make clean && cd .. && rm -rf libgeos
RUN wget https://github.com/CGAL/cgal/releases/download/releases%2FCGAL-4.7/CGAL-4.7.tar.gz && tar xzvf CGAL-4.7.tar.gz && rm CGAL-4.7.tar.gz && cd CGAL-4.7 && cmake . && make -j6 && make install && make clean && cd .. && rm -rf CGAL-4.7
RUN git clone --depth 1 https://github.com/Oslandia/SFCGAL.git && cd SFCGAL && cmake
-- Divide an input geometry into equal-length portions of the specified length.
-- The final segment will contain the "unused" portion of the line and will be
-- shorter than the others.
--
-- Note that segment_length is in the units of the geometry's coordinate system,
-- i.e. degrees for 4326. To use the function with geographic coordinates and
-- a distance in meters, it is recommended to project the geometry into a
-- planar coordinate system, e.g. using _ST_BestSRID.
CREATE OR REPLACE FUNCTION pg_temp.DivideLine(g geometry, segment_length double precision)
RETURNS SETOF geometry AS $$
@dbaston
dbaston / masked_vectorize.py
Created August 1, 2017 13:47
mask-aware numpy vectorize
def masked_vectorize(fn):
vectorized = np.vectorize(fn)
def ret(*args, **kwargs):
# In theory, it should be possible to replace this with
# np.ma.logical_or.reduce([a.mask for a in args])
# In practice, it seems to generate an error when the
# internal storage of the arguments is different
# ValueError: setting an array element with a sequence.
masked_args = [arg for arg in args if isinstance(arg, np.ma.core.MaskedArray)]