Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raycasting GPS coordinates
#1
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. It reads:

Error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Link to the CSV file: Download CSV file

My code looks as follows:

# 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))
I would be happy to hear what you think I can do to solve this.

// Thanks
Reply


Messages In This Thread
Raycasting GPS coordinates - by drybulkfreight - Aug-17-2019, 07:39 AM
RE: Raycasting GPS coordinates - by drybulkfreight - Aug-17-2019, 06:28 PM
RE: Raycasting GPS coordinates - by scidam - Aug-17-2019, 10:04 PM
RE: Raycasting GPS coordinates - by drybulkfreight - Aug-18-2019, 08:12 AM
RE: Raycasting GPS coordinates - by drybulkfreight - Aug-18-2019, 09:33 AM
RE: Raycasting GPS coordinates - by ThomasL - Aug-18-2019, 09:38 AM
RE: Raycasting GPS coordinates - by drybulkfreight - Aug-18-2019, 10:27 AM
RE: Raycasting GPS coordinates - by ThomasL - Aug-18-2019, 11:00 AM
RE: Raycasting GPS coordinates - by drybulkfreight - Aug-18-2019, 11:00 AM
RE: Raycasting GPS coordinates - by ThomasL - Aug-18-2019, 12:16 PM
RE: Raycasting GPS coordinates - by drybulkfreight - Aug-18-2019, 02:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help on making raycasting faster robie972003 2 2,889 Mar-10-2019, 03:36 AM
Last Post: robie972003

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020