Search in sources :

Example 1 with Match

use of org.openstreetmap.osmosis.tagtransform.Match in project osmosis by openstreetmap.

the class TagMatcher method match.

@Override
public Collection<Match> match(Map<String, String> tags, TTEntityType type, String uname, int uid) {
    List<Match> matches = new ArrayList<Match>();
    // loop through the tags to find matches
    for (Entry<String, String> tag : tags.entrySet()) {
        java.util.regex.Matcher keyMatch = keyPattern.matcher(tag.getKey());
        java.util.regex.Matcher valueMatch = valuePattern.matcher(tag.getValue());
        if (keyMatch.matches() && valueMatch.matches()) {
            MatchResult keyRes = keyMatch.toMatchResult();
            MatchResult valueRes = valueMatch.toMatchResult();
            matches.add(new MatchResultMatch(matchID, keyRes, valueRes));
        }
    }
    matchHits += matches.size();
    return matches;
}
Also used : ArrayList(java.util.ArrayList) MatchResult(java.util.regex.MatchResult) Match(org.openstreetmap.osmosis.tagtransform.Match)

Example 2 with Match

use of org.openstreetmap.osmosis.tagtransform.Match in project osmosis by openstreetmap.

the class AndMatcher method match.

@Override
public Collection<Match> match(Map<String, String> tags, TTEntityType entityType, String entityUname, int entityUid) {
    if (this.type != null && this.type != entityType) {
        return null;
    }
    if (this.uname != null && !this.uname.equals(entityUname)) {
        return null;
    }
    if (this.uid != 0 && this.uid != entityUid) {
        return null;
    }
    List<Match> allMatches = new ArrayList<Match>();
    for (Matcher matcher : matchers) {
        Collection<Match> matches = matcher.match(tags, entityType, entityUname, entityUid);
        if (matches == null || matches.isEmpty()) {
            return null;
        }
        allMatches.addAll(matches);
    }
    matchHits++;
    return allMatches;
}
Also used : Matcher(org.openstreetmap.osmosis.tagtransform.Matcher) ArrayList(java.util.ArrayList) Match(org.openstreetmap.osmosis.tagtransform.Match)

Example 3 with Match

use of org.openstreetmap.osmosis.tagtransform.Match in project osmosis by openstreetmap.

the class TagOutput method apply.

@Override
public void apply(Map<String, String> originalTags, Map<String, String> tags, Collection<Match> matches, Map<String, DataSource> dataSources) {
    // each and every matching match
    if (fromMatch != null) {
        DataSource keyDataSourceImpl = this.keyDataSource != null && dataSources.containsKey(this.keyDataSource) ? dataSources.get(this.keyDataSource) : dummyDataSource;
        DataSource valueDataSourceImpl = this.valueDataSource != null && dataSources.containsKey(this.valueDataSource) ? dataSources.get(this.valueDataSource) : dummyDataSource;
        for (Match match : matches) {
            String matchID = match.getMatchID();
            if (matchID != null && matchID.equals(fromMatch)) {
                // process key args
                String[] args = new String[match.getKeyGroupCount()];
                for (int i = 0; i < args.length; i++) {
                    args[i] = match.getKey(i);
                }
                String key = keyFormat.format(keyDataSourceImpl.transform(args));
                // process value args
                args = new String[match.getValueGroupCount()];
                for (int i = 0; i < args.length; i++) {
                    args[i] = match.getValue(i);
                }
                String value = valueFormat.format(valueDataSourceImpl.transform(args));
                // put the tag
                tags.put(key, value);
            }
        }
    } else {
        // simple case
        tags.put(keyFormat.format(null), valueFormat.format(null));
    }
}
Also used : DataSource(org.openstreetmap.osmosis.tagtransform.DataSource) Match(org.openstreetmap.osmosis.tagtransform.Match)

Example 4 with Match

use of org.openstreetmap.osmosis.tagtransform.Match in project osmosis by openstreetmap.

the class TransformHelper method processEntityContainer.

/**
 * Transforms entity container according to configFile.
 *
 * @param entityContainer
 *            The entity to be processed.
 * @return transformed (if needed) entityContainer
 */
protected EntityContainer processEntityContainer(EntityContainer entityContainer) {
    // Entities may have been made read-only at some point in the pipeline.
    // We want a writeable instance so that we can update the tags.
    EntityContainer writeableEntityContainer = entityContainer.getWriteableInstance();
    Entity entity = entityContainer.getEntity();
    Collection<Tag> entityTags = entity.getTags();
    EntityType entityType = entity.getType();
    // Store the tags in a map keyed by tag key.
    Map<String, String> tagMap = new HashMap<String, String>();
    for (Tag tag : entity.getTags()) {
        tagMap.put(tag.getKey(), tag.getValue());
    }
    // Apply tag transformations.
    for (Translation translation : translations) {
        Collection<Match> matches = translation.match(tagMap, TTEntityType.fromEntityType06(entityType), entity.getUser().getName(), entity.getUser().getId());
        if (matches == null || matches.isEmpty()) {
            continue;
        }
        if (translation.isDropOnMatch()) {
            return null;
        }
        Map<String, String> newTags = new HashMap<String, String>();
        for (Output output : translation.getOutputs()) {
            output.apply(tagMap, newTags, matches, translation.getDataSources());
        }
        tagMap = newTags;
    }
    // Replace the entity tags with the transformed values.
    entityTags.clear();
    for (Entry<String, String> tag : tagMap.entrySet()) {
        entityTags.add(new Tag(tag.getKey(), tag.getValue()));
    }
    return writeableEntityContainer;
}
Also used : Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) Translation(org.openstreetmap.osmosis.tagtransform.Translation) HashMap(java.util.HashMap) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) Match(org.openstreetmap.osmosis.tagtransform.Match) TTEntityType(org.openstreetmap.osmosis.tagtransform.TTEntityType) EntityType(org.openstreetmap.osmosis.core.domain.v0_6.EntityType) Output(org.openstreetmap.osmosis.tagtransform.Output) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag)

Example 5 with Match

use of org.openstreetmap.osmosis.tagtransform.Match in project osmosis by openstreetmap.

the class CopyUnmatched method apply.

@Override
public void apply(Map<String, String> originalTags, Map<String, String> tags, Collection<Match> matches, Map<String, DataSource> dataSources) {
    // copy the original, then remove the matches
    Map<String, String> toCopy = new HashMap<String, String>(originalTags);
    for (Match match : matches) {
        if (match.getKeyGroupCount() > 0) {
            toCopy.remove(match.getKey(0));
        }
    }
    // apply the copy
    tags.putAll(toCopy);
}
Also used : HashMap(java.util.HashMap) Match(org.openstreetmap.osmosis.tagtransform.Match)

Aggregations

Match (org.openstreetmap.osmosis.tagtransform.Match)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MatchResult (java.util.regex.MatchResult)1 EntityContainer (org.openstreetmap.osmosis.core.container.v0_6.EntityContainer)1 Entity (org.openstreetmap.osmosis.core.domain.v0_6.Entity)1 EntityType (org.openstreetmap.osmosis.core.domain.v0_6.EntityType)1 Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)1 DataSource (org.openstreetmap.osmosis.tagtransform.DataSource)1 Matcher (org.openstreetmap.osmosis.tagtransform.Matcher)1 Output (org.openstreetmap.osmosis.tagtransform.Output)1 TTEntityType (org.openstreetmap.osmosis.tagtransform.TTEntityType)1 Translation (org.openstreetmap.osmosis.tagtransform.Translation)1