Stats

Popular Posts

Followers

Mathemical搭配Google Map求解旅遊景點最短路徑問題

戴忠淵 於 2010年3月27日星期六 下午10:01 發表

city={{{Kaohsiung}, {Kaohsiung, Fongshan, ‎Daliao, Meinong, Cishan, Gangshan, Neimen, Yanchao, Renwu, Linyuan, Taoyuan, Namasia, Hunei, Tianliao, Jiasian, Liouguei, Maolin, Shanlin, Niaosong, Dashe,
Dashu, Zihguan, Ciaotou, Alian, Lujhu, Yongan, Jiading, Mituo}}, {{Pingtung}, {Hengchun, Pingtung‎, Wandan, Gaoshu, Fangliao, Chaojhou, Shihzih, Mudan, Yanpu, Manjhou, Checheng, Fangshan, Chunrih, Jiadong, Nanjhou, Linbian, Donggang, Liouciou, Changjhih, Neipu, Majia, Sandimen, Wutai, Laiyi, Taiwu, Wanluan, Linluo, Sinpi, Kanding, Sinyuan, Jhutian, Jiouru, Ligang}}};

首先將上述鄉鎮轉換成Google Map可讀取格式
mycity=ToString@#[[1]]<>"+"<>ToString@#[[2]]&/@Tuples[#]&/@city;

求出各縣市的GPS座標
pos=Table[ToExpression@StringCases[Import["http://maps.google.com/maps/geo?q="<>#<>"&output=csv"],NumberString][[{-1,-2}]]&/@mycity[[i]],{i,2}];

將高高屏各鄉鎮畫出
ListPlot[Table[Tooltip[pos[[#,i]],city[[#,2,i]]],{i,Length@pos[[#]]}]&/@Range[2],PlotStyle->PointSize[0.025],AspectRatio->1]


將高高屏所有鄉鎮中抽取15的鄉鎮市作為旅遊點
samplecity = RandomSample[Flatten@mycity, 15]
{"Pingtung+Fangliao", "Pingtung+Linbian", "Kaohsiung+Renwu", "Pingtung+Kanding", "Pingtung+Wanluan", "Kaohsiung+Taoyuan", "Kaohsiung+Kaohsiung", "Kaohsiung+Dashu", "Kaohsiung+Linyuan", "Kaohsiung+Cishan", "Pingtung+Liouciou", "Pingtung+Gaoshu", "Kaohsiung+Fongshan", "Pingtung+Ligang", "Pingtung+Majia"}

計算15個旅遊點的GPS位置
gps=ToExpression@StringCases[Import["http://maps.google.com/maps/geo?q="<>#<>"&output=csv"],NumberString][[{-1,-2}]]&/@samplecity;

求解旅遊的最短路徑
mypath = FindShortestTour[gps][[2]]

輸出鄉鎮順序
samplecity[[#]] & /@ mypath

{"Pingtung+Fangliao", "Pingtung+Linbian", "Pingtung+Kanding", "Pingtung+Wanluan", "Pingtung+Majia", "Pingtung+Gaoshu", "Kaohsiung+Taoyuan", "Kaohsiung+Cishan", "Pingtung+Ligang", "Kaohsiung+Dashu", "Kaohsiung+Renwu", "Kaohsiung+Fongshan", "Kaohsiung+Kaohsiung", "Kaohsiung+Linyuan", "Pingtung+Liouciou"}

最後以ListPlot畫出位置圖
ListPlot[Table[Tooltip[pos[[#,i]],city[[#,2,i]]],{i,Length@pos[[#]]}]&/@Range[2],PlotStyle->PointSize[0.025],AspectRatio->1,Epilog->{Yellow,PointSize[0.05],Point[gps],Red,Thickness[0.01],Line[gps[[mypath[[{#,#+1}]]]]&/@Range[Length@mypath-1]]}]



Tags: ,

讀者回應 ( 0 意見 )

發佈留言

Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.

如果這篇文章對你有幫助,那請留個訊息給我~