Search in sources :

Example 11 with Element

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();
}
Also used : AlertDialogBuilder(de.westnordost.streetcomplete.view.dialogs.AlertDialogBuilder) Element(de.westnordost.osmapi.map.data.Element) OsmElement(de.westnordost.osmapi.map.data.OsmElement) TextView(android.widget.TextView) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Example 12 with Element

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!");
    }
}
Also used : StringMapChanges(de.westnordost.streetcomplete.data.osm.changes.StringMapChanges) Element(de.westnordost.osmapi.map.data.Element) StringMapChangesBuilder(de.westnordost.streetcomplete.data.osm.changes.StringMapChangesBuilder) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest)

Example 13 with Element

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);
        }
    });
}
Also used : Element(de.westnordost.osmapi.map.data.Element) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest)

Example 14 with 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;
}
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)

Example 15 with Element

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);
    }
}
Also used : Relation(de.westnordost.osmapi.map.data.Relation) Element(de.westnordost.osmapi.map.data.Element) Node(de.westnordost.osmapi.map.data.Node) ElementGeometry(de.westnordost.streetcomplete.data.osm.ElementGeometry) Way(de.westnordost.osmapi.map.data.Way)

Aggregations

Element (de.westnordost.osmapi.map.data.Element)22 OsmQuest (de.westnordost.streetcomplete.data.osm.OsmQuest)9 Point (android.graphics.Point)4 MergedElementDao (de.westnordost.streetcomplete.data.osm.persist.MergedElementDao)4 OsmQuestDao (de.westnordost.streetcomplete.data.osm.persist.OsmQuestDao)4 ArrayList (java.util.ArrayList)4 MapDataDao (de.westnordost.osmapi.map.MapDataDao)3 Node (de.westnordost.osmapi.map.data.Node)3 OsmLatLon (de.westnordost.osmapi.map.data.OsmLatLon)3 Relation (de.westnordost.osmapi.map.data.Relation)3 Way (de.westnordost.osmapi.map.data.Way)3 DownloadedTilesDao (de.westnordost.streetcomplete.data.tiles.DownloadedTilesDao)3 SharedPreferences (android.content.SharedPreferences)2 LongSparseArray (android.util.LongSparseArray)2 OpenChangesetsDao (de.westnordost.streetcomplete.data.changesets.OpenChangesetsDao)2 StringMapChanges (de.westnordost.streetcomplete.data.osm.changes.StringMapChanges)2 HashMap (java.util.HashMap)2 List (java.util.List)2 View (android.view.View)1 ImageView (android.widget.ImageView)1