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;
}
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;
}
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();
}
Aggregations