Changes between Initial Version and Version 1 of UserWikiDotDensity


Ignore:
Timestamp:
06/02/11 16:46:43 (14 years ago)
Author:
amuhsen
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UserWikiDotDensity

    v1 v1  
     1This is a function called dot_density which creates a table called dp which has a point geometry for each point in the polygon.  If there are negative numbers in the point count, it creates the positive number of points but flags them in another column. (We often plot dot density maps with red dots for decreases and blue dots for increases.) This function utilizes the [wiki:UserWikiRandomPoint RandomPoint] function.
     2
     3There are still a few improvements we'll likely make.  We'd like to create the geometry column using the addgeometrycolumn function.  We'd like to specify the output table name as a function parameter.  There was also a good idea to use Halton sequences or other "pseudo-random" sequences, instead of truly random points, and Martin Davis implemented a few of the other "pseudo-random" ideas in [http://lin-ear-th-inking.blogspot.com/2010/05/more-random-points-in-jts.html JTS].  Other suggestions are welcomed.
     4
     5This is how we typically use it:
     6{{{
     7#!sql
     8CREATE OR REPLACE VIEW dot_cnt as
     9SELECT the_geom, polygon_id, (quantity_column/1000000)::integer AS numpoints FROM original_data;
     10
     11SELECT dot_density('dot_cnt',
     12'the_geom',
     13'polygon_id',
     14'numpoints');
     15
     16ALTER TABLE dp RENAME TO save_it_for_future_use;
     17}}}
     18
     19And here are the functions:
     20
     21{{{
     22#!sql
     23-- Function: dot_density(text, text, text, text)
     24
     25CREATE OR REPLACE FUNCTION dot_density(geom_table text, geom_col text, zone_col text, num_of_points_col text)
     26  RETURNS SETOF record AS
     27$BODY$
     28DECLARE
     29    counter integer:=0;
     30    tazrec record;
     31    pointrec record;
     32    result record;
     33    num_points integer:=0;
     34    np integer :=0;
     35BEGIN
     36
     37DROP SEQUENCE if exists randpnt_id;
     38CREATE SEQUENCE randpnt_id;
     39
     40DROP TABLE IF EXISTS dp;
     41CREATE TABLE dp(
     42  gid integer PRIMARY KEY,
     43  ser integer,
     44  "zone" integer,
     45  decrease_or_increase integer,
     46  the_geom geometry
     47);
     48
     49FOR tazrec IN EXECUTE 'SELECT ' || zone_col || ' as geom_col , ' || zone_col || ' as zone_col, '|| num_of_points_col || ' as num_of_points_col FROM ' || geom_table LOOP
     50    RAISE INFO 'Treating zone: %' , tazrec.zone_col;
     51    num_points = tazrec.num_of_points_col;
     52
     53    IF num_points !=0 THEN
     54        np := num_points/abs(num_points);
     55    ELSE
     56        np=0;
     57    END IF;
     58
     59    EXECUTE 'INSERT INTO dp SELECT nextval(''randpnt_id'') as gid, generate_series, '|| tazrec.zone_col ||  ', ' || np ||' , randompoint(the_geom) FROM ' || geom_table || ', generate_series(1, '|| abs(num_points) ||') WHERE '|| zone_col || '='|| tazrec.zone_col ;
     60 
     61END LOOP;
     62RETURN;
     63END;
     64$BODY$
     65  LANGUAGE plpgsql VOLATILE
     66}}}