当前位置:网站首页>postgis学习
postgis学习
2022-07-07 21:52:00 【小何又沐风】
以后估计会长期从事gis相关的工作,postgis作为一款功能比较强大的gis数据库还是需要好好学习一下的。虽然几何数据存储用任何一种类型的数据库都可以自行实现,但也仅仅只是存储,后续的坐标转换、空间分析等功能如果都要一一自己实现,那势必非常麻烦,postgis已经集成了这些功能,在此记录一下学习过程,便于以后回顾。
一、安装postgis
postgis是postgresql数据库的一个扩展,所以安装postgis前必须先安装postgresql(点此下载),可根据自己的系统下载对应的安装包,具体安装过程就不说明了,网上有很多教程。
如果你的电脑装有docker,则推荐使用postgis的docker镜像,可以省去安装的麻烦,部署迁移管理也都更加方便。
docker pull postgis/postgis
运行postgis容器,设置容器名称your_container_name和postgres的访问密码your_postgres_passwd
docker run -d --name your_container_name -P -p 5432:5432 -e POSTGRES_PASSWORD=your_postgres_passwd postgis/postgis
以后想要使用postgis数据库,只要启动此容器就行了。
二、创建数据库
- 创建
postgres数据库test,用于学习
create database test
- 进入
test数据库,并增加postgis扩展
create extension postgis
- 查看
postgis版本,如果由版本号显示则说明postgis数据库创建成功
select postgis_full_version()
三、几何数据读写
3.1 测试数据准备
选择WGS84坐标系下的1个点(120, 40, 500),使用EPSG网站进行坐标转换得到另外两个坐标系下的坐标,后面就用这两个坐标进行读写测试。EPSG:3857 WGS 84 / Pseudo-Mercator坐标系下的坐标为(13358338.90, 4865942.28, 500),EPSG:4586 New Beijing / Gauss-Kruger CM 117E 坐标系下的坐标为(756206.42, 4433921.00, 500)
3.2 几何类型
3.2.1 OGC格式
postgis数据库支持OGC标准的空间几何数据类型,其格式分WKT(Well-Known Text)和WKB(Well-Known Binary)两种,前者是文本格式,后者是二进制格式,WKT格式的几何类型数据如下所示(举例,未列全),同一点坐标用空格分隔,不同点坐标用逗号分隔:POINT(0 0) 二维点POINTZ(0 0 0) 三维点POINTZM(0 0 0 0) 三维带属性值的点LINESTRING(0 0, 1 1, 1 2) 二维线POLYGON((0 0, 4 0, 4 4, 0 4, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1)) 二维多边形
3.2.2 postgis格式
postgis对OGC的格式进行了扩展,分为EWKT和EWKB两种,这两种类型主要在数据的表示上进行了简化,如WKT表示有POINT、POINTZ、POINTM、POINTZM四种形式,EWKT则只有POINT和POINTM两种形式,EWKT的POINT可以表示二维或三维,取决于填写的坐标数量,而WKT的二维和三维是根据类型名是否含Z来区分的,其基本对比如下
| 类型 | WKT | EWKT |
|---|---|---|
| 二维点 | POINT(0 0) | POINT(0 0) |
| 三维点 | POINTZ(0 0 0) | POINT(0 0 0) |
| 二维带属性点 | POINTM(0 0 0) | POINTM(0 0 0) |
| 三维带属性点 | POINTZM(0 0 0 0) | POINTM(0 0 0 0) |
3.3 geometry关键字
postgis数据库可以创建带有几何类型字段的数据表,其关键字为geometry,此字段可以存储点线面等几何类型数据以及坐标系参数srid(实际上是EPSG的编号)。在创建表时,该字段可以不带参数,此时字段可以存储不同类型的几何数据和坐标系参数;也可以带上参数,格式为(geomType, srid),geomType表示几何数据类型,srid表示投影参数,如定义一个坐标系为WGS84墨卡托投影的三维点类型字段对应的描述为(pointz,3857)(建表时的类型得用OGC的格式)。
3.4 固定几何类型和坐标系参数的数据表读写
如果表里存储的数据都是相同坐标系和几何类型的,可以在建表时指定,这样在后续的数据插入时就不需要再设置坐标系了。注意:设置了坐标系和类型的表在插入其他类型的坐标系和几何类型时会报错。
3.4.1 创建投影坐标系为3857的几何点表
表名为test,id为自增字段,name为几何数据名称,geom为几何数据
create table test (id serial PRIMARY KEY, name VARCHAR(50), geom geometry(pointz, 3857))
3.4.2 插入一条记录,WKT格式输入geom
st_geomfromtext表示把字符串转化为WKT格式
insert into test (name, geom) values('point1', st_geomfromtext('pointz(13358338.90 4865942.28 500)'))
3.4.3 插入一条记录,EWKT格式输入geom
st_geomfromewkt表示把字符串转化为EWKT格式,可以看出相同的数据在WKT和EWKT两种不同的解析格式导入之后还是一致的。
insert into test (name, geom) values('point1', st_geomfromewkt('point(13358338.90 4865942.28 500)'))
3.4.4 查询记录,WKT格式输出geom
st_astext表示把几何数据转化为WKT格式,可以看到结果里带着POINTZ,坐标与原来一致
select name, st_astext(geom) from test
3.4.5 查询记录,EWKT格式输出geom
st_asewkt表示把几何数据转化为EWKT格式,可以看到结果里带着SRID和POINT,坐标与原来一致
select name, st_asewkt(geom) from test
3.4.6 查询记录,GeoJSON格式输出geom
st_asgeojson表示把几何数据转化为JSON格式,可以看到结果为键值对格式,相比较前两种取值会更方便些
select name, st_asgeojson(geom) from test
3.4.7 查询记录,将geom转换到WGS84坐标系,并以WKT格式输出
st_transform表示将几何数据转到目标坐标系,参数格式为(几何数据,目标坐标系),可以看到输出的坐标与转换前的WGS84坐标一致(小数8位后有误差)
select name, st_astext(st_transform(geom, 4326)) from test
3.4.8 查询记录,将geom转换到WGS84坐标系,并以EWKT格式输出
select name, st_asewkt(st_transform(geom, 4326)) from test
3.4.9 查询记录,将geom转换到WGS84坐标系,并以JSON格式输出
综合类看,基于st_transform和st_asgeojson可以方便地得到WGS84坐标系下的经纬度坐标,便于后续可视化绘制。
select name, st_asgeojson(st_transform(geom, 4326)) from test
3.5 无几何类型和坐标系参数的数据表读写
如果数据表存储的数据类型或坐标系不固定,则可以在建表时不指定,在插入数据时指定。
3.5.1 创建表
geometry参数不用设置
create table test2 (id serial PRIMARY KEY, name VARCHAR(50), geom geometry)
3.5.2 插入一条记录,WKT格式输入geom
使用坐标系为3857的坐标插入一个点
insert into test2 (name, geom) values ('point1', st_geomfromewkt('srid=3857;point(13358338.90 4865942.28 500)'))
3.5.3 插入一条记录,EWKT格式输入geom
使用坐标系为4586的坐标插入一条线,线的点xy相同,z不同,可以看出不同几何类型和坐标系可以成功导入
insert into test2 (name, geom) values ('linestring1', st_geomfromewkt('srid=4586;linestring(756206.42 4433921.00 500, 756206.42 4433921.00 600)'))
3.5.4 查询记录,WKT格式输出geom
select name, st_astext(geom) from test2
3.5.5 查询记录,EWKT格式输出geom
select name, st_asewkt(geom) from test2
3.5.6 查询记录,GeoJSON格式输出geom
select name, st_asgeojson(geom) from test2

