Hello,
I want to test whether they are within a polygon I have drawn. I have an CSV file with 5000 lines of latitude and longitude GPS coordinates. Now I want to set up a test to retun TRUE/FALSE as to whether the the GPS coordinate is inside the polygon.
My module point_in_poly(x,y,poly) works fine when I input the coordinates manually, but when I try to extract them from the CSV file I get an error message which after some Googling I cannot solve. I get the following error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
It reads:
Link to the CSV file: [Download CSV file]
# import
import pandas as pd
import csv
# Test a vertex for inclusion
df = pd.read_csv("ais_ray.csv")
# Limit the dataset
data = df.head(n=150)
latitude_list = data["Latitude"]
longitude_list = data["Longitude"]
# Improved point in polygon test which includes edge
# and vertex points
def point_in_poly(x,y,poly):
# check if point is a vertex
if (x,y) in poly: return "INSIDE THE POLYGON"
# check if point is on a boundary
for i in range(len(poly)):
p1 = None
p2 = None
if i==0:
p1 = poly[0]
p2 = poly[1]
else:
p1 = poly[i-1]
p2 = poly[i]
if p1[1] == p2[1] and p1[1] == y and x > min(p1[0], p2[0]) and x < max(p1[0], p2[0]):
return "INSIDE THE POLYGON"
n = len(poly)
inside = False
p1x,p1y = poly[0]
for i in range(n+1):
p2x,p2y = poly[i % n]
if y > min(p1y,p2y):
if y <= max(p1y,p2y):
if x <= max(p1x,p2x):
if p1y != p2y:
xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
if p1x == p2x or x <= xints:
inside = not inside
p1x,p1y = p2x,p2y
if inside: return "IN THE POLYGON"
else: return "OUTSIDE XXXXX"
# polygon
polygon = [(59.302521,10.594804), (53.658504,9.056718),
(52.308005,4.652078), (58.525959,3.593316)]
lat= latitude_list
lon= longitude_list
print(point_in_poly(lat, lon, polygon))
[1]: https://ufile.io/f14r9twi