Search in sources :

Example 1 with Output

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

the class TransformLoader method parseTranslation.

private Translation parseTranslation(File parentDir, Element element) {
    String name = "";
    String description = "";
    Matcher matcher = null;
    Matcher finder = null;
    List<Output> output = new ArrayList<>();
    Map<String, DataSource> dataSources = Collections.emptyMap();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (!(children.item(i) instanceof Element)) {
            continue;
        }
        Element child = (Element) children.item(i);
        String nodeName = child.getNodeName();
        if (nodeName.equals("name")) {
            name = child.getTextContent();
        } else if (nodeName.equals("description")) {
            description = child.getTextContent();
        } else if (nodeName.equals("match")) {
            matcher = parseMatcher(child);
        } else if (nodeName.equals("find")) {
            finder = parseMatcher(child);
        } else if (nodeName.equals("data")) {
            dataSources = parseDataSources(parentDir, child);
        } else if (nodeName.equals("output")) {
            NodeList outputs = child.getChildNodes();
            for (int j = 0; j < outputs.getLength(); j++) {
                if (!(outputs.item(j) instanceof Element)) {
                    continue;
                }
                Output o = parseOutput((Element) outputs.item(j));
                if (o != null) {
                    output.add(o);
                }
            }
        }
    }
    if (matcher != null) {
        LOG.info("New translation: " + name);
        return new TranslationImpl(name, description, matcher, finder, dataSources, output);
    } else {
        return null;
    }
}
Also used : Matcher(org.openstreetmap.osmosis.tagtransform.Matcher) Output(org.openstreetmap.osmosis.tagtransform.Output) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DataSource(org.openstreetmap.osmosis.tagtransform.DataSource)

Example 2 with Output

use of org.openstreetmap.osmosis.tagtransform.Output 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)

Aggregations

Output (org.openstreetmap.osmosis.tagtransform.Output)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)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 Match (org.openstreetmap.osmosis.tagtransform.Match)1 Matcher (org.openstreetmap.osmosis.tagtransform.Matcher)1 TTEntityType (org.openstreetmap.osmosis.tagtransform.TTEntityType)1 Translation (org.openstreetmap.osmosis.tagtransform.Translation)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1