예제 #1
0
    def buildNetwork(self):
        # edges
        edges = vector.values(
            self.linksLayer,
            'source_vert',
            'target_vert',
            'link_cost',
            'link_id',
            'link_stress',
            'int_id'
        )
        edgeCount = len(edges['link_id'])
        for i in range(edgeCount):
            self.DG.add_edge(
                int(edges['source_vert'][i]),
                int(edges['target_vert'][i]),
                weight=max(edges['link_cost'][i],0),
                link_id=edges['link_id'][i],
                stress=min(edges['link_stress'][i],99),
                int_id=edges['int_id'][i]
            )

        # vertices
        verts = vector.values(
            self.vertsLayer,
            'vert_id',
            'vert_cost',
            'road_id'
        )
        vertCount = len(verts['vert_id'])
        for i in range(vertCount):
            vid = verts['vert_id'][i]
            self.DG.node[vid]['weight'] = max(verts['vert_cost'][i],0)
            self.DG.node[vid]['road_id'] = verts['road_id'][i]
예제 #2
0
    def testValues(self):
        test_data = points()
        test_layer = QgsVectorLayer(test_data, 'test', 'ogr')

        # field by index
        res = vector.values(test_layer, 1)
        self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])

        # field by name
        res = vector.values(test_layer, 'id')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])

        # two fields
        res = vector.values(test_layer, 1, 2)
        self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # two fields by name
        res = vector.values(test_layer, 'id', 'id2')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # two fields by name and index
        res = vector.values(test_layer, 'id', 2)
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])
예제 #3
0
    def processAlgorithm(self, feedback):
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.INPUT))
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, namefieldname, valuefieldname)

        d = {}
        for i in range(len(values[namefieldname])):
            v = values[namefieldname][i]
            if v not in d:
                d[v] = [values[valuefieldname][i]]
            else:
                d[v].append(values[valuefieldname][i])

        data = []
        for k, v in d.items():
            data.append(go.Box(y=list(v),
                               boxmean='sd',
                               name=k
                               ))
        plt.offline.plot(data, filename=output, auto_open=False)
예제 #4
0
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

        namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context)
        valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, namefieldname, valuefieldname)

        d = {}
        for i in range(len(values[namefieldname])):
            v = values[namefieldname][i]
            if v not in d:
                d[v] = [values[valuefieldname][i]]
            else:
                d[v].append(values[valuefieldname][i])

        data = []
        for k, v in d.items():
            data.append(go.Box(y=list(v),
                               boxmean='sd',
                               name=k
                               ))
        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #5
0
파일: BoxPlot.py 프로젝트: havatv/QGIS
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

        namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context)
        valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, valuefieldname)

        x_index = source.fields().lookupField(namefieldname)
        x_var = vector.convert_nulls([i[namefieldname] for i in source.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([x_index]))], '<NULL>')

        msdIndex = self.parameterAsEnum(parameters, self.MSD, context)
        msd = True

        if msdIndex == 1:
            msd = 'sd'
        elif msdIndex == 2:
            msd = False

        data = [go.Box(
                x=x_var,
                y=values[valuefieldname],
                boxmean=msd)]

        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #6
0
파일: BoxPlot.py 프로젝트: rskelly/QGIS
    def processAlgorithm(self, parameters, context, feedback):
        layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, valuefieldname)

        x_var = [i[namefieldname] for i in layer.getFeatures()]

        msdIndex = self.getParameterValue(self.MSD)
        msd = True

        if msdIndex == 1:
            msd = 'sd'
        elif msdIndex == 2:
            msd = False

        data = [go.Box(
                x=x_var,
                y=values[valuefieldname],
                boxmean=msd)]

        plt.offline.plot(data, filename=output, auto_open=False)
예제 #7
0
 def test_attributeValues(self):
     layer = processing.getObject(points())
     attributeValues = values(layer, "ID")
     i = 1
     for value in attributeValues['ID']:
         self.assertEqual(int(i), int(value))
         i+=1
     self.assertEquals(13,i)
