Created
May 16, 2018 00:15
-
-
Save aazbeltran/a26367eba42ff1ab9f8e6513800797e0 to your computer and use it in GitHub Desktop.
Generación de punto aleatorio en un radio de 250m de un punto inicial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
ST_AsGeoJSON( | |
RandomPointsInPolygon( | |
ST_Buffer(ST_MakePoint("Longitud"::numeric, "Latitud"::numeric)::geography, 250)::geometry | |
,1) | |
) | |
FROM contactos_georreferenciado; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION RandomPointsInPolygon(geom geometry, num_points integer) | |
RETURNS SETOF geometry AS | |
$BODY$DECLARE | |
target_proportion numeric; | |
n_ret integer := 0; | |
loops integer := 0; | |
x_min float8; | |
y_min float8; | |
x_max float8; | |
y_max float8; | |
srid integer; | |
rpoint geometry; | |
BEGIN | |
-- Get envelope and SRID of source polygon | |
SELECT ST_XMin(geom), ST_YMin(geom), ST_XMax(geom), ST_YMax(geom), ST_SRID(geom) | |
INTO x_min, y_min, x_max, y_max, srid; | |
-- Get the area proportion of envelope size to determine if a | |
-- result can be returned in a reasonable amount of time | |
SELECT ST_Area(geom)/ST_Area(ST_Envelope(geom)) INTO target_proportion; | |
RAISE DEBUG 'geom: SRID %, NumGeometries %, NPoints %, area proportion within envelope %', | |
srid, ST_NumGeometries(geom), ST_NPoints(geom), | |
round(100.0*target_proportion, 2) || '%'; | |
IF target_proportion < 0.0001 THEN | |
RAISE EXCEPTION 'Target area proportion of geometry is too low (%)', | |
100.0*target_proportion || '%'; | |
END IF; | |
RAISE DEBUG 'bounds: % % % %', x_min, y_min, x_max, y_max; | |
WHILE n_ret < num_points LOOP | |
loops := loops + 1; | |
SELECT ST_SetSRID(ST_MakePoint(random()*(x_max - x_min) + x_min, | |
random()*(y_max - y_min) + y_min), | |
srid) INTO rpoint; | |
IF ST_Contains(geom, rpoint) THEN | |
n_ret := n_ret + 1; | |
RETURN NEXT rpoint; | |
END IF; | |
END LOOP; | |
RAISE DEBUG 'determined in % loops (% efficiency)', loops, round(100.0*num_points/loops, 2) || '%'; | |
END$BODY$ | |
LANGUAGE plpgsql VOLATILE | |
COST 100 | |
ROWS 1000; | |
ALTER FUNCTION RandomPointsInPolygon(geometry, integer) OWNER TO postgres; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment