개발공작소
728x90

 

 

 

 

이번에 라벨링 관련 문제를 보면서 진짜 이 방법 저 방법을 찾아보는데, 그러면서 알게 되거나 사용한 

유용한 메서드들을 하나하나 정리해보려고 한다.

 

 

가장 큰 면적을 가지는 폴리곤을 구하는 함수

 

 

어려울 거 없다. 그냥 아래 메서드의 인자로 Multi-Polygons 배열을 넣어주면 알아서 반복문을 돌리면서

가장 면적이 큰 폴리곤을 return 해준다. 물론 return해주는 곳의

" polyObj.length - 1 "에서 숫자를 바꿔만 주면 2번째로 큰값, 3번째로 큰값, 가장 작은값 등

마음대로 가져올 수 있다.

 

피쳐의 경우 아래와 같이 getPolygons()를 통해 폴리곤들을 인자로 넣어줄 수 있다.

feature.getGeometry().getPolygons()

 

또는 Layer에서는 다음과 같이 getPolygons()를 통해 폴리곤들을 인자로 넣어줄 수 있다.

Layer.getFeatures[피쳐길이].getGeometry().getPolygons()

 

아니면 그냥 배열을 넣어도 상관은 없다.

 

 

getMaxPoly 메서드

function getMaxPoly(polys) {
      var polyObj = [];
      //now need to find which one is the greater and so label only this
      for (var b = 0; b < polys.length; b++) {
        polyObj.push({ poly: polys[b], area: polys[b].getArea() });
      }
      polyObj.sort(function (a, b) { return a.area - b.area });

      return polyObj[polyObj.length - 1].poly;
 }

 

multipolygon.getPolygons().sort(function(polygon1, polygon2) {return polygon2.getArea() - polygon1.getArea();})[0]

 

 

 

해당 메서드는 스택 오버플로우에서 찾아서 가져왔다. 덕분에 편하게 면적이 가장 큰 폴리곤을 구할 수 있었다.

아래 Getting the bigger polygon of Multipolygon에서는 다른 방법으로도 가장 큰 면적의 폴리곤을 구하는

방법도 제시하고 있으니 참고 해볼만할 것 같다.

 

참조링크

 

How to show one label per multi-polygon in open layers 3?

 

How to show one label per multi-polygon in open layers 3?

I'm having an issue trying to figure out how to show one label per multipolygon in OL3. It currently shows the label for every polygon which is not ideal under any circumstance in this particular

stackoverflow.com

 

Getting the bigger polygon of Multipolygon

 

Getting the bigger polygon of Multipolygon

I have a Multipolygon that has 5 separate polygons and I want to get only the geometry of the biggest polygon. How can I get the largest polygon from the multipolygon?

gis.stackexchange.com

 

 

 

 

728x90
profile

개발공작소

@모찌바라기

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!