PostGIS – Drawing a Line with Longitude, Latitude, Distance, and Azimuth

directiongeoserverlinepostgissld

I have table in PostGIS with lon, lat, distance and azimuth columns.
I want to make Geoserver draw a line through first point with given length and direction. I can turn lon and lat to geometry in PostGIS with ST_MakePoint command.

Is it possible to draw line using only SLD with first point and attribute values or do I need to calculate second point manually and turn it to geometry too?

Best Answer

Use the GeoServer SQL view feature, and SQL's ST_Project like,

SELECT id, 
  ST_SetSRID(ST_MakeLine(
    ST_MakePoint(lon,lat),
    ST_Project(
       ST_SetSRID(ST_MakePoint(lon,lat),4326)::geography, 
       distance, 
       pi()*azimuth/180.0)::geometry
  ),4326) AS geom
FROM mytable;

If you have long lines and want to plot them as great circles on a flat map, you might want to densify them with ST_Segmentize in geographic space to show them as great circles.

SELECT id, 
  ST_Segmentize(ST_SetSRID(ST_MakeLine(
    ST_MakePoint(lon,lat),
    ST_Project(
       ST_SetSRID(ST_MakePoint(lon,lat),4326)::geography, 
       distance, 
       pi()*azimuth/180.0)::geometry
  ),4326)::geography,100000)::geometry AS geom
FROM mytable;

Added

SELECT id, 
  ST_SetSRID(ST_MakeLine(
    ST_SetSRID(ST_MakePoint(lon,lat),4326),
    ST_Project(
       ST_SetSRID(ST_MakePoint(lon,lat),4326)::geography, 
       distance, 
       pi()*azimuth/180.0)::geometry
  ),4326) AS geom
FROM mytable