-1

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])
Cloud
  • 399
  • 4
  • 13
  • create shorter version with some data in code so everyone could run it. – furas Dec 24 '16 at 11:49
  • I don't see any “contains_point” in your code. – furas Dec 24 '16 at 11:50
  • BTW: why do you use `exec()` ? can't you use dictionary with variables ? ie. `vars[anchorage_name] = []` instead of `exec("%s = %s" % (anchorage_name, []))` – furas Dec 24 '16 at 11:55
  • There are too many polygons for me to create the polygons manually. That is why I chose to create the variables from a "CSV" file, containing the vertices of the polygon, on-the-fly. – Cloud Dec 24 '16 at 11:58
  • I asked to create simple example polygon only for test - so we could run it. Now nobody can test you code. – furas Dec 24 '16 at 12:04

1 Answers1

0

I found where the mistake is within my code. I realize that there is a difference between using eval and exec. eval returns the result of the statement in the code while exec only produces the side effects of the code. Hence, the line if exec(exec_intersect) == True: should be changed to if eval(exec_intersect) == True: instead. Making this change allowed the code to operate flawlessly.

For more information on the difference between eval and exec, refer to the following link: Difference between eval, exec and compile

Community
  • 1
  • 1
Cloud
  • 399
  • 4
  • 13