예제 #8
0
    def processAlgorithm(self, parameters, context, feedback):
        layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
        fieldname = self.getParameterValue(self.FIELD)
        bins = self.getParameterValue(self.BINS)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, fieldname)

        data = [go.Histogram(x=values[fieldname],
                             nbinsx=bins)]
        plt.offline.plot(data, filename=output, auto_open=False)
예제 #9
0
    def processAlgorithm(self, parameters, context, feedback):
        layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
        xfieldname = self.getParameterValue(self.XFIELD)
        yfieldname = self.getParameterValue(self.YFIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, xfieldname, yfieldname)
        data = [go.Scatter(x=values[xfieldname],
                           y=values[yfieldname],
                           mode='markers')]
        plt.offline.plot(data, filename=output, auto_open=False)
예제 #10
0
파일: PolarPlot.py 프로젝트: rskelly/QGIS
    def processAlgorithm(self, parameters, context, feedback):
        layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
        namefieldname = self.getParameterValue(self.NAME_FIELD)  # NOQA  FIXME unused?
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, valuefieldname)

        data = [go.Area(r=values[valuefieldname],
                        t=np.degrees(np.arange(0.0, 2 * np.pi, 2 * np.pi / len(values[valuefieldname]))))]
        plt.offline.plot(data, filename=output, auto_open=False)
예제 #11
0
    def processAlgorithm(self, feedback):
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.INPUT))
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, valuefieldname)

        data = [go.Area(r=values[valuefieldname],
                        t=np.degrees(np.arange(0.0, 2 * np.pi, 2 * np.pi / len(values[valuefieldname]))))]
        plt.offline.plot(data, filename=output, auto_open=False)
예제 #12
0
    def processAlgorithm(self, feedback):
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.INPUT))
        fieldname = self.getParameterValue(self.FIELD)
        bins = self.getParameterValue(self.BINS)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, fieldname)

        data = [go.Histogram(x=values[fieldname],
                             nbinsx=bins)]
        plt.offline.plot(data, filename=output, auto_open=False)
예제 #13
0
파일: PolarPlot.py 프로젝트: GeoCat/QGIS
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context)  # NOQA  FIXME unused?
        valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, valuefieldname)

        data = [go.Area(r=values[valuefieldname],
                        t=np.degrees(np.arange(0.0, 2 * np.pi, 2 * np.pi / len(values[valuefieldname]))))]
        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #14
0
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        xfieldname = self.parameterAsString(parameters, self.XFIELD, context)
        yfieldname = self.parameterAsString(parameters, self.YFIELD, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, xfieldname, yfieldname)
        data = [go.Scatter(x=values[xfieldname],
                           y=values[yfieldname],
                           mode='markers')]
        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #15
0
파일: BarPlot.py 프로젝트: wongjimsan/QGIS
    def processAlgorithm(self, feedback):
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.INPUT))
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, namefieldname, valuefieldname)

        ind = np.arange(len(values[namefieldname]))
        data = [go.Bar(x=ind,
                       y=values[valuefieldname])]
        plt.offline.plot(data, filename=output, auto_open=False)
예제 #16
0
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        fieldname = self.parameterAsString(parameters, self.FIELD, context)
        bins = self.parameterAsInt(parameters, self.BINS, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, fieldname)

        data = [go.Histogram(x=values[fieldname],
                             nbinsx=bins)]
        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #17
0
    def processAlgorithm(self, progress):
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.INPUT))
        fieldname = self.getParameterValue(self.FIELD)
        bins = self.getParameterValue(self.BINS)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, fieldname)
        plt.close()
        plt.hist(values[fieldname], bins)
        plotFilename = output + '.png'
        lab.savefig(plotFilename)
        with open(output, 'w') as f:
            f.write('<html><img src="' + plotFilename + '"/></html>')
예제 #18
0
    def processAlgorithm(self, progress):
        layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
        xfieldname = self.getParameterValue(self.XFIELD)
        yfieldname = self.getParameterValue(self.YFIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, xfieldname, yfieldname)
        plt.close()
        plt.scatter(values[xfieldname], values[yfieldname])
        plt.ylabel(yfieldname)
        plt.xlabel(xfieldname)
        plotFilename = output + ".png"
        lab.savefig(plotFilename)
        with open(output, "w") as f:
            f.write('<html><img src="' + plotFilename + '"/></html>')
