def test_SagaVectorAlgorithmWithSelection(self): layer = processing.getobject(polygons2()); feature = layer.getFeatures().next() selected = [feature.id()] layer.setSelectedFeatures(selected) outputs=processing.runalg("saga:polygoncentroids",polygons2(),True,None) layer.setSelectedFeatures([]) output=outputs['CENTROIDS'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['ID','POLY_NUM_B','POLY_ST_B'] expectedtypes=['Real','Real','String'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(1, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["2","1","string a"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POINT(270806.69221918 4458924.97720492)' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_modeleremptystring(self): outputs = processing.runalg("modeler:emptystring", union(), None) output = outputs['OUTPUT_LAYER_ALG0'] layer = QGisLayers.getObjectFromUri(output, True) fields = layer.pendingFields() expectednames = [ 'ID', 'POLY_NUM_A', 'POLY_ST_A', 'ID_2', 'POLY_NUM_B', 'POLY_ST_B', 'NewField' ] expectedtypes = [ 'Integer', 'Real', 'String', 'Integer', 'Real', 'String', 'Integer' ] names = [str(f.name()) for f in fields] types = [str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features = processing.getfeatures(layer) self.assertEqual(8, len(features)) feature = features.next() attrs = feature.attributes() expectedvalues = ["1", "1.1", "string a", "2", "1", "string a", "10"] values = [str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt = 'POLYGON((270807.08580285 4458940.1594565,270798.42294527 4458914.62661676,270780.81854858 4458914.21983449,270763.52289518 4458920.715993,270760.3449542 4458926.6570575,270763.78234766 4458958.22561242,270794.30290024 4458942.16424502,270807.08580285 4458940.1594565))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self): '''this tests both the exporting to shp and then the format change in the output layer''' layer = processing.getobject(polygonsGeoJson()); feature = layer.getFeatures().next() selected = [feature.id()] layer.setSelectedFeatures(selected) outputs=processing.runalg("saga:polygoncentroids",polygonsGeoJson(),True, ProcessingUtils.getTempFilename("geojson")) layer.setSelectedFeatures([]) output=outputs['CENTROIDS'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['ID','POLY_NUM_A','POLY_ST_A'] expectedtypes=['Real','Real','String'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(1, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["0","1.1","string a"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POINT(270787.49991451 4458955.46775295)' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_featuresWithSelection(self): layer = processing.getobject(points()) feature = layer.getFeatures().next() selected = [feature.id()] layer.setSelectedFeatures(selected) features = processing.getfeatures(layer) self.assertEqual(1, len(features)) layer.setSelectedFeatures([])
def run_script(iface, **args): input_path = args['input_path'] output_path = args['output_path'] field = args['field'] processing.runalg('qgis:dissolve', input_path, False, field, output_path) layerName = os.path.splitext(os.path.basename(output_path))[0] layer = QgsVectorLayer(output_path, layerName, 'ogr') features = processing.getfeatures(layer) print "Number of categories: " + str(len(features)) for feature in features: print feature.attributes()
def test_qgiscountpointsinpolygon(self): outputs=processing.runalg("qgis:countpointsinpolygon",polygons(),points(),"NUMPOINTS", self.getOutputFile()) output=outputs['OUTPUT'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['ID','POLY_NUM_A','POLY_ST_A','NUMPOINTS'] expectedtypes=['Integer','Real','String','Real'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(2, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["1","1.1","string a","6"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values)
def testWrongformat(self): outputs=processing.runalg("qgis:countpointsinpolygon",polygons(),points(),"NUMPOINTS",ProcessingUtils.getTempFilename("wrongext")) output=outputs['OUTPUT'] self.assertTrue(output.endswith('shp')) layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['ID','POLY_NUM_A','POLY_ST_A','NUMPOINTS'] expectedtypes=['Integer','Real','String','Real'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(2, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["1","1.1","string a","6.0"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values)
def test_scripthexgridfromlayerbounds(self): outputs=processing.runalg("script:hexgridfromlayerbounds",polygons(),10,None) output=outputs['grid'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['longitude','latitude'] expectedtypes=['Real','Real'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(117, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["270765.621834001","4458907.27146471"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POLYGON((270771.39533669 4458907.27146471,270768.50858535 4458902.27146471,270762.73508265 4458902.27146471,270759.84833131 4458907.27146471,270762.73508265 4458912.27146471,270768.50858535 4458912.27146471,270771.39533669 4458907.27146471))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_scriptcreatetilingfromvectorlayer(self): outputs=processing.runalg("script:createtilingfromvectorlayer",union(),10,None) output=outputs['polygons'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['longitude','latitude'] expectedtypes=['Real','Real'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(10, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["270761.415396242","4458948.29588823"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POLYGON((270755.54427424 4458901.23378639,270755.54427424 4458995.35799007,270767.28651824 4458995.35799007,270767.28651824 4458901.23378639,270755.54427424 4458901.23378639))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_modeleroptionalfield(self): outputs = processing.runalg("modeler:optionalfield", points(), None) output = outputs['OUTPUT_ALG0'] layer = QGisLayers.getObjectFromUri(output, True) fields = layer.pendingFields() expectednames = ['id', 'value', 'area', 'perim'] expectedtypes = ['Integer', 'String', 'Real', 'Real'] names = [str(f.name()) for f in fields] types = [str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features = processing.getfeatures(layer) self.assertEqual(1, len(features)) feature = features.next() attrs = feature.attributes() expectedvalues = ["0", "all", "3592.818848", "230.989919"] values = [str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt = 'POLYGON((270839.46818665 4458921.97813894,270778.60197966 4458935.96883677,270786.54279065 4458980.04784113,270803.15756434 4458983.84880322,270839.65586926 4458983.16267036,270855.74530134 4458940.79948673,270839.46818665 4458921.97813894))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_gdalogrogr2ogrWrongExtension(self): outputs=processing.runalg("gdalogr:ogr2ogr",union(),3,"",ProcessingUtils.getTempFilename("wrongext")) output=outputs['OUTPUT_LAYER'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['id','poly_num_a','poly_st_a','id_2','poly_num_b','poly_st_b'] expectedtypes=['Integer','Real','String','Integer','Real','String'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(8, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["1","1.1","string a","2","1","string a"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POLYGON((270807.08580285 4458940.1594565,270798.42294527 4458914.62661676,270780.81854858 4458914.21983449,270763.52289518 4458920.715993,270760.3449542 4458926.6570575,270763.78234766 4458958.22561242,270794.30290024 4458942.16424502,270807.08580285 4458940.1594565))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_modelersagagrass(self): outputs = processing.runalg("modeler:sagagrass", points(), None) output = outputs['CENTROIDS_ALG1'] layer = QGisLayers.getObjectFromUri(output, True) fields = layer.pendingFields() expectednames = ['CAT'] expectedtypes = ['Real'] names = [str(f.name()) for f in fields] types = [str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features = processing.getfeatures(layer) self.assertEqual(12, len(features)) feature = features.next() attrs = feature.attributes() expectedvalues = ["1"] values = [str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt = 'POINT(270839.65586926 4458983.16267036)' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_modeleremptystring(self): outputs=processing.runalg("modeler:emptystring",union(),None) output=outputs['OUTPUT_LAYER_ALG0'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['ID','POLY_NUM_A','POLY_ST_A','ID_2','POLY_NUM_B','POLY_ST_B','NewField'] expectedtypes=['Integer','Real','String','Integer','Real','String','Integer'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(8, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["1","1.1","string a","2","1","string a","10"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POLYGON((270807.08580285 4458940.1594565,270798.42294527 4458914.62661676,270780.81854858 4458914.21983449,270763.52289518 4458920.715993,270760.3449542 4458926.6570575,270763.78234766 4458958.22561242,270794.30290024 4458942.16424502,270807.08580285 4458940.1594565))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_modeleroptionalfield(self): outputs=processing.runalg("modeler:optionalfield",points(),None) output=outputs['OUTPUT_ALG0'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['id','value','area','perim'] expectedtypes=['Integer','String','Real','Real'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(1, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["0","all","3592.818848","230.989919"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POLYGON((270839.46818665 4458921.97813894,270778.60197966 4458935.96883677,270786.54279065 4458980.04784113,270803.15756434 4458983.84880322,270839.65586926 4458983.16267036,270855.74530134 4458940.79948673,270839.46818665 4458921.97813894))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_modelersagagrass(self): outputs=processing.runalg("modeler:sagagrass",points(),None) output=outputs['CENTROIDS_ALG1'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['CAT'] expectedtypes=['Real'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(12, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["1"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POINT(270839.65586926 4458983.16267036)' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_scripthexgridfromlayerbounds(self): outputs = processing.runalg("script:hexgridfromlayerbounds", polygons(), 10, None) output = outputs['grid'] layer = QGisLayers.getObjectFromUri(output, True) fields = layer.pendingFields() expectednames = ['longitude', 'latitude'] expectedtypes = ['Real', 'Real'] names = [str(f.name()) for f in fields] types = [str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features = processing.getfeatures(layer) self.assertEqual(117, len(features)) feature = features.next() attrs = feature.attributes() expectedvalues = ["270765.621834001", "4458907.27146471"] values = [str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt = 'POLYGON((270771.39533669 4458907.27146471,270768.50858535 4458902.27146471,270762.73508265 4458902.27146471,270759.84833131 4458907.27146471,270762.73508265 4458912.27146471,270768.50858535 4458912.27146471,270771.39533669 4458907.27146471))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_scriptcreatetilingfromvectorlayer(self): outputs = processing.runalg("script:createtilingfromvectorlayer", union(), 10, None) output = outputs['polygons'] layer = QGisLayers.getObjectFromUri(output, True) fields = layer.pendingFields() expectednames = ['longitude', 'latitude'] expectedtypes = ['Real', 'Real'] names = [str(f.name()) for f in fields] types = [str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features = processing.getfeatures(layer) self.assertEqual(10, len(features)) feature = features.next() attrs = feature.attributes() expectedvalues = ["270761.415396242", "4458948.29588823"] values = [str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt = 'POLYGON((270755.54427424 4458901.23378639,270755.54427424 4458995.35799007,270767.28651824 4458995.35799007,270767.28651824 4458901.23378639,270755.54427424 4458901.23378639))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
hausdorff = max(distances) return hausdorff origin_layer = processing.getobject(origin_layer) target_layer = processing.getobject(target_layer) target_id_column_index = target_layer.fieldNameIndex(target_id_column_index) """ origin_layer = l1 target_layer = l2 target_id_column_index = 0 interval = 1 """ target_spatial_index = QgsSpatialIndex() target_features = processing.getfeatures(target_layer) origin_fields = origin_layer.pendingFields().toList() origin_fields.append( QgsField("BEST_FIT", QVariant.Int )) origin_fields.append( QgsField("HAUSDORFF", QVariant.Double )) origin_fields.append( QgsField("LEN_DIFF", QVariant.Double )) writer = VectorWriter(output, None, origin_fields, origin_layer.dataProvider().geometryType(), origin_layer.crs() ) outFeat = QgsFeature() # populate the spatial index for feat in target_features: target_spatial_index.insertFeature(feat) origin_features = processing.getfeatures(origin_layer) for origin_feature in origin_features:
hausdorff = max(distances) return hausdorff origin_layer = processing.getobject(origin_layer) target_layer = processing.getobject(target_layer) target_id_column_index = target_layer.fieldNameIndex(target_id_column_index) """ origin_layer = l1 target_layer = l2 target_id_column_index = 0 interval = 1 """ target_spatial_index = QgsSpatialIndex() target_features = processing.getfeatures(target_layer) origin_fields = origin_layer.pendingFields().toList() origin_fields.append(QgsField("BEST_FIT", QVariant.Int)) origin_fields.append(QgsField("HAUSDORFF", QVariant.Double)) origin_fields.append(QgsField("LEN_DIFF", QVariant.Double)) writer = VectorWriter(output, None, origin_fields, origin_layer.dataProvider().geometryType(), origin_layer.crs()) outFeat = QgsFeature() # populate the spatial index for feat in target_features: target_spatial_index.insertFeature(feat)
def test_featuresWithoutSelection(self): layer = processing.getobject(points()) features = processing.getfeatures(layer) self.assertEqual(12, len(features))