Thanks to Paul Norman’s reminder in a previous post, we now have all the pieces we need to complete an ST_Buffer function that exposes all the wonderful goodness of buffer style parameters to geography users and also chooses the best local coordinate system automatically.
We use _ST_BestSRID to choose our local coordinate system.
Remember my previous three disadvantages:
CREATE OR REPLACE FUNCTION ST_Buffer(g1 geography, radius_of_buffer float,num_seg_quarter_circle integer)RETURNS geography AS$BODY$WITH transformed AS (SELECT ST_Transform(g1::geometry, _ST_BestSRID(g1)) AS geom),buffered AS (SELECT ST_Buffer(geom, radius_of_buffer, num_seg_quarter_circle) AS geomFROM transformed),transformed_4326 AS (SELECT ST_Transform(geom, 4326) AS geom FROM buffered)SELECT geom::geography FROM transformed_4326$BODY$LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION ST_Buffer(g1 geography, radius_of_buffer float, buffer_style_parameters text)RETURNS geography AS$BODY$ WITH transformed AS ( SELECT ST_Transform(g1::geometry, _ST_BestSRID(g1)) AS geom),buffered AS ( SELECT ST_Buffer(geom, radius_of_buffer, buffer_style_parameters) AS geom FROM transformed),transformed_4326 AS ( SELECT ST_Transform(geom, 4326) AS geom FROM buffered)SELECT geom::geography FROM transformed_4326 $BODY$LANGUAGE sql VOLATILE;Let’s try this!
SELECT ST_Buffer( ST_GeomFromText( 'LINESTRING(5 2.5,0 -2.50,-5 2.5)' )::geography, 500000, 'join=mitre');
Heart shaped buffer over Null Island.
Example buffer of line with join=mitre mitre_limit=5.0.
This is a hack. Daniel’s note in the previous post still stands — it would be better to do this at a lower level within geography, rather than asking the user to know and employ the best possible local coordinate system.
Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
أكثر...
We use _ST_BestSRID to choose our local coordinate system.
Remember my previous three disadvantages:
- It doesn’t do this at a low enough level to automatically use the best available local coordinate system. This leaves it to the user to choose the best available local coordinate system. I should fix this (but I probably won’t just now…).
- It uses a different function name than you otherwise use for buffering. Can we use the same name as ST_Buffer?
- Finally, it won’t let you use all the other parameters that ST_Buffer exposes through the use of buffer style parameters.
CREATE OR REPLACE FUNCTION ST_Buffer(g1 geography, radius_of_buffer float,num_seg_quarter_circle integer)RETURNS geography AS$BODY$WITH transformed AS (SELECT ST_Transform(g1::geometry, _ST_BestSRID(g1)) AS geom),buffered AS (SELECT ST_Buffer(geom, radius_of_buffer, num_seg_quarter_circle) AS geomFROM transformed),transformed_4326 AS (SELECT ST_Transform(geom, 4326) AS geom FROM buffered)SELECT geom::geography FROM transformed_4326$BODY$LANGUAGE sql VOLATILE;
CREATE OR REPLACE FUNCTION ST_Buffer(g1 geography, radius_of_buffer float, buffer_style_parameters text)RETURNS geography AS$BODY$ WITH transformed AS ( SELECT ST_Transform(g1::geometry, _ST_BestSRID(g1)) AS geom),buffered AS ( SELECT ST_Buffer(geom, radius_of_buffer, buffer_style_parameters) AS geom FROM transformed),transformed_4326 AS ( SELECT ST_Transform(geom, 4326) AS geom FROM buffered)SELECT geom::geography FROM transformed_4326 $BODY$LANGUAGE sql VOLATILE;Let’s try this!
SELECT ST_Buffer( ST_GeomFromText( 'LINESTRING(5 2.5,0 -2.50,-5 2.5)' )::geography, 500000, 'join=mitre');
This is a hack. Daniel’s note in the previous post still stands — it would be better to do this at a lower level within geography, rather than asking the user to know and employ the best possible local coordinate system.
Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
أكثر...