I have made a buffer on the area shown. If you closely in zoom there is a line beneath the buffer. I am using ST_Within for the comparison. There are two linestrings in my area. I am making buffer of about 10 meters on one area and comparing it with the other linestring using ST_WITHIN. The linestring is divided into different segments. ST_Within checks that two geometries should be completely inside. If you can see closely in the picture I only want the linestring which is inside the buffer but ST_within ignores the complete segment because small portion of line is outside buffer and Moreover, I also dont want this extra line which is outside buffer. Is there a way to compare these lines so that I can get only that segment which is inside buffer?. I cannot use ST_Intersects because then I will have other extra intersecting lines as well which I dont want. Here is my query:

Create Table CombinedTable As select area1.Geometry AS Geometry from Area1 area1,Area2 area2 where 

enter image description here


What you can do is use ST_Intersect to find lines that are in contact with the polygon and then use ST_Intersection to get the portion that is inside or on the border of the polygon.

It would look something like:

Create Table CombinedTable As select
ST_Intersection(area1.Geometry,ST_Buffer(SetSrid(area1.geometry,3857),10)  AS Geometry
from Area1 area1,Area2 area2 where
  • You saved me man. Thank you very much. I greatly appreciate.
    – mimetype
    Sep 13 '17 at 15:22
  • Well is it possible to get the Area2 in the intersection part?
    – mimetype
    Sep 13 '17 at 15:31
  • because what I really want is area2 road which is in segments
    – mimetype
    Sep 13 '17 at 15:37
  • Well, that depends on what you mean. You can get the whole polygon segment, just use it without any ST_Intersection. But if you want to cut it to the part of the segment where the line is you will have to define where you want to cut. One way is to buffer also the line (maybe to a larger number to get the full width of the polygon) and do the intersection between the two polygons. That might be what you want, or not. Sep 13 '17 at 16:12
  • I actually dont want the intersection of two polygons in this way I will get a multipolygon as a result. I want linestring of area2 when I combine area1 and area2
    – mimetype
    Sep 13 '17 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.