一、GeoServer安装和发布服务

参考链接

二、PostgreSQL 和 PostGIS安装

PostGIS要下载和PostgreSQL对应版本!!!

1.下载安装postgreSQL

进入PostgreSQL 官网,进入下载导航,点击windows系统,或直接打开如下网址:

https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

image-20230619190726469

下载好后,以管理员身份运行安装包,然后指定安装路径

image-20230619190730656 image-20230619190734772 image-20230619190738449

然后一路next

image-20230619190804495 image-20230619190809934 image-20230619190813402

(注意:可能会报错,不过没关系,点击取消就行)

在程序里找到安装的PostgreSQL 10下面的pgAdmin4运行数据库管理工具

image-20230619190835788

2.下载安装postgis

可使用Application Stack Builder进行下载,但速度比较慢,而且可能会卡死无响应,因此换一种可以直接下载.exe的方法。

下载地址:http://download.osgeo.org/postgis/windows/

==一定一定要先选择对应的PostgreSQL版本!!!==

image-20230619190921297 image-20230619190930515 image-20230619190954970 image-20230619190958005

注意:上面的安装路径一定要选择postgresql的安装路径!!!切记

一路next

重新打开pgadmin,发现出现了postgis数据库了

image-20230619191014760

三、创建空间数据库

1.打开pgAdmin4鼠标右击数据库选项并选择新建数据库:

image-20230619191140391

2.如下图所示,填写“新建数据库”表单,然后单击“确定”:

image-20230619191146545

**3.**选择nyc这个新建的数据库,并打开它以显示对象树,将会看到public架构(schema):

image-20230619191154634

4. 单击下面所示的SQL查询按钮(或转到工具 > 查询工具)。

image-20230619191200307

5.在查询文本区域中输入以下查询语句以加载PostGIS****空间扩展

CREATE EXTENSION postgis;

image-20230619191209241

6.单击工具栏中的执行查询按钮(或按F5)以"执行查询"。

image-20230619191215505

**7.**现在,通过运行PostGIS函数来确认是否安装了PostGIS:

SELECT postgis_full_version();

image-20230619191220006

至此,已经成功地创建了PostGIS空间数据库!

四、加载空间数据(记得设置SRID)

1.首先,返回到选项板,并单击PostGIS部分中的PostGIS shapefile工具,PostGIS shapefile工具将启动。

image-20230619191404852 image-20230619191410009

**2.**填写PostGIS连接部分的连接详细信息,然后单击“ok”按钮。程序将测试连接并在日志窗口中报告。

如果安装时使用默认的信息,就如下所示:

image-20230619191424558 image-20230619191430099

**3.**接下来,打开“Add File”按钮并导航到数据目录文件

image-20230619191436182 image-20230619191441456

4.将文件的SRID(空间参考信息)值更改为4527(数据源的空间参考信息)。请注意,架构、表名和列名已经根据shapefile文件里的信息填充。

image-20230619191448939

5.单击"Options“按钮查看加载选项。加载程序将使用快速“COPY(复制)“模式,并在加载数据后默认创建空间索引

image-20230619191454228 image-20230619191501038

**6.**最后,单击”Import“按钮并观察导入过程。

image-20230619191508673

**7.**加载所有文件后,打开pgAdmin可以看到表已加载到数据库中:数据库>mySDE>架构>public>数据表里。

image-20230619191516817

五、GeoServer连接PostGIS

1.打开GeoServer,点击数据存储中的新建数据源,选择PostGIS

image-20230619191531119

2.选择工作区,连接参数中输入PostgreSQL端口号、数据库、用户名、密码,最后点击保存应用

image-20230619191535390

六、mapbox加载geoserver发布的瓦片服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MapBox加载WMS地图服务</title>
    <script src='https://api.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
        /*隐藏logo*/
        .mapboxgl-ctrl.mapboxgl-ctrl-attrib{
            display: none !important;
        }
        .mapboxgl-ctrl-logo{
            display: none !important;
        }
    </style>
</head>
<body>
<div id='map'></div>
<script >
    mapboxgl.accessToken = 'pk.eyJ1Ijoid3lqcSIsImEiOiJjbDBnZDdwajUxMXRzM2htdWxubDh1MzJrIn0.2e2_rdU2nOUvtwltBIZtZg';
    var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/streets-v10',
        center: [108.438,34.431],
        zoom: 7
    });

    let wmsUrl = "http://localhost:8008/geoserver/mapserver/wms?service=WMS&version=1.1.0&request=GetMap&layers=mapserver:bus_line&styles=&bbox={bbox-epsg-3857}&width=256&height=256&srs=EPSG:3857&format=image/png&TRANSPARENT=TRUE"

    map.on("load",function () {
        map.addLayer({
            'id': 'wms-test-layer',
            'type': 'raster',
            'source': {
                'type': 'raster',
                'tiles': [
                    wmsUrl
                ],
                'tileSize': 256
            },
            'paint': {}
        });
    })
</script>
</body>
</html>

注意

EPSG要设置为3857而不是4326,不然会出现跨域问题