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;
}
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;
}
Aggregations