function st_intersects() does not exist using PostgreSQL 9.3 - postgresql

I have a table as shown below with three columns.
Table: geomet
create table geomet
(
cola float,
colb float,
geopath geometry
);
Insertion of records:
insert into geomet values('12.32232442','43.2324535',point(12.32232442,43.2324535)::geometry);
I have this:
select * from geomet;
cola colb geopath
---------------------------------------------------------------------
12.32232442 43.2324535 01010000004F34D5B407A528409D2B4A09C19D4540
Note: I need to find the Intersect of column geopath in my table as shown above. I have also installed PostGIS.
So I have tried this:
Try 1:
SELECT ST_Intersects(geopath) from geomet;
Got an error:
ERROR: function st_intersects(geometry) does not exist
Try 2:
SELECT ST_Intersects(cola,colb) from geomet;
Got an error:
ERROR: function st_intersects(double precision, double precision) does not exist

ST_Intersects requires two geometries or two geometry fields, as you are checking for the intersection between one geometry and another or one set and another set.
SELECT ST_Intersects(geomA, geomB) FROM some_table;
or
SELECT a.id., b.id, ST_Intersects(a.geom, b.geom)
FROM table a, table b
WHERE a.id > b.id;
will give you all the pairwise intersection between two tables. Obviously, you can use ST_Intersects in a where clause in a similar fashion, to only return rows where the records from two different tables intersect.

Related

What is wrong with my ST_Within query - query result contains point twice although it exists only once

I have two point tables, tab_1 and tab_2. I want to query all points from the first table that are probably the same points from the table 2. So i give the points from table 2 a buffer. Then I want to get the points from table 1 and query from table 2 within a 30 m buffer. My problem is, I get the points from table 1 and table 2 twice. But point 1 from table 1 exists only once and point 1 from table 2 also only once.
My query is:
with
"points1" as
(
select id, geom from tab_1
)
,
"points2" as
(
select id, geom from tab_2
)
select "points1".*, "points2".* from "points1", "points2"
where
st_within(st_transform("points1".geom, 31468), st_buffer(st_transform("points2".geom, 31468), 30)) = true;
id_tab1
geom
id_tab2
geom
st_distance
767074270
POINT (11.6968379 48.132722)
16455
POINT (11.69707 48.13265)
19.041083533921977
767074270
POINT (11.6968379 48.132722)
16455
POINT (11.69707 48.13265)
19.041083533921977
The query should be give only one result:
id_tab1
geom
id_tab2
geom
st_distance
767074270
POINT (11.6968379 48.132722)
16455
POINT (11.69707 48.13265)
19.041083533921977
Is my query wrong?
STEP 1. Query
SELECT *
FROM tab_1
JOIN tab_2
ON ST_DWithin
( ST_Transform(tab_1.geom, 31468)
, ST_Transform(tab_2.geom, 31468)
, 30
)
STEP 2. Spatial index
Most likely, the query cannot use the spatial index (even if it exists) and the function ST_DWithin() properly (ST_Transform() does not allow using an existing spatial index).
Solution - create new spatial indexes for EPSG:31468
CREATE
INDEX tab_1_geom_31468_idx
ON tab_1
USING GIST (ST_Transform(geom, 31468))
;
CREATE
INDEX tab_2_geom_31468_idx
ON tab_2
USING GIST (ST_Transform(geom, 31468))
;

How to fix "ERROR: aggregate functions are not allowed in UPDATE"