예제 #19
0
    def processAlgorithm(self, progress):
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.INPUT))
        xfieldname = self.getParameterValue(self.YFIELD)
        yfieldname = self.getParameterValue(self.XFIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, xfieldname, yfieldname)
        plt.close()
        plt.scatter(values[xfieldname], values[yfieldname])
        plotFilename = output + '.png'
        lab.savefig(plotFilename)
        f = open(output, 'w')
        f.write('<img src="' + plotFilename + '"/>')
        f.close()
예제 #20
0
파일: BarPlot.py 프로젝트: timlinux/QGIS
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)

        namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context)
        valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, valuefieldname)

        x_var = [i[namefieldname] for i in source.getFeatures()]

        data = [go.Bar(x=x_var,
                       y=values[valuefieldname])]
        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #21
0
    def processAlgorithm(self, progress):
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.INPUT))
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, namefieldname, valuefieldname)
        plt.close()

        ind = np.arange(len(values[namefieldname]))
        width = 0.8
        plt.bar(ind, values[valuefieldname], width, color='r')
        plt.xticks(ind, values[namefieldname], rotation=45)
        plotFilename = output + '.png'
        lab.savefig(plotFilename)
        with open(output, 'w') as f:
            f.write('<html><img src="' + plotFilename + '"/></html>')
예제 #22
0
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

        namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context)
        valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, valuefieldname)

        x_var = vector.convert_nulls([i[namefieldname] for i in source.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))], '<NULL>')

        data = [go.Bar(x=x_var,
                       y=values[valuefieldname])]
        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #23
0
파일: PolarPlot.py 프로젝트: sbrunner/QGIS
    def processAlgorithm(self, progress):
        layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, namefieldname, valuefieldname)
        plt.close()
        fig = figure(figsize=(8, 8))
        ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
        N = len(values[valuefieldname])
        theta = np.arange(0.0, 2 * np.pi, 2 * np.pi / N)
        radii = values[valuefieldname]
        width = 2 * np.pi / N
        ax.bar(theta, radii, width=width, bottom=0.0)
        plotFilename = output + ".png"
        lab.savefig(plotFilename)
        with open(output, "w") as f:
            f.write('<html><img src="' + plotFilename + '"/></html>')
예제 #24
0
    def processAlgorithm(self, progress):
        layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        meanfieldname = self.getParameterValue(self.MEAN_FIELD)
        stddevfieldname = self.getParameterValue(self.STDDEV_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, namefieldname, meanfieldname, stddevfieldname)
        plt.close()
        ind = np.arange(len(values[namefieldname]))
        width = 0.8
        plt.bar(
            ind, values[meanfieldname], width, color="r", yerr=values[stddevfieldname], error_kw=dict(ecolor="yellow")
        )

        plt.xticks(ind, values[namefieldname], rotation=45)
        plotFilename = output + ".png"
        lab.savefig(plotFilename)
        with open(output, "w") as f:
            f.write('<html><img src="' + plotFilename + '"/></html>')
예제 #25
0
파일: ToolsTest.py 프로젝트: kalxas/QGIS
    def testValues(self):
        ProcessingConfig.initialize()

        # disable check for geometry validity
        prevInvalidGeoms = ProcessingConfig.getSetting(
            ProcessingConfig.FILTER_INVALID_GEOMETRIES)
        ProcessingConfig.setSettingValue(
            ProcessingConfig.FILTER_INVALID_GEOMETRIES, 0)

        test_data = points()
        test_layer = QgsVectorLayer(test_data, 'test', 'ogr')

        # field by index
        res = vector.values(test_layer, 1)
        self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])

        # field by name
        res = vector.values(test_layer, 'id')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])

        # two fields
        res = vector.values(test_layer, 1, 2)
        self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # two fields by name
        res = vector.values(test_layer, 'id', 'id2')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # two fields by name and index
        res = vector.values(test_layer, 'id', 2)
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # test with selected features
        previous_value = ProcessingConfig.getSetting(
            ProcessingConfig.USE_SELECTED)
        ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
        test_layer.selectByIds([2, 4, 6])
        res = vector.values(test_layer, 1)
        self.assertEqual(set(res[1]), set([5, 7, 3]))

        ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED,
                                         previous_value)
        ProcessingConfig.setSettingValue(
            ProcessingConfig.FILTER_INVALID_GEOMETRIES, prevInvalidGeoms)
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

        xfieldname = self.parameterAsString(parameters, self.XFIELD, context)
        yfieldname = self.parameterAsString(parameters, self.YFIELD, context)
        zfieldname = self.parameterAsString(parameters, self.ZFIELD, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, xfieldname, yfieldname, zfieldname)

        data = [go.Scatter3d(
                x=values[xfieldname],
                y=values[yfieldname],
                z=values[zfieldname],
                mode='markers')]

        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #27
