title: Find points inside the intersection of polygons in PostgreSQL/PostGIS
body: <p>I want to find the points inside the intersection (Figure 1) of polygons in PostgreSQL.</p>
  
  <p><em>Figure 1 example</em>
  <a href="https://i.stack.imgur.com/JTaoj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JTaoj.png" alt="enter image description here"></a></p>
  
  <p>I use psycopg2 and the code that I used is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Example record:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>If I create two polygons that do not intersect then the algorithm seems to work properly as it does not return any point. </p>
  
  <p>Does anyone know what am I doing wrong?</p>