38
PostGIS의 사례로 본 PostgreSQL 확장 2017. 11. 4 장병진 ([email protected])

[Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

Embed Size (px)

Citation preview

Page 1: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PostGIS의사례로본PostgreSQL 확장

2017. 11. 4

장병진 ([email protected])

Page 2: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

Spatial Extension. Why? How?

Page 3: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

가장대표적인 Spatial SQL

3

SELECT superhero.name FROM city, superheroWHERE ST_Contains(city.geom, superhero.geom)AND city.name = 'Gotham';

• city와 superhero 테이블사이에는 JOIN 가능한 Field가없다.

• 하지만, ‘공간’ 을통해연결성을가질수있다!

• 때문에공간적인연결을이용해 JOIN을할수있다.

출처: http://postgis.net/

Page 4: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

실용적인분석의예

1) 전국 3차의료기관별로최근린읍면동을산출하고

2) 각읍면동별로 3차의료기관까지도달가능한예상시간을구하여

3) 각읍면동별예상도달시간의각읍면동별인구수를곱한값을

4) 각시군구별로합산한값을구하여

5) 전국의시군구별 3차의료기관분포가얼마나합리적인지분석하라

4

출처: 세상물정의물리학, 학교와병원과커피숍의사정

Page 5: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

Spatial SQL 이란?

• 공간정보처리함수를이용할수있는보통 SQL이다.

• 공간객체(Geometry/Geography/Raster)가 BLOB으로저장된다.

• 공간데이터는 database의추가적인컬럼(타입)일뿐이다.

• 속성들은 database내의일반속성(컬럼)들과동일하게처리된다.

출처: PostGIS for Managers, Paul Ramsey

http://s3.cleverelephant.ca/2014-postgis-for-managers.pdf

Page 6: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

Spatial SQL의장점

6

“GIS in SQL”

–database 내에서쿼리를이용해데이터를조작하고조회

Shared Editing

–여러사람이동시에편집해도정합성보장

Performance and Scale

–대량의데이터와많은작업량을처리

–전통적인 GIS에서단계별데이터변환/저장시간을줄여줌

Page 7: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

Spatial Database 구성요소

7

Spatial data types

–Geometry(point, line, polygon 등)

Spatial indexing

–공간연산의효율적처리지원

Spatial functions,

–공간적속성과관계질의

–공간분석단위로직제공 참고: http://www.opengeospatial.org/standards/sfs

Page 8: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

Spatial Data Type

8

참고: https://en.wikipedia.org/wiki/Well-known_text

Page 9: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

Spatial Index

9

Page 10: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

Spatial Function

10

• ST_Length• ST_Area• ST_Distance• ST_Buffer• ST_Scale• ST_Rotate• ST_Transform• ST_Intersection• ST_Union• ST_Simpllify• ST_Convexhull• ST_AsText• ST_AsBinary• ST_FromText• A && B• A @ B• A ~ B• A <-> B• A <#> B• A |=| B• A <=> B참고: http://postgis.net/docs/manual-2.3/reference.html#Operators

Page 11: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

기능다양성

비용절감

Spatial DBMS 비교

출처: https://www.slideshare.net/gis_todd/postgis-and-spatial-sql

PostGISSpatialLite

Page 12: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

PostGIS Archtecture

12

출처: https://www.slideshare.net/SimeonNedkov/delft-postgis

PostgreSQL

PostGIS

Extension

GEOS

Proj4

LibXML2

LibLwGeom

GDAL

Page 13: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

PostGIS Archtecture

13

PostgreSQL

PostGIS

Extension

GEOS

Proj4

LibXML2

LibLwGeom

PostgreSQL은그냥 RDBMS가아니고 ORDBMS다.GDAL

출처: https://www.slideshare.net/SimeonNedkov/delft-postgis

Page 14: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

PostGIS Archtecture

14

PostgreSQL

PostGIS

Extension

GEOS

Proj4

LibXML2

LibLwGeom

PostGIS는 ORDBMS의특성을이용해 Native 수준으로Spatial SQL의요소인 Type, Index, Function 들을구현했다.

GDAL

출처: https://www.slideshare.net/SimeonNedkov/delft-postgis

Page 15: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

PostGIS Archtecture

15

PostgreSQL

PostGIS

Extension

GEOS

Proj4

LibXML2

LibLwGeom

Database를바로이용할수없는많은기능들을Sub Library 에서 Low Level로구현해효율을높였다.

GDAL

출처: https://www.slideshare.net/SimeonNedkov/delft-postgis

Page 16: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

PostGIS Archtecture

16

PostgreSQL

PostGIS

Extension

GEOS

Proj4

LibXML2

LibLwGeom

널리사용되는 C++기반공간정보라이브러리를이용해많은유용한고수준분석/처리기능을추가했다.

GDAL

출처: https://www.slideshare.net/SimeonNedkov/delft-postgis

Page 17: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

PostGIS Archtecture

17

PostgreSQL

PostGIS

Extension

GEOS

Proj4

LibXML2

LibLwGeom

