Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if lat/lon coordinates fall inside a US State

Tags:

python

geopy

Using Python, is there a function, library, or other way to check to see if a certain lat/lon coordinate pair fall within a defined geographical boundary such as a US state?

Example:

Does 32.781065, -96.797117 fall within Texas?

like image 575
Matt Keller Avatar asked Jul 29 '14 04:07

Matt Keller


People also ask

How do I know if my latitude and longitude are valid?

The latitude and longitude, if present will always appear in the form of (X, Y) where X and Y are decimal numbers. For a valid (latitude, longitude) pair: -90<=X<=+90 and -180<=Y<=180. They will not contain any symbols for degrees or radians or N/S/E/W.

Can Google Maps find latitude longitude?

To search for a place, enter the latitude and longitude GPS coordinates on Google Maps. You can also find the coordinates of the places you previously found. Besides longitude and latitude, you can use plus codes to share a place without an address.

How do you state your longitude?

When writing latitude and longitude, write latitude first, followed by a comma, and then longitude. For example, the above lines of latitude and longitude would be written as "15°N, 30°E."


2 Answers

You could use the reverse geocode library, and then check that the "admin1" area is a specific US state.

It converts lat/lon coordinates into dictionaries like:

{
  'name': 'Mountain View', 
  'cc': 'US', 
  'lat': '37.38605',
  'lon': '-122.08385', 
  'admin1': 'California', 
  'admin2': 'Santa Clara County'
}
like image 161
mathisonian Avatar answered Oct 25 '22 01:10

mathisonian


You can use the geocoder library which you can install using pip install geocoder.

import geocoder

results = geocoder.google('32.781065, -96.797117', reverse = True)

print(results.current_result.state_long)

enter image description here

like image 43
Michael James Kali Galarnyk Avatar answered Oct 25 '22 02:10

Michael James Kali Galarnyk