PostGIS

3 minute read

PostGIS

QGIS
Enterprise GIS
Spatial Database
PostGIS

Spatial Datbase

환경설정

• 설치하기

PostGIS
PostgreSQL을 통해 PostGIS를 설치할 수 있습니다. SQL 편집기에 create extension postgis;를 입력하여 PostgreSQL extenstion에 PostGIS를 추가할 수 있으며, select postgis_full_version();를 통해 설정이 잘 되었는지 확인할 수 있습니다.

image

QGIS
공식 홈페이지에서 QGIS를 설치할 수 있습니다.

psql
psql은 postgres cli입니다.

pgAdmin
PostgreSQL의 GUI 환경으로, 공식 홈페이지에서 pgAdmin을 설치할 수 있습니다.

• Spatial Database 가져오기

postgis shapefile import/export manager window에서는 postgis shapefile import/export manager로 spatial database를 가져올 수 있습니다.

terminal shp2pgsql을 통해 database를 가져올 수 있습니다.

shp2pgsql -s [SRID] -W euc-kr [shp file] [table 이름]| psql -h [host] -u [user] -d [dbname]
shp2pgsql -s 4326 /Users/minho/Desktop/Coding-Study/sdb_data/BAEA_Nests.shp | psql -h localhost -p 5432 -U postgres -d sdb_course

Vector Gemoety Model

Point: (x, y) 또는 (x, y, z)
Line String: Point의 배열 (Opened)
Polygon: line string의 배열 (Closed)
Multi-part Gemometry: Multi Poligon, Multi Line String, Multi Polygon
Features: Geometry, Attributes(fields, proterties …)

Spatial Reference ID

SRID(Spatial Reference ID - 공간 참조 식별자)는 Coordinate Reference System(공간 참조 시스템)의 식별자입니다. SRID는 아래 특성을 가지고 있습니다.
🔎 select * from spatial_ref_sys로 SRID를 확인할 수 있습니다.
- Coordinate System
- Projection
- Zone
- Datum

Geometry / Geography

- Geometry
평면 기반이며 좌표시스템에 따라 달라집니다. 또한 2차원이기에 수식이 단순하며 많은 함수들을 가지고 있습니다. 하지만 공간이 넓어지면 정확도가 떨어지는 단점이 있습니다.

- Geography
구(지구)기반이며 하나의 좌표시스템(위도/경도)을 사용합니다. 또한 3차원이기에 수식이 복잡하며 적은 함수들을 가지고 있습니다. 하지만 정확도는 높다는 장점이 있습니다.

• Geometry Basic Function

- geometrytype
geometrytype은 geometry의 vector geometry type을 반환합니다.

select geometrytype(geom) from sdb

- st_coorddim
st_coorddim은 geometry의 좌표 차원을 반환합니다.

select st_coorddim(geom) from sdb

- st_dimension
st_dimension은 geometry의 위상 차원을 반환합니다.

select st_dimenstion(geom) from sdb

- st_srid
st_srid는 geometry의 srid를 반환합니다.

select st_srid(geom) from sdb

- st_iscollection
st_iscollection은 geometry가 collection(geometrycollection, compoundcurve, multi…)인지 ture/false로 반환합니다.

select st_iscollection(geom) from sdb

- st_numgeometries
st_numgeometries는 geometry의 갯수를 반환합니다. - st_numinteriorrings??

select st_numinteriorrings(geom) from sdb

- st_npoints
st_npoints는 geometry의 점 갯수를 반환합니다.

select st_numpoints(geom) from sdb

- st_issimple

select st_issimple(geom) from sdb

- st_isempty

select st_isempty(geom) from sdb

- st_isclosed

select st_isclosed(geom) from sdb

- st_isring

select st_isring(geom) from sdb

- st_isvalid
st_isvalid는 유효한 geometry인지 true/false를 반환합니다.

select st_isvalid(geom) from sdb

- st_isvalidreason
st_isvalidreason은 geometry의 근거를 반환합니다.

select st_isvalidreason(geom) from sdb

