2017-02-28 8 views
1

특정 위도/경도가 미국 대륙 내에 있는지 확인하고 싶습니다. 온라인 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는 False를 반환 위도 샘플 경도를 다운로드. 문제가 무엇인지, 어떻게 문제를 해결하여 포인트가 미국 대륙 내에 있는지 테스트 할 수 있는지 잘 모르겠습니다. . 점은 상태 중 하나 인 경우 위도/경도 대신 미국 대륙에서 검사의 모든 상태에 있다면

+2

대륙 미국을 http : // gis.stackexchange.com/questions/84114/shapely-unable-to-tell-if-polygon-contains-point 모양이'lat, lon' 대신'lon, lat'입니까? – TemporalWolf

+1

예, 그것은 lon, lat입니다. 나는 반대로 확인해도 작동하지 않습니다. 나는 상태 쉐이프 파일을 사용하여 끝내었고 지금은 같은 방법으로 모든 상태를 검사하고 있는데, 그 중 하나가 true를 반환하면 사실입니다.하지만 지금은 작동하지 않는 것 같습니다. – Ash

답변

0

내가 체크 결국, 그것은

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