Equivalent of PostGIS' ST_Project on the perfect sphere - postgresql

I have a PostGIS geography point that I want to rotate by a set distance on the ideal sphere (not spheroid). I see the ST_Project() function, but it seems that it rotates points on the spheroid. For example, if I rotate (0,10) by 1 degree north, the result is slightly larger than 1 degree
select ST_AsText(ST_Project(ST_Point(0,10), 111194.68229846345*1,radians(0.0)));
st_astext
---------------------------
POINT(0 11.0052750273178)
(1 row)
Rotating (0,60) by the same distance gives a result that is slightly less than 1 degree.
POINT(0 60.9979713953998)
I can also compute the distance between the points using the spheroid
select ST_Distance(ST_Project(ST_Point(60,89.9)::geography, 111194.68229846345*1,radians(0.0)),ST_Point(60,89.9),'t');
st_distance
------------------
111194.682298475
(1 row)
and the perfect sphere
select ST_Distance(ST_Project(ST_Point(60,89.9)::geography, 111194.68229846345*1,radians(0.0)),ST_Point(60,89.9),'f');
st_distance
-----------------
110698.09475313
(1 row)
So it is not that I got the circumference of the Earth wrong.
For many functions that operate on geography objects, such as ST_DWithin, there is an option to use the spheroid or the perfect sphere. I do not see the option for ST_Project.
In general, my azimuth will be one of 0, 90, 180, and 270, and it has to work properly around the poles.

You just need to create a new spatial projection, where semi-major and semi-minor axes for the spheroid are the same. So for a sphere with a radius of 6370986 m, here is a new SRID=123456:
INSERT INTO spatial_ref_sys(srid, auth_name, srtext, proj4text)
VALUES (123456, NULL, '', '+proj=longlat +a=6370986 +b=6370986 +ellps=sphere +no_defs ');
For geography types, the default SRID=4326 is for the WGS84 ellipsoid. To use the new sphere ellipsoid, you need to set the SRID to a non-default value.
SELECT ST_AsText(ST_Project(ST_SetSRID(ST_Point(0,10), 123456), 111194.68229846345, 0.0));
st_astext
-------------
POINT(0 11)
(1 row)

Related

Why is the distance between a point and a polygon 0 here?

I am using PostGIS, here is the SQL that I am using -
SELECT ST_Distance(
'SRID=4326;POINT(0 55)'::geography,
'SRID=4326;POLYGON((-180 45,-10 45,-10 -45,-180 -45,-180 45))'::geography
);
Visualizing these objects on a map suggests to me that the distance should be non-zero.

What is the area of geom field?

