我想检查特定的纬度/经度是否在美国大陆范围内。我不想使用在线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
样本lon,lat在美国范围内,但poly.contains返回False。
我不确定问题是什么以及如何解决该问题,以便我可以测试某个点是否在美国大陆范围内。
最佳答案
我最终检查了纬度/经度是否在每个州中,而不是在美国大陆上进行了检查,如果某个点在美国某州中,那么它在美国本土中。
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
关于python - 给定纬度/经度,请说明坐标是否在美国大陆范围内,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42501349/