我对Python还比较新。我在Postgres中有以下数据表。这些是带有四个坐标的多边形值,具有相同的Id
和ZONE
名称。我将这些数据存储在名为df1
的Python数据框中。
Id Order Lat Lon Zone
00001 1 50.6373473 3.075029928 A
00001 2 50.63740441 3.075068636 A
00001 3 50.63744285 3.074951754 A
00001 4 50.63737839 3.074913884 A
00002 1 50.6376054 3.0750528 B
00002 2 50.6375896 3.0751209 B
00002 3 50.6374239 3.0750246 B
00002 4 50.6374404 3.0749554 B
我有一些Json数据,其中包含Lon
和Lat
值,我已将它们存储在名为df2
的Python数据框中。
Lat Lon
50.6375524099 3.07507914474
50.6375714407 3.07508201591
我的任务是将
df2
的Lat
和Lon
值与df1
中每个区域的四个坐标进行比较,以提取区域名称并将其添加到df2
中。例如,
(50.637552409 3.07507914474)
属于B区
。#This is ID with Zone
df1 = pd.read_sql_query("""SELECT * from "zmap" """,con=engine)
#This is with lat,lon values
df2 = pd.read_sql_query("""SELECT * from "E1" """,con=engine)
df2['latlon'] = zip(df2.lat, df2.lon)
zones = [
["A", [[50.637347297, 3.075029928], [50.637404408, 3.075068636], [50.637442847, 3.074951754],[50.637378390, 3.074913884]]]]
for i in range(0, len(zones)): # for each zone points
X = mplPath.Path(np.array(zones[i][1]))
# find if points are Zones
Y= X.contains_points(df2.latlon.values.tolist())
# Label points that are in the current zone
df2[Y, 'zone'] = zones[i][0]
目前我已手动为区域'A'完成了此操作。我需要为df2中的坐标生成“区域”。
df2.merge(df1)
就可以了。 - Cainã Max Couto-Silva