Skip to content
Permalink
Browse files
Test improvements - moved writeShape to utilities for DRY. Added clip…
… test to geometry python tests.
  • Loading branch information
timlinux committed Nov 28, 2012
1 parent 9f71d9f commit c98967360e0ab9d502d7ec2a24d75a3c4c9eec8d
@@ -13,6 +13,9 @@
__revision__ = '$Format:%H$'

import os

from PyQt4.QtCore import QVariant

from qgis.core import (QgsGeometry,
QgsVectorLayer,
QgsFeature,
@@ -23,7 +26,8 @@
TestCase,
unittest,
expectedFailure,
unitTestDataPath)
unitTestDataPath,
writeShape)
# Convenience instances in case you may need them
# not used in this test

@@ -241,6 +245,107 @@ def testSimplifyIssue4189(self):
myMinimumLength = len('POLYGON(())')
assert myEndLength > myMinimumLength, myMessage

def testClipping(self):
"""Test that we can clip geometries using other geometries."""
"""Check we can write a vector file."""
myMemoryLayer = QgsVectorLayer(
('LineString?crs=epsg:4326&field=name:string(20)&index=yes'),
'clip-in',
'memory')

assert myMemoryLayer is not None, 'Provider not initialised'
myProvider = myMemoryLayer.dataProvider()
assert myProvider is not None

myFeature1 = QgsFeature()
myFeature1.setGeometry(QgsGeometry.fromPolyline([
QgsPoint(10,10),
QgsPoint(20,10),
QgsPoint(30,10),
QgsPoint(40,10),
]
))
myFeature1.setAttributeMap({0 : QVariant('Johny')})

myFeature2 = QgsFeature()
myFeature2.setGeometry(QgsGeometry.fromPolyline([
QgsPoint(10,10),
QgsPoint(20,20),
QgsPoint(30,30),
QgsPoint(40,40),
]
))
myFeature2.setAttributeMap({0 : QVariant('Be')})

myFeature3 = QgsFeature()
myFeature3.setGeometry(QgsGeometry.fromPolyline([
QgsPoint(10,10),
QgsPoint(10,20),
QgsPoint(10,30),
QgsPoint(10,40),
]
))

myFeature3.setAttributeMap({0 : QVariant('Good')})

myResult, myFeatures = myProvider.addFeatures(
[myFeature1, myFeature2, myFeature3])
assert myResult == True
assert len(myFeatures) == 3

myClipPolygon = QgsGeometry.fromPolygon([[
QgsPoint(20,20),
QgsPoint(20,30),
QgsPoint(30,30),
QgsPoint(30,20),
QgsPoint(20,20),
]]
)
print 'Clip: %s' % myClipPolygon.exportToWkt()
writeShape(myMemoryLayer, 'clipGeometryBefore.shp')
myProvider.rewind()
myProvider.select(myProvider.attributeIndexes())
myFeatures = []
myFeature = QgsFeature()
while myProvider.nextFeature(myFeature):
myGeometry = myFeature.geometry()
if myGeometry.intersects(myClipPolygon):
# Adds nodes where the clip and the line intersec
myCombinedGeometry = myGeometry.combine(myClipPolygon)
# Gives you the areas inside the clip
mySymmetricalGeometry = myGeometry.symDifference(
myCombinedGeometry)
# Gives you areas outside the clip area
myDifferenceGeometry = myCombinedGeometry.difference(
myClipPolygon)
#print 'Original: %s' % myGeometry.exportToWkt()
#print 'Combined: %s' % myCombinedGeometry.exportToWkt()
#print 'Difference: %s' % myDifferenceGeometry.exportToWkt()
print 'Symmetrical: %s' % mySymmetricalGeometry.exportToWkt()

myExpectedWkt = 'LINESTRING(20.0 20.0, 30.0 30.0)'
# There should only be one feature that intersects this clip
# poly so this assertion should work.
self.assertEqual(myExpectedWkt,
mySymmetricalGeometry.exportToWkt())

myNewFeature = QgsFeature()
myNewFeature.setAttributeMap(myFeature.attributeMap())
myNewFeature.setGeometry(mySymmetricalGeometry)
myFeatures.append(myNewFeature)

myNewMemoryLayer = QgsVectorLayer(
('LineString?crs=epsg:4326&field=name:string(20)&index=yes'),
'clip-out',
'memory')
myNewProvider = myNewMemoryLayer.dataProvider()
myResult, myFeatures = myNewProvider.addFeatures(myFeatures)
self.assertTrue(myResult)
self.assertEqual(len(myFeatures), 1)
myNewMemoryLayer.commitChanges()

