I need to cut different lines with an identical geometry but different attributes (eg. colour) with a set of points. The points also have the attribute colour.
My knife points should only cut the lines with the same colour value. Red points should cut only red lines, green points should only cut green lines and so on...
I tried the following:
with knife as(
select st_union(geom) as geom, colour
from points
group by colour)
select lines.colour,(st_dump(st_split(lines.geom,knife.geom))).geom as geom
from lines, knife
where lines.colour=knife.colour
Sadly, my 'selective knife' isn't so selective and cuts all lines regardless of their colour.
Can anybody help?
Edit:
#JimJones I couldn't find a SQL-fiddle that supports the PostGIS extension. But with my data sample the knife somehow works perfectly.
I have no idea why whats wrong with my real data. The real data lines are in fact multilinestrings, could that be a problem? (I somehow struggling in creating multilinestrings with the insert-statement)
Edit2:
found a fiddle with PostGIS
db<>fiddle here
create table points(
id serial,
colour varchar,
geom geometry(point,4326)
);
create table lines(
id serial,
colour varchar,
geom geometry(linestring,4326)
);
insert into lines(colour, geom)
VALUES
('red','linestring(1 1,10 1)'),
('green','linestring(1 1,10 1)'),
('blue','linestring(1 1,10 1)');
insert into points(colour, geom)
VALUES
('red',(st_makepoint(2,1))),
('red',(st_makepoint(4,1))),
('red',(st_makepoint(6,1))),
('red',(st_makepoint(8,1))),
('green',(st_makepoint(2.5,1))),
('green',(st_makepoint(5,1))),
('green',(st_makepoint(7.5,1))),
('blue',(st_makepoint(3,1))),
('blue',(st_makepoint(6,1))),
('blue',(st_makepoint(9,1)));
with knife as(
select st_union(geom) as geom, colour
from points
group by colour)
select lines.colour,(st_dump(st_split(lines.geom,knife.geom))).geom as geom
from lines, knife
where lines.colour=knife.colour
ยดยดยด
[1]: https://dbfiddle.uk/?rdbms=postgres_12&fiddle=8642640bb690dfee7d31006a673e2dcf
Disclaimer: Im a postgres beginner. Even though I'm quite sure that I found the solution to my problem, there could be something wrong. So feel free to correct me if necessary
If you want to ST_Split a line, which you formerly merged by using ST_Union, you sould use ST_Linemerge before the ST_Split and ST_Dump.
Even after a ST_Union, PostGIS seems to 'remember' that theese lines were formerly playing for different teams. So when you ST_Slit with you new knife and ST_Dump the GeometryCollection, the linestring is cut at the 'old soldering points' as well.
If you ST_Linemerge after the ST_Union, PostGIS seems to iron out theese connection points and you can safely use your knife.
In this fiddle, I created 2 linetypes and some knife points. There is one solid line (blue) and some yellow line segments which are partly overlapping but all together have the same spatial extent as the blue line.
I used ST_Union and ST_Collect respectively (grouped by colour) to merge the yellow line segments into one single line (with no effect on the blue line, obviously).
In a second step I splitted and dumped the lines again with my knife points, one time with st_linemerge and one time without st_linemerge for the 'unioned' und 'collected' lines respectively.
The results (I counted the number of new segments and the cummulated line length for each colour) show, that only the split after the linemerged st_union gives the correct result. Using only St_Union results in the right length, but the number of linesegments is wrong. ST_Collect will keep the formerly overlapping parts,so the length as well as the number of segments are not usefull at all(for my purpose).
Related
I have 2 postGIS tables CITIES and WATERBODIES, I want to remove waterbody geometry from cities, I'm trying to use
CREATE TABLE usa_No_water_100 AS
SELECT ST_Difference(usa_100.geom, water_100.geom) AS geom
FROM usa_100, water_100
but this creates a table with 10000 entries, I'm looking for the same 100 rows which I have in cities shape but with waterbodies geometry subtracted.
Geometry layer is as follows
Water layer is as follows
I want to remove the cities shape which is under the water shape, so that geometry will be reduced
PS: I'm open to doing this via python as well, if you have any suggestions
You can to compare the geometries of usa_100 with water_100 table using ST_Difference as you suggested, but to avoid getting the product of both tables you have to put one in a subquery or CTE, e.g.
WITH j (geom) AS (
SELECT ST_Union(geom) FROM water_100
) SELECT ST_Difference(usa_100.geom,j.geom)
FROM usa_100,j
Note: keep in mind what the ST_Difference documentation says:
If A is completely contained in B then an empty geometry is returned.
So, if a polygon on usa_100 entirely lies on a the result of ST_Union of water_100, it will return an empty geometry. If they do not spatially overlap, you do not have to worry about it.
How can I put the values in the table into the polygon function?
Note : ST_GeomFromText('LINESTRING(75 29,77 29,77 29, 75 29)') --coordinates are representations. What I want is to put the values in the table into the function.
you could use the geometry directly (if they were suitable, which they are not, see below).
SELECT ST_MakePolygon(wkb_geometry) FROM madened50 WHERE ...
BUT, a polygon is made of at least 4 points, with the 1st and last point being the same. In your table, the lines have 2 points only, so how exactly do you want to make a polygon from a line?
Don't forget that you can't just add one of point multiple times (the polygon will be invalid where the vertex is repeated, and a lot of function won't work properly with invalid geometries)
If it was only for the last point, which must be the same as the 1st one, you could add it though
SELECT ST_MakePolygon(ST_AddPoint(wkb_geometry, ST_StartPoint(wkb_geometry)))
FROM madened50 WHERE ...
I have a similar case as shown in the below question.
https://gis.stackexchange.com/questions/293695/filling-gaps-between-polygons-using-qgis
That solution uses qgis, but I want to use a postgis function on a table on the geometry column. The solution talks about convex hull but not sure how to use the convex_hull postgis function here. I want to fill the gap by moving/merging the gap to the neighboring polygon.
As long as you have a column to group them by (e.g., "postal_code"), you can do:
SELECT
st_convexhull(st_collect(geom_column))
FROM
my_geom_table
GROUP BY
grouping_column -- e.g., "postal_code"
I have a table with polygon geometries. The polygons are separate to each other like the picture below:
I want to get a single polygon formed after merging these polygons using Postgresql. Below is the expected polygon:
Please ignore the buffer in boundaries of red polygon, it is just to make the picture clearer.
My polygon table has two columns, id and geom. I have tried using ST_Collect, ST_MakePolygon, and ST_ExteriorRing but using these I only get MULTIPOLYGON having these polygons as it is. I need a single polygon. Any help would be appreciated.
You can use similar SQL to this
SELECT
ST_ConcaveHull(
ST_Collect( ARRAY(
select
"Poly"
from table_name
where "OBJECTID" in (5,15,2)
)
),0.99
)
and add some pic to better illustrate
I have a table with points in the Postgres. The points are from the flight path. I need to filter out some points.
.
My question would be how can I select only points which are in line and then make a line from the selected points only if parallel lines are in no more then 20m distance in between lines. Turning points should be ignored.
What I have done so far is to select points that are in one line
WITH routes as (
SELECT
geom,
heading-lag(heading) over (order by time) AS direction
FROM mytable
)
SELECT direction, geom
FROM routes WHERE direction between -10 AND 10;
In my query, I calculated direction from heading, and selected points with a minor difference in the heading.
However, I don't know how to continue.
EDIT
Link to fiddler data table http://sqlfiddle.com/#!17/3262c/9/0
With my query from above, I can filter the points which are marked in red lines. How can I add those points in variables like line1=...
line2=... line3=... line4=... and line5=... ?
Thank you for any help.