Search in sources :

Example 1 with Translation

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

the class TransformLoader method load.

public List<Translation> load(String configFile) {
    List<Translation> translations = new ArrayList<Translation>();
    File file = new File(configFile);
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(file);
        NodeList translationElements = doc.getDocumentElement().getElementsByTagName("translation");
        for (int i = 0; i < translationElements.getLength(); i++) {
            Translation t = parseTranslation(file.getParentFile(), (Element) translationElements.item(i));
            if (t != null) {
                translations.add(t);
            }
        }
    } catch (Exception e) {
        throw new TransformLoadException("Failed to load transform", e);
    }
    return translations;
}
Also used : Translation(org.openstreetmap.osmosis.tagtransform.Translation) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) File(java.io.File)

Example 2 with Translation

use of org.openstreetmap.osmosis.tagtransform.Translation 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 3 with Translation

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

the class TransformHelper method complete.

@Override
public void complete() {
    if (statsFile != null && !statsFile.isEmpty()) {
        StringBuilder builder = new StringBuilder();
        builder.append(configFile);
        builder.append("\n\n");
        for (Translation t : translations) {
            t.outputStats(builder, "");
        }
        Writer writer = null;
        try {
            writer = new FileWriter(new File(statsFile));
            writer.write(builder.toString());
        } catch (IOException e) {
            throw new StatsSaveException("Failed to save stats: " + e.getLocalizedMessage(), e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    logger.log(Level.WARNING, "Unable to close stats file " + statsFile + ".", e);
                }
            }
        }
    }
    sink.complete();
}
Also used : Translation(org.openstreetmap.osmosis.tagtransform.Translation) FileWriter(java.io.FileWriter) IOException(java.io.IOException) StatsSaveException(org.openstreetmap.osmosis.tagtransform.StatsSaveException) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Aggregations

Translation (org.openstreetmap.osmosis.tagtransform.Translation)3 File (java.io.File)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)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 Match (org.openstreetmap.osmosis.tagtransform.Match)1 Output (org.openstreetmap.osmosis.tagtransform.Output)1 StatsSaveException (org.openstreetmap.osmosis.tagtransform.StatsSaveException)1 TTEntityType (org.openstreetmap.osmosis.tagtransform.TTEntityType)1 Document (org.w3c.dom.Document)1 NodeList (org.w3c.dom.NodeList)1