Python Matplotlib-“文件”;包含“U点”;函数不查找多边形内的点

Python Matplotlib-“文件”;包含“U点”;函数不查找多边形内的点,python,matplotlib,Python,Matplotlib,我一直在尝试检查点(即在一个文件中列出的点)是否与多边形(即在另一个文件中标识的点)重合。然而,代码只是没有显示任何匹配项,即使我确信有匹配项。请看一下代码,让我知道我是否犯了错误 # BoxPlot.py # Determine whether the ship is located in a particular anchorage import csv import matplotlib.path as mplPath import numpy as np source_anchora

我一直在尝试检查点(即在一个文件中列出的点)是否与多边形(即在另一个文件中标识的点)重合。然而,代码只是没有显示任何匹配项,即使我确信有匹配项。请看一下代码,让我知道我是否犯了错误

# BoxPlot.py
# Determine whether the ship is located in a particular anchorage

import csv
import matplotlib.path as mplPath
import numpy as np

source_anchorage = "C:\\Users\\Tugboats\\" + \
                   "Tugboats - Terminal Coordinates V003.csv"
source_ship_locations = "C:\\Users\\Tugboats\\" + \
                        "TuggingActivity.csv"
target_file = "C:\\Users\\Tugboats\\" + \
              "OUT - TuggingActivity.csv"
location_processed = []

with open(source_anchorage, 'r') as f:
    inputReader = csv.DictReader(f, delimiter=",")

    # Loading all the data points into the anchorage names as a tuple
    for row in inputReader:
        if row["Acronym"] not in location_processed:
            location_processed.append(row["Acronym"])
            anchorage_name = row["Acronym"].__str__()
            exec("%s = %s" % (anchorage_name, []))

        # Build the polygon with the anchorage_name
        exec("%s.append(%s)" % (anchorage_name, (float(row["Longitude"]), float(row["Latitude"]))))

# Convert all anchorage names into numpy arrays
for location in location_processed:
    exec_create_polygon = "%s = mplPath.Path(np.array(%s))" % (location, location)
    # print(exec_create_polygon)
    exec(exec_create_polygon)

# Code to mark up all the location codes within the CSV file
with open(source_ship_locations, 'r') as f:
    inputReader = csv.DictReader(f, delimiter=",")

    output_file = open(target_file, 'w+', newline="")
    output_writer = csv.writer(output_file, delimiter=",")

    for row in inputReader:
        for location in location_processed:
            exec_intersect = "%s.contains_point([%.9f, %.9f])" % \
                             (location, float(row["LastEntryLong"]), float(row["LastEntryLat"]))
            # print(exec_intersect)
            if exec(exec_intersect) == True:
                print("Match!")
                output_writer.writerow([row["Job_ID"],
                                        row["EarliestTimestamp"],
                                        row["LatestTimestamp"],
                                        row["NumberOfRecords"],
                                        row["License"],
                                        row["ExtShipMMSI"],
                                        row["1stEntryLat"],
                                        row["1stEntryLong"],
                                        row["LastEntryLat"],
                                        row["LastEntryLong"],
                                        row["FirstEntrySOG"],
                                        row["LastEntrySOG"],
                                        location.__str__()])
                break
        output_writer.writerow([row["Job_ID"],
                                row["EarliestTimestamp"],
                                row["LatestTimestamp"],
                                row["NumberOfRecords"],
                                row["License"],
                                row["ExtShipMMSI"],
                                row["1stEntryLat"],
                                row["1stEntryLong"],
                                row["LastEntryLat"],
                                row["LastEntryLong"],
                                row["FirstEntrySOG"],
                                row["LastEntrySOG"],
                                None])

我在代码中找到了错误所在。我意识到使用
eval
exec
是有区别的
eval
返回代码中语句的结果,而
exec
只产生代码的副作用。因此,如果exec(exec\u intersect)==True:行应改为
if eval(exec\u intersect)==True:
。进行此更改使代码能够完美地运行


有关
eval
exec
之间差异的更多信息,请参阅以下链接:

创建代码中包含一些数据的较短版本,以便每个人都可以运行它。我在代码中没有看到任何“包含点”。顺便问一下:为什么要使用
exec()
?你不能用变量字典吗?例如,
vars[anchorage\u name]=[]
而不是
exec(“%s=%s”%(anchorage\u name,[]))
多边形太多,我无法手动创建多边形。这就是为什么我选择从一个包含多边形顶点的“CSV”文件中创建变量的原因。我要求创建一个简单的多边形示例,仅用于测试,以便我们可以运行它。现在没有人可以测试你的代码。