0
    def processAlgorithm(self, context, feedback):
        layer = dataobjects.getLayerFromString(
            self.getParameterValue(self.INPUT))
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, context, valuefieldname)

        x_var = [i[namefieldname] for i in layer.getFeatures()]

        msdIndex = self.getParameterValue(self.MSD)
        msd = True

        if msdIndex == 1:
            msd = 'sd'
        elif msdIndex == 2:
            msd = False

        data = [go.Box(x=x_var, y=values[valuefieldname], boxmean=msd)]

        plt.offline.plot(data, filename=output, auto_open=False)
예제 #28
0
    def processAlgorithm(self, feedback):
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.INPUT))
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        meanfieldname = self.getParameterValue(self.MEAN_FIELD)
        stddevfieldname = self.getParameterValue(self.STDDEV_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, namefieldname, meanfieldname, stddevfieldname)
        plt.close()
        ind = np.arange(len(values[namefieldname]))
        width = 0.8
        plt.bar(ind, values[meanfieldname], width, color='r',
                yerr=values[stddevfieldname],
                error_kw=dict(ecolor='yellow'),
                )

        plt.xticks(ind, values[namefieldname], rotation=45)
        plotFilename = output + '.png'
        lab.savefig(plotFilename)
        with open(output, 'w') as f:
            f.write('<html><img src="' + plotFilename + '"/></html>')
예제 #29
0
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        if source is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))

        xfieldname = self.parameterAsString(parameters, self.XFIELD, context)
        yfieldname = self.parameterAsString(parameters, self.YFIELD, context)
        zfieldname = self.parameterAsString(parameters, self.ZFIELD, context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, xfieldname, yfieldname, zfieldname)

        data = [
            go.Scatter3d(x=values[xfieldname],
                         y=values[yfieldname],
                         z=values[zfieldname],
                         mode='markers')
        ]

        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #30
0
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        if source is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))

        namefieldname = self.parameterAsString(parameters, self.NAME_FIELD,
                                               context)  # NOQA  FIXME unused?
        valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD,
                                                context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, valuefieldname)

        data = [
            go.Area(r=values[valuefieldname],
                    t=np.degrees(
                        np.arange(0.0, 2 * np.pi,
                                  2 * np.pi / len(values[valuefieldname]))))
        ]
        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #31
0
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        if source is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))

        namefieldname = self.parameterAsString(parameters, self.NAME_FIELD,
                                               context)
        valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD,
                                                context)

        output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

        values = vector.values(source, valuefieldname)

        x_var = vector.convert_nulls([
            i[namefieldname] for i in source.getFeatures(
                QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))
        ], '<NULL>')

        data = [go.Bar(x=x_var, y=values[valuefieldname])]
        plt.offline.plot(data, filename=output, auto_open=False)

        return {self.OUTPUT: output}
예제 #32
0
    def processAlgorithm(self, parameters, context, feedback):
        layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
        namefieldname = self.getParameterValue(self.NAME_FIELD)
        valuefieldname = self.getParameterValue(self.VALUE_FIELD)

        output = self.getOutputValue(self.OUTPUT)

        values = vector.values(layer, namefieldname, valuefieldname)

        d = {}
        for i in range(len(values[namefieldname])):
            v = values[namefieldname][i]
            if v not in d:
                d[v] = [values[valuefieldname][i]]
            else:
                d[v].append(values[valuefieldname][i])

        data = []
        for k, v in d.items():
            data.append(go.Box(y=list(v),
                               boxmean='sd',
                               name=k
                               ))
        plt.offline.plot(data, filename=output, auto_open=False)
