Ejemplo n.º 1
0
def test_option_algorithms():
    """ Execute a simple choice  algorithm
    """
    alg = _find_algorithm('pyqgiswps_test:testoptionvalue')

    context = QgsProcessingContext()
    feedback = QgsProcessingFeedback()

    inputs = {
        p.name(): [parse_input_definition(p)]
        for p in alg.parameterDefinitions()
    }
    outputs = {
        p.name(): parse_output_definition(p)
        for p in alg.outputDefinitions()
    }

    inputs['INPUT'][0].data = 'value1'

    parameters = dict(
        input_to_processing(ident, inp, alg, context)
        for ident, inp in inputs.items())

    assert parameters['INPUT'] == 0

    # Run algorithm
    results = run_algorithm(alg,
                            parameters=parameters,
                            feedback=feedback,
                            context=context,
                            outputs=outputs)

    assert results['OUTPUT'] == 'selection is 0'
    assert outputs['OUTPUT'].data == "selection is 0"
Ejemplo n.º 2
0
def test_geometry_script(outputdir, data):
    """ Test geometry script
    """
    alg = _find_algorithm('script:testinputgeometry')

    inputs  = { p.name(): [parse_input_definition(p)] for p in  alg.parameterDefinitions() }
    outputs = { p.name(): parse_output_definition(p) for p in  alg.outputDefinitions() }
   
    inp  = inputs['INPUT'][0]
    inp.data_format = Format.from_definition(FORMATS.WKT)
    inp.data = 'CRS=EPSG:4326;MULTIPOINT((3.5 5.6), (4.8 10.5))'

    # Load source project
    source = QgsProject()
    rv = source.read(str(data/'france_parts.qgs'))
    assert rv == True

    context  = Context(source, outputdir)
    feedback = QgsProcessingFeedback() 

    parameters = dict( input_to_processing(ident, inp, alg, context) for ident,inp in inputs.items() )  

    # Check marshalled value 
    value = parameters['INPUT']
    assert isinstance( value, QgsReferencedGeometry )
    assert value.wkbType() == QgsWkbTypes.MultiPoint

    context.wms_url = "http://localhost/wms/?MAP=test/{name}.qgs".format(name=alg.name())
    # Run algorithm
    with chdir(outputdir):
        results = run_algorithm(alg, parameters=parameters, feedback=feedback, context=context, outputs=outputs)   
   
    out = json.loads(outputs.get('OUTPUT').data)
    assert out['type'] == 'MultiPoint'
Ejemplo n.º 3
0
def test_centroides_algorithms(outputdir, data):
    """ Execute an algorithm from a model
    """
    alg = _find_algorithm('model:centroides')

    # Load source project
    source = QgsProject()
    rv = source.read(str(data / 'france_parts.qgs'))
    assert rv == True

    context = Context(source, outputdir)
    feedback = QgsProcessingFeedback()

    inputs = {
        p.name(): [parse_input_definition(p)]
        for p in alg.parameterDefinitions()
    }
    outputs = {
        p.name(): parse_output_definition(p)
        for p in alg.outputDefinitions()
    }

    inputs['input'][0].data = 'france_parts'
    inputs['native:centroids_1:OUTPUT'][0].data = 'output_layer'

    parameters = dict(
        input_to_processing(ident, inp, alg, context)
        for ident, inp in inputs.items())

    assert isinstance(parameters['native:centroids_1:OUTPUT'],
                      QgsProcessingOutputLayerDefinition)

    destination_name = parameters['native:centroids_1:OUTPUT'].destinationName
    assert destination_name == 'output_layer'

    # Destination project
    destination_project = get_valid_filename(alg.id())

    context.wms_url = "http://localhost/wms/?MAP=test/{name}.qgs".format(
        name=destination_project)
    # Run algorithm
    with chdir(outputdir):
        results = run_algorithm(alg,
                                parameters=parameters,
                                feedback=feedback,
                                context=context,
                                outputs=outputs)

    assert context.destination_project.count() == 1

    out = outputs.get('native:centroids_1:OUTPUT')
    assert out.data_format.mime_type == "application/x-ogc-wms"

    query = parse_qs(urlparse(out.url).query)
    assert query['layers'][0] == destination_name

    # Get the layer
    layers = context.destination_project.mapLayersByName(destination_name)
    assert len(layers) == 1
