假設我有一個字典,其中每個元素是一個由GPS座標元組定義的四邊形,並且還有一個包含一系列行程的起點和終點的GPS座標的元組:(((origin_latitude,origin_longitude) ,(dest_latitude,dest_longitude)),((...),(...)))。下面是兩個四邊形和兩個行程的示例:for循環中的多處理
dictionary={0:((0,0),(0,1),(1,1),(1,0)),1:((3,3),(3,4),(4,4),(4,3))}
trips=(((0.5,0.5),(3.5,3.5)),((-1,-1),(-2,-2)))
欲每一行到原點四邊形號,目的四邊形號碼,始發地和目的地(跳閘參考號)之間的組合數目。這裏分類是我在做什麼:
import matplotlib.path as mplPath
def is_in_zone(quadri,point):
bbPath = mplPath.Path(quadri)
return bbPath.contains_point(point)
def get_zone_nbr(dictio,trip):
start_zone=-1
end_zone=-1
trip_ref=-1
for key,coordinates in dictio.iteritems():
if is_in_zone(coordinates,trip[0]):
start_zone=key
if is_in_zone(coordinates,trip[1]):
end_zone=key
if start_zone>-1 and end_zone>-1:
trip_ref=len(dictio)*start_zone+end_zone
break
return (start_zone,end_zone,trip_ref)
if __name__=='__main__':
dictionary={0:((0,0),(0,1),(1,1),(1,0)),1:((3,3),(3,4),(4,4),(4,3))}
trips=(((0.5,0.5),(3.5,3.5)),((-1,-1),(-2,-2)))
for t in trips:
get_zone_nbr(dictionary,t)
我的字典將大約是30,所以函數get_zone_nbr會很慢。我有數百萬次的旅程。你看到任何明顯的方法來優化get_zone_nbr()?或者任何能夠使代碼運行得更快的東西(例如多處理,但我不確定如何在循環中使用它)。
感謝您的幫助,邁克!我會試試看。 – gjy