- st_setsrid

select st_setsrid(geom) from sdb

- st_transform

select st_transform(geom) from sdb

• Geometry Measurement Function

- st_length
st_length은 geometry의 거리를 반환합니다. 이때, geometry는 위도/경도의 2d cartesian length(degree)를 geography는 타원에서의 length(meter)를 계산합니다.

select st_length(geom) from sdb

- st_3dlength

- st_area
st_area은 geometry의 넓이를 반환합니다. 이때, geometry는 위도/경도의 2d cartesian area(degree^2)를 geography는 타원에서의 넓이(meter^2)를 계산합니다.

select st_isvalidreason(geom) from sdb

- st_distance

- st_3ddistance

- st_distancesphere

- st_3ddistancesphere

- st_closespoint

- st_shortesline

- st_maxdistance

- st_longgestdistance

• Geometry Coordinates Function

- st_asewkt
st_asewkt는 geometry를 sri와 함께 wkt(Well-Known Text)형태로 반환합니다.

select st_asewkt(geom) from sdb

- st_astext
st_astext는 geometry를 wkt형태로 반환합니다.

select st_astext(geom) from sdb

- st_ asgeojson
st_asgeojson은 geometry를 geojson형태로 반환합니다.

select st_asgeojson(geom) from sdb

FeatureCollection은 st_asgeojson과 json_agg를 통해 만들 수 있습니다.

select json_build_object(
  'type', 'FeatureCollection',
  'features', json_agg(ST_AsGeoJSON(t.*)::json)
)
from (
  values
    (1, 'one', 'POINT(1 1)'::geometry),
    (2, 'two', 'POINT(2 2)'),
    (3, 'three', 'POINT(3 3)')
) as t(id, name, geom);
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": { "type": "Point", "coordinates": [1, 1] },
      "properties": { "id": 1, "name": "one" }
    },
    {
      "type": "Feature",
      "geometry": { "type": "Point", "coordinates": [2, 2] },
      "properties": { "id": 2, "name": "two" }
    },
    {
      "type": "Feature",
      "geometry": { "type": "Point", "coordinates": [3, 3] },
      "properties": { "id": 3, "name": "three" }
    }
  ]
}

🔎 geojson은 위치정보를 표현하기 위한 표준형식입니다.
- st_ asgml st_asgml은 geometry를 GML(Geometry Markup Language)형태로 반환합니다.

select st_asgml(geom) from sdb

- st_ askml st_askml은 geometry를 KML(Keyhole Markup Language)형태로 반환합니다.

select st_askml(geom) from sdb

- st_ asmvt

- st_ z
st_x은 geometry(point)의 x좌표를 반환합니다.

select st_x(geom) from sdb

- st_ y
st_y은 geometry(point)의 y좌표를 반환합니다.

select st_y(geom) from sdb

- st_ z
st_z은 geometry(point)의 z좌표를 반환합니다.

select st_z(geom) from sdb

- st_ m

- st_ startpoint
st_startpoint는 geometry(line string)의 시작 점을 반환합니다.

select st_startpoint(geom) from sdb

- st_ endpoint
st_endpoint는 geometry(line string)의 끝점을 반환합니다.

select st_endpoint(geom) from sdb

- st_ pointn

- st_ lineinerpolatepoint

- st_ geometyn

- st_ exteriorring

- st_ interiorringn

• Geometry Relationship Function

- st_intersects

- st_disjoint

- st_contains

- st_covers

- st_within

- st_coveredby

- st_crosses

- st_dwithin

- st_dfullywithin

- st_equals

- st_overlaps

- st_touches

- st_relate

st_envelop

http://data.nsdi.go.kr/dataset

epsg(European Petroleum Survey Group) 좌표계를 관리하는 기관 4326

_.shp: geometry 정보(점, 선, 다각형) _.dbf: 각 geometry에 대한 속성정보 저장 _.shx: geometry와 속성(attributes) 정보 연결 _.prj: 좌표계 정보 저장

https://m.blog.naver.com/whentlr/220205257624

Updated: