def nearest_qmed_catchments(self, subject_catchment, limit=None, dist_limit=500):
        """
        Return a list of catchments sorted by distance to `subject_catchment` **and filtered to only include catchments
        suitable for QMED analyses**.

        :param subject_catchment: catchment object to measure distances to
        :type subject_catchment: :class:`floodestimation.entities.Catchment`
        :param limit: maximum number of catchments to return. Default: `None` (returns all available catchments).
        :type limit: int
        :param dist_limit: maximum distance in km. between subject and donor catchment. Default: 500 km. Increasing the
                           maximum distance will increase computation time!
        :type dist_limit: float or int
        :return: list of catchments sorted by distance
        :rtype: list of :class:`floodestimation.entities.Catchment`
        """

        dist_sq = Catchment.distance_to(subject_catchment).label('dist_sq')  # Distance squared, calculated using SQL
        query = self.db_session.query(Catchment, dist_sq). \
            join(Catchment.amax_records). \
            join(Catchment.descriptors). \
            filter(Catchment.id != subject_catchment.id,  # Exclude subject catchment itself
                   Catchment.is_suitable_for_qmed,  # Only catchments suitable for QMED estimation
                   Catchment.country == subject_catchment.country,  # SQL dist method does not cover cross-boundary dist
                   # Within the distance limit
                   dist_sq <= dist_limit ** 2). \
            group_by(Catchment,
                     Descriptors.centroid_ngr_x,
                     Descriptors.centroid_ngr_y). \
            order_by(dist_sq). \
            having(func.count(AmaxRecord.catchment_id) >= 10)  # At least 10 AMAX records

        if limit:
            rows = query[0:limit]  # Each row is tuple of (catchment, distance squared)
        else:
            rows = query.all()

        # Add real `dist` attribute to catchment list using previously calculated SQL dist squared
        catchments = []
        for row in rows:
            catchment = row[0]
            catchment.dist = sqrt(row[1])
            catchments.append(catchment)

        return catchments
def nearest_qmed_catchments ( self , subject_catchment , limit = None , dist_limit = 500 ) : dist_sq = Catchment . distance_to ( subject_catchment ) . label ( 'dist_sq' ) query = self . db_session . query ( Catchment , dist_sq ) . join ( Catchment . amax_records ) . join ( Catchment . descriptors ) . filter ( Catchment . id != subject_catchment . id , Catchment . is_suitable_for_qmed , Catchment . country == subject_catchment . country , dist_sq <= dist_limit ** 2 ) . group_by ( Catchment , Descriptors . centroid_ngr_x , Descriptors . centroid_ngr_y ) . order_by ( dist_sq ) . having ( func . count ( AmaxRecord . catchment_id ) >= 10 ) if limit : rows = query [ 0 : limit ] else : rows = query . all ( ) catchments = [ ] for row in rows : catchment = row [ 0 ] catchment . dist = sqrt ( row [ 1 ] ) catchments . append ( catchment ) return catchments
Return a list of catchments sorted by distance to `subject_catchment` **and filtered to only include catchments
        suitable for QMED analyses**.

        :param subject_catchment: catchment object to measure distances to
        :type subject_catchment: :class:`floodestimation.entities.Catchment`
        :param limit: maximum number of catchments to return. Default: `None` (returns all available catchments).
        :type limit: int
        :param dist_limit: maximum distance in km. between subject and donor catchment. Default: 500 km. Increasing the
                           maximum distance will increase computation time!
        :type dist_limit: float or int
        :return: list of catchments sorted by distance
        :rtype: list of :class:`floodestimation.entities.Catchment`