20.07.2013 Views

PyQGIS Documentation - Linfiniti Geo Blog

PyQGIS Documentation - Linfiniti Geo Blog

PyQGIS Documentation - Linfiniti Geo Blog

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER<br />

FIVE<br />

GEOMETRY HANDLING<br />

Points, linestrings, polygons (and more complex shapes) are commonly referred to as geometries. They are represented<br />

using Qgs<strong>Geo</strong>metry class.<br />

To extract information from geometry there are accessor functions for every vector type. How do accessors work:<br />

asPoint() returns QgsPoint<br />

asPolyline() returns a list of QgsPoint items<br />

asPolygon() returns a list of rings, every ring consists of a list of QgsPoint items. First ring is outer, subsequent<br />

rings are holes.<br />

You can use Qgs<strong>Geo</strong>metry.isMultipart() to find out whether the feature is multipart. For multipart features<br />

there are similar accessor functions: asMultiPoint(), asMultiPolyline(), asMultiPolygon().<br />

There are several options how to create a geometry:<br />

• from coordinates:<br />

gPnt = Qgs<strong>Geo</strong>metry.fromPoint(QgsPoint(1,1))<br />

gLine = Qgs<strong>Geo</strong>metry.fromPolyline( [ QgsPoint(1,1), QgsPoint(2,2) ] )<br />

gPolygon = Qgs<strong>Geo</strong>metry.fromPolygon( [ [ QgsPoint(1,1), QgsPoint(2,2), QgsPoint(2,1) ] ] )<br />

• from well-known text (WKT):<br />

gem = Qgs<strong>Geo</strong>metry.fromWkt("POINT (3 4)")<br />

• from well-known binary (WKB):<br />

g = Qgs<strong>Geo</strong>metry()<br />

g.setWkbAndOwnership(wkb, len(wkb))<br />

21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!