GIS
[GIS] ST_AsGeoJSON함수를 이용하여 공간정보를 JSON타입으로 변환해보자.
모찌바라기
2022. 5. 21. 14:40
728x90
반응형
테이블에 공간정보가 있고, 그 공간정보를 JSON형태로 가져오고 싶을 때 쓸 수 있는
ST_AsGeoJSON 함수에 대해 정리해보겠다. 샘플테이블은 링크에서 생성한 뷰테이블을 쓰도록 하겠다.
기본문법
text ST_AsGeoJSON(record feature, text geomcolumnname, integer maxdecimaldigits=9, boolean pretty_bool=false);
text ST_AsGeoJSON(geometry geom, integer maxdecimaldigits=9, integer options=8);
text ST_AsGeoJSON(geography geog, integer maxdecimaldigits=9, integer options=0);
Postgres에서 제공하는 문서에서는 위와 같이 기본문법을 소개하고 있다. 하지만 보통 자주 쓰는 건 그냥
geom만 넣는 것.. 아래 샘플코드를 보도록 하자.
샘플코드
SELECT ST_AsGeoJSON(geom) as geom from new_geo_table;
이렇게 JSON형태로 가져오게 된다. 이렇게 가져온 JSON데이터를 객체로 받아서 View에서 쓸 곳에 쓰면 되겠다.
728x90
반응형