Showing posts with label GIS. Show all posts
Showing posts with label GIS. Show all posts

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 

Sunday, 7 May 2017

[GIS] GIS files - Shapefile

ESRI Shapefile(shp),或簡稱shapefile,是美國環境系統研究所公司(ESRI)開發的一種空間數據開放格式。[1]目前,該文件格式已經成為了地理信息軟件界的一個開放標準,這表明ESRI公司在全球的地理信息系統市場的重要性。Shapefile也是一種重要的交換格式,它能夠在ESRI與其他公司的產品之間進行數據互操作。

Shapefile文件用於描述幾何體對象:點,折線與多邊形。例如,Shapefile文件可以存儲井、河流、湖泊等空間對象的幾何位置。除了幾何位置,shp文件也可以存儲這些空間對象的屬性,例如一條河流的名字,一個城市的溫度等等。

除了Shapefile以外還有其他的檔案,而所有的文件都必須位於同一個目錄之中。必須文件有:

.shp — 圖形格式,用於保存元素的幾何實體。
.shx — 圖形索引格式。幾何體位置索引,記錄每一個幾何體在shp文件之中的位置,能夠加快向前或向後搜索一個幾何體的效率。
.dbf — 屬性數據格式,以dBase IV的數據表格式存儲每個幾何形狀的屬性數據。
其他可選的文件:
.prj — 投幀式,用於保存地理坐標系統與投影信息,是一個存儲well-known text投影描述符的文本文件。
.sbn and .sbx — 幾何體的空間索引
.fbn and .fbx — 只讀的Shapefiles的幾何體的空間索引
.ain and .aih — 列表中活動字段的屬性索引。
.ixs — 可讀寫Shapefile文件的地理編碼索引
.mxs — 可讀寫Shapefile文件的地理編碼索引(ODB格式)
.atx — .dbf文件的屬性索引,其文件名格式為shapefile.columnname.atx (ArcGIS 8及之後的版本)
.shp.xml — 以XML格式保存元數據。
.cpg — 用於描述.dbf文件的代碼頁,指明其使用的字符編碼。

在每個.shp, .shx與.dbf文件之中,圖形在每個文件的排序是一致的。也就是說,.shp的第一條記錄與.shx及.dbf之中的第一條記錄相對應,如此類推。此外,在.shp與.shx之中,有許多字段的字節序是不一樣的。因此用戶在編寫讀取這些文件格式的程序時,必須十分小心地處理不同文件的不同字節序。

Shapefile通常以X與Y的方式來處理地理坐標,一般X對應經度,Y對應緯度,用戶必須註意X,Y的順序。

各文件必須具有相同的前綴,例如,roads.shp、roads.shx 和 roads.dbf。

在 ArcCatalog(或任何 ArcGIS 應用程序)中查看 Shapefile 時,將僅能看到一個代表 Shapefile 的文件;但可以使用 Windows 資源管理器查看與 Shapefile 相關聯的所有文件。覆制 Shapefile 時,建議在 ArcCatalog 中或者使用地理處理工具執行該操作。但如果在 ArcGIS 之外覆制 shapefile,確保覆制組成該 shapefile 的所有文件。

data source : Wiki

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...