给定 latitude/longitude 说明坐标是否在美国大陆内
Given latitude/longitude say if the coordinate is within continental US or not
我想检查某个特定的 latitude/longitude 是否在美国本土。我不想使用在线 API,我正在使用 Python.
我下载了this shapefile
from shapely.geometry import MultiPoint, Point, Polygon
import shapefile
sf = shapefile.Reader("cb_2015_us_nation_20m")
shapes = sf.shapes()
fields = sf.fields
records = sf.records()
points = shapes[0].points
poly = Polygon(points)
lon = -112
lat = 48
point = Point(-112, 48)
poly.contains(point)
#should return True because it is in continental US but returns False
样本经度、纬度在美国边界内,但 poly.contains returns 错误。
我不确定问题是什么以及如何解决问题,以便我可以测试某个点是否在美国大陆内。
我最终检查了 lat/lon 是否在每个州,而不是在大陆 U.S 中检查,如果一个点在其中一个州,那么它在大陆 U.S..
from shapely.geometry import MultiPoint, Point, Polygon
import shapefile
#return a polygon for each state in a dictionary
def get_us_border_polygon():
sf = shapefile.Reader("./data/states/cb_2015_us_state_20m")
shapes = sf.shapes()
#shapes[i].points
fields = sf.fields
records = sf.records()
state_polygons = {}
for i, record in enumerate(records):
state = record[5]
points = shapes[i].points
poly = Polygon(points)
state_polygons[state] = poly
return state_polygons
#us border
state_polygons = get_us_border_polygon()
#check if in one of the states then True, else False
def in_us(lat, lon):
p = Point(lon, lat)
for state, poly in state_polygons.iteritems():
if poly.contains(p):
return state
return None
我 运行 你的代码并绘制了多边形。它看起来像这样:
如果您 运行 使用此代码:
import geopandas as gpd
import matplotlib.pyplot as plt
shapefile = gpd.read_file("path/to/shapes.shp")
shapefile.plot()
plt.show()
# credit to
你会看到这个:
所以,1,你没有在看美国大陆,2,你的情节很无聊。你的代码虽然有效,但将 return 与 geopandas 情节一致。
我想检查某个特定的 latitude/longitude 是否在美国本土。我不想使用在线 API,我正在使用 Python.
我下载了this shapefile
from shapely.geometry import MultiPoint, Point, Polygon
import shapefile
sf = shapefile.Reader("cb_2015_us_nation_20m")
shapes = sf.shapes()
fields = sf.fields
records = sf.records()
points = shapes[0].points
poly = Polygon(points)
lon = -112
lat = 48
point = Point(-112, 48)
poly.contains(point)
#should return True because it is in continental US but returns False
样本经度、纬度在美国边界内,但 poly.contains returns 错误。 我不确定问题是什么以及如何解决问题,以便我可以测试某个点是否在美国大陆内。
我最终检查了 lat/lon 是否在每个州,而不是在大陆 U.S 中检查,如果一个点在其中一个州,那么它在大陆 U.S..
from shapely.geometry import MultiPoint, Point, Polygon
import shapefile
#return a polygon for each state in a dictionary
def get_us_border_polygon():
sf = shapefile.Reader("./data/states/cb_2015_us_state_20m")
shapes = sf.shapes()
#shapes[i].points
fields = sf.fields
records = sf.records()
state_polygons = {}
for i, record in enumerate(records):
state = record[5]
points = shapes[i].points
poly = Polygon(points)
state_polygons[state] = poly
return state_polygons
#us border
state_polygons = get_us_border_polygon()
#check if in one of the states then True, else False
def in_us(lat, lon):
p = Point(lon, lat)
for state, poly in state_polygons.iteritems():
if poly.contains(p):
return state
return None
我 运行 你的代码并绘制了多边形。它看起来像这样:
如果您 运行 使用此代码:
import geopandas as gpd
import matplotlib.pyplot as plt
shapefile = gpd.read_file("path/to/shapes.shp")
shapefile.plot()
plt.show()
# credit to
你会看到这个:
所以,1,你没有在看美国大陆,2,你的情节很无聊。你的代码虽然有效,但将 return 与 geopandas 情节一致。