좌표계변환라이브러리를붙여다양한표준및비표준좌표계를처리한다.

GDAL

출처: https://www.slideshare.net/SimeonNedkov/delft-postgis

Page 18: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

PostGIS Archtecture

18

PostgreSQL

PostGIS

Extension

GEOS

Proj4

LibXML2

LibLwGeom

XML 라이브러리를이용해GML, KML 등 XML 기반공간정보표준을다룬다.

GDAL

출처: https://www.slideshare.net/SimeonNedkov/delft-postgis

Page 19: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

PostGIS Archtecture

19

PostgreSQL

PostGIS

Extension

GEOS

Proj4

LibXML2

LibLwGeom

널리사용되는 C++기반 Raster GIS 라이브러리를이용해Raster 데이터처리와분석을가능하게했다.

GDAL

Page 20: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

Spatial SQL 실습

20

Page 21: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

먼저 SQL 워밍업~

21

SELECT*FROMliquor_licenses;

출처: https://www.slideshare.net/gis_todd/postgis-and-spatial-sql

Page 22: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

조건절을추가했고요.

22

SELECT*FROMliquor_licensesWHERElicense_ty='Tavern';

Page 23: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

함수도써볼까요?

23

SELECTCOUNT(establish)FROMliquor_licensesWHERElicense_ty='Brew Pub';

Page 24: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

종류별로분석도해보지요.

24

SELECTAVG(char_length(establish)),license_tyFROMliquor_licensesGROUP BYlicense_ty;

Page 25: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

표준편차같은것도분석해볼수있지요.

25

SELECTAVG(char_length(establish)),STDDEV(char_length(establish)),license_tyFROMliquor_licensesGROUP BYlicense_ty;

Page 26: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

드디어공간컬럼

26

SELECTST_AsText(geom)FROMliquor_licenses;

POINT (127.43 37.32)

Page 27: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

길이를구하는거야껌이지요~~~

27

SELECTST_LENGTH (geom) ,strnameFROMstreet_centerlines;

Page 28: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

헉! 사실은공간정보지식이필요해요ㅜ.ㅜ

28

도로길이총합이 1보다작아요~~~~

Page 29: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

미터단위의좌표계로변환해야하네요.

29

SELECTST_LENGTH(ST_TRANSFORM(geom,5179)),strnameFROMstreet_centerlines; • EPSG:5179

- 미터단위 TM 좌표계- 국토지리정보원온맵, 네이버지도에서사용중- 전국규모사용에적합

Page 30: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

이제면적도계산할수있어요.

30

SELECTobjectid,ST_AREA(ST_TRANSFORM(geom,5179))FROMbuildings;

Page 31: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

버… 버… 버퍼링알아요? – 공간컬럼추가

31

ALTER TABLEliquor_licensesADD Columnbuffergeometry(Polygon,4326)

• EPSG:4326- 도단위경위도좌표계- GPS에서오는좌표값과동일- 전세계적자료다룰때가장많이사용

Page 32: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

버… 버… 버퍼링알아요? – 공간컬럼에넣기

32

UPDATEliquor_licensesSETBuffer=ST_TRANSFORM(ST_BUFFER(ST_TRANSFORM(geom,5179),30),4326);

Page 33: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

IT의피인 JSON과 XML로변환

33

SELECT ST_AsGeoJSON(geom) FROM floodplain_city;SELECT ST_AsGML(geom) FROM floodplain_city;

{

"type": "Feature",

"geometry": {

"type": "Point",

"coordinates": [127.43 37.32]

},

"properties": {

"name": "Dinagat Islands"

}

}

<Feature>

<name>Dinagat Islands</name>

<position>

<gml:Point srsDimension="2"

srsName="http://www.opengis.net/def/crs/EPSG/0/

4326">

<gml:pos>127.43 37.32</gml:pos>

</gml:Point>

</position>

</Feature>

Page 34: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

선이몇개점으로그려졌는지쯤이야~

34

SELECTST_NPoints(geom)FROMstreet_centerlines;

Page 35: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

공간적으로걸치는것을이용해 JOIN을~

35

SELECTa.strname,a.gid,a.geom,a.leftadd1,a.leftadd2,a.rgtadd1,a.rgtadd2FROMstreet_centerlines a, floodplain_city bWHEREST_Crosses(a.geom,b.geom);

Page 36: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

한지역(공간) 안에서만분석하려면~

36

SELECT a.establish, a.license_ty,a.gid,a.geomFROM liquor_licenses a, zoning bWHERE st_within(a.geom, b.geom)ANDzone_name='LOW DENSITY MIXED-USE NEIGHBORHOOD DISTRICT';

Page 37: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

PgDay Seoul 2017

지역과타입으로분석

37

SELECTCOUNT(a.establish),a.license_ty, b.zone_name ,a.gid, a.geomFROMliquor_licenses a, zoning bWHEREST_WITHIN(a.geom,b.geom)GROUP BYb.zone_name, a.license_ty;

Page 38: [Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진

들어주셔서감사합니다~

38