首页 > 其他分享 >RFS_Server_06 上传并发布数据

RFS_Server_06 上传并发布数据

时间:2024-06-05 21:46:22浏览次数:11  
标签:RFS layer 06 name db Server user table port

操作描述:云服务器Ubuntu20.04系统Docker中有两个容器:Postgres容器和GeoServer容器。将数据存储至Postgres数据库容器中,并通过GeoServer连接,发布地图服务。

此文档中使用的Postgres数据库名称为pg01,GeoServer服务器名称为geoserver01

1    基础操作:使用工具连接云服务器Docker中Postgres数据库容器

1-1    通过QGIS连接

QGIS用于测试数据库连接、浏览postgres中的空间数据。

右侧“浏览器”部分中,找到PostgreSQL选项,右键新建链接,输入以下几项:

  • 名称:自定义的

  • 主机:服务器IP,如果是本地服务器,应为localhost。

  • 端口:前期已将pg01容器5432端口与云服务器8432端口匹配,所以现在应该连接服务器的8432端口。

  • 数据库:前期操作中,已在pg01容器中新建名称为postgres的数据库。或者自己新建其他数据库,填写数据库的名称。

  • 用户名和密码:pg01账户名称为postgres,密码前期已自定义。

点击下方“测试连接”按钮,失败或成功会有消息提示。

