Compare commits

...

8 Commits

Author SHA1 Message Date
Javier Goizueta
9e2f2b7c2d Fix the tests for adjacent union function 2016-02-26 11:55:20 +01:00
Stuart Lynn
69b6aa6aca cdb_union_adjacent documentation 2016-02-24 13:59:25 -05:00
Stuart Lynn
e84b467cbc cdb_union_adgacent 2016-02-24 13:51:18 -05:00
Rafa de la Torre
abfda1c75e Update CONTRIBUTING.md
minor change (just a space)
2016-02-23 17:23:33 +01:00
Rafa de la Torre
8f478ef22c Update README.md
Remove FIXME that should be already fixed.
2016-02-23 17:18:19 +01:00
Javier Goizueta
c7bb50be5a Fix: Make extension publicly available 2016-02-22 17:39:58 +01:00
Javier Goizueta
ef17e2fe4c Add header 2016-02-22 16:14:52 +01:00
Javier Goizueta
f3b8546063 Fix syntax 2016-02-22 16:14:28 +01:00
12 changed files with 152 additions and 5 deletions

View File

@@ -12,7 +12,7 @@ name must be created.
### Version numbers
The version of both the SQL extension and the Python package shall
follow the[Semantic Versioning 2.0](http://semver.org/) guidelines:
follow the [Semantic Versioning 2.0](http://semver.org/) guidelines:
* When backwards incompatibility is introduced the major number is incremented
* When functionally is added (in a backwards-compatible manner) the minor number

View File

@@ -7,8 +7,6 @@ CartoDB Spatial Analysis extension for PostgreSQL.
* *pg* contains the PostgreSQL extension source code
* *python* Python module
FIXME: should it be `./extension` and `./lib/python' ?
## Requirements
* pip

View File

@@ -1,3 +1,6 @@
--DO NOT MODIFY THIS FILE, IT IS GENERATED AUTOMATICALLY FROM SOURCES
-- Complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION crankshaft" to load this file. \quit
-- Internal function.
-- Set the seeds of the RNGs (Random Number Generators)
-- used internally.
@@ -133,4 +136,13 @@ BEGIN
RETURN ST_Collect(points);
END;
$$
LANGUAGE plpgsql VOLATILE
LANGUAGE plpgsql VOLATILE;
-- Make sure by default there are no permissions for publicuser
-- NOTE: this happens at extension creation time, as part of an implicit transaction.
-- REVOKE ALL PRIVILEGES ON SCHEMA cdb_crankshaft FROM PUBLIC, publicuser CASCADE;
-- Grant permissions on the schema to publicuser (but just the schema)
GRANT USAGE ON SCHEMA cdb_crankshaft TO publicuser;
-- Revoke execute permissions on all functions in the schema by default
-- REVOKE EXECUTE ON ALL FUNCTIONS IN SCHEMA cdb_crankshaft FROM PUBLIC, publicuser;

View File

@@ -0,0 +1,25 @@
### Union Adjacent
This is an aggregate function that will take a set of polygons and return a geometry array
of regions where the polygons are continuous. Basically it combines polygons
which are touching in to single polygons.
It takes a single value:
* `geometry` a list of geometries to be clustered and joined
and returns
* `geometry[]` an array of the joined geometries.
An example usage would be something like:
```postgresql
with joined_polygons as (
select cdb_union_adjacent(the_geom) regions from some_table
)
select unnest(region) the_geom from joined_polygons
```
which will produce a table with regions of continuous polygons from the original
table.

View File

@@ -0,0 +1,3 @@
--DO NOT MODIFY THIS FILE, IT IS GENERATED AUTOMATICALLY FROM SOURCES
-- Complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION crankshaft" to load this file. \quit

View File

@@ -51,4 +51,4 @@ BEGIN
RETURN ST_Collect(points);
END;
$$
LANGUAGE plpgsql VOLATILE
LANGUAGE plpgsql VOLATILE;

View File

@@ -0,0 +1,43 @@
CREATE OR REPLACE FUNCTION _cdb_final_union_adjacent( joined_geoms geometry[] )
RETURNS geometry[] AS $$
BEGIN
RETURN joined_geoms;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION _cdb_state_update_union_adjacent(clusters geometry[], new_geom geometry)
RETURNS geometry[] AS $$
DECLARE
joins geometry[] :='{}';
unjoined geometry[] :='{}';
i integer;
combined geometry;
BEGIN
joins := (select array_agg(g)
from unnest(clusters) a(g)
where ST_TOUCHES(g, new_geom));
unjoined := (select array_agg(g)
from unnest(clusters) a(g)
where ST_TOUCHES(g, new_geom) = false);
IF array_length(joins, 1) > 0 THEN
joins := array_append(joins, new_geom);
combined := ST_UNION(joins);
ELSE
combined := new_geom;
END IF;
unjoined := array_append(unjoined, combined);
RETURN unjoined;
END
$$
LANGUAGE plpgsql;
CREATE AGGREGATE cdb_union_adjacent(geometry)(
SFUNC=_cdb_state_update_union_adjacent,
STYPE=geometry[],
FINALFUNC=_cdb_final_union_adjacent,
INITCOND='{}'
);

View File

@@ -0,0 +1,9 @@
-- Make sure by default there are no permissions for publicuser
-- NOTE: this happens at extension creation time, as part of an implicit transaction.
-- REVOKE ALL PRIVILEGES ON SCHEMA cdb_crankshaft FROM PUBLIC, publicuser CASCADE;
-- Grant permissions on the schema to publicuser (but just the schema)
GRANT USAGE ON SCHEMA cdb_crankshaft TO publicuser;
-- Revoke execute permissions on all functions in the schema by default
-- REVOKE EXECUTE ON ALL FUNCTIONS IN SCHEMA cdb_crankshaft FROM PUBLIC, publicuser;

View File

@@ -0,0 +1,22 @@
\i test/fixtures/touching_polygons.sql
-- test table (polygons, some of which touch and some which dont)
CREATE TABLE touching_polygons(cartodb_id integer, the_geom geometry);
INSERT INTO touching_polygons VALUES
(1, ST_GeomFromText('POLYGON ((0 0, 1 0,1 1, 0 1, 0 0 ))')),
(2, ST_GeomFromText('POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))')),
(1, ST_GeomFromText('POLYGON ((0 1, 1 1,1 2, 0 2, 0 1 ))')),
(4, ST_GeomFromText('POLYGON ((3 0, 4 0, 4 1, 3 1, 3 0))')),
(5, ST_GeomFromText('POLYGON ((3 1, 4 1, 4 2, 3 2, 3 1))'));
WITH joined_polygons AS (
SELECT cdb_crankshaft.cdb_union_adjacent(the_geom) the_geom FROM touching_polygons
),
unnested_polygons as (
select unnest(joined_polygons.the_geom) the_geom from joined_polygons
)
select ST_ASTEXT(unnested_polygons.the_geom) from unnested_polygons;
st_astext
------------------------------------------------
POLYGON((1 0,0 0,0 1,0 2,1 2,1 1,2 1,2 0,1 0))
POLYGON((4 1,4 0,3 0,3 1,3 2,4 2,4 1))
(2 rows)

View File

@@ -0,0 +1,9 @@
\i test/fixtures/touching_polygons.sql
WITH joined_polygons AS (
SELECT cdb_crankshaft.cdb_union_adjacent(the_geom) the_geom FROM touching_polygons
),
unnested_polygons as (
select unnest(joined_polygons.the_geom) the_geom from joined_polygons
)
select ST_ASTEXT(unnested_polygons.the_geom) from unnested_polygons;

View File

@@ -0,0 +1,18 @@
SELECT cdb_crankshaft._cdb_random_seeds(1234);
-- Use regular user role
SET ROLE test_regular_user;
-- Add to the search path the schema
SET search_path TO public,cartodb,cdb_crankshaft;
-- Exercise public functions
SELECT ppoints.code, m.quads
FROM ppoints
JOIN cdb_moran_local('ppoints', 'value') m
ON ppoints.cartodb_id = m.ids
ORDER BY ppoints.code;
SELECT round(cdb_overlap_sum(
'0106000020E61000000100000001030000000100000004000000FFFFFFFFFF3604C09A0B9ECEC42E444000000000C060FBBF30C7FD70E01D44400000000040AD02C06481F1C8CD034440FFFFFFFFFF3604C09A0B9ECEC42E4440'::geometry,
'values', 'value'
), 2);

View File

@@ -0,0 +1,8 @@
-- test table (polygons, some of which touch and some which dont)
CREATE TABLE touching_polygons(cartodb_id integer, the_geom geometry);
INSERT INTO touching_polygons VALUES
(1, ST_GeomFromText('POLYGON ((0 0, 1 0,1 1, 0 1, 0 0 ))')),
(2, ST_GeomFromText('POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))')),
(1, ST_GeomFromText('POLYGON ((0 1, 1 1,1 2, 0 2, 0 1 ))')),
(4, ST_GeomFromText('POLYGON ((3 0, 4 0, 4 1, 3 1, 3 0))')),
(5, ST_GeomFromText('POLYGON ((3 1, 4 1, 4 2, 3 2, 3 1))'));