3.5.7 查询记录,将geom转换到WGS84坐标系,并以WKT格式输出
select name, st_astext(st_transform(geom, 4326)) from test2
3.5.8 查询记录,将geom转换到WGS84坐标系,并以EWKT格式输出
select name, st_asewkt(st_transform(geom, 4326)) from test2
3.5.9 查询记录,将geom转换到WGS84坐标系,并以JSON格式输出
select name, st_asgeojson(st_transform(geom, 4326)) from test2
四、几何数据空间分析
待更新
边栏推荐
- Have all the fresh students of 2022 found jobs? Is it OK to be we media?
- USB (XVIII) 2022-04-17
- USB (XVI) 2022-04-28
- Unity3d Learning Notes 6 - GPU instantiation (1)
- B / Qurt Utilisateur Guide (36)
- windows设置redis开启自动启动
- 产业共融新势能,城链科技数字峰会厦门站成功举办
- System design overview
- 【7.5】15. Sum of three numbers
- Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
猜你喜欢

LM12丨Rolling Heikin Ashi二重K线滤波器

B_QuRT_User_Guide(38)

Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
![给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」

Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched

0-1 knapsack problem

【实验分享】通过Console口登录到Cisco设备

Lm12 rolling heikin Ashi double K-line filter

C inheritance and interface design polymorphism

List. How to achieve ascending and descending sort() 2020.8.6
随机推荐
648. Word replacement
Caip2021 preliminary VP
[untitled]
Markdown
谷歌浏览器怎么登录及开启同步功能
USB (XIV) 2022-04-12
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
List. How to achieve ascending and descending sort() 2020.8.6
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
【汇总】看过的一些Panel与视频
Windows set redis to start automatically
【7.4】25. K 个一组翻转链表
First week of July
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
Dependency injection 2 advantage lifecycle
POJ2392 SpaceElevator [DP]
2022第六季完美童模陕西总决赛圆满落幕
Anxin vb01 offline voice module access intelligent curtain guidance
Count the top 10 films at the box office and save them in another file
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process