use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class MainActivity method confirmUndo.
private void confirmUndo(final OsmQuest quest) {
Element element = questController.getOsmElement(quest);
View inner = LayoutInflater.from(this).inflate(R.layout.dialog_undo, null, false);
ImageView icon = inner.findViewById(R.id.icon);
icon.setImageResource(quest.getType().getIcon());
TextView text = inner.findViewById(R.id.text);
text.setText(QuestUtil.getHtmlTitle(getResources(), quest.getType(), element));
new AlertDialogBuilder(this).setTitle(R.string.undo_confirm_title).setView(inner).setPositiveButton(R.string.undo_confirm_positive, (dialog, which) -> {
questController.undoOsmQuest(quest);
unsyncedChangesCounter.decrement(quest.getChangesSource());
}).setNegativeButton(R.string.undo_confirm_negative, null).show();
}
use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class QuestController method solveOsmQuest.
private boolean solveOsmQuest(long questId, Bundle answer, String source) {
// race condition: another thread (i.e. quest download thread) may have removed the
// element already (#282). So in this case, just ignore
OsmQuest q = osmQuestDB.get(questId);
if (q == null)
return false;
Element element = osmElementDB.get(q.getElementType(), q.getElementId());
if (element == null)
return false;
StringMapChanges changes;
try {
StringMapChangesBuilder changesBuilder = new StringMapChangesBuilder(element.getTags());
q.getOsmElementQuestType().applyAnswerTo(answer, changesBuilder);
changes = changesBuilder.create();
} catch (IllegalArgumentException e) {
// if applying the changes results in an error (=a conflict), the data the quest(ion)
// was based on is not valid anymore -> like with other conflicts, silently drop the
// user's change (#289) and the quest
osmQuestDB.delete(questId);
return false;
}
if (!changes.isEmpty()) {
q.setChanges(changes, source);
q.setStatus(QuestStatus.ANSWERED);
osmQuestDB.update(q);
openChangesetsDao.setLastQuestSolvedTimeToNow();
return true;
} else {
throw new RuntimeException("OsmQuest " + questId + " (" + q.getType().getClass().getSimpleName() + ") has been answered by the user but the changeset is empty!");
}
}
use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class QuestController method retrieveNextAt.
public void retrieveNextAt(long questId, QuestGroup group) {
workerHandler.post(() -> {
if (group == QuestGroup.OSM) {
OsmQuest osmQuest = osmQuestDB.getNextNewAt(questId, getQuestTypeNames());
if (osmQuest == null)
return;
Element element = osmElementDB.get(osmQuest.getElementType(), osmQuest.getElementId());
if (element == null)
return;
relay.onQuestSelected(osmQuest, group, element);
}
});
}
use of de.westnordost.osmapi.map.data.Element 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;
}
use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.
the class OverpassMapDataParser method onEndElement.
@Override
protected void onEndElement() {
String name = getName();
Element element = null;
ElementGeometry geometry = null;
switch(name) {
case MEMBER:
wayNodes = null;
break;
case NODE:
Node node = factory.createNode(id, version, lat, lon, tags, null, null);
geometry = elementGeometryCreator.create(node);
element = node;
break;
case WAY:
Way way = factory.createWay(id, version, nodes, tags, null, null);
geometry = elementGeometryCreator.create(way);
element = way;
nodes = null;
nodePositionsByWay = null;
wayNodes = null;
break;
case RELATION:
Relation relation = factory.createRelation(id, version, members, tags, null, null);
geometry = elementGeometryCreator.create(relation);
element = relation;
members = null;
nodePositionsByWay = null;
break;
}
if (element != null) {
tags = null;
handler.handle(element, geometry);
}
}
Aggregations