Exemple #1
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()
def test_buffer_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 = 'france_parts'
    inputs['OUTPUT_VECTOR'][0].data = 'buffer'
    inputs['DISTANCE'][0].data = 0.05

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

    workdir = outputdir.strpath

    context  = Context(source, workdir)
    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)

    # Run algorithm
    with chdir(outputdir.strpath):
        results = Processing.runAlgorithm(alg, parameters=parameters, onFinish=handle_algorithm_results,
                                          feedback=feedback, context=context)   
    
    assert context.destination_project.count() == 1

    handle_layer_outputs(results, context)
    assert results['OUTPUT_VECTOR'] == parameters['OUTPUT_VECTOR'].destinationName

    output_uri = "http://localhost/wms/?MAP=test/{name}.qgs".format(name=alg.name())

    write_outputs( alg, results, outputs, output_uri, context )

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

    query = parse_qs(urlparse(out.url).query)
    assert query['layer'][0] == parameters['OUTPUT_VECTOR'].destinationName

    # Get the layer 
    srclayer = QgsProcessingUtils.mapLayerFromString('france_parts', context)
    assert srclayer is not None
    layers  = context.destination_project.mapLayersByName(results['OUTPUT_VECTOR'])
    assert len(layers) == 1
    assert layers[0].featureCount() == srclayer.featureCount()
Exemple #3
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
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(data.join('france_parts.qgs').strpath)
    assert rv == True

    workdir = outputdir.strpath

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

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

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

    # Run algorithm
    with chdir(outputdir.strpath):
        results = Processing.runAlgorithm(alg, parameters=parameters, onFinish=handle_algorithm_results,
                                          feedback=feedback, context=context)   
    
    assert context.destination_project.count() == 1

    handle_layer_outputs(results, context)
    assert results['OUTPUT'] == parameters['OUTPUT'].destinationName

    output_uri = "http://localhost/wms/MAP=test/{name}.qgs".format(name=alg.name())

    write_outputs( alg, results, outputs, output_uri, context )
    assert outputs 
Exemple #5
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'

    workdir = outputdir.strpath

    context = ProcessingContext(workdir, '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)

    # Run algorithm
    with chdir(outputdir.strpath):
        results = Processing.runAlgorithm(alg,
                                          parameters=parameters,
                                          onFinish=handle_algorithm_results,
                                          feedback=feedback,
                                          context=context)

    assert context.destination_project.count() == 1

    handle_layer_outputs(results, context)
    assert results['OUTPUT'] == parameters['OUTPUT'].destinationName

    output_uri = "http://localhost/wms/MAP=test/{name}.qgs".format(
        name=alg.name())

    write_outputs(alg, results, outputs, output_uri, context)
    assert outputs

    assert context.destination_project.fileName() == outputdir.join(
        alg.name() + '.qgs').strpath

    # 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
Exemple #6
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