PostGIS function to move a polygon to centre over new point coordinates












3















Background:



(Note: I am trying to do as much of this as possible in PostGIS/PostgreSQL)



I have a PostgreSQL table containing location data of an aircraft in orbit around a city. Every second a new row is added with new position data.



From this table I have created a view which shows a constantly updating point of only the current position. This view therefore contains only one row.



The Task:



I have created a custom polygon in a different table, which acts as a buffer around the current position point. I want to reposition/move this polygon to always be centred over the constantly updating current position coordinates. Does anyone know how to do this? I have already experimented with rotate and scale. I am happy with them. It is moving the polygon that is confusing me.



I have tried creating a view that uses ST_Translate for the polygon, however from my understanding, this function only moves coordinates by a fixed amount from the origin. It doesn’t allow me to specify exactly what lon lat coordinates I want it to move to. Does anyone know a function/how to move a polygon to new coordinates, centred on a point? Again, I am trying to achieve this from PostGIS.










share|improve this question









New contributor




Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    ST_Translate moves a geometry by a specified amount in x, y and, optionally, z directions. So, to calculate how much you need to move the polygon, calculate the change in x and y between your new point and the centroid of the polygon

    – John Powell
    1 hour ago
















3















Background:



(Note: I am trying to do as much of this as possible in PostGIS/PostgreSQL)



I have a PostgreSQL table containing location data of an aircraft in orbit around a city. Every second a new row is added with new position data.



From this table I have created a view which shows a constantly updating point of only the current position. This view therefore contains only one row.



The Task:



I have created a custom polygon in a different table, which acts as a buffer around the current position point. I want to reposition/move this polygon to always be centred over the constantly updating current position coordinates. Does anyone know how to do this? I have already experimented with rotate and scale. I am happy with them. It is moving the polygon that is confusing me.



I have tried creating a view that uses ST_Translate for the polygon, however from my understanding, this function only moves coordinates by a fixed amount from the origin. It doesn’t allow me to specify exactly what lon lat coordinates I want it to move to. Does anyone know a function/how to move a polygon to new coordinates, centred on a point? Again, I am trying to achieve this from PostGIS.










share|improve this question









New contributor




Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    ST_Translate moves a geometry by a specified amount in x, y and, optionally, z directions. So, to calculate how much you need to move the polygon, calculate the change in x and y between your new point and the centroid of the polygon

    – John Powell
    1 hour ago














3












3








3








Background:



(Note: I am trying to do as much of this as possible in PostGIS/PostgreSQL)



I have a PostgreSQL table containing location data of an aircraft in orbit around a city. Every second a new row is added with new position data.



From this table I have created a view which shows a constantly updating point of only the current position. This view therefore contains only one row.



The Task:



I have created a custom polygon in a different table, which acts as a buffer around the current position point. I want to reposition/move this polygon to always be centred over the constantly updating current position coordinates. Does anyone know how to do this? I have already experimented with rotate and scale. I am happy with them. It is moving the polygon that is confusing me.



I have tried creating a view that uses ST_Translate for the polygon, however from my understanding, this function only moves coordinates by a fixed amount from the origin. It doesn’t allow me to specify exactly what lon lat coordinates I want it to move to. Does anyone know a function/how to move a polygon to new coordinates, centred on a point? Again, I am trying to achieve this from PostGIS.










share|improve this question









New contributor




Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












Background:



(Note: I am trying to do as much of this as possible in PostGIS/PostgreSQL)



I have a PostgreSQL table containing location data of an aircraft in orbit around a city. Every second a new row is added with new position data.



From this table I have created a view which shows a constantly updating point of only the current position. This view therefore contains only one row.



The Task:



I have created a custom polygon in a different table, which acts as a buffer around the current position point. I want to reposition/move this polygon to always be centred over the constantly updating current position coordinates. Does anyone know how to do this? I have already experimented with rotate and scale. I am happy with them. It is moving the polygon that is confusing me.



I have tried creating a view that uses ST_Translate for the polygon, however from my understanding, this function only moves coordinates by a fixed amount from the origin. It doesn’t allow me to specify exactly what lon lat coordinates I want it to move to. Does anyone know a function/how to move a polygon to new coordinates, centred on a point? Again, I am trying to achieve this from PostGIS.







qgis postgis postgresql polygon move






share|improve this question









New contributor




Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Vince

14.6k32748




14.6k32748






New contributor




Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









DemusDemus

161




161




New contributor




Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Demus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    ST_Translate moves a geometry by a specified amount in x, y and, optionally, z directions. So, to calculate how much you need to move the polygon, calculate the change in x and y between your new point and the centroid of the polygon

    – John Powell
    1 hour ago














  • 1





    ST_Translate moves a geometry by a specified amount in x, y and, optionally, z directions. So, to calculate how much you need to move the polygon, calculate the change in x and y between your new point and the centroid of the polygon

    – John Powell
    1 hour ago








1




1





ST_Translate moves a geometry by a specified amount in x, y and, optionally, z directions. So, to calculate how much you need to move the polygon, calculate the change in x and y between your new point and the centroid of the polygon

– John Powell
1 hour ago





