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












5















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




















  • 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
    Feb 22 at 12:12
















5















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




















  • 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
    Feb 22 at 12:12














5












5








5








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
















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















share|improve this question













share|improve this question




share|improve this question








edited Feb 22 at 12:44









Vince

14.7k32749




14.7k32749










asked Feb 22 at 11:36









DemusDemus

283




283








  • 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
    Feb 22 at 12:12














  • 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
    Feb 22 at 12:12








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
Feb 22 at 12:12





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
Feb 22 at 12:12










1 Answer
1






active

oldest

votes


















6














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
























  • thank you so much! That worked perfectly. Really appreciate it.

    – Demus
    Feb 26 at 11:53











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
});


}
});














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









6














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
























  • thank you so much! That worked perfectly. Really appreciate it.

    – Demus
    Feb 26 at 11:53
















6














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
























  • thank you so much! That worked perfectly. Really appreciate it.

    – Demus
    Feb 26 at 11:53














6












6








6







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 Feb 22 at 12:12









JGHJGH

12.9k21138




12.9k21138













  • thank you so much! That worked perfectly. Really appreciate it.

    – Demus
    Feb 26 at 11:53



















  • thank you so much! That worked perfectly. Really appreciate it.

    – Demus
    Feb 26 at 11:53

















thank you so much! That worked perfectly. Really appreciate it.

– Demus
Feb 26 at 11:53





thank you so much! That worked perfectly. Really appreciate it.

– Demus
Feb 26 at 11:53


















draft saved

draft discarded




















































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

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?