0

QUESTION: I have 2 tables with bike and hike trails of a country (Poland).

Table bicycle_merge

way      | route  | name        |network    |osmc_color| state  
geometry | text   | text        |text       |text      | text
----------------------------------------------------------------
{geo1}   |bicycle |szlak1       |ncn        |blue      |proposed
{geo2}   |bicycle |szlak2       |lcn        |red       |null
{geo3}   |bicycle |szlak3       |ncn        |green     |proposed
{geo4}   |bicycle |szlak4       |rcn        |blue      |proposed
{geo5}   |bicycle |szlak5       |lcn        |blue      |null
{geo6}   |bicycle |szlak6       |ncn        |yellow    |proposed
....and so on

Table hiking_merge

way      | route  | name        |network    |osmc_color| state  
geometry | text   | text        |text       |text      | text
----------------------------------------------------------------
{geo1}   |hiking  |szlak1       |ncn        |blue      |proposed
{geo2}   |hiking  |szlak2       |ncn        |red       |null
{geo3}   |hiking  |szlak3       |ncn        |green     |proposed
{geo4}   |hiking  |szlak4       |ncn        |blue      |proposed
{geo5}   |hiking  |szlak5       |ncn        |blue      |null
{geo6}   |hiking  |szlak6       |ncn        |yellow    |proposed
...and so on

{geo1},{geo2} etc. is a unique geometry of every trail.

Every trail has a unique geometry but sometimes bicycle and hiking routes overlap. The picture below shows example in QGIS: QIS I would like to delete the overlapping parts as I already have them in a different database.

EDIT: I have tried a solution suggested by Julia Leder :

create table intersections as
(select st_intersection(b.way, h.way) as overlapping_section
from bicycle_merge b, hiking_merge h
where st_intersects(g.way, h.way));

Not all overlapping lines were detected. I'm not sure why? Here's a picture to show the issue:

enter image description here

Voyteck
  • 81
  • 2
  • 12

3 Answers3

0

Are you looking for this:

(SELECT way,route,name,color,description,network FROM bicycle_merge
UNION
SELECT way,route,name,color,description,network FROM hiking_merge)
EXCEPT
(SELECT way,route,name,color,description,network FROM bicycle_merge
INTERSECT
SELECT way,route,name,color,description,network FROM hiking_merge);

This is the MS SQL syntax. For Oracle, write MINUS instead of EXCEPT.

Hellmar Becker
  • 2,824
  • 12
  • 18
  • Weird, that query returned exactly the same result as I would join bicycle_merge and hiking_merge tables. Exept and Intersect didn't change anything. I'm on Postgres 9.3 by the way. – Voyteck Mar 30 '17 at 13:07
0
select *
from
    (
        select *
        from bicycle_merge bm
        where not exists (
            select 1
            from hiking_merge
            where geometry = bm.geometry
        )
    )
    union
    (
        select *
        from hicking_merge hm
        where not exists (
            select 1
            from bicycle_merge
            where geometry = hm.geometry
        )
    )
Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
  • I understand this approach but how can I select 1? I just want to get rid of the overlapping lines which have different unique geometry so I just can't type in geometry value. Postgres has to check one by one, I guess. Sorry, I'm still like a baby in a fog :( – Voyteck Mar 30 '17 at 13:17
  • @Voyteck You mean you want geometry `1` once in the output? Otherwise I don't understand your comment. – Clodoaldo Neto Mar 30 '17 at 13:19
  • Sorry I will try to edit my question again as I can see it is not clear. I will do some drawings etc :) – Voyteck Mar 30 '17 at 13:37
  • I have updated my question again. I hope it's clear now. – Voyteck Mar 30 '17 at 18:06
0

This can be relatively easily done with PostGIS functions. You need to find the overlapping section between the two geometries, which can be done with

ST_Intersection(a.geometry, b.geometry)

Then you need to subtract that section from the original geometry from either biking or hiking geometry, which would be done with

ST_Difference(a.geometry, b.geometry)

To put this all together:

create table intersections as
(select st_intersection(st_buffer(b.way,10), st_buffer(h.way,10)) as overlapping_section
from bicycle_merge b, hiking_merge h
where st_intersects(st_buffer(g.way, 10), st_buffer(h.way,10)));


update bicycle_merge b 
set way = st_difference(b.way, overlapping_section)
from intersections where st_intersects(b.way, overlapping_section);

update hiking_merge h 
set way = st_difference(h.way, overlapping_section)
from intersections where st_intersects(h.way, overlapping_section);

This may need to be adjusted somewhat depeding on types of geometries and how you want to store them, but the above should give you the idea.

The 2nd parameter of st_buffer function takes a radius of the buffer. The units of this number depend on the srid of geometry. They can be in feet, meters or degrees depending on the SRID of your geometry, which you can check by

select st_srid(way) from biking_merge limit 0;
Julia Leder
  • 786
  • 4
  • 8
  • This is a very good approach but didn't work in 100%. I have updated my question according to your anwser. Thank you. – Voyteck Mar 30 '17 at 19:49
  • @Voyteck can you post st_astext(way) of the two problematic geometries? – Julia Leder Mar 30 '17 at 19:50
  • @ Julia Leder I'm not sure how to do that. I can say that these are not the only ones which were not detected. Here's a picture of different ones https://s8.postimg.org/4cqczapkl/image.png – Voyteck Mar 30 '17 at 19:54
  • @ Julia Leder Sorry, I updated a link to picture. I was thinking about st_touches or st_overlaps? Maybe that will work? The overlapping geometries are the same as they were imported from the same line which contained different attributes. – Voyteck Mar 30 '17 at 19:58
  • @Voyteck it's possible that the geometries lie close to each other but do not actually intersect? I've adjusted my answer to user a buffer. Also added a note on the use of st_buffer function at the end of the answer – Julia Leder Mar 30 '17 at 20:04
  • @ Julia Leder Now the query runs forever :( As I wrote above, the lines are taken from one line which contained different attributes. These were split into different bicycle and hiking routes so the geometry has to be the same. – Voyteck Mar 30 '17 at 20:14
  • @Voyteck According to the specs st_intersects would return true if the geometries touch or overlap, so in theory that wouldn't help. `Overlaps, Touches, Within all imply spatial intersection. If any of the aforementioned returns true, then the geometries also spatially intersect. ` – Julia Leder Mar 30 '17 at 20:16
  • @ Julia Leder I guess I'm stuck at the moment. – Voyteck Mar 30 '17 at 20:21
  • @Voyteck I'm not really following how this is the same geometry if you obviously have two distrinct geometries in other places? Which srid are you using? Have you made sure that the units used by SRID are not degrees? – Julia Leder Mar 30 '17 at 20:24
  • @Voyteck how big are the tables? – Julia Leder Mar 30 '17 at 20:24
  • bicycle_merge is 1454 lines and hiking_merge is 1679 lines. EPSG 4326. – Voyteck Mar 30 '17 at 20:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139532/discussion-between-voyteck-and-julia-leder). – Voyteck Mar 30 '17 at 20:32