Forums

Raycasting GPS coordinates

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

You're trying to check the truthiness of something where truthiness is not properly defined. Check the value that you're using at that line in the case where it works and the case where it doesn't, so you can see why the behaviour is different.

Pandas follows the numpy convention of raising an error when you try to convert something to a bool. This happens in a if or when using the boolean operations, and, or, or not. It is not clear what the result of.

example

5 == pd.Series([12,2,5,10])

The result you get is a Series of booleans, equal in size to the pd.Series in the right hand side of the expression. So, you get an error. The problem here is that you are comparing a pd.Series with a value, so you'll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False. You need to further aggregate the result so that a single boolean value results from the operation. For that you'll have to use either any or all depending on whether you want at least one (any) or all values to satisfy the condition.

(5 == pd.Series([12,2,5,10])).all()
# False

or

(5 == pd.Series([12,2,5,10])).any()
# True

Thanks for clarification, @haviernick.