Monday 22 May 2017

[開發筆記] 離線地圖 Offline Map with GeoServer and Leaflet.js

現在網路地圖應用越來越多,可是很多時候會遇到沒有網路狀況,這個時候Google Map、Open Street Map、 Bind MAP 等等都是無法使用的。
但是我們又不太可能像Google它們一樣架出一個GeoServer然後有很多的圖資,這樣的情況下就需要離線地圖,
我這裡用的是把一張tiff圖檔當底圖,然後架一個geoServer提供給瀏覽器來讀取,架構如下圖
2016-01-22_142209.jpg

下載與安裝GeoServer  (Download & install GeoServer)

Download : http://geoserver.org/download/

下載好後,就跟著下面步驟安裝Following the below steps to install. 

2016-01-18_105550.jpg 
2016-01-18_105641.jpg
這裡你可以修改安裝的目錄路徑 (You can change the install location. I used to install in programs folder. )
2016-01-18_105727.jpg
2016-01-18_105754.jpg
GeoServer需要Java,所以這裡要指定Java的目錄,如果沒有安裝,請點擊URL去下載安裝。
GeoServer安裝程式會確認是否有安裝。
GeoServer need Java. If you did not have it, please down and install java.
2016-01-18_105819.jpg
你可以修改你的圖資目錄的路徑,因為有電腦有存取的權限問題。
2016-01-18_105852.jpg
User account and Password. You can change them if you want. 
2016-01-18_105921.jpg
2016-01-18_105950.jpg
I change the default port number to 8085
GeoServer 是一台網路主機,所以需要port. 我特地修改成8085以免跟其他程式有相沖

2016-01-18_110018.jpg
啟動GeoServer的方式。
It depends on how you like to use GeoServer.
如果你是用來開發測試,建議使用"Run manually" ,手動啟動
If you just install GeoServer for development, I recommend to select “Run manually”
如果你是用來當正式環境運行,建議使用"Install as a service" ,自動啟動

If you just install GeoServer for production environment, select “Install as a service”
2016-01-18_110138.jpg
2016-01-18_110211.jpg
確認沒有問題,就按install
2016-01-18_110229.jpg

2016-01-18_110320.jpg
GeoServer
啟動GeoServer
Start --> All apps --> Start GeoServer
2016-01-21_160932.jpg
A window will pop up, Please wait for that it done.This window cannot be closed
跳出一個視窗,請等他執行完畢。 這個視窗不能關閉
2016-01-21_161209.jpg
GO TO GEOSERVER ADMIN PAGE
開啟GeoServer管理頁面
2016-01-21_164722.jpg
It will redirect to GeoServer Web Admin Page and login into admin page. If you did not change defaulte account and password, it will be admin and geoserver
開啟後就登入 admin 頁面。 如果你沒有改預設帳號跟密碼, 帳號是admin 密碼是geoserver

2016-01-21_165405.jpg
Admin page:
2016-01-21_165725.jpg
Add layer
the next step is to create a layer which we want to display.
The step is : Workplace --> Stores --> Layers
下一步我們要建立一個圖層。順序是 Workplace --> Stores --> Layers
Create a Workplace
Select “Workspace” on left menu
在左選單上選 “Workspace”
2016-01-21_170004.jpg
Click “Add new workspace”
點擊 “Add new workspace”
2016-01-21_170446.jpg
Type in the Name and Namespace URL

They are just a name. I don’t know what Namespace URL use for.
輸入你要的名稱和 Namespace URL(隨你意思取)
2016-01-21_170645.jpg
Create a STORE
Select store on the left menu

Click “Add new Store”
在左選單上選"Store "
點擊 “Add new Store”
2016-01-21_183805.jpg
Click “GeoTIFF”
點擊 “GeoTIFF”
2016-01-21_183931.jpg
Fill up the Basic Store Info and then click “Save” button
Workspace : hoc
Data Source Name: singapore map
URL : file://C:\MapData\SG\SG.tif  (select a image file you want to show)

Enable “Enabled”
2016-01-22.png 
click publish
點擊"Publish"
2016-01-18_133645.jpg 
Edit Layer
2016-01-18_133831.jpg
Save it without change
直接儲存不用修改
2016-01-18_133920.jpg
VIEW LAYER 檢視圖層
Select “Layer Preview” on the left menu and click "OpenLayers"
左選單上選 “Layer Preview” and 點擊 "OpenLayers"
2016-01-22_093249.jpg
A window will pop up 
檢視視窗就出現,等圖片出來
2016-01-22_094106.jpg
LEFTLET & GEOSERVER
Download example file : https://github.com/kennethhutw/OfflineMapWithGeoSe...
下載範例檔: https://github.com/kennethhutw/OfflineMapWithGeoServerAndLeaflet
 
<!DOCTYPE html>
<html>
<head>
    <title>Leaflet Leaflet with GeoServer Example</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
</head>
<body>
    <div id="map" style="width: 600px; height: 400px"></div>
    <script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
    <script>
        var map = L.map('map').setView([1.3571318,103.8080804], 13);
        mapLink = '<a href="http://www.kenneth.net">(Kenneth)</a>';
        L.tileLayer.wms("http://localhost:8080/geoserver/hoc/wms", {
                layers: 'hoc:SG',
                format: 'image/png',
                transparent: true,
                version: '1.1.0',
                attribution: "Hu ChiaWei "+mapLink
            }).addTo(map);    
        L.marker([1.3571318,103.8080804]).addTo(map)
            .bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
        var popup = L.popup();
        function onMapClick(e) {
            popup
                .setLatLng(e.latlng)
                .setContent("You clicked the map at " + e.latlng.toString())
                .openOn(map);
        }
        map.on('click', onMapClick);
    </script>
</body>
</html>
Result
2016-01-22_160213.jpg 

No comments:

Post a Comment

how-to-recursively-create-subfolder-in-each-folder-of-a-directory-in-cmd

test.cmd: @echo off setlocal for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do ( rem enter the directory pushd %%a echo...