예제 #33
0
    def testValues(self):
        ProcessingConfig.initialize()

        # disable check for geometry validity
        prevInvalidGeoms = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
        ProcessingConfig.setSettingValue(ProcessingConfig.FILTER_INVALID_GEOMETRIES, 0)

        test_data = points()
        test_layer = QgsVectorLayer(test_data, 'test', 'ogr')

        # field by index
        res = vector.values(test_layer, 1)
        self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])

        # field by name
        res = vector.values(test_layer, 'id')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])

        # two fields
        res = vector.values(test_layer, 1, 2)
        self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # two fields by name
        res = vector.values(test_layer, 'id', 'id2')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # two fields by name and index
        res = vector.values(test_layer, 'id', 2)
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # test with selected features
        previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
        ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
        test_layer.selectByIds([2, 4, 6])
        res = vector.values(test_layer, 1)
        self.assertEqual(set(res[1]), set([5, 7, 3]))

        ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value)
        ProcessingConfig.setSettingValue(ProcessingConfig.FILTER_INVALID_GEOMETRIES, prevInvalidGeoms)
예제 #34
0
    def testValues(self):
        ProcessingConfig.initialize()

        test_data = points2()
        test_layer = QgsVectorLayer(test_data, 'test', 'ogr')

        # field by index
        res = vector.values(test_layer, 0)
        self.assertEqual(res[0], [1, 2, 3, 4, 5, 6, 7, 8])

        # field by name
        res = vector.values(test_layer, 'id')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8])

        # two fields
        res = vector.values(test_layer, 0, 3)
        self.assertEqual(res[0], [1, 2, 3, 4, 5, 6, 7, 8])
        self.assertEqual(res[3], [2, 1, 0, 2, 1, 0, 0, 0])

        # two fields by name
        res = vector.values(test_layer, 'id', 'id_2')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8])
        self.assertEqual(res['id_2'], [2, 1, 0, 2, 1, 0, 0, 0])

        # two fields by name and index
        res = vector.values(test_layer, 'id', 3)
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8])
        self.assertEqual(res[3], [2, 1, 0, 2, 1, 0, 0, 0])

        # test with selected features
        previous_value = ProcessingConfig.getSetting(
            ProcessingConfig.USE_SELECTED)
        ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
        test_layer.selectByIds([2, 4, 6])
        res = vector.values(test_layer, 0)
        self.assertEqual(set(res[0]), set([5, 7, 3]))

        ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED,
                                         previous_value)
예제 #35
0
    def testValues(self):
        ProcessingConfig.initialize()

        test_data = points2()
        test_layer = QgsVectorLayer(test_data, 'test', 'ogr')

        # field by index
        res = vector.values(test_layer, 0)
        self.assertEqual(res[0], [1, 2, 3, 4, 5, 6, 7, 8])

        # field by name
        res = vector.values(test_layer, 'id')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8])

        # two fields
        res = vector.values(test_layer, 0, 3)
        self.assertEqual(res[0], [1, 2, 3, 4, 5, 6, 7, 8])
        self.assertEqual(res[3], [2, 1, 0, 2, 1, 0, 0, 0])

        # two fields by name
        res = vector.values(test_layer, 'id', 'id_2')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8])
        self.assertEqual(res['id_2'], [2, 1, 0, 2, 1, 0, 0, 0])

        # two fields by name and index
        res = vector.values(test_layer, 'id', 3)
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8])
        self.assertEqual(res[3], [2, 1, 0, 2, 1, 0, 0, 0])

        # test with selected features
        previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
        ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
        test_layer.selectByIds([2, 4, 6])
        res = vector.values(test_layer, 0)
        self.assertEqual(set(res[0]), set([5, 7, 3]))

        ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value)