I want to check the area of geometry values.
The geometry values are POLYGON or POINT or MULTI POLYGON.
The field has the type of geometry
I check the srid of the geom field:
select st_srid(geometry)
from my_table
And I got srid=32636.
I checked here:
https://epsg.io/32636 and it seems that the units are in meters.
Now I want to get the area (in meters) of each value:
select st_area(geometry)
from my_table
And I'm getting very small values (0.0002, or 0.000097 or 0.33, ....).
I want to be sure:
Does those values means square meter (m^2) ?
So the values are less than 1 square meter ?
Since your SRS unit is metre, ST_Area will return the area in square metres. The following example calculates the area of a polygon using SRS's that have different units:
WITH j (geom) AS (
VALUES ('SRID=32636;
POLYGON((-1883435.029648588 6673769.700215263,-1883415.1158478875 6673776.142528819,-1883411.8478185558 6673765.073005969,-1883431.7724919873 6673758.967942359,-1883435.029648588 6673769.700215263))'::GEOMETRY))
SELECT
ST_Area(geom) AS sqm,
ST_Area(
ST_Transform(geom,2249)) AS sqft
FROM j;
sqm | sqft
-------------------+-------------------
237.6060612927441 | 2341.135411173445
EPSG 32636: Units are metres (Ellipsoid WGS84)
EPSG 2249: Units are feet (Ellipsoid GRS1980)
To your questions:
Does those values means square meter (m^2) ?
Yes.
So the values are less than 1 square meter ?
Yes. I'm curious about what are your geometries about. Perhaps you mixed up different SRS?
Unrelated note: Spatial operations with SRS's that have the same unit might still deliver different results, as they might also use different ellipsoids. The example below will calculate the area of the same geometry using SRS's that have metre as unit but a different ellipsoid. Note the difference in the result:
WITH j (geom) AS (
VALUES ('SRID=32636;
POLYGON((-1883435.029648588 6673769.700215263,-1883415.1158478875 6673776.142528819,-1883411.8478185558 6673765.073005969,-1883431.7724919873 6673758.967942359,-1883435.029648588 6673769.700215263))'::GEOMETRY))
SELECT
ST_Area(geom) AS sqm_32636,
ST_Area(
ST_Transform(geom,26986)) AS sqm_26986
FROM j;
sqm_32636 | sqm_26986
-------------------+--------------------
237.6060612927441 | 217.49946674261872
EPSG 32636: Units are metres (Ellipsoid WGS84)
EPSG 26986: Units are metres (Ellipsoid GRS1980)
.. but if you stick to the same ellipsoid and unit, the math makes more sense:
WITH j (geom) AS (
VALUES ('SRID=32636;
POLYGON((-1883435.029648588 6673769.700215263,-1883415.1158478875 6673776.142528819,-1883411.8478185558 6673765.073005969,-1883431.7724919873 6673758.967942359,-1883435.029648588 6673769.700215263))'::GEOMETRY))
SELECT
ST_Area(
ST_Transform(geom,2249)) AS sqft_2249,
ST_Area(
ST_Transform(geom,2249)) * 0.3048 ^ 2 AS sqm_2249, -- manually converted from sqm to sqft
ST_Area(
ST_Transform(geom,26986)) AS sqm_26986
FROM j;
sqft_2249 | sqm_2249 | sqm_26986
-------------------+--------------------+--------------------
2341.135411173445 | 217.49859674966302 | 217.49946674261872
Demo: db<>fiddle

How to check if polygon in one table intersect with point and radius from second table?

How can I check if polygon in one table intersect with point and radius from second table ?
first table we have field (name: area) ,type geometry which contains polygon.
second table we have 2 fields:
field (name: pt) ,type: geometry which contains point
field (name: radius) ,type: int
The geometry values in WKB format
I want to check if the polygon intersect with the circle (point + radius).
How can I do it ?
You can use the ST_Distance function to find the distance between the polygon and the point. If the distance between them is less than the radius, then the polygon would intersect with a circle around the point with that radius.
Example query:
SELECT *
FROM polygon_table, circle_table
WHERE ST_Distance(polygon_table.area, circle_table.pt) <= circle_table.radius;
Use ST_Contains to check if the point is inside of the polygon and then calculate the buffer around your point and see if they intersect, with ST_Buffer and ST_Intersects respectively. Something like:
SELECT *
FROM polygon_table t1, circle_table t2
WHERE
ST_Contains(t1.area, t2.pt) AND
ST_Intersects(ST_Buffer(t2.pt,t2.radius),t1.area)
Note: the buffer will be created using the unit of your SRS. For instance, if you're using WGS84 it will be in degrees. If you want it in metres instead, use geography instead of geometry or simply cast it in real time, e.g. t1.area::geography.

Rotate geometry around its local axis

I tried to rotate a geometry around its local axis, but haven't found a way to do so. I know that there is ST_Rotate (see https://postgis.net/docs/ST_Rotate.html) for 2D calculations and (among others) ST_RotateX (see https://postgis.net/docs/ST_RotateX.html), but these methods rotate a geometry around the origin. I also tried to abuse ST_Affine when I tried to change (what seems to be) the origin (namely 0/0/0):
SELECT ST_Affine(
ST_GeomFromText(ST_AsText(runway_area)),
1, 0, 0, 0,
cos(rotRadians), -sin(rotRadians), 0, sin(rotRadians), cos(rotRadians),
--- use the geometry's centroid instead of 0, 0, 0
ST_X(ST_GeomFromText(ST_AsText(runway_area))), ST_Y(ST_GeomFromText(ST_AsText(runway_area))), ST_Z(ST_GeomFromText(ST_AsText(runway_area)))
)
It didn't work out - all I got was something that was way away from the intended location. Do I miss a very fundamental method by PostGIS here to rotate a geometry around one of its local axis?
Take a closer look at ST_Rotate, and note that there are three argument signatures:
geometry ST_Rotate(geometry geomA, float rotRadians);
geometry ST_Rotate(geometry geomA, float rotRadians, float x0, float y0);
geometry ST_Rotate(geometry geomA, float rotRadians, geometry pointOrigin);
The x0 and y0 or pointOrigin arguments for the last two signatures do what your questions asks: they allow the rotation to pivot around a custom defined coordinate.
And an example from the docs show how to rotate a geometry 60 degrees clockwise around the centroid:
SELECT ST_AsEWKT(ST_Rotate(geom, radians(-60.0), ST_Centroid(geom)))
FROM (SELECT 'LINESTRING (50 160, 50 50, 100 50)'::geometry AS geom) AS foo;
st_asewkt
--------------------------------------------------------------
LINESTRING(116.4225 130.6721,21.1597 75.6721,46.1597 32.3708)
(1 row)
You could combine ST_Rotate with ST_Centroid, example:
ST_Rotate(ST_GeomFromText(ST_AsText(runway_area)), -pi()/3, ST_Centroid(ST_GeomFromText(ST_AsText(runway_area)))
The last three arguments to ST_Affine do not represent the origin, but the global "shift" in the affine transformation. The documentation for ST_RotateX shows how to use this function to generate a rotation around the x-axis. All these parameters are zero since it's a rotation without translation.
If you want to employ a general axis, you would need to construct the corresponding rotation matrix and substitute its elements for the arguments a,b,c,d,e,f,g,h,i of ST_Affine and set xoff, yoff, zoff to zero.

Draw Camera Range with Postgis

i am working on some camera data. I have some points which consist of azimuth, angle, distance, and of course coordinate field attributes. In postgresql postgis I want to draw shapes like this with functions.
how can i draw this pink range shape?
at first should i draw 360 degree circle then extracting out of my shape... i dont know how?
I would create a circle around the point(x,y) with your radius distance, then use the info below to create a triangle that has a larger height than the radius.
Then using those two polygons do an ST_Intersection between the two geometries.
NOTE: This method only works if the angle is less than 180 degrees.
Note, that if you extend the outer edges and meet it with a 90 degree angle from the midpoint of your arc, you have a an angle, and an adjacent side. Now you can SOH CAH TOA!
Get Points B and C
Let point A = (x,y)
To get the top point:
point B = (x + radius, y + (r * tan(angle)))
to get the bottom point:
point C = (x + radius, y - (r * tan(angle)))
Rotate your triangle to you azimouth
Now that you have the triangle, you need to rotate it to your azimuth, with a pivot point of A. This means you need point A at the origin when you do the rotation. The rotation is the trickiest part. Its used in computer graphics all the time. (Actually, if you know OpenGL you could get it to do the rotation for you.)
NOTE: This method rotates counter-clockwise through an angle (theta) around the origin. You might have to adjust your azimuth accordingly.
First step: translate your triangle so that A (your original x,y) is at 0,0. Whatever you added/subtracted to x and y, do the same for the other two points.
(You need to translate it because you need point A to be at the origin)
Second step: Then rotate points B and C using a rotation matrix. More info here, but I'll give you the formula:
Your new point is (x', y')
Do this for points B and C.
Third step: Translate them back to the original place by adding or subtracting. If you subtracted x last time, add it this time.
Finally, use points {A,B,C} to create a triangle.
And then do a ST_Intersection(geom_circle,geom_triangle);
Because this takes a lot of calculations, it would be best to write a program that does all these calculations and then populates a table.
PostGIS supports curves, so one way to achieve this that might require less math on your behalf would be to do something like:
SELECT ST_GeomFromText('COMPOUNDCURVE((0 0, 0 10), CIRCULARSTRING(0 10, 7.071 7.071, 10 0), (10 0, 0 0))')
This describes a sector with an origin at 0,0, a radius of 10 degrees (geographic coordinates), and an opening angle of 45°.
Wrapping that with additional functions to convert it from a true curve into a LINESTRING, reduce the coordinate precision, and to transform it into WKT:
SELECT ST_AsText(ST_SnapToGrid(ST_CurveToLine(ST_GeomFromText('COMPOUNDCURVE((0 0, 0 10), CIRCULARSTRING(0 10, 7.071 7.071, 10 0), (10 0, 0 0))')), 0.01))
Gives:
This requires a few pieces of pre-computed information (the position of the centre, and the two adjacent vertices, and one other point on the edge of the segment) but it has the distinct advantage of actually producing a truly curved geometry. It also works with segments with opening angles greater than 180°.
A tip: the 7.071 x and y positions used in the example can be computed like this:
x = {radius} cos {angle} = 10 cos 45 ≈ 7.0171
y = {radius} sin {angle} = 10 sin 45 ≈ 7.0171
Corner cases: at the antimeridian, and at the poles.