My problem is: I have 1000 parcels and 1 of them is mine, what I have to do is to find which are my neighbourhoods which means whose parcels are touched with my parcel.
The perfect example is showed on the picture that I have sent you here.
Red rectangle is my parcel and black rectangles are my neighbourhoods what I have to do is to find who are they.
I have created 2 tables, first table(parcels) contains all parcels without mine and second table(Myparcel) contains just my parcel.
As spatial data, I have the geometry of all parcels ( column geom) and the number of parcels.My parcel is red color and neighbourhood parcels are black
I’m trying to solve a problem, I have been trying for several days to solve it but no results!
So the final results is that I wanna know which parcels are touched with my parcel.
I have created 2 tables and here is my code which is not working
select st_touches(k.geom, s.geom) from kultura5_maxsip1 k, kultura5_maxsip s
2
Answers
ST_Touches()
likely won’t return much, if anything, because typically geometries are naturally shifted around, unless youST_Snap()
them together, or apply a uniformST_SnapToGrid()
. Also, the intuitive understanding of "touch" is misleading here, becauseST_Touches()
returnsTrue
if one of your shapes ends precisely where the other one begins, without crossing or overlapping. If one’s entirely inside the other, they aren’t considered touching either.It’s good to read about DE-9IM or simply go through the PostGIS doc to see which functions look for what types of spatial relationships. You can easily test all of them constructing very simple shapes with
ST_GeomFromText()
and then inspecting them withST_AsText()
or in QGIS. Demo:You can read about SRIDs on EPSG.io. It’s important to remember that they define your target area limits but also the unit of measurement. If you use global 4326 and then measure an ST_Distance() between shapes in that system, it’ll be in degrees, not meters. If you use 21781, you won’t be able to encode coordinates outside Switzerland with some buffer. There are systems in miles/feet, also.
These parcels will touch in
ST_Touches()
sense, because they re-use the same, perfectly round coordinates.Real-life data usually comes from error-prone measurements in suboptimal conditions. As soon as they move a tiniest bit, they no longer touch:
But if you ask which ones now intersect (have any spatial relationship) or are within a certain distance (that you’d consider close enough to agree they are touching in the colloquial sense):
You can read about Hausdorff distance, which is a bit better at telling you how close entire shapes are to each other, not just how long is the shortest shortcut between them.
ST_DWithin()
isTrue
based on whether the minimum distance, the shortest shortcut between shapes is below the limit.You could try using DBScan (implemented in PostGIS) for this use case to determine neighbouring clusters. Please give this a shot:
cluster_id
represents the cluster a row belongs to. With aepsilon
value of 0, the function clusters by intersection only. If your polygons are not intersecting, you could buffer them by a small value.