postgresqlgispostgisbing-mapsbing-api

Draw sector by center point, radius, interior angle and azymuth in postgis


I'm building a javascript application using Bing Maps API, and I would like to make up sector geometries from center points and sector parameters.

I have a table 'points' in PostgreSQL database with PostGIS on top and it contains point geometries:

id  st_astext(geom)
1   POINT(4.331 50.869)
2   POINT(4.323 50.832)
3   POINT(4.373 50.853)
4   POINT(4.356 50.837)

I have another table 'segemnts' where each entry has following attributes: azimuth (in degrees), beamwidth (in degrees), range (in meters) and centerid that is basically foreign key to 'points' table:

centerid    azimuth beamwidth   range
1           210     60          750
2           135     30          500
3           80      60          600
4           165     90          750

How do I get a table or a view in my database to select circle segments that have above points as a center point, range as radius, beamwidth as interior angle, and azimuth as a direction?

enter image description here


Solution

  • You may employ st_buffer() to get circle of given radius around your points, than build triangle with interior angle equal to your beamwidth, using st_project(), than you may intersect those to get sector geometries, like:

    create view sectors as 
    select s1.*, st_intersection(st_buffer(p1.geom::geography, s1.range, 50)::geometry, st_makepolygon(st_makeline(array[p1.geom, st_project(p1.geom::geography, s1.range*2, radians(s1.azimuth-s1.beam/2))::geometry, st_project(p1.geom::geography, s1.range*2, radians(s1.azimuth+s1.beam/2))::geometry, p1.geom]))) as geom
    from sector s1
    left join points p1
    on p1.id=s1.centerid
    

    Pay attention, I have used type casting between geography and geometry to match expected function parameters, also I have multiplied s1.range by two, so it is big enough to cut whole sector and I used 50 as a third st_buffer() parameter so you sector is smooth enough.

    Your sample data renders into something, like:

    enter image description here