예제 #36
0
    def testValues(self):
        context = QgsProcessingContext()

        # disable check for geometry validity
        context.setFlags(QgsProcessingContext.Flags(0))

        test_data = points()
        test_layer = QgsVectorLayer(test_data, 'test', 'ogr')

        # field by index
        res = vector.values(test_layer, context, 1)
        self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])

        # field by name
        res = vector.values(test_layer, context, 'id')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])

        # two fields
        res = vector.values(test_layer, context, 1, 2)
        self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # two fields by name
        res = vector.values(test_layer, context, 'id', 'id2')
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # two fields by name and index
        res = vector.values(test_layer, context, 'id', 2)
        self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
        self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

        # test with selected features
        context.setFlags(QgsProcessingContext.UseSelectionIfPresent)
        test_layer.selectByIds([2, 4, 6])
        res = vector.values(test_layer, context, 1)
        self.assertEqual(set(res[1]), set([5, 7, 3]))
    def processAlgorithm(self, progress):
        progress.setPercentage(0)
        # Retrieve the values of the parameters entered by the user
        roadsLayer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.ROADS_LAYER))
        destsLayer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.DESTINATIONS_LAYER))
        stress = self.getParameterValue(self.STRESS)
        maxCost = self.getParameterValue(self.MAX_COST)
        keepRaw = self.getParameterValue(self.KEEP_RAW)
        keepRoutes = self.getParameterValue(self.KEEP_ROUTES)
        keepSums = self.getParameterValue(self.KEEP_SUMS)

        # build the raw output layer
        if keepRaw:
            rawFields = QgsFields()
            rawFields.append(QgsField('path_id', QVariant.Int))
            rawFields.append(QgsField('sequence', QVariant.Int))
            rawFields.append(QgsField('from_road_id', QVariant.Int))
            rawFields.append(QgsField('to_road_id', QVariant.Int))
            rawFields.append(QgsField('int_id', QVariant.Int))
            rawFields.append(QgsField('int_cost', QVariant.Int))
            rawFields.append(QgsField('road_id', QVariant.Int))
            rawFields.append(QgsField('road_cost', QVariant.Int))
            rawFields.append(QgsField('cmtve_cost', QVariant.Int))
            rawWriter = self.getOutputFromName(self.RAW_LAYER).getVectorWriter(
                rawFields, QGis.WKBLineString, roadsLayer.crs())
        if keepRoutes:
            routeFields = QgsFields()
            routeFields.append(QgsField('path_id', QVariant.Int))
            routeFields.append(QgsField('from_road_id', QVariant.Int))
            routeFields.append(QgsField('to_road_id', QVariant.Int))
            routeFields.append(QgsField('cmtve_cost', QVariant.Int))
            routeWriter = self.getOutputFromName(self.ROUTES_LAYER).getVectorWriter(
                routeFields, QGis.WKBLineString, roadsLayer.crs())

        sumFields = QgsFields()
        sumFields.append(QgsField('road_id', QVariant.Int))
        sumFields.append(QgsField('use_count', QVariant.Int))
        if keepSums:
            sumWriter = self.getOutputFromName(self.SUMS_LAYER).getVectorWriter(
                sumFields, QGis.WKBLineString, roadsLayer.crs())
        progress.setPercentage(2)

        # establish db connection
        progress.setInfo('Getting DB connection')
        self.setDbFromRoadsLayer(roadsLayer)
        self.setLayersFromDb()

        # get network
        progress.setInfo('Building network')
        nu = NXUtils(self.vertsLayer,self.linksLayer)
        nu.buildNetwork()
        if not stress:
            DG = nu.getNetwork()
        else:
            DG = nu.getStressNetwork(stress)
        progress.setPercentage(10)

        '''# Build spatial index of road features
        progress.setInfo('Indexing road features')
        index = vector.spatialindex(roadsLayer)

        # Get nearest Road ID for input layer
        progress.setInfo('Getting road IDs')
        destinations = {}
        for feat in vector.features(destsLayer):
            roadMatch = QgsFeatureId()
            destGeom = QgsGeometry(feat.geometry())
            roadMatch = index.nearestNeighbor(destGeom.asPoint(),1)[0]
            roadFeat = roadsLayer.getFeatures(QgsFeatureRequest().setFilterFid(roadMatch))[0]
            roadGeom = QgsGeometry(roadFeat.geometry())
            destinations[feat.id()] = {
                'roadId': roadMatch,
                'distance': 0
            }'''
        # Assume nearest Road ID and distance fields exist as road_id and road_dist

        roadIds = list()
        for val in vector.values(destsLayer,'road_id')['road_id']:
            if val.is_integer():
                roadIds.append(int(val))
            else:
                raise GeoAlgorithmExecutionException(
                    self.tr('Bad road_id values. Input field was %s. Check that \
                        these are integer values.' % 'road_id'))

        # count pairs
        roadPairCount = len(roadIds) ** 2 - len(roadIds)
        progress.setInfo('%i total destination pairs identified' % roadPairCount)

        # loop through each destination and get shortest routes to all others
        try:
            rowCount = 0
            pairCount = 0
            for fromRoad in roadIds:
                for toRoad in roadIds:
                    if not fromRoad == toRoad:
                        # set counts
                        pairCount += 1
                        seq = 0
                        cost = 0
                        if pairCount % 1000 == 0:
                            progress.setInfo('Shortest path for pair %i of %i'
                                    % (pairCount, RoadPairCount))

                        # set feature for route output
                        routeFeat = None
                        if keepRoutes:
                            routeFeat = QgsFeature(routeFields)
                            routeFeat.setAttribute(0,pairCount) #path_id
                            routeFeat.setAttribute(1,fromRoad) #from_road_id
                            routeFeat.setAttribute(2,toRoad) #to_road_id












                        # check the path and iterate through it
                        if nx.has_path(DG,source=fromRoad,target=toRoad):
                            shortestPath = nx.shortest_path(
                                DG,
                                source=fromRoad,
                                target=toRoad,
                                weight='weight'
                            )
                            for i, v1 in enumerate(shortestPath, start=1):
                                if i == len(shortestPath):
                                    continue    #Leave out because this is the last Roadex

                                rowCount += 1
                                v2 = shortestPath[i]
                                seq += 1
                                roads[roadId]['count'] += 1

                                # set costs
                                linkCost = DG.edge[v1][v2]['weight']
                                intCost = 0
                                cost += linkCost
                                cost += intCost

                                # create the new features
                                if roadId in roads and roads.get(roadId).get('geom'):
                                    geom = roads.get(roadId).get('geom')
                                    if keepRaw:
                                        rawFeat = QgsFeature(rawFields)
                                        rawFeat.setAttribute(0,pairCount) #path_id
                                        rawFeat.setAttribute(1,seq) #sequence
                                        rawFeat.setAttribute(2,fromRoad) #from_road_id
                                        rawFeat.setAttribute(3,toRoad) #to_road_id
                                        rawFeat.setAttribute(4,DG.node[v2]['int_id']) #int_id
                                        rawFeat.setAttribute(5,intCost) #int_cost
                                        rawFeat.setAttribute(6,roadId) #road_id
                                        rawFeat.setAttribute(7,linkCost) #road_cost
                                        rawFeat.setAttribute(8,cost) #cmtve_cost
                                        rawFeat.setGeometry(geom)
                                        rawWriter.addFeature(rawFeat)
                                        del rawFeat
                                    if keepRoutes:
                                        routeFeat.setAttribute(3,cost) #cmtve_cost
                                        if not routeFeat.constGeometry():
                                            routeFeat.setGeometry(geom)
                                        else:
                                            routeFeat.setGeometry(
                                                geom.combine(routeFeat.constGeometry())
                                            )
                                        routeWriter.addFeature(routeFeat)
                                    if keepSums:
                                        sumFeat = roads.get(roadId).get('feat')
                                        useCount = roads.get(roadId).get('count')
                                        sumFeat.setAttribute(1,useCount) #use_count

                        del routeFeat
                        progress.setPercentage(10 + 90*pairCount/RoadPairCount)

            for roadId, r in roads.iteritems():
                if r.get('count') > 0:
                    if keepSums:
                        sumWriter.addFeature(r.get('feat'))

        except Exception, e:
            raise GeoAlgorithmExecutionException('Uncaught error: %s' % e)
