Search in sources :

Example 1 with DataSource

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

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

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

the class TransformLoader method parseDataSources.

private Map<String, DataSource> parseDataSources(File parentDir, Element parent) {
    NodeList children = parent.getChildNodes();
    Map<String, DataSource> dataSources = new HashMap<>();
    for (int i = 0; i < children.getLength(); i++) {
        if (!(children.item(i) instanceof Element)) {
            continue;
        }
        Element child = (Element) children.item(i);
        String name = child.getNodeName();
        if (name.equals("source")) {
            DataSource ds;
            String id = child.getAttribute("source_id");
            try {
                ds = DataSources.valueOf(child.getAttribute("type")).create(parentDir, child.getAttributes());
                dataSources.put(id, ds);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(DataSourceCSV.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    return dataSources;
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) DataSource(org.openstreetmap.osmosis.tagtransform.DataSource)

Aggregations

DataSource (org.openstreetmap.osmosis.tagtransform.DataSource)3 Element (org.w3c.dom.Element)2 NodeList (org.w3c.dom.NodeList)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Match (org.openstreetmap.osmosis.tagtransform.Match)1 Matcher (org.openstreetmap.osmosis.tagtransform.Matcher)1 Output (org.openstreetmap.osmosis.tagtransform.Output)1