Outdated version of the documentation. Find the latest one here.

도형 다루기

Points, linestrings and polygons that represent a spatial feature are commonly referred to as geometries. In QGIS they are represented with the QgsGeometry class.

도형 한 개가 실제로는 단순(단일 영역, single-part) 도형의 집합인 경우가 종종 있습니다. 이런 도형을 다중 영역(multi-part) 도형이라고 합니다. 다중 영역 도형이 한 가지 유형의 단순 도형으로만 이루어져 있을 경우 다중 포인트, 다중 라인스트링, 다중 폴리곤이라 부릅니다. 예를 들어 여러 개의 섬으로 이루어진 국가라면 다중 폴리곤으로 표현할 수 있습니다.

도형의 좌표는 어떤 좌표계(CRS)라도 될 수 있습니다. 레이어에서 피처를 불러올 때, 해당 도형은 레이어의 좌표계를 따르는 좌표를 가지게 될 겁니다.

Description and specifications of all possible geometries construction and relationships are available in the OGC Simple Feature Access Standards for advanced details.

도형 작성

There are several options for creating a geometry:

  • 좌표로부터

    gPnt = QgsGeometry.fromPoint(QgsPoint(1,1))
    gLine = QgsGeometry.fromPolyline([QgsPoint(1, 1), QgsPoint(2, 2)])
    gPolygon = QgsGeometry.fromPolygon([[QgsPoint(1, 1), QgsPoint(2, 2),
                                        QgsPoint(2, 1)]])

    Coordinates are given using QgsPoint class.

    Polyline (Linestring) is represented by a list of points. Polygon is represented by a list of linear rings (i.e. closed linestrings). First ring is outer ring (boundary), optional subsequent rings are holes in the polygon.

    다중 영역 도형은 한 단계 심화됩니다. 다중 포인트는 포인트의 목록, 다중 라인스트링은 라인스트링의 목록, 다중 폴리곤은 폴리곤의 목록입니다.

  • WKT(well-known text)로부터

    gem = QgsGeometry.fromWkt("POINT(3 4)")
  • WKB(well-known binary)로부터

    >>> g = QgsGeometry()
    >>> wkb = '010100000000000000000045400000000000001440'.decode('hex')
    >>> g.fromWkb(wkb)
    >>> g.exportToWkt()
    'Point (42 5)'

도형에 접근

First, you should find out geometry type, wkbType() method is the one to use — it returns a value from QGis.WkbType enumeration

>>> gPnt.wkbType() == QGis.WKBPoint
>>> gLine.wkbType() == QGis.WKBLineString
>>> gPolygon.wkbType() == QGis.WKBPolygon
>>> gPolygon.wkbType() == QGis.WKBMultiPolygon

As an alternative, one can use type() method which returns a value from QGis.GeometryType enumeration. There is also a helper function isMultipart() to find out whether a geometry is multipart or not.

To extract information from geometry there are accessor functions for every vector type. How to use accessors

>>> gPnt.asPoint()
(1, 1)
>>> gLine.asPolyline()
[(1, 1), (2, 2)]
>>> gPolygon.asPolygon()
[[(1, 1), (2, 2), (2, 1), (1, 1)]]


The tuples (x,y) are not real tuples, they are QgsPoint objects, the values are accessible with x() and y() methods.

For multipart geometries there are similar accessor functions: asMultiPoint(), asMultiPolyline(), asMultiPolygon().

도형 관계계산 및 연산

QGIS uses GEOS library for advanced geometry operations such as geometry predicates (contains(), intersects(), ...) and set operations (union(), difference(), ...). It can also compute geometric properties of geometries, such as area (in the case of polygons) or lengths (for polygons and lines)

Here you have a small example that combines iterating over the features in a given layer and performing some geometric computations based on their geometries.

# we assume that 'layer' is a polygon layer
features = layer.getFeatures()
for f in features:
  geom = f.geometry()
  print "Area:", geom.area()
  print "Perimeter:", geom.length()

Areas and perimeters don’t take CRS into account when computed using these methods from the QgsGeometry class. For a more powerful area and distance calculation, the QgsDistanceArea class can be used. If projections are turned off, calculations will be planar, otherwise they’ll be done on the ellipsoid.

d = QgsDistanceArea()

print "distance in meters: ", d.measureLine(QgsPoint(10,10),QgsPoint(11,11))

QGIS에 포함되어 있는, 벡터 데이터를 분석하고 변환하는 데 사용할 수 있는 알고리즘들의 수많은 예시가 있습니다. 다음은 링크들은 그 가운데 몇몇 코드를 보여줍니다.

Additional information can be found in following sources: