Cute Happy Ghost
본문 바로가기
QGIS·Geo

[openlayers,postgis] st_centroid 함수를 이용하여 center 설정하기,

by JENN_tech7 2022. 6. 21.
728x90
SMALL

postgis에 내장된 st_centroid 함수를 사용해 각 geom의 센터를 구할 수 있다

 <select id="getCenterBySgg"  resultType="java.util.HashMap" >
    select st_x(st_centroid(geom)) as st_cent_x, st_y(st_centroid(geom)) as st_cent_y 
    from layer_sgg_edge 
    <trim prefix="WHERE" prefixOverrides="and">
        <if test="ctprvn_cd != null and ctprvn_cd != ''">
            AND ctprvn_cd = #{ctprvn_cd}::varchar
        </if>
        <if test="sig_cd != null and sig_cd != ''">
            AND sig_cd = #{sig_cd}::varchar
        </if>
    </trim>

 

 

ajax를 통해서 값을 가져온 다음 설정좌표대로 transform을 하여 변환해주고

map.getView().setCenter를 사용하여 center을 설정해준다

$.ajax({ 
        url: "/living/getCenterBySgg.do",
        data: {ctprvn_cd: sidoValue},
        dataType : 'json',
        type: 'post',
        success: function(result){
            console.log([result[0].st_cent_x, result[0].st_cent_y]);
            map.getView().setCenter(ol.proj.transform([result[0].st_cent_x, result[0].st_cent_y], 'EPSG:4326', 'EPSG:3857'));

        },
        error : function(err){
            console.log("error");
        }
    });

 

 

 

고정되어있는 지도라면 setCenter후 setZoom을 해주면 된다

하지만 나같은 경우는 사이즈가 고정되어있지않고 그때그때 해당영역을 불러와야되서 센터를 설정해주는 것만으로는 안됐다

그래서 추후에는 아예 영역을 가져와서 고정시켜주는 소스로 변환하게된다

그 소스는 다음 글에서..

728x90
LIST

댓글