<commit_before>import _map from 'lodash-es/map';
import _values from 'lodash-es/values';

import { bisector as d3_bisector } from 'd3-array';

import { osmEntity, osmIsSimpleMultipolygonOuterMember } from '../osm';
import { svgPath, svgSegmentWay, svgTagClasses } from './index';


export function svgAreas(projection, context) {
    // Patterns only work in Firefox when set directly on element.
    // (This is not a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=750632)
    var patterns = {
        landuse: {
            cemetery: 'cemetery',
            construction: 'construction',
            farm: 'farmland',
            farmland: 'farmland',
            forest: 'forest',
            grave_yard: 'cemetery',
            grass: 'grass',
            meadow: 'meadow',
            military: 'construction',
            orchard: 'orchard'
        },
        natural: {
            beach: 'beach',
            sand: 'beach',
            scrub: 'scrub',
            wetland: 'wetland',
            wood: 'forest'
        }
    };

    function setPattern(d) {
        // Skip pattern filling if this is a building (buildings don't get patterns applied)
        if (d.tags.building && d.tags.building !== 'no') {
            this.style.fill = this.style.stroke = '';
            return;
        }

        for (var tag in patterns) {
            var value = d.tags[tag];
            if (value) {
                var pattern = patterns[tag][value];
                if (pattern) {
                    this.style.fill = this.style.stroke = 'url("#pattern-' + pattern + '")';
                    return;
                }
            }
        }

        this.style.fill = this.style.stroke = '';
    }


    function drawTargets(selection, graph, entities, filter) {
        var targetClass = context.getDebug('target') ? 'pink ' : 'nocolor ';
        var nopeClass = context.getDebug('target') ? 'red ' : 'nocolor ';
        var getPath = svgPath(projection).geojson;
        var activeID = context.activeID();

        // The targets and nopes will be MultiLineString sub-segments of the ways
        var data = { targets: [], nopes: [] };

        entities.forEach(function(way) {
            var features = svgSegmentWay(way, graph, activeID);
            data.targets.push.apply(data.targets, features.passive);
            data.nopes.push.apply(data.nopes, features.active);
        });


        // Targets allow hover and vertex snapping
        var targetData = data.targets.filter(getPath);<commit_msg>Further change fill pattern definitions to allow iterating over "rules"<commit_after>import _map from 'lodash-es/map';
import _values from 'lodash-es/values';

import { bisector as d3_bisector } from 'd3-array';

import { osmEntity, osmIsSimpleMultipolygonOuterMember } from '../osm';
import { svgPath, svgSegmentWay, svgTagClasses } from './index';


export function svgAreas(projection, context) {
    // Patterns only work in Firefox when set directly on element.
    // (This is not a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=750632)
    var patterns = {
        // tag - value - rules (pattern)
        landuse: {
            cemetery: [ { pattern: 'cemetery' } ],
            construction: [ { pattern: 'construction' } ],
            farm: [ { pattern: 'farmland' } ],
            farmland: [ { pattern: 'farmland' } ],
            forest: [ { pattern: 'forest' } ],
            grave_yard: [ { pattern: 'cemetery' } ],
            grass: [ { pattern: 'grass' } ],
            meadow: [ { pattern: 'meadow' } ],
            military: [ { pattern: 'construction' } ],
            orchard: [ { pattern: 'orchard' } ]
        },
        natural: {
            beach: [ { pattern: 'beach' } ],
            sand: [ { pattern: 'beach' } ],
            scrub: [ { pattern: 'scrub' } ],
            wetland: [ { pattern: 'wetland' } ],
            wood: [ { pattern: 'forest' } ]
        }
    };

    function setPattern(d) {
        // Skip pattern filling if this is a building (buildings don't get patterns applied)
        if (d.tags.building && d.tags.building !== 'no') {
            this.style.fill = this.style.stroke = '';
            return;
        }

        for (var p in patterns) { // this iterates over potential tag values
            if (patterns.hasOwnProperty(p)) {
                var value = d.tags[p];
                if (value) {
                    var rules = patterns[p];
                    for (var r in rules) { // this iterates over potential tag values
                        if (rules.hasOwnProperty(r)) {
                            if (value === r) {
                                var cases = rules[r];
                                for (var c in cases) { // this iterates over rules for a tag-value pair
                                    var match = cases[c];
                                    // todo: no checks yet -- first case is good to go
                                    this.style.fill = this.style.stroke = 'url("#pattern-' + match.pattern + '")';
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }

        this.style.fill = this.style.stroke = '';
    }


    function drawTargets(selection, graph, entities, filter) {
        var targetClass = context.getDebug('target') ? 'pink ' : 'nocolor ';
        var nopeClass = context.getDebug('target') ? 'red ' : 'nocolor ';
        var getPath = svgPath(projection).geojson;
        var activeID = context.activeID();

        // The targets and nopes will be MultiLineString sub-segments of the ways
        var data = { targets: [], nopes: [] };

        entities.forEach(function(way) {
            var features = svgSegmentWay(way, graph, activeID);
            data.targets.push.apply(data.targets, features.passive);
            data.nopes.push.apply(data.nopes, features.active);
        });


        // Targets allow hover and vertex snapping
        var targetData = data.targets.filter(getPath);
{'fraction_non_alphanumeric': 0.12245233399079553, 'fraction_numerical': 0.0026298487836949377, 'mean_word_length': 1.6700307152259763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3265578', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1668, 'n_words': 434}