two-polygons.py

Script to illustrate problem - Calvin Hamilton, 2018-01-30 08:37 PM

Download (912 Bytes)

 
1
from qgis.core import (QgsFeature,
2
    QgsVectorLayer, QgsPoint, QgsFeature,
3
    QgsMapLayerRegistry)
4
    
5
poly1 = QgsVectorLayer("Polygon?crs=epsg:4326", "Polygon 1", "memory")
6
dp = poly1.dataProvider()
7

    
8
pts = [QgsPoint(-117.9031, 44.4169), QgsPoint(-103.86080, 44.700146), QgsPoint(-104.655656, 38.257599), QgsPoint(-117.9031, 44.4169)]
9
f = QgsFeature()
10
f.setGeometry(QgsGeometry.fromPolygon([pts]))
11
dp.addFeatures([f])
12
poly1.updateExtents()
13
QgsMapLayerRegistry.instance().addMapLayer(poly1)
14

    
15
poly2 = QgsVectorLayer("Polygon?crs=epsg:4326", "Polygon 2", "memory")
16
dp = poly2.dataProvider()
17

    
18
pts = [QgsPoint(-115.27676, 40.242037), QgsPoint(-101.32007, 41.387353), QgsPoint(-102.1608432, 33.314810), QgsPoint(-115.27676, 40.242037)]
19
f = QgsFeature()
20
f.setGeometry(QgsGeometry.fromPolygon([pts]))
21
dp.addFeatures([f])
22
poly2.updateExtents()
23
QgsMapLayerRegistry.instance().addMapLayer(poly2)