根据一个 table 的纬度和经度是否存在于另一个的多边形中,加入两个 table(来自不同的数据库)
Joining two tables (from different databases) based on whether lat and long from one table are present in the polygon of the other
我正在尝试根据纬度和经度(存储在一个 table) 落在另一个 table 定义的多边形内。仅出于某些背景,我的第一个 table(在 database_1 中包含纬度和经度)看起来像:
Code price Latitude Longitude
0 A001 1200 43.65 -79.1
1 A3421 150 40.78 -73.9
2 B 300 42.82 -67.3
3 HCO 450 22.22 -22.2
4 WREA 200 39.80 32.3
而另一个 table(包含由列几何定义的几何类型的多边形)存储在 database_2 中:
tier_name tier2_name geometry
0 Sample 1 Sample 1 [GEOMETRY - 2.7 KiB]
1 Sample 2 Sample 2 [GEOMETRY - 2.7 KiB]
2 Sample 3 Sample 3 [GEOMETRY - 2.7 KiB]
3 Sample 4 Sample 4 [GEOMETRY - 2.7 KiB]
4 Sample 5 Sample 5 [GEOMETRY - 2.7 KiB]
... ... ... ...
理想情况下,我希望我的结果包含适当的 tier_name 和 tier_name2 以及第一个 table:
的所有字段
Code price Latitude Longitude tier_name tier2_name
0 A001 1200 43.65 -79.1 Sample 5 Sample 5 <-- The point (43.65 and -79.1) is in the polygon sample 5
1 A3421 150 40.78 -73.9 Sample 5 Sample 5
2 B 300 42.82 -67.3 <-- This point is in none of the polygons of dataframe #2
3 HCO 450 22.22 -22.2 Sample 2 Sample 2
4 WREA 200 39.80 32.3 Sample 3 Sample 3
... ... ... ... ... ... ....
我的解决方案尝试(因为我使用的是 python)是根据是否 st_within 进行连接,如下所示:
cnx = mysql.connector.connect(user='root', password='', unix_socket='/data/mysql/mysql.sock', database='database_2')
test=pd.read_sql("""SELECT tier_name, tier2_name, st_astext(geometry),
FROM `table_2`
JOIN `database_1`.`table_1` as Table1
ON ST_WITHIN(Point([Table1.Latitude,
Table1.Longitude], 4326), `table_2`.geometry)
""", cnx)
但是,我不断收到的错误是:
1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[database_1].[Table_1] Table_1
ON ST_WITHIN(geometry::P' at line 5
对于 MariaDB,您应该使用反引号来引用标识符(不是必需的)。假设数据库都在同一个 MariaDB 实例上,这个查询应该可以工作
SELECT
`t1`.`Code`,
`t1`.`price`,
`t1`.`Latitude`,
`t1`.`Longitude`,
`t2`.`tier_name`,
`t2`.`tier2_name`
FROM `database_2`.`table_2` t2
JOIN `database_1`.`Table_1` t1
ON ST_WITHIN(Point(`t1`.`Latitude`, `t1`.`Longitude`), `t2`.`geometry`)
我正在尝试根据纬度和经度(存储在一个 table) 落在另一个 table 定义的多边形内。仅出于某些背景,我的第一个 table(在 database_1 中包含纬度和经度)看起来像:
Code price Latitude Longitude
0 A001 1200 43.65 -79.1
1 A3421 150 40.78 -73.9
2 B 300 42.82 -67.3
3 HCO 450 22.22 -22.2
4 WREA 200 39.80 32.3
而另一个 table(包含由列几何定义的几何类型的多边形)存储在 database_2 中:
tier_name tier2_name geometry
0 Sample 1 Sample 1 [GEOMETRY - 2.7 KiB]
1 Sample 2 Sample 2 [GEOMETRY - 2.7 KiB]
2 Sample 3 Sample 3 [GEOMETRY - 2.7 KiB]
3 Sample 4 Sample 4 [GEOMETRY - 2.7 KiB]
4 Sample 5 Sample 5 [GEOMETRY - 2.7 KiB]
... ... ... ...
理想情况下,我希望我的结果包含适当的 tier_name 和 tier_name2 以及第一个 table:
的所有字段 Code price Latitude Longitude tier_name tier2_name
0 A001 1200 43.65 -79.1 Sample 5 Sample 5 <-- The point (43.65 and -79.1) is in the polygon sample 5
1 A3421 150 40.78 -73.9 Sample 5 Sample 5
2 B 300 42.82 -67.3 <-- This point is in none of the polygons of dataframe #2
3 HCO 450 22.22 -22.2 Sample 2 Sample 2
4 WREA 200 39.80 32.3 Sample 3 Sample 3
... ... ... ... ... ... ....
我的解决方案尝试(因为我使用的是 python)是根据是否 st_within 进行连接,如下所示:
cnx = mysql.connector.connect(user='root', password='', unix_socket='/data/mysql/mysql.sock', database='database_2')
test=pd.read_sql("""SELECT tier_name, tier2_name, st_astext(geometry),
FROM `table_2`
JOIN `database_1`.`table_1` as Table1
ON ST_WITHIN(Point([Table1.Latitude,
Table1.Longitude], 4326), `table_2`.geometry)
""", cnx)
但是,我不断收到的错误是:
1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[database_1].[Table_1] Table_1
ON ST_WITHIN(geometry::P' at line 5
对于 MariaDB,您应该使用反引号来引用标识符(不是必需的)。假设数据库都在同一个 MariaDB 实例上,这个查询应该可以工作
SELECT
`t1`.`Code`,
`t1`.`price`,
`t1`.`Latitude`,
`t1`.`Longitude`,
`t2`.`tier_name`,
`t2`.`tier2_name`
FROM `database_2`.`table_2` t2
JOIN `database_1`.`Table_1` t1
ON ST_WITHIN(Point(`t1`.`Latitude`, `t1`.`Longitude`), `t2`.`geometry`)