예제 #38
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """

        # Retrieve the feature source and sink. The 'dest_id' variable is used
        # to uniquely identify the feature sink, and must be included in the
        # dictionary returned by the processAlgorithm function.
        source = self.parameterAsSource(
            parameters,
            self.INPUT,
            context
        )

        # If source was not found, throw an exception to indicate that the
        # algorithm encountered a fatal error. The exception text can be any
        # string, but in this case we use the pre-built invalidSourceError
        # method to return a standard helper text for when a source cannot be
        # evaluated
        if source is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))

        fields_to_transform = self.parameterAsFields(
            parameters, self.FIELDS_TO_TRANSFORM, context)
        source_fields = source.fields()
        if not fields_to_transform:
            # no fields selected, use all numeric ones
            fields_to_transform = [source_fields.at(i).name()
                                   for i in range(len(source_fields))
                                   if source_fields.at(i).isNumeric()]
        self.transformed_fields = QgsFields()

        transformation_name = self.name()

        fields_to_transform_idxs = []
        for f in fields_to_transform:
            idx = source.fields().lookupField(f)
            if idx >= 0:
                fields_to_transform_idxs.append(idx)
                field_to_transform = source.fields().at(idx)
                if field_to_transform.isNumeric():
                    transformed_field = QgsField(field_to_transform)
                    transformed_field.setName(
                        "%s_%s" % (field_to_transform.name(),
                                   transformation_name))
                    transformed_field.setType(QVariant.Double)
                    transformed_field.setLength(20)
                    transformed_field.setPrecision(6)
                    self.transformed_fields.append(transformed_field)

        out_fields = QgsProcessingUtils.combineFields(
            source_fields, self.transformed_fields)

        (sink, self.dest_id) = self.parameterAsSink(
            parameters,
            self.OUTPUT,
            context,
            out_fields,
            source.wkbType(),
            source.sourceCrs()
        )

        # Send some information to the user
        feedback.pushInfo('CRS is {}'.format(source.sourceCrs().authid()))

        # If sink was not created, throw an exception to indicate that the
        # algorithm encountered a fatal error. The exception text can be any
        # string, but in this case we use the pre-built invalidSinkError method
        # to return a standard helper text for when a sink cannot be evaluated
        if sink is None:
            raise QgsProcessingException(
                self.invalidSinkError(parameters, self.OUTPUT))

        total = 100.0 / len(fields_to_transform)
        transformed_values = {}

        for current, fieldname_to_transform in enumerate(fields_to_transform):
            original_values = vector.values(
                source, fieldname_to_transform)[fieldname_to_transform]
            transformed_values[fieldname_to_transform] = self.transform_values(
                original_values, parameters, context)
            feedback.setProgress(int(current * total))

        feedback.setProgress(0)

        # Compute the number of steps to display within the progress bar and
        # get features from source
        total = 100.0 / source.featureCount() if source.featureCount() else 0

        for current, source_feature in enumerate(source.getFeatures()):
            # Stop the algorithm if cancel button has been clicked
            if feedback.isCanceled():
                break

            sink_feature = QgsFeature(out_fields)

            # copy original fields
            for field in source.fields():
                sink_feature[field.name()] = source_feature[field.name()]

            for original_fieldname, transformed_field in zip(
                    fields_to_transform, self.transformed_fields):
                sink_feature[transformed_field.name()] = \
                    transformed_values[original_fieldname][current]
            sink_feature.setGeometry(source_feature.geometry())

            # Add a feature in the sink
            sink.addFeature(sink_feature, QgsFeatureSink.FastInsert)

            # Update the progress bar
            feedback.setProgress(int(current * total))

        # Return the results of the algorithm. In this case our only result is
        # the feature sink which contains the processed features, but some
        # algorithms may return multiple feature sinks, calculated numeric
        # statistics, etc. These should all be included in the returned
        # dictionary, with keys matching the feature corresponding parameter
        # or output names.
        return {self.OUTPUT: self.dest_id}