Search in sources :

Example 16 with Element

use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.

the class TagFilterValue method matches.

public boolean matches(Object obj) {
    if (!(obj instanceof Element))
        return false;
    Element element = (Element) obj;
    if (value == null) {
        return element.getTags() != null && element.getTags().containsKey(key);
    }
    String tagValue = element.getTags() != null ? element.getTags().get(key) : null;
    if (tagValue == null)
        return op.startsWith("!");
    if (isValueRegex()) {
        if (op.equals("!~"))
            return !tagValue.matches(value);
        if (op.equals("~"))
            return tagValue.matches(value);
    } else {
        if (op.equals("!="))
            return !tagValue.equals(value);
        if (op.equals("="))
            return tagValue.equals(value);
    }
    return false;
}
Also used : Element(de.westnordost.osmapi.map.data.Element)

Example 17 with Element

use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.

the class AOsmQuestChangesUpload method upload.

public synchronized void upload(AtomicBoolean cancelState) {
    int commits = 0, obsolete = 0;
    changesetIdsCache = new HashMap<>();
    unlockedQuests = new ArrayList<>();
    for (OsmQuest quest : questDB.getAll(null, QuestStatus.ANSWERED)) {
        // break so that the unreferenced stuff is deleted still
        if (cancelState.get())
            break;
        Element element = elementDB.get(quest.getElementType(), quest.getElementId());
        long changesetId = getChangesetIdOrCreate(quest.getOsmElementQuestType(), quest.getChangesSource());
        if (uploadQuestChange(changesetId, quest, element, false, false)) {
            commits++;
        } else {
            obsolete++;
        }
    }
    cleanUp();
    String logMsg = "Committed " + commits + " changes";
    if (obsolete > 0) {
        logMsg += " but dropped " + obsolete + " changes because there were conflicts";
    }
    Log.i(TAG, logMsg);
    if (!unlockedQuests.isEmpty()) {
        int unlockedQuestsCount = unlockedQuests.size();
        if (visibleQuestListener != null) {
            visibleQuestListener.onQuestsCreated(unlockedQuests, QuestGroup.OSM);
        }
        Log.i(TAG, "Unlocked " + unlockedQuestsCount + " new quests");
    }
    closeOpenChangesets();
    if (commits > 0) {
        // changesets are closed delayed after X minutes of inactivity
        ChangesetAutoCloserJob.scheduleJob();
    }
}
Also used : Element(de.westnordost.osmapi.map.data.Element) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest) Point(android.graphics.Point)

Example 18 with Element

use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.

the class AOsmQuestChangesUpload method handleElementConflict.

private boolean handleElementConflict(long changesetId, OsmQuest quest, boolean alreadyHandlingChangesetConflict) {
    Element element = updateElementFromServer(quest.getElementType(), quest.getElementId());
    // element anymore, drop it (#720)
    if (element != null) {
        if (!questIsApplicableToElement(quest, element)) {
            Log.d(TAG, "Dropping quest " + getQuestStringForLog(quest) + " because the quest is no longer applicable to the element");
            deleteConflictingQuest(quest);
            return false;
        }
    }
    return uploadQuestChange(changesetId, quest, element, true, alreadyHandlingChangesetConflict);
}
Also used : Element(de.westnordost.osmapi.map.data.Element)

Example 19 with Element

use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.

the class AOsmQuestChangesUpload method uploadQuestChange.

boolean uploadQuestChange(long changesetId, OsmQuest quest, Element element, boolean alreadyHandlingElementConflict, boolean alreadyHandlingChangesetConflict) {
    Element elementWithChangesApplied = changesApplied(element, quest);
    if (elementWithChangesApplied == null) {
        deleteConflictingQuest(quest);
        return false;
    }
    int[] newVersion = { element.getVersion() };
    try {
        osmDao.uploadChanges(changesetId, Collections.singleton(elementWithChangesApplied), diffElement -> {
            if (diffElement.clientId == elementWithChangesApplied.getId()) {
                newVersion[0] = diffElement.serverVersion;
            /* It is not necessary (yet) to handle updating the element's id because
					   StreetComplete does not add or delete elements */
            }
        });
    } catch (OsmConflictException e) {
        return handleConflict(changesetId, quest, element, alreadyHandlingElementConflict, alreadyHandlingChangesetConflict, e);
    }
    Element updatedElement = copyElement(elementWithChangesApplied, newVersion[0]);
    closeQuest(quest);
    // save with new version when persisting to DB
    elementDB.put(updatedElement);
    statisticsDB.addOne(quest.getType().getClass().getSimpleName());
    unlockedQuests.addAll(questUnlocker.unlockNewQuests(updatedElement));
    return true;
}
Also used : OsmConflictException(de.westnordost.osmapi.common.errors.OsmConflictException) Element(de.westnordost.osmapi.map.data.Element)

Example 20 with Element

use of de.westnordost.osmapi.map.data.Element in project StreetComplete by westnordost.

the class AOsmQuestChangesUpload method changesApplied.

private Element changesApplied(Element element, OsmQuest quest) {
    // The element can be null if it has been deleted in the meantime (outside this app usually)
    if (element == null) {
        Log.d(TAG, "Dropping quest " + getQuestStringForLog(quest) + " because the associated element has already been deleted");
        return null;
    }
    Element copy = copyElement(element, element.getVersion());
    StringMapChanges changes = quest.getChanges();
    if (changes.hasConflictsTo(copy.getTags())) {
        Log.d(TAG, "Dropping quest " + getQuestStringForLog(quest) + " because there has been a conflict while applying the changes");
        return null;
    }
    changes.applyTo(copy.getTags());
    return copy;
}
Also used : StringMapChanges(de.westnordost.streetcomplete.data.osm.changes.StringMapChanges) Element(de.westnordost.osmapi.map.data.Element)

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