Forums

Shapely for testing coordinates within polygons

Hello,

I am playing around with Shapely. However, I am having trouble making Shapely accept a list of GPS coordinates as the logical test.

I get the following error message:

TypeError: must be real number, not list

CSV File: LINK

from shapely.geometry import Point, Polygon
import pandas as pd
import csv

# Read the csv file
df = pd.read_csv("ais3.csv", encoding = "ISO-8859-1", decimal='.')


# Create dataframe with only relevant data
df1 = df[['Latitude','Longitude']].copy()


# convert data to numbers
df1[['Latitude', 'Longitude']].astype(float).values


# Store our latitude and longitude as list
latitude_list = df1["Latitude"].tolist()
longitude_list = df1["Longitude"].tolist()


# Create Point objects
p1 = Point(latitude_list, longitude_list)


# Create a Polygon
coords = [(59.29569,10.605997), (53.687444,9.060655),(53.90742,16.172993), (56.928762,16.923587)]
poly = Polygon(coords)


print(p1.within(poly))

From the error, I'm guessing that Shapely doesn't accept a list of GPS coordinates to whichever function that you're passing the list to. Check the documentation of Shapely to see if there's an equivalent function that accepts a list.

Hello Glenn,

I tried adjust the code adding the following:

    # Create Point objects
p1 = Point.coords(latitude_list, longitude_list)

However, now I get this error message:

  File "C:/Users/Peter/Desktop/Python Programming/Test Project/ais/Shapely.py", line 20, in <module>
    p1 = Point.coords(latitude_list, longitude_list)

TypeError: 'property' object is not callable

Point.coords is a property that gives back the coordinates of a point, it's not a function that you call with coordinates. The Point constructor only creates a single point, so you can't use it with lists of coordinates. You probably need to loop through your coordinates and create a Point for each pair.