場景
Geoserver簡介
GeoServer是OGC Web服務器規范的J2EE實現,利用GeoServer可以方便地發布地圖數據,允許用戶對特征數據進行更新、刪除、插入操作,通過GeoServer可以比較容易地在用戶之間迅速共享空間地理信息。GeoServer是開源軟件。
GeoServer 是基於 Java 的軟件服務器,允許用戶查看和編輯地理空間數據。使用開放地理空間聯盟(OGC)提出的開放標准,GeoServer 在地圖創建和數據共享方面具有極大的靈活性。
GeoServer允許您向世界顯示您的空間信息。實施Web地圖服務(WMS)標准,GeoServer可以創建各種輸出格式的地圖。一個免費的地圖庫OpenLayers已集成到GeoServer中,從而使地圖生成快速簡便。GeoServer基於GeoTools(一種開放源Java GIS工具包)構建。
Geoserver官網
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
Geoserver下載
進入官網,點擊Stable下版本標簽進入下載頁面
然后選擇二進制文件進行下載,也可以選擇后面的war包下載部署到Tomcat中運行
下載解壓之后的目錄如下
修改Geoserver端口號
找到解壓目錄下start.ini,右鍵編輯,找到jetty.port,這里將端口號修改為8000
啟動Geoserver
找到bin下的startup.bat,雙擊啟動,如果控制台沒有報錯,則保持命令行開啟
登錄Geoserver
打開瀏覽器輸入
http://localhost:8000/geoserver
登錄用戶名密碼
admin
geoserver
獲取地圖數據文件
在發布之前,需要有地圖數據源文件,這里可以從官方示例文檔中進行下載
https://docs.geoserver.org/stable/en/user/_downloads/30e405b790e068c43354367cb08e71bc/nyc_roads.zip
將數據源解壓后
就能看到我們所需要的shp文件。
將解壓后的整個目錄放在前面Geoserver解壓目錄下data_dir/data下
完整路徑
geoserver/data_dir/data/nyc_roads
新建工作區
點擊工作區-添加新的工作區
然后配置工作區信息
注意:
工作區名稱是描述項目的標識符。它不能超過10個字符或包含空格。名稱空間URI(統一資源標識符)通常可以是與項目相關聯的URL,其中添加了一個表示工作區的尾隨標識符。歸檔的名稱空間URI不需要解析為實際的有效web地址。
然后勾選上將其設置為默認工作區(可選),點擊保存。
創建一個數據存儲
點擊數據存儲-添加新的數據存儲
然后選擇矢量數據源下的第一個Directory of spatial files(shapefiles)
選擇上面新建的工作區,以及選擇shapefiles文件的目錄
這里的目錄就是上面放在data下的shape的目錄。
注意這里只能選擇到shp文件所在的目錄,而后面shp文件的全名稱需要在選擇后手動添加上
比如這里選擇完成之后的路徑為
點擊保存
發布圖層
在新建圖層頁面,點擊上面新建圖層,然后點擊添加圖層的下拉框,選擇上面新建的數據源,然后在下面會出現圖層,點擊后面的發布,會進入圖層發布前的編輯
然后往下拉找打計算邊界的兩個地方
點擊從數據中計算和Compute from natice bounds
然后再點擊上面的發布標簽下
將這里的WMS Settings下的Default Style修改為Line,默認就是如此。
然后點擊保存
圖層預覽
然后找到菜單中Layer Preview,找到上面剛保存的圖層
點擊后面的OpenLayers
然后就會在新標簽頁中顯示地圖的預覽
其中地址欄中的
http://localhost:8000/geoserver/nyc/wms
就可以用來給前端OpenLayers的url進行使用
官方文檔原文
以上所有發布shapefile的過程可參照官方文檔
https://docs.geoserver.org/stable/en/user/gettingstarted/shapefile-quickstart/index.html
Publishing a shapefile
This tutorial walks through the steps of publishing a Shapefile with GeoServer.
Note
This tutorial assumes that GeoServer is running at http://localhost:8080/geoserver
.
Data preparation
First let’s gather that the data that we’ll be publishing.
-
Download the file
nyc_roads.zip
. This archive contains a shapefile of roads from New York City that will be used during in this tutorial. -
Unzip the
nyc_roads.zip
into a new directory namednyc_roads
. The archive contains the following four files:nyc_roads.shp nyc_roads.shx nyc_roads.dbf nyc_roads.prj
-
Move the
nyc_roads
directory into<GEOSERVER_DATA_DIR>/data
, where<GEOSERVER_DATA_DIR>
is the root of the GeoServer data directory. If no changes have been made to the GeoServer file structure, the path isgeoserver/data_dir/data/nyc_roads
.
Creating a new workspace
The next step is to create a workspace for the shapefile. A workspace is a container used to group similar layers together.
Note
This step is optional if you’d like to use an existing workspace. Usually, a workspace is created for each project, which can include stores and layers that are related to each other.
-
In a web browser, navigate to
http://localhost:8080/geoserver
. -
Log into GeoServer as described in the Logging In section.
-
Navigate to
Workspaces page
-
Click the Add new workspace button.
-
You will be prompted to enter a workspace Name and Namespace URI.
Configure a new workspace
-
Enter the Name as
nyc
and the Namespace URI ashttp://geoserver.org/nyc
.Note
A workspace name is a identifier describing your project. It must not exceed ten characters or contain spaces. A Namespace URI (Uniform Resource Identifier) can usually be a URL associated with your project with an added trailing identifier indicating the workspace. The Namespace URI filed does not need to resolve to an actual valid web address.
nyc workspace
-
Click the Submit button. The
nyc
workspace will be added to the Workspaces list.
Create a store
Once the workspace is created, we are ready to add a new store. The store tells GeoServer how to connect to the shapefile.
-
Navigate to
-
You should see a list of stores, including the type of store and the workspace that the store belongs to.
-
In order to add the shapefile, you need to create a new store. Click the Add new Store button. You will be redirected to a list of the data sources supported by GeoServer. Note that the data sources are extensible, so your list may look slightly different.
Stores
-
Click Shapefile. The New Vector Data Source page will display.
-
Begin by configuring the Basic Store Info.
-
Select the workspace
nyc
from the drop down menu. -
Enter the Data Source Name as
NYC Roads
-
Enter a brief Description (such as “Roads in New York City”).
-
-
Under Connection Parameters, browse to the location URL of the shapefile, typically
nyc_roads/nyc_roads.shp
.Basic Store Info and Connection Parameters
-
Click Save. You will be redirected to the New Layer page in order to configure the
nyc_roads
layer.
Creating a layer
Now that the store is loaded, we can publish the layer.
-
On the New Layer page, click Publish beside the
nyc_roads
layer name.New layer
-
The Edit Layer page defines the data and publishing parameters for a layer. Enter a short Title and an Abstract for the
nyc_roads
layer.Basic Resource Information
-
Generate the layer’s bounding boxes by clicking the Compute from data and then Compute from native bounds links.
Generating bounding boxes
-
Click the Publishing tab at the top of the page.
-
We can set the layer’s style here. Under WMS Settings, ensure that the Default Style is set to line.
Select Default Style
-
Finalize the layer configuration by scrolling to the bottom of the page and clicking Save.
Previewing the layer
In order to verify that the nyc_roads
layer is published correctly, we can preview the layer.
-
Navigate to the Layer Preview screen and find the
nyc:nyc_roads
layer.Layer Preview
-
Click the OpenLayers link in the Common Formats column.
-
An OpenLayers map will load in a new tab and display the shapefile data with the default line style. You can use this preview map to zoom and pan around the dataset, as well as display the attributes of features.
Preview map of nyc_roads