当前位置:网站首页>PostGIS learning

PostGIS learning

2022-07-07 23:48:00 Xiao He and Mufeng

It is estimated that they will be engaged in gis Related work ,postgis As a powerful gis The database still needs to be well studied . Although geometric data storage can be realized by any type of database , But it's just storage , Subsequent coordinate conversion 、 Spatial analysis and other functions should be realized one by one , It's bound to be very troublesome ,postgis These functions have been integrated , Record the learning process here , For later review .

One 、 install postgis

postgis yes postgresql An extension of the database , So install postgis You have to install postgresql Download this ), You can download the corresponding installation package according to your own system , The specific installation process is not explained , There are many tutorials online .

If your computer is equipped with docker, It is recommended to use postgis Of docker Mirror image , It can save the trouble of installation , Deployment and migration management are also more convenient .

docker pull postgis/postgis

function postgis Containers , Set container name your_container_name and postgres Access code for your_postgres_passwd

docker run -d --name your_container_name -P -p 5432:5432 -e POSTGRES_PASSWORD=your_postgres_passwd postgis/postgis

Want to use later postgis database , Just start this container .

Two 、 Create database

  1. establish postgres database test, For learning
create database test
  1. Get into test database , And increase postgis Expand
create extension postgis
  1. see postgis edition , If it is displayed by the version number, it indicates postgis Database created successfully
select postgis_full_version()

3、 ... and 、 Geometric data reading and writing

3.1 Test data preparation

choice WGS84 In a coordinate system 1 A little bit (120, 40, 500), Use EPSG The website gets the coordinates under the other two coordinate systems by coordinate conversion , Then use these two coordinates for reading and writing tests .
EPSG:3857 WGS 84 / Pseudo-Mercator The coordinates in the coordinate system are (13358338.90, 4865942.28, 500),EPSG:4586 New Beijing / Gauss-Kruger CM 117E The coordinates in the coordinate system are (756206.42, 4433921.00, 500)

3.2 Geometric type

3.2.1 OGC Format

postgis Database support OGC Standard spatial geometry data types , Its format is divided into WKTWell-Known Text) and WKBWell-Known Binary) Two kinds of , The former is text format , The latter is in binary format ,WKT The geometric type data in the format is shown below ( give an example , Not listed completely ), The coordinates of the same point are Space Separate , Coordinates of different points are comma Separate :
POINT(0 0) Two dimensional points
POINTZ(0 0 0) 3D point
POINTZM(0 0 0 0) Three dimensional points with attribute values
LINESTRING(0 0, 1 1, 1 2) 2D line
POLYGON((0 0, 4 0, 4 4, 0 4, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1)) Two dimensional polygon

3.2.2 postgis Format

postgis Yes OGC Has been extended , It is divided into EWKT and EWKB Two kinds of , These two types are mainly simplified in the representation of data , Such as WKT Express POINTPOINTZPOINTMPOINTZM Four forms ,EWKT only POINT and POINTM Two forms ,EWKT Of POINT It can represent two-dimensional or three-dimensional , Depends on the number of coordinates filled , and WKT The two and three dimensions of are based on whether the type name contains Z Distinguishable , The basic comparison is as follows

type WKTEWKT
Two dimensional points POINT(0 0)POINT(0 0)
3D point POINTZ(0 0 0)POINT(0 0 0)
Two dimensional points with attributes POINTM(0 0 0)POINTM(0 0 0)
3D points with attributes POINTZM(0 0 0 0)POINTM(0 0 0 0)

3.3 geometry keyword

