use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project GeoGig by boundlessgeo.
the class OSMUnmapOp method unmapNode.
private void unmapNode(SimpleFeature feature, FeatureMapFlusher mapFlusher) {
boolean modified = false;
String id = feature.getID();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.nodeType());
Optional<RevFeature> rawFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.NODE_TYPE_NAME + "/" + id).call(RevFeature.class);
Map<String, String> tagsMap = Maps.newHashMap();
long timestamp = System.currentTimeMillis();
int version = 1;
long changeset = -1;
String user = UNKNOWN_USER;
Collection<Tag> tags = Lists.newArrayList();
if (rawFeature.isPresent()) {
ImmutableList<Optional<Object>> values = rawFeature.get().getValues();
tags = OSMUtils.buildTagsCollectionFromString(values.get(NODE_TAGS_FIELD_INDEX).get().toString());
for (Tag tag : tags) {
tagsMap.put(tag.getKey(), tag.getValue());
}
Optional<Object> timestampOpt = values.get(NODE_TIMESTAMP_FIELD_INDEX);
if (timestampOpt.isPresent()) {
timestamp = ((Long) timestampOpt.get()).longValue();
}
Optional<Object> versionOpt = values.get(NODE_VERSION_FIELD_INDEX);
if (versionOpt.isPresent()) {
version = ((Integer) versionOpt.get()).intValue();
}
Optional<Object> changesetOpt = values.get(NODE_CHANGESET_FIELD_INDEX);
if (changesetOpt.isPresent()) {
changeset = ((Long) changesetOpt.get()).longValue();
}
Optional<Object> userOpt = values.get(NODE_USER_FIELD_INDEX);
if (userOpt.isPresent()) {
user = (String) userOpt.get();
}
}
Map<String, String> unaliased = Maps.newHashMap();
Collection<Property> properties = feature.getProperties();
for (Property property : properties) {
String name = property.getName().getLocalPart();
if (name.equals("id") || Geometry.class.isAssignableFrom(property.getDescriptor().getType().getBinding())) {
continue;
}
Object value = property.getValue();
if (value != null) {
String tagName = name;
if (mapping != null) {
if (unaliased.containsKey(name)) {
tagName = unaliased.get(name);
} else {
tagName = mapping.getTagNameFromAlias(path, tagName);
unaliased.put(name, tagName);
}
}
if (!DefaultField.isDefaultField(tagName)) {
if (tagsMap.containsKey(tagName)) {
if (!modified) {
String oldValue = tagsMap.get(tagName);
modified = !value.equals(oldValue);
}
} else {
modified = true;
}
tagsMap.put(tagName, value.toString());
}
}
}
if (!modified && rawFeature.isPresent()) {
// no changes after unmapping tags, so there's nothing else to do
return;
}
Collection<Tag> newTags = Lists.newArrayList();
Set<Entry<String, String>> entries = tagsMap.entrySet();
for (Entry<String, String> entry : entries) {
newTags.add(new Tag(entry.getKey(), entry.getValue()));
}
featureBuilder.set("tags", OSMUtils.buildTagsString(newTags));
featureBuilder.set("location", feature.getDefaultGeometry());
featureBuilder.set("changeset", changeset);
featureBuilder.set("timestamp", timestamp);
featureBuilder.set("version", version);
featureBuilder.set("user", user);
featureBuilder.set("visible", true);
if (rawFeature.isPresent()) {
// the feature has changed, so we cannot reuse some attributes.
// We reconstruct the feature and insert it
featureBuilder.set("timestamp", System.currentTimeMillis());
featureBuilder.set("changeset", null);
featureBuilder.set("version", null);
featureBuilder.set("visible", true);
mapFlusher.put("node", featureBuilder.buildFeature(id));
} else {
// The feature didn't exist, so we have to add it
mapFlusher.put("node", featureBuilder.buildFeature(id));
}
}
use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project GeoGig by boundlessgeo.
the class MappingRule method hasCorrectTags.
private boolean hasCorrectTags(Feature feature, Collection<Tag> tags) {
if (filter.isEmpty() || (filter.size() == 1 && filter.containsKey("geom")) && (exclude == null || exclude.isEmpty())) {
return true;
}
boolean ret = false;
ArrayList<String> tagNames = Lists.newArrayList();
for (Tag tag : tags) {
tagNames.add(tag.getKey());
if (exclude != null && exclude.keySet().contains(tag.getKey())) {
List<String> values = exclude.get(tag.getKey());
if (values != null) {
if (values.isEmpty() || values.contains(tag.getValue())) {
return false;
}
}
}
if (filter.keySet().contains(tag.getKey())) {
List<String> values = filter.get(tag.getKey());
if (values.isEmpty() || values.contains(tag.getValue())) {
ret = true;
}
}
}
if (ret) {
for (String mandatory : getMandatoryTags()) {
if (!tagNames.contains(mandatory)) {
return false;
}
}
}
return ret;
}
use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project GeoGig by boundlessgeo.
the class OSMUtils method buildTagsCollectionFromString.
public static Collection<Tag> buildTagsCollectionFromString(String tagsString) {
Collection<Tag> tags = Lists.newArrayList();
if (tagsString != null) {
String[] tokens = tagsString.split("\\|");
for (String token : tokens) {
int idx = token.lastIndexOf(':');
if (idx != -1) {
Tag tag = new Tag(token.substring(0, idx), token.substring(idx + 1));
tags.add(tag);
} else {
LOGGER.info("found tag token '{}' with no value in tagString '{}'", token, tagsString);
}
}
}
return tags;
}
use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project requery by requery.
the class UpsertTest method testInsertOneToManyInsert.
@Test
public void testInsertOneToManyInsert() {
Event event = new Event();
UUID id = UUID.randomUUID();
event.setId(id);
event.setName("test");
Tag t1 = new Tag();
t1.setId(UUID.randomUUID());
Tag t2 = new Tag();
t2.setId(UUID.randomUUID());
event.getTags().add(t1);
event.getTags().add(t2);
data.insert(event);
HashSet<Tag> set = new HashSet<>(event.getTags());
assertEquals(2, set.size());
assertTrue(set.containsAll(Arrays.asList(t1, t2)));
assertSame(2, data.select(Tag.class).get().toList().size());
}
use of org.openstreetmap.osmosis.core.domain.v0_6.Tag in project opennms by OpenNMS.
the class MonitoringLocationsMigratorOffline method execute.
@Override
public void execute() throws OnmsUpgradeException {
if (monitoringLocationsConfig == null)
return;
log("Moving monitoring locations into the database...\n");
long count = 0;
try {
Connection connection = null;
final DBUtils dbUtils = new DBUtils(getClass());
try {
connection = DataSourceFactory.getInstance().getConnection();
dbUtils.watch(connection);
PreparedStatement insertLocation = connection.prepareStatement("INSERT INTO monitoringlocations (id, monitoringarea, geolocation, latitude, longitude, priority) VALUES (?,?,?,?,?,?)");
PreparedStatement insertPollingPackage = connection.prepareStatement("INSERT INTO monitoringlocationspollingpackages (monitoringlocationid, packagename) VALUES (?,?)");
PreparedStatement insertCollectionPackage = connection.prepareStatement("INSERT INTO monitoringlocationscollectionpackages (monitoringlocationid, packagename) VALUES (?,?)");
PreparedStatement insertTag = connection.prepareStatement("INSERT INTO monitoringlocationstags (monitoringlocationid, tag) VALUES (?,?)");
dbUtils.watch(insertLocation);
dbUtils.watch(insertPollingPackage);
dbUtils.watch(insertCollectionPackage);
dbUtils.watch(insertTag);
for (LocationDef location : monitoringLocationsConfig.getLocations()) {
// id
insertLocation.setString(1, location.getLocationName());
// monitoringarea
insertLocation.setString(2, location.getMonitoringArea());
if (location.getGeolocation() != null && !"".equals(location.getGeolocation().trim())) {
// geolocation
insertLocation.setString(3, location.getGeolocation());
} else {
insertLocation.setNull(3, Types.VARCHAR);
}
if (location.getCoordinates() != null && !"".equals(location.getCoordinates())) {
String[] latLong = location.getCoordinates().split(",");
if (latLong.length == 2) {
// latitude
insertLocation.setDouble(4, Double.valueOf(latLong[0]));
// longitude
insertLocation.setDouble(5, Double.valueOf(latLong[1]));
} else {
insertLocation.setNull(4, Types.DOUBLE);
insertLocation.setNull(5, Types.DOUBLE);
}
} else {
insertLocation.setNull(4, Types.DOUBLE);
insertLocation.setNull(5, Types.DOUBLE);
}
if (location.getPriority() == null) {
// priority
insertLocation.setNull(6, Types.INTEGER);
} else {
// priority
insertLocation.setLong(6, location.getPriority());
}
insertLocation.execute();
count++;
if (location.getPollingPackageName() != null && !"".equals(location.getPollingPackageName())) {
// monitoringlocationid
insertPollingPackage.setString(1, location.getLocationName());
// packagename
insertPollingPackage.setString(2, location.getPollingPackageName());
insertPollingPackage.execute();
}
if (location.getCollectionPackageName() != null && !"".equals(location.getCollectionPackageName())) {
// monitoringlocationid
insertCollectionPackage.setString(1, location.getLocationName());
// packagename
insertCollectionPackage.setString(2, location.getCollectionPackageName());
insertCollectionPackage.execute();
}
for (Tag tag : location.getTags()) {
if (tag.getName() != null && !"".equals(tag.getName().trim())) {
// monitoringlocationid
insertTag.setString(1, location.getLocationName());
// tag
insertTag.setString(2, tag.getName());
insertTag.execute();
}
}
}
} finally {
dbUtils.cleanUp();
}
} catch (Throwable e) {
throw new OnmsUpgradeException("Can't fix services configuration because " + e.getMessage(), e);
}
log("Moved %d monitoring locations into the database\n", count);
}
Aggregations