Ejemplo n.º 4
0
def test_selectfeatures_algorithm(outputdir, data):
    """ Test simple layer output 
    """
    alg = _find_algorithm('pyqgiswps_test:simplebuffer')

    inputs = {
        p.name(): [parse_input_definition(p)]
        for p in alg.parameterDefinitions()
    }
    outputs = {
        p.name(): parse_output_definition(p)
        for p in alg.outputDefinitions()
    }

    inputs['INPUT'][0].data = 'layer:france_parts?' + urlencode(
        (('select', 'OBJECTID=2662 OR OBJECTID=2664'), ))
    inputs['OUTPUT_VECTOR'][0].data = 'buffer'
    inputs['DISTANCE'][0].data = 0.05

    # Load source project
    source = QgsProject()
    rv = source.read(str(data / 'france_parts.qgs'))
    assert rv == True

    context = Context(source, outputdir)
    feedback = QgsProcessingFeedback()

    parameters = dict(
        input_to_processing(ident, inp, alg, context)
        for ident, inp in inputs.items())

    assert isinstance(parameters['OUTPUT_VECTOR'],
                      QgsProcessingOutputLayerDefinition)
    assert isinstance(parameters['DISTANCE'], float)

    context.wms_url = "http://localhost/wms/?MAP=test/{name}.qgs".format(
        name=alg.name())
    # Run algorithm
    with chdir(outputdir):
        results = run_algorithm(alg,
                                parameters=parameters,
                                feedback=feedback,
                                context=context,
                                outputs=outputs)

    assert context.destination_project.count() == 1

    out = outputs.get('OUTPUT_VECTOR')
    assert out.data_format.mime_type == "application/x-ogc-wms"

    destination_name = parameters['OUTPUT_VECTOR'].destinationName

    query = parse_qs(urlparse(out.url).query)
    assert query['layers'][0] == destination_name

    # Get the layer
    layers = context.destination_project.mapLayersByName(destination_name)
    assert len(layers) == 1
    assert layers[0].featureCount() == 2
Ejemplo n.º 5
0
def test_output_vector_algorithm(outputdir, data):
    """ Test simple vector layer output 
    """
    alg = _find_algorithm('pyqgiswps_test:vectoroutput')

    inputs = {
        p.name(): [parse_input_definition(p)]
        for p in alg.parameterDefinitions()
    }
    outputs = {
        p.name(): parse_output_definition(p)
        for p in alg.outputDefinitions()
    }

    inputs['INPUT'][0].data = 'france_parts'
    inputs['DISTANCE'][0].data = 0.05

    # Load source project
    source = QgsProject()
    rv = source.read(str(data / 'france_parts.qgs'))
    assert rv == True

    context = Context(source, outputdir)
    feedback = QgsProcessingFeedback()

    parameters = dict(
        input_to_processing(ident, inp, alg, context)
        for ident, inp in inputs.items())

    assert isinstance(parameters['DISTANCE'], float)

    context.wms_url = "http://localhost/wms/?MAP=test/{name}.qgs".format(
        name=alg.name())
    # Run algorithm
    with chdir(outputdir):
        results = run_algorithm(alg,
                                parameters=parameters,
                                feedback=feedback,
                                context=context,
                                outputs=outputs)

    assert context.destination_project.count() == 1

    out = outputs.get('OUTPUT')
    assert out.data_format.mime_type == "application/x-ogc-wms"

    output_name = 'my_output_vector'

    query = parse_qs(urlparse(out.url).query)
    assert query['layers'][0] == output_name

    # Get the layer
    srclayer = QgsProcessingUtils.mapLayerFromString('france_parts', context)
    assert srclayer is not None

    layers = context.destination_project.mapLayersByName(output_name)
    assert len(layers) == 1
    assert layers[0].name() == 'my_output_vector'
    assert layers[0].featureCount() == srclayer.featureCount()
Ejemplo n.º 6
0
def test_layer_algorithm(outputdir, data):
    """ Copy layer 
    """
    alg = _find_algorithm('pyqgiswps_test:testcopylayer')

    inputs = {
        p.name(): [parse_input_definition(p)]
        for p in alg.parameterDefinitions()
    }
    outputs = {
        p.name(): parse_output_definition(p)
        for p in alg.outputDefinitions()
    }

    inputs['INPUT'][0].data = 'france_parts'
    inputs['OUTPUT'][0].data = 'france_parts_2'

    # Load source project
    source = QgsProject()
    rv = source.read(str(data / 'france_parts.qgs'))
    assert rv == True

    context = Context(source, outputdir)
    feedback = QgsProcessingFeedback()

    parameters = dict(
        input_to_processing(ident, inp, alg, context)
        for ident, inp in inputs.items())

    destination = get_valid_filename(alg.id())

    assert isinstance(parameters['OUTPUT'], QgsProcessingOutputLayerDefinition)

    context.wms_url = "http://localhost/wms/MAP=test/{name}.qgs".format(
        name=destination)

    # Run algorithm
    with chdir(outputdir):
        results = run_algorithm(alg,
                                parameters=parameters,
                                feedback=feedback,
                                context=context,
                                outputs=outputs)

    output = parameters['OUTPUT']
    assert output.destinationName == 'france_parts_2'
    assert output.sink.staticValue() == './OUTPUT.shp'
    assert context.destination_project.count() == 1