postgis The database can create data tables with geometry type fields , The keyword is geometry, This field can store geometric type data such as points, lines and planes, as well as coordinate system parameters srid( It's actually EPSG The number of ). When the table is created , This field can be without parameters , At this time, fields can store different types of geometric data and coordinate system parameters ; You can also bring parameters , The format is (geomType, srid),geomType Represents the geometric data type ,srid Represents the projection parameter , For example, define a coordinate system as WGS84 Mercator projection The corresponding description of the 3D point type field of is (pointz,3857)( The type used when creating tables OGC The format of ).

3.4 Data table reading and writing of fixed geometry type and coordinate system parameters

If the data stored in the table is of the same coordinate system and geometric type , You can specify , In this way, there is no need to set the coordinate system during subsequent data insertion . Be careful : Tables with coordinate systems and types set will report errors when inserting other types of coordinate systems and geometric types .

3.4.1 Create a projection coordinate system of 3857 Geometric point table of

Table, test,id Is a self-increment field ,name Is the geometric data name ,geom For geometric data

create table test (id serial PRIMARY KEY, name VARCHAR(50), geom geometry(pointz, 3857))

3.4.2 Insert a record ,WKT Format input geom

st_geomfromtext Means to convert a string into WKT Format

insert into test (name, geom) values('point1', st_geomfromtext('pointz(13358338.90 4865942.28 500)'))

3.4.3 Insert a record ,EWKT Format input geom

st_geomfromewkt Means to convert a string into EWKT Format , It can be seen that the same data is WKT and EWKT The two different parsing formats are consistent after import .

insert into test (name, geom) values('point1', st_geomfromewkt('point(13358338.90 4865942.28 500)'))

 Insert picture description here

3.4.4 Query log ,WKT Format output geom

st_astext It means converting geometric data into WKT Format , You can see the result with POINTZ, The coordinates are consistent with the original

select name, st_astext(geom) from test

 Insert picture description here

3.4.5 Query log ,EWKT Format output geom

st_asewkt It means converting geometric data into EWKT Format , You can see the result with SRID and POINT, The coordinates are consistent with the original

select name, st_asewkt(geom) from test

 Insert picture description here

3.4.6 Query log ,GeoJSON Format output geom

st_asgeojson It means converting geometric data into JSON Format , You can see that the result is in the format of key value pairs , Compared with the first two values, it will be more convenient

select name, st_asgeojson(geom) from test

 Insert picture description here

3.4.7 Query log , take geom The switch to WGS84 Coordinate system , And WKT Format output

st_transform Indicates that the geometric data is transferred to the target coordinate system , The parameter format is ( Geometric data , Target coordinate system ), You can see the output coordinates and the coordinates before the conversion WGS84 The coordinates are the same ( decimal 8 There is error after bit )

select name, st_astext(st_transform(geom, 4326)) from test

 Insert picture description here

3.4.8 Query log , take geom The switch to WGS84 Coordinate system , And EWKT Format output

select name, st_asewkt(st_transform(geom, 4326)) from test

 Insert picture description here

3.4.9 Query log , take geom The switch to WGS84 Coordinate system , And JSON Format output

Comprehensive category , be based on st_transform and st_asgeojson It's easy to get WGS84 Longitude and latitude coordinates in the coordinate system , Convenient for subsequent visual rendering .

select name, st_asgeojson(st_transform(geom, 4326)) from test

 Insert picture description here

3.5 Data table reading and writing without geometry type and coordinate system parameters

If the data type or coordinate system stored in the data table is not fixed , You can not specify , Specify... When inserting data .

3.5.1 Create table

geometry Parameters need not be set

create table test2 (id serial PRIMARY KEY, name VARCHAR(50), geom geometry)

3.5.2 Insert a record ,WKT Format input geom

The coordinate system used is 3857 Insert a point with the coordinates of

insert into test2 (name, geom) values ('point1', st_geomfromewkt('srid=3857;point(13358338.90 4865942.28 500)'))

3.5.3 Insert a record ,EWKT Format input geom

The coordinate system used is 4586 Insert a line with the coordinates of , Point of line xy identical ,z Different , It can be seen that different geometry types and coordinate systems can be successfully imported

insert into test2 (name, geom) values ('linestring1', st_geomfromewkt('srid=4586;linestring(756206.42 4433921.00 500, 756206.42 4433921.00 600)'))

 Insert picture description here

3.5.4 Query log ,WKT Format output geom

select name, st_astext(geom) from test2

 Insert picture description here

3.5.5 Query log ,EWKT Format output geom

select name, st_asewkt(geom) from test2

 Insert picture description here

3.5.6 Query log ,GeoJSON Format output geom

select name, st_asgeojson(geom) from test2

 Insert picture description here

3.5.7 Query log , take geom The switch to WGS84 Coordinate system , And WKT Format output

select name, st_astext(st_transform(geom, 4326)) from test2

 Insert picture description here

3.5.8 Query log , take geom The switch to WGS84 Coordinate system , And EWKT Format output

select name, st_asewkt(st_transform(geom, 4326)) from test2

 Insert picture description here

3.5.9 Query log , take geom The switch to WGS84 Coordinate system , And JSON Format output

select name, st_asgeojson(st_transform(geom, 4326)) from test2

 Insert picture description here

Four 、 Spatial analysis of geometric data

To be updated

原网站

版权声明
本文为[Xiao He and Mufeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072107180718.html