Пример #1
0
    def testAssignment(self):
        req = QgsFeatureRequest().setFilterFids([8, 9]).setFilterRect(
            QgsRectangle(1, 2, 3, 4)).setInvalidGeometryCheck(
                QgsFeatureRequest.GeometrySkipInvalid).setLimit(6).setFlags(
                    QgsFeatureRequest.ExactIntersect).setSubsetOfAttributes(
                        [1, 4]).setTimeout(6).setRequestMayBeNested(True)

        context = QgsExpressionContext()
        scope = QgsExpressionContextScope()
        scope.setVariable('a', 6)
        context.appendScope(scope)
        req.setExpressionContext(context)
        method = QgsSimplifyMethod()
        method.setMethodType(QgsSimplifyMethod.PreserveTopology)
        req.setSimplifyMethod(method)
        context = QgsCoordinateTransformContext()
        req.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3857'),
                              context)

        req2 = QgsFeatureRequest(req)
        self.assertEqual(req2.limit(), 6)
        self.assertCountEqual(req2.filterFids(), [8, 9])
        self.assertEqual(req2.filterRect(), QgsRectangle(1, 2, 3, 4))
        self.assertEqual(req2.spatialFilterType(),
                         Qgis.SpatialFilterType.BoundingBox)
        self.assertEqual(req2.invalidGeometryCheck(),
                         QgsFeatureRequest.GeometrySkipInvalid)
        self.assertEqual(req2.expressionContext().scopeCount(), 1)
        self.assertEqual(req2.expressionContext().variable('a'), 6)
        self.assertEqual(
            req2.flags(), QgsFeatureRequest.ExactIntersect
            | QgsFeatureRequest.SubsetOfAttributes)
        self.assertEqual(req2.subsetOfAttributes(), [1, 4])
        self.assertEqual(req2.simplifyMethod().methodType(),
                         QgsSimplifyMethod.PreserveTopology)
        self.assertEqual(req2.destinationCrs().authid(), 'EPSG:3857')
        self.assertEqual(req2.timeout(), 6)
        self.assertTrue(req2.requestMayBeNested())

        # copy distance within request
        req = QgsFeatureRequest().setDistanceWithin(
            QgsGeometry.fromWkt('LineString( 0 0, 10 0, 11 2)'), 1.2)
        req2 = QgsFeatureRequest(req)
        self.assertEqual(req2.spatialFilterType(),
                         Qgis.SpatialFilterType.DistanceWithin)
        self.assertEqual(req2.referenceGeometry().asWkt(),
                         'LineString (0 0, 10 0, 11 2)')
        self.assertEqual(req2.distanceWithin(), 1.2)
        self.assertEqual(req2.filterRect(),
                         QgsRectangle(-1.2, -1.2, 12.2, 3.2))
Пример #2
0
 def testLimit(self):
     req = QgsFeatureRequest()
     self.assertEqual(req.limit(), -1)
     req.setLimit(6)
     self.assertEqual(req.limit(), 6)
Пример #3
0
def count_qgis_features(qgis_layer,
                        qgis_feature_request=None,
                        bbox_filter=None,
                        attribute_filters=None,
                        search_filter=None,
                        extra_expression=None,
                        extra_subset_string=None,
                        **kwargs):
    """Returns a list of QgsFeatures from the QGIS vector layer,
    with optional filter options.

    The API can be used in two distinct ways (that are not mutually exclusive):

    1. pass in a pre-configured QgsFeatureRequest instance
    2. pass a series of filter attributes and let this method configure
    the QgsFeatureRequest.

    :param qgis_layer: the QGIS vector layer instance
    :type qgis_layer: QgsVectorLayer
    :param qgis_feature_request: the QGIS feature request
    :type qgis_feature_request: QgsFeatureRequest, optional
    :param bbox_filter: BBOX filter in layer's CRS, defaults to None
    :type bbox_filter: QgsRectangle, optional
    :param attribute_filters: dictionary of attribute filters combined with AND, defaults to None
    :type attribute_filters: dict, optional
    :param search_filter: string filter for all fields
    :type search_filter: str, optional
    :param: exclude_fields: list of fields to exclude from returned data, '__all__' for no attributes
    :param: extra_expression: extra expression for filtering features
    :type: extra_expression: str, optional
    :param: extra_subset_string: extra subset string (provider side WHERE condition) for filtering features
    :type: extra_subset_string: str, optional
    :return: list of features
    :rtype: QgsFeature list
    """

    # Remove no_filters condition because featureCount()
    # is cached,  so it could fail on multi-processes deploy
    # ------------------------------------------------------

    # Fast track for no filters
    # no_filters = (attribute_filters is None
    #     and (bbox_filter is None or bbox_filter.isEmpty()) and
    #     attribute_filters is None and
    #     extra_expression is None and
    #     extra_subset_string is None)
    #
    # if qgis_feature_request is not None:
    #     no_filters = (no_filters and
    #         qgis_feature_request.filterRect().isEmpty() and
    #         qgis_feature_request.filterType() == QgsFeatureRequest.FilterNone)
    # else:
    if not qgis_feature_request:
        qgis_feature_request = QgsFeatureRequest()

    # if no_filters:
    #     # Try to patch a possible Oracle views QGIS featureCount bug.
    #     qgs_fc = qgis_layer.featureCount()
    #     if qgs_fc != -1:
    #         return qgis_layer.featureCount()

    qgis_feature_request.setNoAttributes()
    if qgis_feature_request.limit() != -1:
        qgis_feature_request = QgsFeatureRequest(qgis_feature_request)
        qgis_feature_request.setLimit(-1)

    return len(
        __get_qgis_features(
            qgis_layer,
            qgis_feature_request,
            bbox_filter,
            attribute_filters,
            search_filter,
            False,  #with_geometry,
            None,  #page,
            None,  #page_size,
            None,  #ordering,
            None,  #exclude_fields,
            extra_expression,
            extra_subset_string))