writeShape(myNewMemoryLayer, 'clipGeometryAfter.shp')

if __name__ == '__main__':
unittest.main()

@@ -75,28 +75,28 @@ def testAddFeatures(self):
f = QgsFeature()
provider.select()
while provider.nextFeature(f):
attrMap = f.attributeMap()
myMessage = ('Expected: %s\nGot: %s\n' %
attrMap = f.attributeMap()
myMessage = ('Expected: %s\nGot: %s\n' %
("Johny", str(attrMap[0].toString())))

assert str(attrMap[0].toString()) == "Johny", myMessage
assert str(attrMap[0].toString()) == "Johny", myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
myMessage = ('Expected: %s\nGot: %s\n' %
(20, attrMap[1].toInt()[0]))

assert attrMap[1].toInt()[0] == 20, myMessage
assert attrMap[1].toInt()[0] == 20, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
myMessage = ('Expected: %s\nGot: %s\n' %
(0.3, attrMap[2].toFloat()[0]))

assert (attrMap[0].toFloat()[0] - 0.3) < 0.0000001, myMessage
assert (attrMap[0].toFloat()[0] - 0.3) < 0.0000001, myMessage

geom = f.geometry()
geom = f.geometry()

myMessage = ('Expected: %s\nGot: %s\n' %
myMessage = ('Expected: %s\nGot: %s\n' %
("POINT(10.0 10.0)", str(geom.exportToWkt())))

assert str(geom.exportToWkt()) == "POINT(10.0 10.0)", myMessage
assert str(geom.exportToWkt()) == "POINT(10.0 10.0)", myMessage

def testFromUri(self):
"""Test we can construct the mem provider from a uri"""
@@ -18,17 +18,17 @@

from qgis.core import (QgsVectorLayer,
QgsFeature,
QgsField,
QgsGeometry,
QgsPoint,
QgsVectorFileWriter,
QgsCoordinateReferenceSystem)

from utilities import (unitTestDataPath,
from utilities import (#unitTestDataPath,
getQgisTestApp,
TestCase,
unittest,
expectedFailure
#expectedFailure,
writeShape
)
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

@@ -58,28 +58,7 @@ def testWrite(self):
assert myResult == True
assert len(myFeatures) > 0

myFileName = os.path.join(str(QDir.tempPath()), 'writetest.shp')
print myFileName
# Explicitly giving all options, not really needed but nice for clarity
myErrorMessage = QString()
myOptions = QStringList()
myLayerOptions = QStringList()
mySelectedOnlyFlag = False
mySkipAttributesFlag = False
myGeoCrs = QgsCoordinateReferenceSystem()
myGeoCrs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
myResult = QgsVectorFileWriter.writeAsVectorFormat(
self.mMemoryLayer,
myFileName,
'utf-8',
myGeoCrs,
'ESRI Shapefile',
mySelectedOnlyFlag,
myErrorMessage,
myOptions,
myLayerOptions,
mySkipAttributesFlag)
assert myResult == QgsVectorFileWriter.NoError
writeShape(self.mMemoryLayer, 'writetest.shp')

if __name__ == '__main__':
unittest.main()
@@ -15,7 +15,8 @@
import sys
from PyQt4 import QtGui, QtCore
from qgis.core import (QgsApplication,
QgsCoordinateReferenceSystem)
QgsCoordinateReferenceSystem,
QgsVectorFileWriter)
from qgis.gui import QgsMapCanvas
from qgis_interface import QgisInterface
import hashlib
@@ -159,4 +160,26 @@ def setCanvasCrs(theEpsgId, theOtfpFlag=False):
# Reproject all layers to WGS84 geographic CRS
CANVAS.mapRenderer().setDestinationCrs(myCrs)


def writeShape(theMemoryLayer, theFileName):
myFileName = os.path.join(str(QtCore.QDir.tempPath()), theFileName)
print myFileName
# Explicitly giving all options, not really needed but nice for clarity
myErrorMessage = QtCore.QString()
myOptions = QtCore.QStringList()
myLayerOptions = QtCore.QStringList()
mySelectedOnlyFlag = False
mySkipAttributesFlag = False
myGeoCrs = QgsCoordinateReferenceSystem()
myGeoCrs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
myResult = QgsVectorFileWriter.writeAsVectorFormat(
theMemoryLayer,
myFileName,
'utf-8',
myGeoCrs,
'ESRI Shapefile',
mySelectedOnlyFlag,
myErrorMessage,
myOptions,
myLayerOptions,
mySkipAttributesFlag)
assert myResult == QgsVectorFileWriter.NoError

0 comments on commit c989673

Please sign in to comment.