QGIS有往数据库上传数据的功能,但多次测试并没有成功,所以一般QGIS仅用于浏览postgres中的空间数据。![](file://G:\04%20开发\01%20博客存档\RFS_Server_06%20上传并发布数据\Snipaste_2024-06-05_13-00-01.png?msec=1717591837770)

1-2    通过DataGrip连接

DataGrip和PyCharm不同,没有社区版和正式版之分,必须购买正版或破解。DataGrip主要用于连接数据库,查看数据表、执行数据库命令等操作。

连接云服务器的Postgres数据库:

先连接已有数据库postgres,成功连接后再新建名为bellotergis的数据库。在数据库中引入PostGIS插件,才能在Postgres数据库中使用空间数据,否则导入的只有属性表。

在控制台运行如下代码:

create extension postgis; -- PostGIS的矢量数据扩展
create extension postgis_raster; -- PostGIS的栅格数据扩展

验证是否扩展成功,执行下面语句不报错即可。

select ST_SetSRID(ST_Point(-108,30.741),4326),ST_GeomFromText('POINT(-106.51 29.741)',4326)

或是运行如下语句:

SELECT postgis_lib_version();

参考文档:PostgreSQL扩展PostGIS - lqqgis - 博客园

现在可以通过Datagrip查看数据表,若数据为空间要素,则会带有geom字段。

1-3    通过GDAL连接

1-3-1    安装GDAL

GDAL是Python包,主要用于读取电脑上的空间数据并上传到服务器中。

环境:miniconda3 24.40 + Python3.11

安装GDAL经常会出现报错,base环境下似乎可以pip install gdall或conda install gdal成功安装,但虚拟环境下唯一成功的方式是通过Anaconda Prompt (miniconda3) 安装,运行如下命令:

# 记得进入虚拟环境,如果安装不上可以通过Anaconda Prompt进入cmd安装。pip无法使用的话使用conda install
conda install gdal
conda install psycopg2

有可能会遇到miniconda或python版本较老,上述方法仍旧无法安装的情况,最后重建了虚拟环境,安装成功。

1-3-2    GDAL上传shapefile文件

设置好参数,运行如下python代码:

# 定义参数
shapefile_path = 'C:/Users/****/Desktop/Database/TOWN_POINT.shp'
db_name = 'bellotergis'
db_user = 'postgres'
db_password = '12345678'
db_host = '47.113.***.***'
db_port = '8432'
table_name = 'TOWN_POINT_TEST'
# 上传shapefile的函数

import subprocess
def upload_shapefile_to_postgis(shapefile_path, db_name, db_user, db_password, db_host, db_port, table_name):
    # Construct the ogr2ogr command
    ogr2ogr_command = [
        'ogr2ogr',
        '-f', 'PostgreSQL',
        f'PG:dbname={db_name} user={db_user} password={db_password} host={db_host} port={db_port}',
        shapefile_path,
        '-nln', table_name,  # Set the table name
        '-overwrite',  # Overwrite the table if it exists
        '-lco', 'GEOMETRY_NAME=geom',  # Specify geometry column name
        '-lco', 'FID=gid',  # Specify FID column name
        '-lco', 'SPATIAL_INDEX=GIST',  # Create a spatial index
        # '-progress',  # Show progress
        # '-skipfailures'  # Skip failures
    ]

    # Run the command
    try:
        subprocess.check_call(ogr2ogr_command)
        print("Shapefile successfully uploaded to PostGIS.")
    except subprocess.CalledProcessError as e:
        print(f"Error occurred: {e}")
# 运行函数
upload_shapefile_to_postgis(shapefile_path, db_name, db_user, db_password, db_host, db_port, table_name)

1-3-3    GDAL上传gpkg中的单个要素

设置好参数,运行如下python代码:

# 设置参数
geopackage_path = 'C:/Users/****/Desktop/Database/TEST_GEOPACKAGE.gpkg'
layer_name = 'TEST_LAYER'  # Specify the layer name inside the GeoPackage
db_name = 'bellotergis'
db_user = 'postgres'
db_password = '12345678'
db_host = '47.113.***.***'
db_port = '8432'
table_name = 'TEST_GPKG'
# 上传GeoPackage的函数

import subprocess
def upload_geopackage_to_postgis(geopackage_path, layer_name, db_name, db_user, db_password, db_host, db_port, table_name):
    # Construct the ogr2ogr command
    ogr2ogr_command = [
        'ogr2ogr',
        '-f', 'PostgreSQL',
        f'PG:dbname={db_name} user={db_user} password={db_password} host={db_host} port={db_port}',
        geopackage_path,
        '-nln', table_name,  # Set the table name
        '-overwrite',  # Overwrite the table if it exists
        '-lco', 'GEOMETRY_NAME=geom',  # Specify geometry column name
        '-lco', 'FID=gid',  # Specify FID column name
        '-lco', 'SPATIAL_INDEX=GIST',  # Create a spatial index
        '-progress',  # Show progress
        # '-skipfailures',  # Skip failures
        layer_name  # Specify the layer name in the GeoPackage
    ]

    # Run the command
    try:
        subprocess.check_call(ogr2ogr_command)
        print("GeoPackage successfully uploaded to PostGIS.")
    except subprocess.CalledProcessError as e:
        print(f"Error occurred: {e}")
# 运行函数
upload_geopackage_to_postgis(geopackage_path, layer_name, db_name, db_user, db_password, db_host, db_port, table_name)

1-3-4    批量上传gpkg中的所有要素

# 设置参数
geopackage_path = 'C:/Users/****/Desktop/Database/TEST_GEOPACKAGE.gpkg'
db_name = 'bellotergis'
db_user = 'postgres'
db_password = '12345678'
db_host = '47.113.***.***'
db_port = '8432'
# 未测试,批量导入gpkg中所有文件
import subprocess
import os
from osgeo import ogr

# Enable or disable exceptions,没有这一句的话会有warning,不影响程序运行
ogr.UseExceptions()

def upload_geopackage_to_postgis(geopackage_path, db_name, db_user, db_password, db_host, db_port):
    # Open the GeoPackage to list layers
    gpkg = ogr.Open(geopackage_path)
    if gpkg is None:
        print(f"Failed to open GeoPackage: {geopackage_path}")
        return

    # Iterate over each layer in the GeoPackage
    for i in range(gpkg.GetLayerCount()):
        layer = gpkg.GetLayerByIndex(i)
        layer_name = layer.GetName()
        table_name = layer_name  # You can customize the table name if needed

        # Construct the ogr2ogr command
        ogr2ogr_command = [
            'ogr2ogr',
            '-f', 'PostgreSQL',
            f'PG:dbname={db_name} user={db_user} password={db_password} host={db_host} port={db_port}',
            geopackage_path,
            '-nln', table_name,  # Set the table name
            '-overwrite',  # Overwrite the table if it exists
            '-lco', 'GEOMETRY_NAME=geom',  # Specify geometry column name
            '-lco', 'FID=gid',  # Specify FID column name
            '-lco', 'SPATIAL_INDEX=GIST',  # Create a spatial index
            '-progress',  # Show progress
            '-skipfailures',  # Skip failures
            layer_name  # Specify the layer name in the GeoPackage
        ]

        # Run the command
        try:
            subprocess.check_call(ogr2ogr_command)
            print(f"Layer '{layer_name}' successfully uploaded to PostGIS as table '{table_name}'.")
        except subprocess.CalledProcessError as e:
            print(f"Error occurred while uploading layer '{layer_name}': {e}")
# 运行函数
upload_geopackage_to_postgis(geopackage_path, db_name, db_user, db_password, db_host, db_port)

1-4    通过GeoServer连接

通过域名:接口的方式访问GeoServer,输入账户名、密码登录。

  • 新建工作空间:数据->工作空间->添加新的工作空间,名称为GeoTest

  • 新建存储仓库:数据->存储仓库->添加新的存储仓库->选择矢量数据源PostGIS - PostGIS Database

如能成功保存,则是连接成功。

2    发布服务

2-1    数据预处理要求

  • 数据文件名称、路径、字段名称均不含中文

  • 统一使用WGS 1984地理坐标系

2-3    设置默认配置

矢量数据发布缓存的默认设置,更改后不用每次发布都去勾选

Tile Caching -> Caching Defaults -> Vector Layers

勾选如下几个:
application/json;type=geojson

application/json;type=topojson

application/json;type=utfgrid

application/vnd.mapbox-vector-tile

取消勾选:

image/jpeg

image/png

2-4    将矢量数据发布为地图服务

数据 -> 图层 -> 添加新的资源 -> 选择存储仓库 -> 找到要发布的数据,点击发布 ->进入编辑图层界面

“数据”菜单的“边框”中,点击如下两个按钮,计算XY范围

Tile Caching菜单的“Tile cache configuration”中,确定红框中的勾选如下图。(可以通过2-3设置默认配置 一劳永逸 )

点击保存,完成发布。

2-5    预览矢量数据

方法1 Tile Caching ->找到要预览的图层,在预览中选择EPSG:4326/pbf

预览效果如下:

方法2:数据 -> 图层预览 -> 找到要预览的图层,点击“常用格式”下的OpenLayers

预览效果如下:

2-6    矢量数据调用

暂无

3    上传.tif遥感影像并发布服务

3-1    将数据上传至服务器中

将数据上传至GeoServer容器映射的服务器路径中。按照以前创建GeoServer容器时选择的路径,应为:/root/projects/geoserver_data/。将数据上传至此路径即可。

3-2    将遥感影像发布为地图服务

在GeoServer界面中:数据 -> 存储仓库 -> 添加新的存储仓库 -> 栅格数据源  GeoTIFF - Tagged Image File Format with Geographic information 进入添加栅格数据源的界面。

填写数据源名称,点击 URL右侧的浏览,选择要发布的数据,保存即可,现在已经为.tif数据新增了存储仓库,还需要发布图层。

数据 -> 图层 -> 添加新的资源 -> 选择存储.tif的存储仓库 -> 点击发布 -> 保存,.tif 遥感影像发布完成。

3-3    预览遥感影像

方法1:Tile Caching -> 切片图层 -> 找到要发布的数据,在预览 下选择一个方式即可预览。

方法2:数据 -> 图层预览 -> 找到要预览的图层,点击“常用格式”下的OpenLayers

预览效果如下:

标签:RFS,layer,06,name,db,Server,user,table,port
From: https://www.cnblogs.com/preyer/p/18233877

相关文章

  • 科研日记3【2024-06-05】
    文献阅读2021年伊朗谢里夫理工大学ZamaniH等人在IEEETAP上的QualityImprovementofMillimeter-WaveImagingSystemsUsingOptimizedDualPolarizedArrays[1]背景:使用极化分集天线,可提高系统的SNR和可靠性;交叉极化和共极化数据分别保留了图像的边缘和平滑部分,利用共极......
  • 构建HTTP Server容器Docker构建一个简单的HTTP Server。
    构建HTTPServer容器Docker构建一个简单的HTTPServer。HTTPServer在访问根路径/时返回"Hello,World!"。提供完整的Dockerfile和启动命令。步骤1:创建HTTPServer的Python脚本首先,创建一个目录来存放HTTPServer文件:mkdirhttp_servercdhttp_server在这个目录中,创建一......
  • 【调试笔记-20240601-Linux-在 OpenWRT-23.05 上配置 frpc 实现内网穿透】
    调试笔记-系列文章目录调试笔记-20240601-Linux-在OpenWRT-23.05上配置frpc实现内网穿透文章目录调试笔记-系列文章目录调试笔记-20240601-Linux-在OpenWRT-23.05上配置frpc实现内网穿透前言一、调试环境操作系统:OpenWrt23.05.3调试环境调试目标二、调试步......
  • SQL Server 学习随笔-2
    @@@@@SQLServer中go的作用在SQLServerManagementStudio(SSMS)或其他支持SQLServer的查询工具中,GO不是一个SQL标准语句,而是SSMS环境中的一个批处理分隔符。GO命令用于指示SSMS或其他工具将前面的SQL语句作为一个独立的批处理发送到SQLServer进行执行。使用GO命令的主要......
  • 2024-06-05:用go语言,给定三个正整数 n、x 和 y, 描述一个城市中由 n 个房屋和 n 条街道
    2024-06-05:用go语言,给定三个正整数n、x和y,描述一个城市中由n个房屋和n条街道连接的情况。城市中存在一条额外的街道连接房屋x和房屋y。需要计算对于每个街道数(从1到n),有多少房屋对满足从一个房屋到另一个房屋经过的街道数正好为该街道数。在结果数组中,索引k对......
  • 云动态摘要 2024-06-05
    给您带来云厂商的最新动态,最新产品资讯和最新优惠更新。最新优惠与活动[1元/年起]618大促-对象存储分会场腾讯云 2024-06-03对象存储限时破价秒杀,标准存储新老同享历史低价,新客首单低至1元,爆款资源包低于2折购云服务器ECS试用产品续用阿里云 2024-04-14云服务器ECS......
  • CS106B(2022 winter) hw4
    重生之你在美国当总统题目描述We'llsaythatacountryisdisaster-readyifeverycityeitheralreadyhasemergencysuppliesorisimmediatelydownthehighwayfromacitythathasthem.YourtaskistowriteafunctionboolcanBeMadeDisasterReady(constMap<......
  • sqlserver 通过压缩bak文件实现从服务器还原数据库《数据差异数个小时》
    十年河东,十年河西,莫欺少年穷学无止境,精益求精1、备份主服务器数据库并压缩publicvoidDbBack(){varbakname=@"ChargeDB_"+DateTime.Now.ToString("yyyyMMdd")+".bak";stringfilepath=@"D:\dbback\"+bakna......
  • Ubuntu server 24 (Linux) 安装部署smartdns 搭建智能DNS服务器
    SmartDNS是推荐本地运行的DNS服务器,SmartDNS接受本地客户端的DNS查询请求,从多个上游DNS服务器获取DNS查询结果,并将访问速度最快的结果返回给客户端,提高网络访问速度和准确性。支持指定域名IP地址,达到禁止过滤的效果。一安装smartdns1 关闭Ubuntu自带dns解析systemd-resolv......
  • 【JS】JavaScript编程语言-谷歌浏览器调试之前端代码(2024-06-05)
    1、在浏览器中调试调试是指在一个脚本中找出并修复错误的过程。所有的现代浏览器和大多数其他环境都支持调试工具——开发者工具中的一个令调试更加容易的特殊用户界面。它也可以让我们一步步地跟踪代码以查看当前实际运行情况。在这里我们将会使用Chrome(谷歌浏览器)。2......