How do I determine if a lat/long point is within a polygon?

2024/11/16 0:57:05

I have a shapefile of all the counties that make up my state. Using the shapefile (which contains geometric for the district polygons) I was able to use geopandas to plot the shapes in a figure. I have some addresses that I have geocoded into latitude and longitude coordinates and I'd like to be able to determine which county (or polygon) the coordinates are within. I see that geopandas has a within function, but I don't quite understand how to use it.

The end goal will be for a user to input and address and the program returns the county name. There are only a few dozen counties so I was thinking of using a for loop to iterate through the rows and check each polygon to see if the provided coordinate lies within.

Answer

I believe shapely's documentations will be helpful to understand how within function works. But, i order to give you more insight, try code below:

from shapely.geometry import Point, Polygon
pol = Polygon([[0,0], [0,2], [2,2], [2,0]])
pnt = Point(1,1)
pnt.within(pol)

result:

True

Explanation

pol is a rectangle(square) with 2 units on each side. We used created a new point with coordinates of (1,1) which locates exactly in the center of the square. Using within function on the Point object and giving the Polygon as an argument to this function you can check if the point is in the area or not.

https://en.xdnf.cn/q/119593.html

Related Q&A

How to return different types of arrays?

The high level problem Im having in C# is to make a single copy of a data structure that describes a robot control network packet (Ethercat), and then to use that single data structure to extract data …

How do I pass an array of strings to a python script as an argument?

Ive written a swift app that outputs an array of strings. I would like to import this array into a python script for further processing into an excel file via xlsxwriter, I would like to do this as an …

Match values of different dataframes

This dataframe is the principal with the original tweets. "original_ds_.csv" id tweet --------------------------------------------- 78 "onetoone"…

EOF while parsing

def main():NUMBER_OF_DAYS = 10NUMBER_OF_HOURS = 24data = []for i in range(NUMBER_OF_DAYS):data.append([])for j in range(NUMBER_OF_HOURS):data[i].append([])data[i][j].append(0)data[i][j].append(0)for k …

Why is bool(x) where x is any integer equal to True

I expected bool(1) to equate to True using Python - it does - then I expected other integers to error when converted to bool but that doesnt seem to be the case:>>> x=23 #<-- replace with a…

Getting TypeError while fetching value from table using Python and Django

I am getting error while fetching value from table using Python and Django. The error is below:Exception Type: TypeError Exception Value: not all arguments converted during string formattingMy code…

ValueError: The view **** didnt return an HttpResponse object. It returned None instead

Im using Django forms to handle user input for some point on my Django app. but it keeps showing this error whenever the user tries to submit the form. ValueError: The view *my view name goes here* di…

Game Development in Python, ruby or LUA? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Problem with this error: (-215:Assertion failed) !ssize.empty() in function cv::resize OpenCV

I got stuck with this error after running resize function line: import cv2 import numpy as np import matplotlib.pyplot as pltnet = cv2.dnn.readNetFromDarknet(yolov3_custom.cfg, yolov3_custom_last.weigh…

When I run it tells me this : NameError: name lock is not defined?

• Assume that you have an array (data=[]) containing 500,000 elements and that each element has been assigned a random value between 1 and 10 (random.randint(1,10)) .for i in range (500000):data[i]…