简体   繁体   中英

Find points inside the intersection of polygons in PostgreSQL/PostGIS

I want to find the points inside the intersection (Figure 1) of polygons in PostgreSQL.

Figure 1 example 在此处输入图片说明

I use psycopg2 and the code that I used is:

intersects = """select ST_Intersects( ST_GeographyFromText('SRID=4326; POLYGON(( 32.0361328 33.6877818, 31.9042969 33.5780147,33.5742188 11.3507967,66.2695313 20.4270128, 51.9433594 34.270836, 32.0361328 33.6877818))'),
              ST_GeographyFromText('SRID=4326; POLYGON((33.7060547 37.1953306,36.6943359 16.0880422,64.9072266 12.4258478,64.8632813 37.0551771,33.5742188 37.1953306,33.7060547 37.1953306))')), col.vessel_hash,ST_X(col.the_geom) AS long, ST_Y(col.the_geom) AS lat
        from samplecol as col"""

        cursor.execute(intersects)
        pointsINtw = cursor.fetchall()
        count = 0;
        shipsrecords = open("/home/antonis/Desktop/testme1.txt", "w")
        for ex in pointsINtw:
            if str(ex[0])=='True':
                count = count + 1
                shipsrecords.write(str(ex) + "\n")

        print (CBLUE + "Number of returned results: " + CBLUEEND), count

Example record:

vessel_hash  | speed |  latitude   |  longitude  | course | heading |        timestamp         |                      the_geom                      
--------------+--------+---------+-------+-------------+-------------+--------+---------+--------------------------+----------------------------------------------------
 103079215239 | 5     | -5.41844510 | 36.12160900 | 314    | 511     | 2016-06-12T06:31:04.000Z | 0101000020E61000001BF33AE2900F424090AF4EDF7CAC15C0

The problem is that above code does not work properly. I create two polygons like Figure 1 and I know that inside the intersection exist some points. But the code always returns all points from db.

If I create two polygons that do not intersect then the algorithm seems to work properly as it does not return any point.

Does anyone know what am I doing wrong?

demo:db<>fiddle (of your query, with your polygons, own points),

visualisation of the situation (maybe Chrome necessary)

ST_Intersects() only checks if the two given polygons share some space. It is true, they share. But there is no part within your query that includes the check with the points. You only call the intersection check but without any usage of your point column.


I believe you need to calculate the intersection polygon ( ST_Intersection() ) instead of only check for its existance. After that you can take this result to check whether your points are in it or not ( ST_Contains() ):

Pseudocode:

SELECT
    ST_Contains(
        ST_Intersection(my_geometry1, my_geometry2),
        my_pointgeometry
    )
...

demo:db<>fiddle (Demo uses geometry instead of geography and the polygon needs to get valid for some reasons; so you need to adapt this to your use case)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM