How to get distance between two points Postgres? - postgresql

Here is a query:
SELECT ST_DistanceSpheroid(geometry(a.location), ST_GeomFromText('POINT(28.828042, 47.023565)', 4326), 'SPHEROID["WGS 84",6378137,298.257223563]')
FROM users a
WHERE a.id=1
I got this error:
ERROR: ОШИБКА: parse error - invalid geometry
HINT: "POINT(28.828042, 4" <-- parse error at position 18 within geometry
SQL state: XX000
Where did I make mistake??

Well-known Text representations of points don't need a comma. Try this:
POINT(28.828042 47.023565)

Related

How to fix 'BOOM! Could not generate outside point' error when using ST_Distance_Spheroid with different SRS in PostGIS

I am trying to calculate the nearest feature distance from a point using PostGIS ST_Distance_Spheroid function.
Case 1:
The point and the feature collection are in the same SRS and I am expecting the distance to be in meters.
Here is my query:
SELECT
name,
ST_DistanceSpheroid(geom, ST_GeomFromText('POINT(-2.0363502486833616 52.688988284010456)', 4326), 'SPHEROID["WGS 84",6378137,298.257223563]' ) AS distance_m
FROM green_belt
ORDER BY geom <-> ST_GeomFromText('POINT(-2.0363502486833616 52.688988284010456)', 4326)
LIMIT 1;
The above query is working as expected. Here is the result:
Case 2:
When the SRS are different for the point and the feature collection, I am using the below query:
SELECT
name,
ST_DistanceSpheroid(geom, ST_Transform(ST_GeomFromText('POINT(-2.0363502486833616 52.688988284010456)', 4326), 27700), 'SPHEROID["WGS 84",6378137,298.257223563]' ) AS distance_m
FROM aonb
ORDER BY geom <-> ST_Transform(ST_GeomFromText('POINT(-2.0363502486833616 52.688988284010456)', 4326), 27700)
LIMIT 1;
But in this case, I am getting an error:
ERROR: BOOM! Could not generate outside point!
CONTEXT: SQL function "st_distance_spheroid" statement 2
SQL state: XX000
I even tried by transforming the coordinates and use it in the first query to get the distance:
SELECT
name,
ST_DistanceSpheroid(geom, ST_GeomFromText('POINT(357980.44 331724.97)', 27700), 'SPHEROID["Airy 1830",6377563.396,299.3249646]' ) AS distance_m
FROM aonb
ORDER BY geom <-> ST_GeomFromText('POINT(357980.44 331724.97)', 27700) LIMIT 1;
Even with this query, I got the same error
ERROR: BOOM! Could not generate outside point!
SQL state: XX000

Convert rows into Column in Postgress Error

Hello I have created a view, but want to pivot it.
OUTPUT before pivoting:
expected output:
my full query:
SELECT *
FROM CROSSTAB(
'SELECT DISTINCT GROUP_DEST::TEXT,DEST::TEXT,TIER::TEXT,RATE::TEXT FROM VBB_TIER ORDER BY 1,2')
AS CT(ROW_NAME TEXT, TIER_1 TEXT, TIER_2 TEXT )
I getting this error and unable to resolve:
ERROR: invalid source data SQL statement
DETAIL: The provided SQL must return 3 columns: rowid, category, and values.
SQL state: 22023
Using filtered aggregation is typically a lot easier than the somewhat convoluted crosstab() function:
select group_dest,
dest,
max(rate) filter (where tier in ('0-100', ('0-150')) as tier_1,
max(rate) filter (where tier in ('101-200', '151-350') as tier_2
from vbb_tier
group by group_dest, dest;

Trying to FOR LOOP a column with ST_CONTAINS in PgAdmin4

I am working with PgAdmin4 to create a View that consists of a large set of geometric data. Part of this data is polylines that exist within polygons. I am attempting to write a code that can loop through all of my polyline data in a given column, and check if it is in a given polygon, and return true/false. So far this is what I have.
DO
$$
BEGIN
FOR i IN (SELECT "geom" FROM "street map"."segment_id")
LOOP
SELECT ST_CONTAINS(
(SELECT "geom" FROM "street map"."cc_districts" WHERE "district number" = 1),
(i)
)
RETURN NEXT i
END LOOP;
END
$$
The error I receive when running this code is as follows:
ERROR: loop variable of loop over rows must be a record or row variable or list of scalar variables
LINE 4: FOR i IN (SELECT "geom" FROM "street map"."segment_id")
^
SQL state: 42601
Character: 18
From what I understand, "i" must refer to a "row variable", and I tried to define that variable with this piece of code:
(SELECT "geom" FROM "street map"."segment_id")
Any ideas to get this going would be very helpful.
A simple join would be much more efficient here
SELECT line.*, polygon.id IS NOT NULL AS is_in_polygon
FROM line
LEFT JOIN polygon
ON ST_Contains(polygon.geometry, line.geometry)
AND polygon.id = 1
Which can be translated as:
Get every field of a line record, and true if the polygon.id exists (is not null), false otherwise (more below). Name this boolean field is_in_polygon.
Do this on every line.
Join (link) each line to the polygon layer. If there is no match, keep the line information and put NULL for every polygon field (this is a left join). If there is a match, keep both line and polygon information.
A match is found if the polygon.geometry contains the line.geometry and if the polygon.id = 1
I have found a way to make this work without doing a JOIN or needing a FOR LOOP. The following code works.
SELECT * FROM "street map"."segment_id"
WHERE
ST_WITHIN(
ST_CENTROID((ST_SetSRID(geom, 4326))),
ST_SetSRID((SELECT geom FROM "street map"."cc_districts" WHERE "district number" = 1),4326))
This lets me do what I was intending by runing the process on all rows in a given column. I swapped from ST_CONTAINS to ST_WITHIN, and I also am now checking if the centroid of the polyline is within the given polygon by using ST_CENTROID. I found that the error goes away by asserting the SRID of the geometry to 4326 using ST_SetSRID. I'm not sure why that works, as my geoms already have an SRID of 4326.
Thanks for all of those who answered

passing column name in ST_GeomFromText instead of value

Below query works fine for me
select ST_GeomFromText('POINT(-111.7844620 32.9667050)',4326) from elec_common.abc;
geometry column has same value as above which is -111.7844620 32.9667050
however the below query shows an error
select ST_GeomFromText('POINT(geometry)',4326) from elec_common.abc;
SQL Error [XX000]: ERROR: parse error - invalid geometry Hint:
"POINT(ge" <-- parse error at position 8 within geometry
org.postgresql.util.PSQLException: ERROR: parse error - invalid
geometry Hint: "POINT(ge" <-- parse error at position 8 within
geometry

Use function result as columns in query with PostgreSQL

I'm using PostgreSQL 9.1 and let's say I have a type in PostgreSQL like:
CREATE TYPE sticker AS (
customer_id integer,
customer_machine_id integer,
);
and I have a plpgsql named get_sticker that returns the type sticker...
I can do this fine:
select get_sticker(a_value), * from foo_bar;
But, this returns the result in a tuple (which totally makes sense). But, how can I convert (basically unpack) to columns?
It seems like it would be something like the following, but it fails.
select get_sticker(a_value).*, * from foo_bar; -- <<<< FAIL
Error message:
ERROR: syntax error at or near "."
SQL state: 42601
You need an extra set of parentheses:
select (get_sticker(a_value)).*, * from foo_bar;