ST_Translate moves a geometry by a specified amount in x, y and, optionally, z directions. So, to calculate how much you need to move the polygon, calculate the change in x and y between your new point and the centroid of the polygon

– John Powell
1 hour ago










1 Answer
1






active

oldest

votes


















3














The easiest would be to have your polygon centered on (0;0) and to use st_translate using the target point X;Y as the delta X and delta Y.



Using any polygon, you would compute the delta by removing the polygon centroid X and Y from the target point X and Y:



WITH pt AS (SELECT 'point(-75.5 47.2)'::geometry geom),
poly AS (SELECT 'polygon((-70 41, -71 41, -71 40,-70 40, -70 41))'::geometry geom)
SELECT st_asText(
ST_TRANSLATE(
poly.geom,
st_x(pt.geom) - st_x(st_centroid(poly.geom)),
st_y(pt.geom) - st_y(st_centroid(poly.geom))
)
)
FROM pt, poly;

st_astext
---------------------------------------------------------
POLYGON((-75 47.7,-76 47.7,-76 46.7,-75 46.7,-75 47.7))
(1 row)





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "79"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Demus is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313197%2fpostgis-function-to-move-a-polygon-to-centre-over-new-point-coordinates%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    The easiest would be to have your polygon centered on (0;0) and to use st_translate using the target point X;Y as the delta X and delta Y.



    Using any polygon, you would compute the delta by removing the polygon centroid X and Y from the target point X and Y:



    WITH pt AS (SELECT 'point(-75.5 47.2)'::geometry geom),
    poly AS (SELECT 'polygon((-70 41, -71 41, -71 40,-70 40, -70 41))'::geometry geom)
    SELECT st_asText(
    ST_TRANSLATE(
    poly.geom,
    st_x(pt.geom) - st_x(st_centroid(poly.geom)),
    st_y(pt.geom) - st_y(st_centroid(poly.geom))
    )
    )
    FROM pt, poly;

    st_astext
    ---------------------------------------------------------
    POLYGON((-75 47.7,-76 47.7,-76 46.7,-75 46.7,-75 47.7))
    (1 row)





    share|improve this answer




























      3














      The easiest would be to have your polygon centered on (0;0) and to use st_translate using the target point X;Y as the delta X and delta Y.



      Using any polygon, you would compute the delta by removing the polygon centroid X and Y from the target point X and Y:



      WITH pt AS (SELECT 'point(-75.5 47.2)'::geometry geom),
      poly AS (SELECT 'polygon((-70 41, -71 41, -71 40,-70 40, -70 41))'::geometry geom)
      SELECT st_asText(
      ST_TRANSLATE(
      poly.geom,
      st_x(pt.geom) - st_x(st_centroid(poly.geom)),
      st_y(pt.geom) - st_y(st_centroid(poly.geom))
      )
      )
      FROM pt, poly;

      st_astext
      ---------------------------------------------------------
      POLYGON((-75 47.7,-76 47.7,-76 46.7,-75 46.7,-75 47.7))
      (1 row)





      share|improve this answer


























        3












        3








        3







        The easiest would be to have your polygon centered on (0;0) and to use st_translate using the target point X;Y as the delta X and delta Y.



        Using any polygon, you would compute the delta by removing the polygon centroid X and Y from the target point X and Y:



        WITH pt AS (SELECT 'point(-75.5 47.2)'::geometry geom),
        poly AS (SELECT 'polygon((-70 41, -71 41, -71 40,-70 40, -70 41))'::geometry geom)
        SELECT st_asText(
        ST_TRANSLATE(
        poly.geom,
        st_x(pt.geom) - st_x(st_centroid(poly.geom)),
        st_y(pt.geom) - st_y(st_centroid(poly.geom))
        )
        )
        FROM pt, poly;

        st_astext
        ---------------------------------------------------------
        POLYGON((-75 47.7,-76 47.7,-76 46.7,-75 46.7,-75 47.7))
        (1 row)





        share|improve this answer













        The easiest would be to have your polygon centered on (0;0) and to use st_translate using the target point X;Y as the delta X and delta Y.



        Using any polygon, you would compute the delta by removing the polygon centroid X and Y from the target point X and Y:



        WITH pt AS (SELECT 'point(-75.5 47.2)'::geometry geom),
        poly AS (SELECT 'polygon((-70 41, -71 41, -71 40,-70 40, -70 41))'::geometry geom)
        SELECT st_asText(
        ST_TRANSLATE(
        poly.geom,
        st_x(pt.geom) - st_x(st_centroid(poly.geom)),
        st_y(pt.geom) - st_y(st_centroid(poly.geom))
        )
        )
        FROM pt, poly;

        st_astext
        ---------------------------------------------------------
        POLYGON((-75 47.7,-76 47.7,-76 46.7,-75 46.7,-75 47.7))
        (1 row)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        JGHJGH

        12.4k21136




        12.4k21136






















            Demus is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Demus is a new contributor. Be nice, and check out our Code of Conduct.













            Demus is a new contributor. Be nice, and check out our Code of Conduct.












            Demus is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313197%2fpostgis-function-to-move-a-polygon-to-centre-over-new-point-coordinates%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Polycentropodidae

            Magento 2 Error message: Invalid state change requested

            Paulmy