Search in sources :

Example 1 with OsmElementKey

use of de.westnordost.streetcomplete.data.osm.persist.OsmElementKey in project StreetComplete by westnordost.

the class OsmQuestDownload method getPreviousQuestsIdsByElementKey.

private Map<OsmElementKey, Long> getPreviousQuestsIdsByElementKey(OsmElementQuestType questType, BoundingBox bbox) {
    String questTypeName = questType.getClass().getSimpleName();
    Map<OsmElementKey, Long> result = new HashMap<>();
    for (OsmQuest quest : osmQuestDB.getAll(bbox, null, questTypeName, null, null)) {
        result.put(new OsmElementKey(quest.getElementType(), quest.getElementId()), quest.getId());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest) OsmElementKey(de.westnordost.streetcomplete.data.osm.persist.OsmElementKey)

Example 2 with OsmElementKey

use of de.westnordost.streetcomplete.data.osm.persist.OsmElementKey in project StreetComplete by westnordost.

the class OsmQuestDownload method download.

public boolean download(final OsmElementQuestType questType, BoundingBox bbox, final Set<LatLon> blacklistedPositions) {
    if (!isQuestTypeEnabledForBoundingBox(questType, bbox)) {
        Log.i(TAG, getQuestTypeName(questType) + ": " + "Skipped because it is disabled for this country");
        return true;
    }
    final ArrayList<ElementGeometryDao.Row> geometryRows = new ArrayList<>();
    final Map<OsmElementKey, Element> elements = new HashMap<>();
    final ArrayList<OsmQuest> quests = new ArrayList<>();
    final Map<OsmElementKey, Long> previousQuests = getPreviousQuestsIdsByElementKey(questType, bbox);
    long time = System.currentTimeMillis();
    boolean success = questType.download(bbox, (element, geometry) -> {
        if (mayCreateQuestFrom(questType, element, geometry, blacklistedPositions)) {
            Element.Type elementType = element.getType();
            long elementId = element.getId();
            OsmQuest quest = new OsmQuest(questType, elementType, elementId, geometry);
            geometryRows.add(new ElementGeometryDao.Row(elementType, elementId, quest.getGeometry()));
            quests.add(quest);
            OsmElementKey elementKey = new OsmElementKey(elementType, elementId);
            elements.put(elementKey, element);
            previousQuests.remove(elementKey);
        }
    });
    if (!success)
        return false;
    // geometry and elements must be put into DB first because quests have foreign keys on it
    geometryDB.putAll(geometryRows);
    elementDB.putAll(elements.values());
    int newQuestsByQuestType = osmQuestDB.addAll(quests);
    if (questListener != null) {
        Iterator<OsmQuest> it = quests.iterator();
        while (it.hasNext()) {
            // it is null if this quest is already in the DB, so don't call onQuestCreated
            if (it.next().getId() == null)
                it.remove();
        }
        if (!quests.isEmpty())
            questListener.onQuestsCreated(quests, QuestGroup.OSM);
    }
    if (!previousQuests.isEmpty()) {
        if (questListener != null) {
            questListener.onQuestsRemoved(previousQuests.values(), QuestGroup.OSM);
        }
        osmQuestDB.deleteAll(previousQuests.values());
    }
    // note: this could be done after ALL osm quest types have been downloaded if this
    // turns out to be slow if done for every quest type
    geometryDB.deleteUnreferenced();
    elementDB.deleteUnreferenced();
    int obsoleteAmount = previousQuests.size();
    Log.i(TAG, getQuestTypeName(questType) + ": " + "Added " + newQuestsByQuestType + " new and " + "removed " + obsoleteAmount + " already resolved quests." + " (Total: " + quests.size() + ")" + " in " + (System.currentTimeMillis() - time) + "ms");
    return true;
}
Also used : HashMap(java.util.HashMap) Element(de.westnordost.osmapi.map.data.Element) ArrayList(java.util.ArrayList) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest) OsmElementKey(de.westnordost.streetcomplete.data.osm.persist.OsmElementKey) ElementGeometryDao(de.westnordost.streetcomplete.data.osm.persist.ElementGeometryDao)

Aggregations

OsmQuest (de.westnordost.streetcomplete.data.osm.OsmQuest)2 OsmElementKey (de.westnordost.streetcomplete.data.osm.persist.OsmElementKey)2 HashMap (java.util.HashMap)2 Element (de.westnordost.osmapi.map.data.Element)1 ElementGeometryDao (de.westnordost.streetcomplete.data.osm.persist.ElementGeometryDao)1 ArrayList (java.util.ArrayList)1