| 1 | = Buffer geodetic geometry using meters = |
| 2 | |
| 3 | Constructs a buffer of a geodetic (lat/long) geometry using a distance in meters. |
| 4 | Uses a transformation to a suitable UTM zone, so only works for geometries with extent less than a few degrees of longitude. |
| 5 | |
| 6 | Note: not sure if this is better than simply casting to `geography`? |
| 7 | |
| 8 | Sources: |
| 9 | * https://blog.datapolitan.com/2013/11/22/st_buffer_meters/ |
| 10 | * [https://gist.github.com/datapolitan/9ef9489eac5686ca3b34 st_buffer_meters.sql] |
| 11 | * [https://gist.github.com/datapolitan/ffdeb086b3c4531d9f4e utmzone.sql] |
| 12 | |
| 13 | {{{ |
| 14 | /* Function: ST_Buffer_Meters(geometry, double precision) |
| 15 | DROP FUNCTION ST_Buffer_Meters(geometry, double precision); |
| 16 | Usage: SELECT ST_Buffer_Meters(the_geom, num_meters) FROM sometable; */ |
| 17 | |
| 18 | |
| 19 | CREATE OR REPLACE FUNCTION ST_Buffer_Meters(geometry, double precision) |
| 20 | RETURNS geometry AS |
| 21 | $BODY$ |
| 22 | DECLARE |
| 23 | orig_srid int; |
| 24 | utm_srid int; |
| 25 | |
| 26 | BEGIN |
| 27 | orig_srid:= ST_SRID($1); |
| 28 | utm_srid:= utmzone(ST_Centroid($1)); |
| 29 | |
| 30 | RETURN ST_transform(ST_Buffer(ST_transform($1, utm_srid), $2), orig_srid); |
| 31 | END; |
| 32 | $BODY$ LANGUAGE 'plpgsql' IMMUTABLE |
| 33 | COST 100; |
| 34 | |
| 35 | /* Function: utmzone(geometry) |
| 36 | DROP FUNCTION utmzone(geometry); |
| 37 | Usage: SELECT ST_Transform(the_geom, utmzone(ST_Centroid(the_geom))) FROM sometable; */ |
| 38 | |
| 39 | CREATE OR REPLACE FUNCTION utmzone(geometry) |
| 40 | RETURNS integer AS |
| 41 | $BODY$ |
| 42 | DECLARE |
| 43 | geomgeog geometry; |
| 44 | zone int; |
| 45 | pref int; |
| 46 | |
| 47 | BEGIN |
| 48 | geomgeog:= ST_Transform($1,4326); |
| 49 | |
| 50 | IF (ST_Y(geomgeog))>0 THEN |
| 51 | pref:=32600; |
| 52 | ELSE |
| 53 | pref:=32700; |
| 54 | END IF; |
| 55 | |
| 56 | zone:=floor((ST_X(geomgeog)+180)/6)+1; |
| 57 | |
| 58 | RETURN zone+pref; |
| 59 | END; |
| 60 | $BODY$ LANGUAGE 'plpgsql' IMMUTABLE |
| 61 | COST 100; |
| 62 | }}} |
| 63 | |