Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add match by osm entity ID #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface Matcher {

Collection<Match> match(Map<String, String> tags, TTEntityType type, String uname, int uid);
Collection<Match> match(long id, Map<String, String> tags, TTEntityType type, String uname, int uid);


void outputStats(StringBuilder output, String indent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface Translation {

Collection<Match> match(Map<String, String> tags, TTEntityType entityType, String uname, int uid);
Collection<Match> match(long id, Map<String, String> tags, TTEntityType entityType, String uname, int uid);


Map<String, DataSource> getDataSources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public AndMatcher(Collection<Matcher> matchers, TTEntityType type, String uname,


@Override
public Collection<Match> match(Map<String, String> tags, TTEntityType entityType, String entityUname,
int entityUid) {
public Collection<Match> match(long entityId, Map<String, String> tags, TTEntityType entityType, String entityUname,
int entityUid) {
if (this.type != null && this.type != entityType) {
return null;
}
Expand All @@ -43,7 +43,7 @@ public Collection<Match> match(Map<String, String> tags, TTEntityType entityType

List<Match> allMatches = new ArrayList<Match>();
for (Matcher matcher : matchers) {
Collection<Match> matches = matcher.match(tags, entityType, entityUname, entityUid);
Collection<Match> matches = matcher.match(entityId, tags, entityType, entityUname, entityUid);
if (matches == null || matches.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This software is released into the Public Domain. See copying.txt for details.
package org.openstreetmap.osmosis.tagtransform.impl;

import org.openstreetmap.osmosis.tagtransform.Match;
import org.openstreetmap.osmosis.tagtransform.Matcher;
import org.openstreetmap.osmosis.tagtransform.TTEntityType;

import java.util.*;


public class IdMatcher implements Matcher {

private Set<Long> ids;
private long matchHits;

IdMatcher(Set<Long> ids) {
this.ids = ids;
}

@Override
public Collection<Match> match(long id, Map<String, String> tags, TTEntityType type, String uname, int uid) {
if (!ids.contains(id)) {
return null;
}
matchHits += 1;
return Collections.singleton(NULL_MATCH);
}


@Override
public void outputStats(StringBuilder output, String indent) {
output.append(indent);
output.append("Ids[");
int i = 0;
for (Long id: ids) {
if (++i>1) {
output.append(",");
}
if (i>5) {
output.append("...");
break;
}
output.append(id);
}
output.append("]: ");
output.append(matchHits);
output.append('\n');
}

private static final Match NULL_MATCH = new Match() {
@Override
public int getValueGroupCount() {
return 0;
}


@Override
public String getValue(int group) {
return null;
}


@Override
public String getMatchID() {
return null;
}


@Override
public int getKeyGroupCount() {
return 0;
}


@Override
public String getKey(int group) {
return null;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public NoTagMatcher(String keyPattern, String valuePattern) {


@Override
public Collection<Match> match(Map<String, String> tags, TTEntityType type, String uname, int uid) {
public Collection<Match> match(long id, Map<String, String> tags, TTEntityType type, String uname, int uid) {
// loop through the tags to find matches
for (Entry<String, String> tag : tags.entrySet()) {
java.util.regex.Matcher keyMatch = keyPattern.matcher(tag.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public OrMatcher(Collection<Matcher> matchers, TTEntityType type, String uname,


@Override
public Collection<Match> match(Map<String, String> tags, TTEntityType entityType, String entityUname,
int entityUid) {
public Collection<Match> match(long entityId, Map<String, String> tags, TTEntityType entityType, String entityUname,
int entityUid) {
if (this.type != null && this.type != entityType) {
return null;
}
Expand All @@ -43,7 +43,7 @@ public Collection<Match> match(Map<String, String> tags, TTEntityType entityType

List<Match> allMatches = new ArrayList<Match>();
for (Matcher matcher : matchers) {
Collection<Match> matches = matcher.match(tags, entityType, entityUname, entityUid);
Collection<Match> matches = matcher.match(entityId, tags, entityType, entityUname, entityUid);
if (matches != null) {
allMatches.addAll(matches);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public TagMatcher(String matchID, String keyPattern, String valuePattern) {


@Override
public Collection<Match> match(Map<String, String> tags, TTEntityType type, String uname, int uid) {
public Collection<Match> match(long id, Map<String, String> tags, TTEntityType type, String uname, int uid) {
List<Match> matches = new ArrayList<Match>();

// loop through the tags to find matches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected EntityContainer processEntityContainer(EntityContainer entityContainer
Entity entity = entityContainer.getEntity();
Collection<Tag> entityTags = entity.getTags();
EntityType entityType = entity.getType();
long id = entity.getId();

// Store the tags in a map keyed by tag key.
Map<String, String> tagMap = new HashMap<String, String>();
Expand All @@ -125,7 +126,7 @@ protected EntityContainer processEntityContainer(EntityContainer entityContainer

// Apply tag transformations.
for (Translation translation : translations) {
Collection<Match> matches = translation.match(tagMap, TTEntityType.fromEntityType06(entityType), entity
Collection<Match> matches = translation.match(id, tagMap, TTEntityType.fromEntityType06(entityType), entity
.getUser().getName(), entity.getUser().getId());
if (matches == null || matches.isEmpty()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
Expand Down Expand Up @@ -162,6 +164,8 @@ private Matcher parseMatcher(Element matcher) {
String k = matcher.getAttribute("k");
String v = matcher.getAttribute("v");
return new NoTagMatcher(k, v);
} else if (name.equals("ids")) {
return parseIds(matcher);
}
return null;
}
Expand Down Expand Up @@ -189,6 +193,23 @@ private Map<String, DataSource> parseDataSources(File parentDir, Element parent)
return dataSources;
}

private Matcher parseIds(Element idsElement) {
NodeList children = idsElement.getChildNodes();
Set<Long> ids = new HashSet<>();
for (int i = 0; i < children.getLength(); i++) {
if (!(children.item(i) instanceof Element)) {
continue;
}

Element element = (Element)children.item(i);
if (element.getTagName().equals("id")) {
Long id = Long.valueOf(element.getTextContent());
ids.add(id);
}
}
return new IdMatcher(ids);
}

private TTEntityType getType(String type) {
if (type == null || type.isEmpty() || type.equals("all")) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public boolean isDropOnMatch() {


@Override
public Collection<Match> match(Map<String, String> tags, TTEntityType type, String uname, int uid) {
Collection<Match> matches = matcher.match(tags, type, uname, uid);
public Collection<Match> match(long id, Map<String, String> tags, TTEntityType type, String uname, int uid) {
Collection<Match> matches = matcher.match(id, tags, type, uname, uid);
if (matches != null && !matches.isEmpty()) {
Collection<Match> finds;
if (finder == null) {
finds = null;
} else {
finds = finder.match(tags, type, uname, uid);
finds = finder.match(id, tags, type, uname, uid);
}
if (finds != null && !finds.isEmpty()) {
if (matches instanceof ArrayList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
<tag k="crossing" v="toucan"/>
<tag k="highway" v="crossing"/>
</node>
<way id="4" version="1" timestamp="2007-01-29T08:48:14Z"/>
<way id="4" version="1" timestamp="2007-01-29T08:48:14Z">
<tag k="highway" v="secondary"/>
</way>
<way id="5" version="1" timestamp="2007-01-29T08:48:14Z">
<tag k="highway" v="motorway"/>
</way>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,19 @@
</output>
</translation>
-->

<translation>
<name>Add tag to feature identified by id</name>
<description>Add a tag to a specific feature, if it does not exist already</description>
<match type="way" mode="and">
<ids>
<id>4</id>
<id>5</id>
</ids>
<notag k="highway" v=".*"/>
</match>
<output>
<copy-all/>
<tag k="highway" v="secondary"/>
</output>
</translation>
</translations>