function sortSpatialRelations(matches, sort_by, thresh, reverse=true) {
    // sort
    function sortFunc(a, b) {
        if (reverse == false) {
            var trueVal = 1;
        } else {
            var trueVal = -1;
        };
        if (a[1][sort_by] < b[1][sort_by]) {
            // a is less than b by some ordering criterion
            return -trueVal;
        };
        if (a[1][sort_by] > b[1][sort_by]) {
            // a is greater than b by the ordering criterion
            return trueVal;
        };
        // a must be equal to b
        return 0;
    };
    matches.sort(sortFunc);

    // filter by threshold
    newMatches = [];
    for (m of matches) {
        if (m[1][sort_by] >= thresh) {
            newMatches.push(m);
        };
    };

    return newMatches;
}
/*
function calcAllSpatialRelations(features1, features2, onSuccess) {
    // calc relations from 1 to 2
    // calculate for each feature sequentially with timeout in between
    // to avoid locking up the entire gui
    
    // background worker approach

    // define how to process
    var matches1 = [];
    function processOne(i) {
        console.log('processing '+i+' of '+features1.length);
        var feature1 = features1[i];
        //var related = calcSpatialRelations(feature1, features2);
        //matches1.push([feature1,related]);
        function onFeatureFinished(featmatches) {
            //console.log(i+' feature finished')
            if (i+1 < features1.length) {
                // process next one after x milliseconds
                //console.log('calling on next one')
                matches1.push([feature1,featmatches]);
                setTimeout(function(){processOne(i+1)}, 10);
            } else {
                // finished
                console.log('everything finished!')
                //console.log(matches1);
                // calc relations from 2 to 1 by reversing the calcs
                var matches2 = [];
                for (feature2 of features2) {
                    var related2 = [];
                    for (m1 of matches1) {
                        var [f1,related1] = m1;
                        for (r1 of related1) {
                            var [f2,stats] = r1;
                            if (feature2 === f2) {
                                // reverse and add the stats to related
                                newStats = {contains:stats.within, within:stats.contains, equality:stats.equality}
                                related2.push([f1,newStats]);
                            };
                        };
                    };
                    matches2.push([feature2,related2]);
                };
                // run success func
                onSuccess(matches1, matches2);
            };
        };
        calcSpatialRelationsInBackground(i, feature1, features2, onFeatureFinished);
    };
    // begin
    processOne(0);
};
*/
function calcAllSpatialRelations(features1, features2, onSuccess) {
calc relations from 1 to 2
calculate for each feature sequentially with timeout in between
to avoid locking up the entire gui

background worker approach


function calcAllSpatialRelations ( features1 features2 onSuccess ) { calc relations from 1 to 2 calculate for each feature sequentially with timeout in between to avoid locking up the entire gui background worker approach
function sortSpatialRelations(matches, sort_by, thresh, reverse=true) {
    function sortFunc(a, b) {
        if (reverse == false) {
            var trueVal = 1;
        } else {
            var trueVal = -1;
        };
        if (a[1][sort_by] < b[1][sort_by]) {
            return -trueVal;
        };
        if (a[1][sort_by] > b[1][sort_by]) {
            return trueVal;
        };
        return 0;
    };
    matches.sort(sortFunc);
    newMatches = [];
    for (m of matches) {
        if (m[1][sort_by] >= thresh) {
            newMatches.push(m);
        };
    };
    return newMatches;
}
function sortSpatialRelations ( matches , sort_by , thresh , reverse = true ) { function sortFunc ( a , b ) { if ( reverse == false ) { var trueVal = 1 ; } else { var trueVal = - 1 ; } ; if ( a [ 1 ] [ sort_by ] < b [ 1 ] [ sort_by ] ) { return - trueVal ; } ; if ( a [ 1 ] [ sort_by ] > b [ 1 ] [ sort_by ] ) { return trueVal ; } ; return 0 ; } ; matches . sort ( sortFunc ) ; newMatches = [ ] ; for ( m of matches ) { if ( m [ 1 ] [ sort_by ] >= thresh ) { newMatches . push ( m ) ; } ; } ; return newMatches ; }
function calcAllSpatialRelations(features1, features2, onSuccess) {
 calc relations from 1 to 2
 calculate for each feature sequentially with timeout in between
 to avoid locking up the entire gui
function calcAllSpatialRelations ( features1 features2 onSuccess ) { calc relations from 1 to 2 calculate for each feature sequentially with timeout in between to avoid locking up the entire gui
// sort // a is less than b by some ordering criterion // a is greater than b by the ordering criterion // a must be equal to b // filter by threshold