開放街圖OSM的公共圖資可以透過overpass-turbo介面查詢獲得。
若想利用Java程式碼查詢,可參考以下查詢範例。
其中,查詢指令可參考overpass-turbo範例指令,測試成功再代入程式。
/*
QueryOSM.java
展示如何用retrofit套件,同步及非同步(適用於Android),存取如下OSM圖資服務
http://overpass-api.de/api/interpreter?data=xxx
// 建立服務連線客戶端及請求內容
OverpassService requestClient = OverpassServiceProvider.get();
String request = composeRequest();
// 非同步查詢圖資,適用於手機平板Android平台
asyncRequest(requestClient, request);
// 同步查詢圖資,適用於桌機Application應用
OverpassQueryResult result = syncRequest(requestClient, request);
postProcess(result);
執行步驟:
javac QueryOSM.java
java QueryOSM
run:
[out:json][timeout:25];
(
node[tourism](25.1735, 121.446, 25.1775, 121.455);
relation[!highway][type!='route'][!boundary](25.1735, 121.446, 25.1775, 121.455);
);
out center;
end of asyncRequest()
21 elements...
1. id:4326372453, type:node, lat:25.1761808, lon:121.4490491, name:五虎碑, wheelchair:limited
2. id:4384988658, type:node, lat:25.1750071, lon:121.4522078, name:文錙藝術中心, wheelchair:yes
3. id:4492221396, type:node, lat:25.1736267, lon:121.4473809, name:三化牆
4. id:4492221397, type:node, lat:25.1744069, lon:121.4474037, name:地球村雕像, wheelchair:limited
5. id:4492221399, type:node, lat:25.1735175, lon:121.4484658, name:淡江大學花牆
6. id:4492221425, type:node, lat:25.1749949, lon:121.450678, name:閱讀的少女, wheelchair:yes
7. id:4502075211, type:node, lat:25.1751384, lon:121.4523232, name:旅者
8. id:4502075212, type:node, lat:25.1739938, lon:121.4505047, name:李雙澤紀念碑, wheelchair:yes
9. id:4502075213, type:node, lat:25.1761919, lon:121.4499374, name:福園金鷹銅雕, wheelchair:no
10. id:4502075222, type:node, lat:25.1738907, lon:121.4475716, name:驚聲銅像, wheelchair:limited
11. id:4507662408, type:node, lat:25.1741082, lon:121.4474671, name:溫馨, wheelchair:yes
12. id:5012978611, type:node, lat:25.1741784, lon:121.4507282, name:黃河母親, wheelchair:no
13. id:5072580167, type:node, lat:25.1769149, lon:121.4495309
14. id:5130535622, type:node, lat:25.1757202, lon:121.4496844, name:會文館, wheelchair:yes
15. id:5132288341, type:node, lat:25.1741586, lon:121.4508061
16. id:5132288342, type:node, lat:25.174208, lon:121.4475417
17. id:6050843218, type:node, lat:25.1770813, lon:121.449821
18. id:8991981256, type:node, lat:25.1750049, lon:121.4480033, name:淡江願景牆, wheelchair:yes
19. id:3974590, type:relation, lat:0.0, lon:0.0, type:multipolygon, name:操場
20. id:3983402, type:relation, lat:0.0, lon:0.0, type:multipolygon, name:松濤廣場
21. id:7530081, type:relation, lat:0.0, lon:0.0, type:multipolygon
Response{protocol=http/1.1, code=200, message=OK, url=http://overpass-api.de/api/interpreter?data=%5Bout%3Ajson%5D%5Btimeout%3A25%5D%3B%0A%28%0Anode%5Btourism%5D%2825.1735%2C%20121.446%2C%2025.1775%2C%20121.455%29%3B%0Arelation%5B%21highway%5D%5Btype%21%3D%27route%27%5D%5B%21boundary%5D%2825.1735%2C%20121.446%2C%2025.1775%2C%20121.455%29%3B%0A%29%3B%0Aout%20center%3B}
end of syncRequest()
21 elements...
1. id:4326372453, type:node, lat:25.1761808, lon:121.4490491, name:五虎碑, wheelchair:limited
2. id:4384988658, type:node, lat:25.1750071, lon:121.4522078, name:文錙藝術中心, wheelchair:yes
3. id:4492221396, type:node, lat:25.1736267, lon:121.4473809, name:三化牆
4. id:4492221397, type:node, lat:25.1744069, lon:121.4474037, name:地球村雕像, wheelchair:limited
5. id:4492221399, type:node, lat:25.1735175, lon:121.4484658, name:淡江大學花牆
6. id:4492221425, type:node, lat:25.1749949, lon:121.450678, name:閱讀的少女, wheelchair:yes
7. id:4502075211, type:node, lat:25.1751384, lon:121.4523232, name:旅者
8. id:4502075212, type:node, lat:25.1739938, lon:121.4505047, name:李雙澤紀念碑, wheelchair:yes
9. id:4502075213, type:node, lat:25.1761919, lon:121.4499374, name:福園金鷹銅雕, wheelchair:no
10. id:4502075222, type:node, lat:25.1738907, lon:121.4475716, name:驚聲銅像, wheelchair:limited
11. id:4507662408, type:node, lat:25.1741082, lon:121.4474671, name:溫馨, wheelchair:yes
12. id:5012978611, type:node, lat:25.1741784, lon:121.4507282, name:黃河母親, wheelchair:no
13. id:5072580167, type:node, lat:25.1769149, lon:121.4495309
14. id:5130535622, type:node, lat:25.1757202, lon:121.4496844, name:會文館, wheelchair:yes
15. id:5132288341, type:node, lat:25.1741586, lon:121.4508061
16. id:5132288342, type:node, lat:25.174208, lon:121.4475417
17. id:6050843218, type:node, lat:25.1770813, lon:121.449821
18. id:8991981256, type:node, lat:25.1750049, lon:121.4480033, name:淡江願景牆, wheelchair:yes
19. id:3974590, type:relation, lat:0.0, lon:0.0, type:multipolygon, name:操場
20. id:3983402, type:relation, lat:0.0, lon:0.0, type:multipolygon, name:松濤廣場
21. id:7530081, type:relation, lat:0.0, lon:0.0, type:multipolygon
end of main()
BUILD SUCCESSFUL (total time: 1 minute 2 seconds)
參考:
a. Retrofit 2 – Synchronous and asynchronous call example
https://howtodoinjava.com/retrofit2/retrofit-sync-async-calls/
b. https://github.com/zsoltk/overpasser
hu.supercluster.overpasser.adapter
OverpassQueryResult
OverpassQueryResult.Element
OverpassQueryResult.Element.Tags
OverpassService
c. 引用函數庫
compile/run:
okhttp-3.14.9.jar
okio-1.17.2.jar
converter-gson-2.10.2.jar
gson-2.8.5.jar
retrofit-2.10.2.jar
compile/run tests:
byte-buddy-1.11.3.jar
byte-buddy-agent-1.11.3.jar
mockito-core-3.11.2.jar
objenesis-3.2.jar
*/
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import hu.supercluster.overpasser.library.output.OutputFormat;
import hu.supercluster.overpasser.library.output.OutputModificator;
import hu.supercluster.overpasser.library.output.OutputOrder;
import hu.supercluster.overpasser.library.output.OutputVerbosity;
import hu.supercluster.overpasser.library.query.OverpassQuery;
import hu.supercluster.overpasser.adapter.OverpassQueryResult;
import hu.supercluster.overpasser.adapter.OverpassQueryResult.Element;
import hu.supercluster.overpasser.adapter.OverpassQueryResult.Element.Tags;
import hu.supercluster.overpasser.adapter.OverpassService;
import hu.supercluster.overpasser.adapter.OverpassServiceProvider;
import java.lang.reflect.Field;
import java.util.List;
/**
*
* @author seke
*/
public class QueryOSM {
// 可利用下址測試查詢指令
// http://overpass-turbo.eu/
public static String composeRequest()
{
/* 查詢範例1:
在(47.48047027491862,19.039797484874725,47.51331674014172,19.07404761761427)範圍內
列出所有非私人停車場
A. 查詢指令
["out":"json"]["timeout":"30"];
(
node
["amenity"="parking"]
["access"!="private"]
(47.48047027491862,19.039797484874725,47.51331674014172,19.07404761761427);
<;
);
out body center qt 100;
B. 組合查詢指令方法
String query = new OverpassQuery()
.format(OutputFormat.JSON)
.timeout(30)
.filterQuery()
.node()
.amenity("parking")
.tagNot("access", "private")
.boundingBox(
47.48047027491862, 19.039797484874725,
47.51331674014172, 19.07404761761427
)
.end()
.output(OutputVerbosity.BODY, OutputModificator.CENTER, OutputOrder.QT, 100)
.build()
;
*/
/* 查詢範例2:
在(25.1735, 121.446, 25.1775, 121.455)範圍內,撈取
非座椅設施,商店,觀光點,辦公室,繄急設施之節點
屬於大學設施之線條,關係
不屬於公路、路線、森林、邊界之線條,關係
列出其中心位置及相關屬性(標籤)
A. 查詢指令
[out:json][timeout:25];
// gather results
(
node[amenity][amenity!=bench](25.1735, 121.446, 25.1775, 121.455);
node[shop](25.1735, 121.446, 25.1775, 121.455);
node[tourism](25.1735, 121.446, 25.1775, 121.455);
node[office](25.1735, 121.446, 25.1775, 121.455);
node[emergency](25.1735, 121.446, 25.1775, 121.455);
//way[amenity="university"](25.1735, 121.446, 25.1775, 121.455);
relation[amenity="university"](25.1735, 121.446, 25.1775, 121.455);
way[!highway][type!="route"][landuse!="forest"][!boundary](25.1735, 121.446, 25.1775, 121.455);
relation[!highway][type!="route"][!boundary](25.1735, 121.446, 25.1775, 121.455);
);
// print results
out center;
---
註: 淡江大學之範圍為 25.1735, 121.446, 25.1775, 121.455
*/
String query = String.join("\n",
"[out:json][timeout:25];",
"(",
// "node[amenity][amenity!=bench](25.1735, 121.446, 25.1775, 121.455);",
// "node[shop](25.1735, 121.446, 25.1775, 121.455);",
"node[tourism](25.1735, 121.446, 25.1775, 121.455);",
// "node[office](25.1735, 121.446, 25.1775, 121.455);",
// "node[emergency](25.1735, 121.446, 25.1775, 121.455);",
// "relation[amenity='university'](25.1735, 121.446, 25.1775, 121.455);",
// "way[!highway][type!='route'][landuse!='forest'][!boundary](25.1735, 121.446, 25.1775, 121.455);",
"relation[!highway][type!='route'][!boundary](25.1735, 121.446, 25.1775, 121.455);",
");",
"out center;");
System.out.println(query);
return query;
}
public static void asyncRequest(OverpassService apiClient, String request)
{
//Call call = service.interpreter(query);
apiClient.interpreter(request).enqueue(new Callback()
{
@Override
public void onResponse(Call call, Response response)
{
if(response.isSuccessful()==false)
{
System.out.println("asyncRequest: response.isSuccessful(): false");
System.out.println(response.errorBody());
return;
}
OverpassQueryResult result = response.body();
postProcess(result);
}
public void onFailure(Call call, Throwable t) {
// DO failure handling
System.out.println("onFailure");
System.out.println(t.getLocalizedMessage());
}
});
System.out.println("end of asyncRequest()");
}
public static OverpassQueryResult syncRequest(OverpassService apiClient, String request)
{
OverpassQueryResult result = null;
Call callSync = apiClient.interpreter(request);
try
{
Response response = callSync.execute();
//OverpassQueryResult apiResponse = response.body();
//API response
System.out.println(response);
result = response.body();
}
catch (Exception ex)
{
ex.printStackTrace();
}
System.out.println("end of syncRequest()");
return result;
}
public static void postProcess(OverpassQueryResult result)
{
if(result==null) return;
// DO success handling
StringBuilder sb = new StringBuilder();
System.out.println(result.elements.size() + " elements...");
int count = 1;
for (Element p : result.elements)
{
sb.append(count); count++;
sb.append(String.format(". id:%s, type:%s, lat:%s, lon:%s", p.id, p.type, p.lat, p.lon));
Tags tags = p.tags;
for (Field f : tags.getClass().getFields()) {
f.setAccessible(true);
try
{
if (f.get(tags) != null) {
sb.append(String.format(", %s:%s", f.getName(), f.get(tags)));
}
}
catch (IllegalAccessException e)
{ // shouldn't happen because I used setAccessible
}
}
//if(p.tags. != null)
// sb.append(String.format("tags:%s", p.tags.name));
sb.append("\n");
}
System.out.println(sb.toString());
}
public static void main(String args[])
{
// 建立服務連線客戶端及請求內容
OverpassService requestClient = OverpassServiceProvider.get();
String request = composeRequest();
// 非同步請求
asyncRequest(requestClient, request);
// 同步請求
OverpassQueryResult result = syncRequest(requestClient, request);
postProcess(result);
System.out.println("end of main()");
}
}
2021年8月27日 星期五
how to query OSM data in Java?
2021年8月17日 星期二
tips on installation of xrdp for remote desktop connection to linux machines
大家都知道,Linux桌面跑的是X Windows視窗系統。微軟Windows如果想要連上Linux桌面,一種作法是本身主機消耗硬碟,裝上X Windows伺服軟體VcXsrv,甚至加上微軟應用商店的免費Linux作業系統,例如Ubuntu,此作法步驟可詳Lainme's Blog。另一種作法是遠方Linux主機安裝tightvnc server及xrdp套件,微軟Windows只要沿用原來的遠端桌面連線(mstsc.exe),即可使用rdp協定,連上Linux桌面。後者作法,剪貼簿可用,聲音則待個別測試。以下將依據Linux作業系統為 Ubuntu 或 CentOS,分別介紹其安裝訣竅。
A. Ubuntu 安裝 xrdp 作法
-- 先去除舊套件干擾 sudo apt-get remove xrdp tightvncserver # 只刪執行檔 sudo apt-get purge xrdp tightvncserver # 刪除執行檔及相關組態檔 sudo dpkg --purge xrdp tightvncserver # 刪除執行檔及相關組態檔 sudo dpkg -S /etc/xrdp/startwm.sh # 查檔案所屬套件 -- 安裝tightvncserver及xrdp套件 sudo apt-get install tightvncserver # 一定要先安裝此視窗套件 sudo apt-get install xrdp # 然後安裝此傳輸套件,才會順利偵測運用視窗套件 sudo vi /etc/xrdp/startwm.sh # 加入管理視窗指令 xfce4-session # 若未安裝,可使用apt-get install xfce4-session -- 啟動服務 sudo service xrdp status # 詢問xrdp服務狀態 sudo service xrdp start # 啟動xrdp服務 註: 若連線過程,顯示詢息,缺rsakeys.ini金鑰,可如下產生金鑰 cd /etc/xrdp sudo xrdp-keygen xrdp # generate /etc/xrdp/rsakeys.ini -- 測試連線 sudo netstat -tulpn # 觀看是否3389及3350連接埠有監聽服務 -- rdp連線過程,適用於 Ubuntu 16.04.6 LTS (1) xrdp (3389) # 選擇 sesman-Xvnc 模式 (2) xrdp-sesman (3350,root) (3) xrdp-chansrv # 執行 /etc/xrdp/startwm.sh 及 xfce4-session (4) Xvnc (59xx) -- rdp連線過程,適用於 Ubuntu 18.04.5 LTS (1) xrdp (3389) # 選擇 Xorg 模式 (2) xrdp-sesman (3350,root) (3) xrdp-chansrv # 執行 /etc/xrdp/startwm.sh 及 xfce4-session (4) Xorg 註: xrdp安裝apt-get內建版本即可,不須安裝 https://github.com/neutrinolabs/xrdp.gitB. CentOS 安裝 xrdp 作法
-- 安裝tightvnc-server及xrdp套件 sudo yum erase xrdp tightvnc tightvnc-server sudo yum install tightvnc-server # 要先裝tightvnc-server sudo yum install xrdp # 再裝xrdp -- sudo repoquery -l xrdp # 查套件安裝哪些檔案 sudo rpm -ql /usr/bin/Xvnc # 查檔案所屬套件 -- cat /etc/xrdp/startwm.sh # 確認系統裝有如下任一種視窗管理程式 SESSIONS="gnome-session blackbox fluxbox startxfce4 startkde xterm" -- 啟動服務 sudo service xrdp status # 詢問xrdp服務狀態 sudo service xrdp start # 啟動xrdp服務 -- 測試連線 sudo netstat -antup | grep xrdp # 觀看是否3389及3350連接埠有監聽服務 註: 若連線過程,顯示訊息,缺權限,可如下給予selinux權限 sudo chcon --type=bin_t /usr/sbin/xrdp sudo chcon --type=bin_t /usr/sbin/xrdp-sesman -- rdp連線過程,適用於 CentOS 6.10 (1) xrdp (3389) # 選擇 sesman-Xvnc 模式 (2) xrdp-sesman (3350,root) (3) xrdp-chansrv # 執行 /etc/xrdp/startwm.sh 及 gnome-session (4) Xvnc (59xx) 註: 若為舊 CentOS-6,因自2020/11/30已不再提供維護更新,故須更新repo來源,如下: sudo rm CentOS-SCLo-scl* sudo curl https://www.getpagespeed.com/files/centos6-eol.repo --output /etc/yum.repos.d/CentOS-Base.repo sudo curl https://www.getpagespeed.com/files/centos6-epel-eol.repo --output /etc/yum.repos.d/epel.repocurl \ https://www.getpagespeed.com/files/centos6-epel-eol.repo --output /etc/yum.repos.d/epel.repo sudo yum update # 更新到6.10版 參考: 阿吉的部落格: 使用 xrdp 遠端登入 Linux (Gentoo)
訂閱:
文章 (Atom)