1.什么是GeoWebCache
GeoWebCache是地圖緩存軟件公司成員開發的一個基於java的開源項目。和其他的緩存系統相似,它作為一個客戶端和地圖服務的代理。它可以單獨部署,適用於任何基於WMS標准的地圖服務。
2.配置GeoWebCache緩存路徑
打開webapps/geoserver/WEB-INF下的web.xml文件,在display-name節點后面添加一下內容:
<web-app> <display-name>GeoServer</display-name> <context-param> <param-name>GEOWEBCACHE_CACHE_DIR</param-name> <param-value>E:\ebook\GeoServer_Beginners+Guide\geo_web_cache_dir</param-value> </context-param>
重新加載geoserver應用。打開geoserver服務網站,進入Tile Layers,從列表中找到tiger:ne_50m_populated_places圖層,選擇下拉列表中的“EPSG:4326/png”。縮放地圖,然后再查看geo_web_cache_dir目錄,能看到已經緩存的文件。
3.配置GeoWebCache緩存大小
幾個選項,”Eanble disk quota”是否啟動磁盤配置;“Disk quota check frequency”磁盤配置檢測周期,默認為10秒;“Maximum tile cache size”緩存最大空間,默認為5MIB;”When enforcing disk quota limits, remove tiles that are:“按照什么方式執行磁盤回收,有兩種方式:”使用次數最少”、“最近很少使用”。
4.“Caching Defaults”配置
“Provided Services”-“Enable direct integration with GeoServer WMS”
直接集成是關於終端使用在WMS的GetMap請求。如果使用默認配置,你將不得不使用自定義終端告訴GeoServer你想去接收地圖從緩存中。有效請求地址:
http://localhost:8080/geoserver/gwc/service/wms?
啟用該配置,使用相同的語法你可以請求一個非緩存的圖層:
http://localhost:8080/geoserver/<workspace>/wms?tiled=true Apart
WMS-C
是Web Mapping Services Cached的縮寫,它是終端查詢瓦圖的默認方法。如果禁用該選項,終端請求地址會報400錯誤。
http://localhost:8080/geoserver/gwc/service/wms
TMS and WMTS
用於終端請求TMS和WMTS服務,這兩個服務都遵循OGC標注。不同點在於,WMTS請求時需要GetFeatureInfo。
5.Configuring gridsets
grisets配置了CRS、瓦片尺寸、地圖級別、邊界。一當自定義了gridsets,那么在請求地圖服務時,需要傳遞對恆gridsets的約束名稱,例如EPSG:4269。如何添加一個編碼為EPSG:4269的gridsets?
打開geoserver,選擇Tile Caching-Gridsets,點擊“Create a new gridset”進入新建界面。設置CRS為EPSG:4269,計算Gridsets的邊界。如果你想設置更小的邊界,可自己手動輸入。
每個gridsets都必須設置瓦片尺寸,默認為256*256。
接下來,必須設置zoom level。這里設置了十個級別,其中Tiles列表示Column * row,例如zoom為0,瓦片個數為2列*1行等於2個。
接下來打開“圖層”,選擇tiger:tl_2011_us_county圖層,進入配置頁面,選擇“Tile Caching”頁簽。把頁面拖到最底部,看到“Available gridsets”,在下來列表中選中“EPSG:4269”,點擊添加圖標。現在可以在“Tile Caching”-“Tile Layers”中找到“cite:tl_2011_47_concity”圖層,並在下拉列表中用”EPSG:4269”打開。
請求地圖后,在緩存目錄中查看緩存圖層,可看到:
7.Openlayers如何使用geoserver緩存的瓦片
首先看一個比較常規的例子,下面代碼使用openlayers 2.X請求geoserer的瓦片地圖:
var mapOptions = { projection: "EPSG:4326", maxExtent: new OpenLayers.Bounds(-180.0, -90.0, 180.0, 90.0), units: "degrees" }; var map = new OpenLayers.Map('myMap', mapOptions); var demolayer = new OpenLayers.Layer.WMS( 'tiger:ne_50m_populated_places', 'http://localhost:8082/geoserver/tiger/wms', { layers: 'tiger:ne_50m_populated_places', styles: 'PopulatedPlacesStroke', format: 'image/png' }, {singleTile: 'True'} ); map.addLayer(demolayer); map.zoomTo(4); map.panTo(new OpenLayers.LonLat(12.0,42.0));
在網站上打卡請求地址,縮放地圖。到緩存目錄下查看緩存目錄tiger_ne_50m_populated_places是否有緩存?沒有。geoserver默認是沒有集成GeoWebCache。所以在通過geoserver地址請求時,不會緩存瓦片。那么如何使用緩存?有兩種方式:
方式一:如何要使用緩存,不得不約束gridsets屬性。我們使用的EPSG:4326,所以我們需要設置和EPSG:4326 gridset相同的zoom級別:
var mapOptions = { resolutions: [ 0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125, 0.02197265625, 0.010986328125, 0.0054931640625, 0.00274658203125, 0.001373291015625, 0.0006866455078125, 0.0003433227539062, 0.0001716613769531, 0.0000858306884766, 0.0000429153442383, 0.0000214576721191, 0.0000107288360596, 0.0000053644180298, 0.0000026822090149, 0.0000013411045074, 0.0000006705522537, 0.0000003352761269 ], projection: "EPSG:4326", maxExtent: new OpenLayers.Bounds(-180.0, -90.0, 180.0, 90.0), units: "degrees" };
請求地址也需要更變,直接請求GeoWebCache的地址:
'tiger:ne_50m_populated_places', 'http://localhost:8082/geoserver/gwc/service/wms',
接下來需要匹配瓦片的尺寸。調整代碼如下:
var demolayer = new OpenLayers.Layer.WMS( 'tiger:ne_50m_populated_places', 'http://localhost:8082/geoserver/gwc/service/wms', { layers: 'tiger:ne_50m_populated_places', styles: 'PopulatedPlacesStroke', format: 'image/png' }, {tileSize: new OpenLayers.Size(256,256)} );
現在我們在瀏覽器中請求打開網站,縮放地圖。查看緩存目錄,現在有新增的緩存了。
方式二:之前我們講過“Caching defaults”-“Provided Services”中的“Enable direct integration with GeoServer WMS”選項。該選項默認沒有勾選,如果勾選則直接把GeoWebCache集成到geoserver。url還是修改為原來的geoserver地址,代碼如下:
var demolayer = new OpenLayers.Layer.WMS( 'tiger:ne_50m_populated_places', 'http://localhost:8082/geoserver/tiger/wms', { layers: 'tiger:ne_50m_populated_places', styles: 'PopulatedPlacesStroke', tiled: 'true', format: 'image/png' }, {tileSize: new OpenLayers.Size(256,256)} );
在“Tile Layers”中,點擊“tiger:ne_50m_populated_places”行的“Empty”按鈕,清理緩存。請求網站地址。檢查緩存目錄,有生成新的緩存文件。





