pythondjangogispostgisgeodjango

How to check if two polygons have internal points in common, with geodjango and postgis


I am am using geodjango with postgis backend. Giving two polygons I want to check if they overlap. I mean, they have interior points in common. If we check

A.function(B)

In the following picture "Example 1" would be False, "Example 2", would be False (cause they only have edges in common) and "Example 3" Would be True, cause they have interior points in common. If both polygons are equal, the the function would return True as well.

Examples


Solution

  • The way to do it is using the geodjango functions "touches" and "intersects", wich use the "ST_Touches" and "ST_Intersects" postgis functions in the background

    if A.intersects(B) and not A.touches(B):
        print("Geometries have interior points in common")
    
    1. ST_Touches: Returns TRUE if A and B intersect, but their interiors do not intersect.
    2. ST_intersects: Compares two geometries and returns true if they intersect. Geometries intersect if they have any point in common.