Skip to main content
5 of 7
added 30 characters in body

ST_Within is only working on selective rows

I have two tables (Table A, 10k rows, and Table B, 100k rows). Table A contains a latitude and longitude while Table B contains a geometry type object (representing a polygon). I wanted to merge these two tables based on whether the latitude and longitude in Table A fall within the Polygon of Table B. To do this, I came up with the following code:

cnx = mysql.connector.connect(
        user='root', password='', unix_socket='/data/mysql/mysql.sock', database='topology')
    
fedInfo1 = pd.read_sql("""SELECT table1.`locCode`,
                    table2.`FEDUID`, table2.`FEDENAME`
                    FROM `Table2` table2
                    RIGHT JOIN `Table1` table1 
                    ON ST_WITHIN(POINT(table1.`longitude`, table1.`latitude`), table2.`geom`)
                    WHERE erp.`locCode` != 'TEST'
                    AND erp.`siteStatus` = 'ACTIVE'
                    """, cnx) #The where clauses are additional clauses (not related to the question)

The issue is that this code seems to work for only SOME rows (i.e. I don't have the amount of matches that I would like to see).

For example, I plotted the lat and long of one of the blank matches: Point

I verified it and this point should fall within the Polygon (in wkt format) of the geom column in Table2:

Polygon Representation

The problem after running the SQL query, I don't get a match between these two. Any ideas what I maybe missing?


To recreate the problem, I have configured the following SQL code and since I do need to give geometries as wkt formats, I am unable to include it as part of the problem, however, I have shared here: https://filedropper.com/d/s/rqDhS1b36w5HZvCB6ON245yNlWcMhE

This will contain SQL statements necessary to re-create part of table1 and table2. I hope this helps.