From c288c75a48a7ddd1d2a9431ec803ed1013452e90 Mon Sep 17 00:00:00 2001 From: Chumva Date: Thu, 26 Jul 2018 19:23:57 +0300 Subject: [PATCH] add ability to offline edit osm edits --- .../main/java/net/osmand/osm/edit/Entity.java | 2 +- .../net/osmand/plus/osmedit/EditPoiData.java | 6 +- .../plus/osmedit/OpenstreetmapRemoteUtil.java | 27 +++++--- .../plus/osmedit/OpenstreetmapUtil.java | 10 +-- .../plus/osmedit/OpenstreetmapsDbHelper.java | 67 +++++++++++++------ .../UploadOpenstreetmapPointAsyncTask.java | 8 +-- 6 files changed, 72 insertions(+), 48 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/edit/Entity.java b/OsmAnd-java/src/main/java/net/osmand/osm/edit/Entity.java index b7222ec7cc..02c1e19b55 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/edit/Entity.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/edit/Entity.java @@ -54,7 +54,7 @@ public abstract class Entity implements Serializable { result = prime * result + ((type == null) ? 0 : type.hashCode()); return result; } - + @Override public String toString() { return type + " " + id; //$NON-NLS-1$ diff --git a/OsmAnd/src/net/osmand/plus/osmedit/EditPoiData.java b/OsmAnd/src/net/osmand/plus/osmedit/EditPoiData.java index 391086afbd..a7dde51bd1 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/EditPoiData.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/EditPoiData.java @@ -90,10 +90,10 @@ public class EditPoiData { } } - private void initTags(Entity node) { + private void initTags(Entity entity) { checkNotInEdit(); - for (String s : node.getTagKeySet()) { - tryAddTag(s, node.getTag(s)); + for (String s : entity.getTagKeySet()) { + tryAddTag(s, entity.getTag(s)); } retrieveType(); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java index ceef2b38b4..93ecf05da3 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java @@ -236,7 +236,7 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { } ser.endTag(null, "node"); //$NON-NLS-1$ } - + private void writeWay(Way way, EntityInfo i, XmlSerializer ser, long changeSetId, String user) throws IllegalArgumentException, IllegalStateException, IOException { ser.startTag(null, "way"); //$NON-NLS-1$ @@ -356,17 +356,22 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { } - public EntityInfo loadNode(Node n) { - long nodeId = n.getId(); // >> 1; + public EntityInfo loadEntity(Entity n) { + long entityId = n.getId(); // >> 1; + boolean isWay = false; + if (n instanceof Way) { // check if entity is a way + isWay = true; + } try { - String res = sendRequest( - getSiteApi() + "api/0.6/node/" + nodeId, "GET", null, ctx.getString(R.string.loading_poi_obj) + nodeId, false); //$NON-NLS-1$ //$NON-NLS-2$ + String api = isWay ? "api/0.6/way/" : "api/0.6/node/"; + String res = sendRequest(getSiteApi() + api + entityId, "GET", null, + ctx.getString(R.string.loading_poi_obj) + entityId, false); //$NON-NLS-1$ //$NON-NLS-2$ if (res != null) { OsmBaseStorage st = new OsmBaseStorage(); st.setConvertTagsToLC(false); st.parseOSM(new ByteArrayInputStream(res.getBytes("UTF-8")), null, null, true); //$NON-NLS-1$ - EntityId id = new Entity.EntityId(EntityType.NODE, nodeId); - Node entity = (Node) st.getRegisteredEntities().get(id); + EntityId id = new Entity.EntityId(isWay ? EntityType.WAY : EntityType.NODE, entityId); + Entity entity = st.getRegisteredEntities().get(id); // merge non existing tags Map updatedTags = new HashMap<>(); for (String tagKey : entity.getTagKeySet()) { @@ -382,7 +387,7 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { } } n.replaceTags(updatedTags); - if(MapUtils.getDistance(n.getLatLon(), entity.getLatLon()) < 10) { + if (!isWay && MapUtils.getDistance(n.getLatLon(), entity.getLatLon()) < 10) { // avoid shifting due to round error n.setLatitude(entity.getLatitude()); n.setLongitude(entity.getLongitude()); @@ -393,7 +398,7 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { } } catch (IOException | XmlPullParserException e) { - log.error("Loading node failed " + nodeId, e); //$NON-NLS-1$ + log.error("Loading entity failed " + entityId, e); //$NON-NLS-1$ Toast.makeText(ctx, ctx.getResources().getString(R.string.shared_string_io_error), Toast.LENGTH_LONG).show(); } @@ -406,8 +411,8 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { } } - private boolean deletedTag(Node node, String tag) { - return node.getTagKeySet().contains(EditPoiData.REMOVE_TAG_PREFIX + tag); + private boolean deletedTag(Entity entity, String tag) { + return entity.getTagKeySet().contains(EditPoiData.REMOVE_TAG_PREFIX + tag); } @Override diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapUtil.java index b620199e07..1ff114ea4b 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapUtil.java @@ -9,12 +9,12 @@ import net.osmand.osm.edit.EntityInfo; import java.util.Set; public interface OpenstreetmapUtil { - + EntityInfo getEntityInfo(long id); - + Entity commitEntityImpl(OsmPoint.Action action, Entity n, EntityInfo info, String comment, boolean closeChangeSet, @Nullable Set changedTags); - + void closeChangeSet(); - + Entity loadEntity(Amenity n); -} +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapsDbHelper.java b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapsDbHelper.java index 08d73c1702..4609217588 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapsDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapsDbHelper.java @@ -5,7 +5,9 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import net.osmand.osm.edit.Entity; import net.osmand.osm.edit.Node; +import net.osmand.osm.edit.Way; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -18,7 +20,7 @@ import java.util.Set; public class OpenstreetmapsDbHelper extends SQLiteOpenHelper { - private static final int DATABASE_VERSION = 5; + private static final int DATABASE_VERSION = 6; public static final String OPENSTREETMAP_DB_NAME = "openstreetmap"; //$NON-NLS-1$ private static final String OPENSTREETMAP_TABLE_NAME = "openstreetmaptable"; //$NON-NLS-1$ private static final String OPENSTREETMAP_COL_ID = "id"; //$NON-NLS-1$ @@ -28,11 +30,13 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper { private static final String OPENSTREETMAP_COL_ACTION = "action"; //$NON-NLS-1$ private static final String OPENSTREETMAP_COL_COMMENT = "comment"; //$NON-NLS-1$ private static final String OPENSTREETMAP_COL_CHANGED_TAGS = "changed_tags"; + private static final String OPENSTREETMAP_COL_ENTITY_TYPE = "entity_type"; private static final String OPENSTREETMAP_TABLE_CREATE = "CREATE TABLE " + OPENSTREETMAP_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$ OPENSTREETMAP_COL_ID + " bigint,"+ OPENSTREETMAP_COL_LAT + " double," + OPENSTREETMAP_COL_LON + " double," + OPENSTREETMAP_COL_TAGS + " VARCHAR(2048)," + - OPENSTREETMAP_COL_ACTION + " TEXT, " + OPENSTREETMAP_COL_COMMENT + " TEXT, " + OPENSTREETMAP_COL_CHANGED_TAGS + " TEXT);"; + OPENSTREETMAP_COL_ACTION + " TEXT, " + OPENSTREETMAP_COL_COMMENT + " TEXT," + + " " + OPENSTREETMAP_COL_CHANGED_TAGS + " TEXT, " + OPENSTREETMAP_COL_ENTITY_TYPE + " TEXT);"; List cache = null; public OpenstreetmapsDbHelper(Context context) { @@ -53,6 +57,9 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper { if (oldVersion < 5) { db.execSQL("ALTER TABLE " + OPENSTREETMAP_TABLE_NAME + " ADD " + OPENSTREETMAP_COL_CHANGED_TAGS + " TEXT"); } + if (oldVersion < 6) { + db.execSQL("ALTER TABLE " + OPENSTREETMAP_TABLE_NAME + " ADD " + OPENSTREETMAP_COL_ENTITY_TYPE + " TEXT"); + } } public List getOpenstreetmapPoints() { @@ -66,7 +73,8 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper { SQLiteDatabase db = getWritableDatabase(); if (db != null) { StringBuilder tags = new StringBuilder(); - Iterator> eit = p.getEntity().getTags().entrySet().iterator(); + Entity entity = p.getEntity(); + Iterator> eit = entity.getTags().entrySet().iterator(); while(eit.hasNext()) { Entry e = eit.next(); if(Algorithms.isEmpty(e.getKey()) || Algorithms.isEmpty(e.getValue())) { @@ -97,10 +105,12 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper { OPENSTREETMAP_COL_TAGS + ", " + OPENSTREETMAP_COL_ACTION + ", " + OPENSTREETMAP_COL_COMMENT + ", " + - OPENSTREETMAP_COL_CHANGED_TAGS + ")" + - " VALUES (?, ?, ?, ?, ?, ?, ?)", + OPENSTREETMAP_COL_CHANGED_TAGS + ", " + + OPENSTREETMAP_COL_ENTITY_TYPE + ")" + + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{p.getId(), p.getLatitude(), p.getLongitude(), tags.toString(), - OsmPoint.stringAction.get(p.getAction()), p.getComment(), chTags == null ? null : changedTags.toString()}); + OsmPoint.stringAction.get(p.getAction()), p.getComment(), + chTags == null ? null : changedTags.toString(), Entity.EntityType.valueOf(entity)}); db.close(); checkOpenstreetmapPoints(); return true; @@ -134,27 +144,42 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper { OPENSTREETMAP_COL_ACTION + "," + OPENSTREETMAP_COL_COMMENT + "," + OPENSTREETMAP_COL_TAGS + "," + - OPENSTREETMAP_COL_CHANGED_TAGS + + OPENSTREETMAP_COL_CHANGED_TAGS + "," + + OPENSTREETMAP_COL_ENTITY_TYPE + " FROM " + OPENSTREETMAP_TABLE_NAME, null); if (query.moveToFirst()) { do { OpenstreetmapPoint p = new OpenstreetmapPoint(); - Node entity = new Node(query.getDouble(1), - query.getDouble(2), - query.getLong(0)); - String tags = query.getString(5); - String[] split = tags.split("\\$\\$\\$"); - for(int i=0; i(Arrays.asList(changedTags.split("\\$\\$\\$")))); + if (entity != null) { + String tags = query.getString(5); + String[] split = tags.split("\\$\\$\\$"); + for (int i = 0; i < split.length - 1; i += 2) { + entity.putTagNoLC(split[i].trim(), split[i + 1].trim()); + } + String changedTags = query.getString(6); + if (changedTags != null) { + entity.setChangedTags(new HashSet<>(Arrays.asList(changedTags.split("\\$\\$\\$")))); + } + p.setEntity(entity); + p.setAction(query.getString(3)); + p.setComment(query.getString(4)); + points.add(p); } - p.setEntity(entity); - p.setAction(query.getString(3)); - p.setComment(query.getString(4)); - points.add(p); + } while (query.moveToNext()); } query.close(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java index 5a88ab060d..1d99aee0c0 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java @@ -54,13 +54,7 @@ public class UploadOpenstreetmapPointAsyncTask OpenstreetmapPoint p = (OpenstreetmapPoint) point; EntityInfo entityInfo = null; if (OsmPoint.Action.CREATE != p.getAction()) { - Entity entity = p.getEntity(); - if (entity instanceof Node) { - entityInfo = remotepoi.loadNode((Node) entity); - } else { - loadErrorsMap.put(point, "Entity is not node"); - return loadErrorsMap; - } + entityInfo = remotepoi.loadEntity(p.getEntity()); } Entity n = remotepoi.commitEntityImpl(p.getAction(), p.getEntity(), entityInfo, p.getComment(), false, null);