Search in sources :

Example 6 with Tag

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));
    }
}
Also used : Optional(com.google.common.base.Optional) LineString(com.vividsolutions.jts.geom.LineString) Point(com.vividsolutions.jts.geom.Point) Geometry(com.vividsolutions.jts.geom.Geometry) ReadOSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.ReadOSMMappingLogEntry) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) OSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry) RevFeature(org.locationtech.geogig.api.RevFeature) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) Property(org.opengis.feature.Property) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 7 with Tag

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;
}
Also used : Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag)

Example 8 with Tag

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;
}
Also used : Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag)

Example 9 with Tag

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());
}
Also used : Event(io.requery.test.model3.Event) Tag(io.requery.test.model3.Tag) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with Tag

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);
}
Also used : LocationDef(org.opennms.upgrade.implementations.monitoringLocations16.LocationDef) Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) PreparedStatement(java.sql.PreparedStatement) Tag(org.opennms.upgrade.implementations.monitoringLocations16.Tag) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Aggregations

Tag (org.openstreetmap.osmosis.core.domain.v0_6.Tag)10 IOException (java.io.IOException)5 Point (com.vividsolutions.jts.geom.Point)4 Event (io.requery.test.model3.Event)4 Tag (io.requery.test.model3.Tag)4 Test (org.junit.Test)4 Geometry (com.vividsolutions.jts.geom.Geometry)3 UUID (java.util.UUID)3 WayNode (org.openstreetmap.osmosis.core.domain.v0_6.WayNode)3 NoConnectionsException (org.voltdb.client.NoConnectionsException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Optional (com.google.common.base.Optional)2 LineString (com.vividsolutions.jts.geom.LineString)2 EntityMapper (io.requery.jackson.EntityMapper)2 StringWriter (java.io.StringWriter)2 Entry (java.util.Map.Entry)2 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)2 RevFeature (org.locationtech.geogig.api.RevFeature)2 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)2 OSMMappingLogEntry (org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry)2