I am trying to update a column based on a few conditions, using a calculation.
The theory I am using is as follows;
If column1 contains 'string' then 'calculation of column2 and column3' gets put in column4.
The calculation works, but I am struggling to find a way to UPDATE a column by using these IF conditions and a SUM.
I have searched stack and postgres documentation. I see that there are a number of aggregate errors but none specifically solve this problem.
UPDATE table1
SET "column4" = CASE
WHEN "column1" ILIKE '%Y%' THEN SUM(CAST("column2" AS
numeric(4,2))) / SUM(CAST("column3" AS numeric(4,2)))
END;
The error which I am getting is as follows;
ERROR: aggregate functions are not allowed in UPDATE
LINE 7: WHEN "column1" ILIKE '%Y%' THEN (SUM(CAST("...
Perform your calculations in a Common Table Expression:
WITH cte_avg AS (
SELECT SUM(CAST("column2" AS numeric(4,2))) / SUM(CAST("column3" AS numeric(4,2))) AS avg
FROM table1
)
UPDATE table1
SET "column4" = cte_avg.avg
FROM cte_avg
WHERE "column1" LIKE '%Y%'

How to check an ascending ordered column value in where clause in postgresql?

I am new to postgresql. I want to join two tables if one geometry of first table is contained by the geometry of second table. So, I have written and executed this part of the query as following and it is running fine.
select edge.start_id, cls.gid
from edge_table edge
inner join cluster_info cls on st_contains(cls.geom,st_setsrid(edge.start_geom,3067));
But it is giving the start_id and its containing geom id (as mentioned cls.gid in the query) in a random order such as following:
start_id gid
26040 2493
43323 2490
26208 2400
42754 2433
43537 2434
1379 2434
43570 2904
42887 2475
43689 2495
43211 2904
But I need to insert the result in another column named start_cls in my edge table. I need to identify the row where the cls.gid should be inserted. So, I need to check the value of start_id for each row and the cls.gid corresponding to that start_id should be put in that row. Assume, four rows of my edge table are following:
gid start_id end_id start_geom end_geom start_cls end_cls
1 81608 81608 01010000007368912D8B622341E5D022EBEAF65A41 01010000007368912D8B622341E5D022EBEAF65A41
2 81557 81520 010100000085EB51F89C0723418B6CE7DB9F8E5A41 0101000000986E1203DE0723416DE7FB51A38E5A41
3 189898 80812 01010000006F1283C0A093214179E926F1A1005B41 0101000000BE9F1A6FF3942141022B871EEC005B41
4 80952 80476 0101000000666666E67F832341F2D24DBA38B45A41 0101000000736891EDB48423413BDF4F755AB45A41
I need to fill the start_cls column first. So, the cls.gid value of 81608 (first start_id) should be there at first row under start_cls column. So, I have given one where clause as following:
select edge.start_id, cls.gid
from edge_table edge
inner join cluster_info cls on st_contains(cls.geom,st_setsrid(edge.start_geom,3067))
where (select start_id from edge_table) = edge.start_id;
But, it is giving following error:
ERROR: more than one row returned by a subquery used as an expression
********** Error **********
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
I tried with the following query too but no luck.
select edge.start_id, cls.gid
from edge_table edge
inner join cluster_info cls on st_contains(cls.geom,st_setsrid(edge.start_geom,3067))
where (select start
from (select start_id as start
from edge_table) as s) = edge.start_id;
Please help with this query. It has some geometry part but the main problem is in postgresql query organisation. So, I have raised this question in stackoverflow instead of gis.stackexchange.

More than one row error when populating a table

I have create a table using create table and with these column:
create table myschema.mytable(
id serial PRIMARY KEY,
row_num integer,
col_num integer,
pix_centroid geometry,
pix_val double precision
)
When I am trying to populate it:
insert into pixelbased (id, row_num, col_num, pix_centroid, pix_val)
values (
DEFAULT,
(select((ST_PixelAsPolygons(rast, 1)).x) from mytable where rid=3),
(select((ST_PixelAsPolygons(rast, 1)).x) from mytable where rid=3),
(select(ST_Centroid((ST_PixelAsPolygons(rast, 1)).geom)) from rwanda8 where rid=3),
(select(ST_PixelAsPolygons(rast, 1)).val from mytable where rid=3)
)
I am encountered with the following error:
ERROR: more than one row returned by a subquery used as an expression.
I know that since I have more than one row for every column it does make sense to have such an error. But I really need to have all the columns calculated as mentioned. Anyone knows what should I do?
In fact I want to insert the result the following query in the table:
select
(ST_PixelAsPolygons(rast, 1)).val as geomval1,
(ST_PixelAsPolygons(rast, 1)).x as X,
(ST_PixelAsPolygons(rast, 1)).y as Y,
(ST_Centroid((ST_PixelAsPolygons(rast, 1)).geom)) as geom
from rwanda8
where rid=3
Anyone knows what should I do?
Just use the select query in instead of the values
insert into pixelbased (row_num, col_num, pix_centroid, pix_val)
select
(ST_PixelAsPolygons(rast, 1)).val as geomval1,
(ST_PixelAsPolygons(rast, 1)).x as X,
(ST_PixelAsPolygons(rast, 1)).y as Y,
(ST_Centroid((ST_PixelAsPolygons(rast, 1)).geom)) as geom
from rwanda8 where rid=3
Do not insert the id as it is a serial and will generate itself.
One of your subqueries returns more than 1 row. So use LIMIT 0,1 or something to get only one single value per subquery.
If you need more than 1 value per column, you should review your insert algorithm and use a cursor for instance.

Using two different rows from the same table in an expression

I'm using PostgreSQL + PostGIS.
In table I have a point and line geometry in the same column of the same table, in different rows. To get the line I run:
SELECT the_geom
FROM filedata
WHERE id=3
If i want to take point I run:
SELECT the_geom
FROM filedata
WHERE id=4
I want take point and line together, like they're shown in this WITH expression, but using a real query against the table instead:
WITH data AS (
SELECT 'LINESTRING (50 40, 40 60, 50 90, 30 140)'::geometry AS road,
'POINT (60 110)'::geometry AS poi)
SELECT ST_AsText(
ST_Line_Interpolate_Point(road, ST_Line_Locate_Point(road, poi))) AS projected_poi
FROM data;
You see in this example data comes from a hand-created WITH expression. I want take it from my filedata table. My problem is i dont know how to work with data from two different rows of one table at the same time.
One possible way:
A subquery to retrieve another value from a different row.
SELECT ST_AsText(
ST_Line_Interpolate_Point(
the_geom
,ST_Line_Locate_Point(
the_geom
,(SELECT the_geom FROM filedata WHERE id = 4)
)
)
) AS projected_poi
FROM filedata
WHERE id = 3;
Use a self-join:
SELECT ST_AsText(
ST_Line_Interpolate_Point(fd_road.the_geom, ST_Line_Locate_Point(
fd_road.the_geom,
fd_poi.the_geom
)) AS projected_poi
FROM filedata fd_road, filedata fd_poi
WHERE fd_road.id = 3 AND fd_poi.id = 4;
Alternately use a subquery to fetch the other row, as Erwin pointed out.
The main options for using multiple rows from one table in a single expression are:
Self-join the table with two different aliases as shown above, then filter the rows;
Use a subquery expression to get a value for all but one of the rows, as Erwin's answer shows;
Use a window function like lag() and lead() to get a row relative to the current row within the query result; or
JOIN on a subquery that returns a table
The latter two are more advanced options that solve problems that're difficult or inefficient to solve with the simpler self-join or subquery expression.