I have been trying to check whether the points (i.e. listed within a file) coincide within the polygons (i.e. identified within another file). However, the code is just not showing any matches, even though I am sure that there are. Please take a look at the code and let me know whether I made an error.
# 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])