Ich habe versucht zu überprüfen, ob die Punkte (d. H. In einer Datei aufgelistet) innerhalb der Polygone (d. H. In einer anderen Datei identifiziert) übereinstimmen. Der Code zeigt jedoch keine Übereinstimmungen, obwohl ich sicher bin, dass es solche gibt. Bitte sehen Sie sich den Code an und lassen Sie mich wissen, ob ich einen Fehler gemacht habe.Matplotlib - "contains_point" -Funktion nicht die Punkte innerhalb des Polygons
# 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])
kürzere Version mit einigen Daten in Code erstellen, so dass jeder könnte es laufen. – furas
Ich sehe keinen "contains_point" in Ihrem Code. – furas
BTW: Warum benutzen Sie 'exec()'? Kannst du kein Wörterbuch mit Variablen verwenden? dh. 'vars [anchorage_name] = []' anstelle von 'exec ("% s =% s "% (anchorage_name, []))' – furas