2017-03-09 13 views
1

은 지금 나는 GeoJson 파일을 다음과 같은 기능은 매끈한 사용 :어떻게 파이썬에서 많은 수의 지오 코드를 역전시킬 수 있습니까?

그것은 좌표에 소요 반환이 작동 동네 이름

def get_neighb(lat, lon): 
    """Input Latitude and Longitude, Returns Neighborhood Name""" 
    point = Point(lon, lat) 
    found = False 
    for feature in geo_data['features']: 
     polygon = shape(feature['geometry']) 
     if polygon.contains(point): 
      return(feature['properties']['neighborhood']) 
      found = True 
    if found is False: 
     return('NA') 

# Initialize list 
tn = ['']*data.shape[0] 
for i in range(len(tn)): 
    tn[i] = get_neighb(data.latitude[i], data.longitude[i]) 

,하지만 정말 느리고, 어떤 생각을하는 방법에 속도를 높여 현재 4,000,000 행으로 실행하고 있습니다.

+0

단지 작은 니트릭이지만 실제로는 발견 된 변수가 필요하지 않습니다. –

답변

1

예를 들어 PostGIS 데이터베이스의 중장비 기계를 피하려면 rtree 패키지를 (문서의 내용대로) "저렴한 공간 데이터베이스"로 사용하는 것이 좋습니다. 그 아이디어는 대부분 다음과 같습니다 :

#!/usr/bin/env python 
from itertools import product 
from random import uniform, sample, seed 
from rtree import index 
from shapely.geometry import Point, Polygon, box, shape 
from shapely.affinity import translate 

seed(666) 

#generate random polygons, in your case, the polygons are stored 
#in geo_data['features'] 
P = Polygon([(0, 0), (0.5, 0), (0.5, 0.5), (0, 0.5), (0, 0)]) 
polygons = [] 
for dx, dy in product(range(0, 100), range(0, 100)): 
    polygons.append(translate(P, dx, dy)) 

#construct the spatial index and insert bounding boxes of all polygons 
idx = index.Index() 
for pid, P in enumerate(polygons): 
    idx.insert(pid, P.bounds) 

delta = 0.5 
for i in range(0, 1000): 
    #generate random points 
    x, y = uniform(0, 10), uniform(0, 10) 
    pnt = Point(x, y) 

    #create a region around the point of interest 
    bounds = (x-delta, y-delta, x+delta, y+delta) 

    #also possible, but much slower 
    #bounds = pnt.buffer(delta).bounds 

    #the index tells us which polygons are worth checking, i.e., 
    #the bounding box of which intersects with the region constructed in previous step 
    for candidate in idx.intersection(bounds): 
     P = polygons[candidate] 

     #test only these candidates 
     if P.contains(pnt): 
      print(pnt, P) 
2

모든 행을 확인하지 않는 전략을 찾아야합니다. 가장 간단한 방법은 모든 모양을 지오 인식 데이터베이스에 덤프하고 쿼리하는 것입니다. 포스트 기스 (post-gis) 나 신축성있는 검색 같은 것.

또 다른 전략은 모든 이웃의 중심을 찾은 다음 KD 트리를 사용하여 근처의 중심을 가진 이웃 만 필터링 할 수 있습니다.

+0

SpatiaLite도 있습니다. PostGIS보다 조금 더 실행하기 쉬운 SQLite의 공간 확장. – Alexander