Ejemplo n.º 7
0
def test_option_multi_algorithms():
    """ Execute a multiple choice  algorithm
    """
    alg = _find_algorithm('pyqgiswps_test:testmultioptionvalue')

    context = QgsProcessingContext()
    feedback = QgsProcessingFeedback()

    inputs = {
        p.name(): parse_input_definition(p)
        for p in alg.parameterDefinitions()
    }
    outputs = {
        p.name(): parse_output_definition(p)
        for p in alg.outputDefinitions()
    }

    source = inputs['INPUT']
    inputs['INPUT'] = [source.clone(), source.clone()]
    inputs['INPUT'][0].data = 'value1'
    inputs['INPUT'][1].data = 'value3'

    parameters = dict(
        input_to_processing(ident, inp, alg, context)
        for ident, inp in inputs.items())

    assert parameters['INPUT'] == [0, 2]

    # Run algorith
    results = run_algorithm(alg,
                            parameters=parameters,
                            feedback=feedback,
                            context=context,
                            outputs=outputs)

    assert results['OUTPUT'] == 'selection is 0,2'
    assert outputs['OUTPUT'].data == "selection is 0,2"
Ejemplo n.º 8
0
def test_simple_algorithms():
    """ Execute a simple algorithm
    """
    alg = _find_algorithm('pyqgiswps_test:testsimplevalue')

    context = QgsProcessingContext()
    feedback = QgsProcessingFeedback()

    inputs = {
        p.name(): [parse_input_definition(p)]
        for p in alg.parameterDefinitions()
    }
    outputs = {
        p.name(): parse_output_definition(p)
        for p in alg.outputDefinitions()
    }

    inputs['PARAM1'][0].data = '1'
    inputs['PARAM2'][0].data = 'stuff'

    parameters = dict(
        input_to_processing(ident, inp, alg, context)
        for ident, inp in inputs.items())

    assert parameters['PARAM1'] == 1
    assert parameters['PARAM2'] == 'stuff'

    # Run algorithm
    results = run_algorithm(alg,
                            parameters=parameters,
                            feedback=feedback,
                            context=context,
                            outputs=outputs)

    assert results['OUTPUT'] == "1 stuff"
    assert outputs['OUTPUT'].data == "1 stuff"
Ejemplo n.º 9
0
def test_context(outputdir, data):
    """ Context with Copy layer
    """
    alg = _find_algorithm('pyqgiswps_test:testcopylayer')

    inputs = {
        p.name(): [parse_input_definition(p)]
        for p in alg.parameterDefinitions()
    }
    outputs = {
        p.name(): parse_output_definition(p)
        for p in alg.outputDefinitions()
    }

    inputs['INPUT'][0].data = 'france_parts'
    inputs['OUTPUT'][0].data = 'france_parts_2'

    context = ProcessingContext(str(outputdir), 'france_parts.qgs')
    feedback = QgsProcessingFeedback()

    parameters = dict(
        input_to_processing(ident, inp, alg, context)
        for ident, inp in inputs.items())

    assert isinstance(parameters['OUTPUT'], QgsProcessingOutputLayerDefinition)

    destination = get_valid_filename(alg.id())

    context.wms_url = "http://localhost/wms/MAP=test/{name}.qgs".format(
        name=destination)
    # Run algorithm
    with chdir(outputdir):
        results = run_algorithm(alg,
                                parameters=parameters,
                                feedback=feedback,
                                context=context,
                                outputs=outputs)

    assert context.destination_project.count() == 1

    # Save destination project
    ok = context.write_result(context.workdir, destination)

    assert ok
    assert context.destination_project.fileName() == str(
        outputdir / (destination + '.qgs'))

    # WFS configuration inserted
    WFSLayers = context.destination_project.readListEntry('WFSLayers', '/')[0]
    assert len(WFSLayers) != 0

    # All Vector Layers has been published in WFS
    mapLayers = context.destination_project.mapLayers()
    assert len(WFSLayers) == len([
        lid for lid, lyr in mapLayers.items()
        if lyr.type() == QgsMapLayer.VectorLayer
    ])

    # Verifying th WFS configuration
    for lid in WFSLayers:
        lyr = context.destination_project.mapLayer(lid)
        # Is the WFS layer id references a Map Layer
        assert lyr
        # Is the WFS layer id references a Vector Layer
        assert lyr.type() == QgsMapLayer.VectorLayer
        # The WFS layer precision is defined
        assert context.destination_project.readNumEntry(
            "WFSLayersPrecision", "/" + lid)[0] == 6