Matplotlib - " contains_point"函数找不到多边形内的点

时间:2016-12-24 11:42:20

标签: 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_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])

1 个答案:

答案 0 :(得分:0)

我发现错误在我的代码中。我意识到使用evalexec之间存在差异。 eval返回代码中语句的结果,而exec仅产生代码的副作用。因此,行if exec(exec_intersect) == True:应改为if eval(exec_intersect) == True:。进行此更改可使代码完美运行。

有关evalexec之间差异的详细信息,请参阅以下链接:Difference between eval, exec and compile