add ability to offline edit osm edits

This commit is contained in:
Chumva 2018-07-26 19:23:57 +03:00
parent 30681c6f64
commit c288c75a48
6 changed files with 72 additions and 48 deletions

View file

@ -90,10 +90,10 @@ public class EditPoiData {
} }
} }
private void initTags(Entity node) { private void initTags(Entity entity) {
checkNotInEdit(); checkNotInEdit();
for (String s : node.getTagKeySet()) { for (String s : entity.getTagKeySet()) {
tryAddTag(s, node.getTag(s)); tryAddTag(s, entity.getTag(s));
} }
retrieveType(); retrieveType();
} }

View file

@ -356,17 +356,22 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
} }
public EntityInfo loadNode(Node n) { public EntityInfo loadEntity(Entity n) {
long nodeId = n.getId(); // >> 1; long entityId = n.getId(); // >> 1;
boolean isWay = false;
if (n instanceof Way) { // check if entity is a way
isWay = true;
}
try { try {
String res = sendRequest( String api = isWay ? "api/0.6/way/" : "api/0.6/node/";
getSiteApi() + "api/0.6/node/" + nodeId, "GET", null, ctx.getString(R.string.loading_poi_obj) + nodeId, false); //$NON-NLS-1$ //$NON-NLS-2$ 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) { if (res != null) {
OsmBaseStorage st = new OsmBaseStorage(); OsmBaseStorage st = new OsmBaseStorage();
st.setConvertTagsToLC(false); st.setConvertTagsToLC(false);
st.parseOSM(new ByteArrayInputStream(res.getBytes("UTF-8")), null, null, true); //$NON-NLS-1$ st.parseOSM(new ByteArrayInputStream(res.getBytes("UTF-8")), null, null, true); //$NON-NLS-1$
EntityId id = new Entity.EntityId(EntityType.NODE, nodeId); EntityId id = new Entity.EntityId(isWay ? EntityType.WAY : EntityType.NODE, entityId);
Node entity = (Node) st.getRegisteredEntities().get(id); Entity entity = st.getRegisteredEntities().get(id);
// merge non existing tags // merge non existing tags
Map<String, String> updatedTags = new HashMap<>(); Map<String, String> updatedTags = new HashMap<>();
for (String tagKey : entity.getTagKeySet()) { for (String tagKey : entity.getTagKeySet()) {
@ -382,7 +387,7 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
} }
} }
n.replaceTags(updatedTags); 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 // avoid shifting due to round error
n.setLatitude(entity.getLatitude()); n.setLatitude(entity.getLatitude());
n.setLongitude(entity.getLongitude()); n.setLongitude(entity.getLongitude());
@ -393,7 +398,7 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
} }
} catch (IOException | XmlPullParserException e) { } 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.makeText(ctx, ctx.getResources().getString(R.string.shared_string_io_error),
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
} }
@ -406,8 +411,8 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
} }
} }
private boolean deletedTag(Node node, String tag) { private boolean deletedTag(Entity entity, String tag) {
return node.getTagKeySet().contains(EditPoiData.REMOVE_TAG_PREFIX + tag); return entity.getTagKeySet().contains(EditPoiData.REMOVE_TAG_PREFIX + tag);
} }
@Override @Override

View file

@ -5,7 +5,9 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import net.osmand.osm.edit.Entity;
import net.osmand.osm.edit.Node; import net.osmand.osm.edit.Node;
import net.osmand.osm.edit.Way;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.util.ArrayList; import java.util.ArrayList;
@ -18,7 +20,7 @@ import java.util.Set;
public class OpenstreetmapsDbHelper extends SQLiteOpenHelper { 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$ 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_TABLE_NAME = "openstreetmaptable"; //$NON-NLS-1$
private static final String OPENSTREETMAP_COL_ID = "id"; //$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_ACTION = "action"; //$NON-NLS-1$
private static final String OPENSTREETMAP_COL_COMMENT = "comment"; //$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_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$ private static final String OPENSTREETMAP_TABLE_CREATE = "CREATE TABLE " + OPENSTREETMAP_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
OPENSTREETMAP_COL_ID + " bigint,"+ OPENSTREETMAP_COL_ID + " bigint,"+
OPENSTREETMAP_COL_LAT + " double," + OPENSTREETMAP_COL_LON + " double," + OPENSTREETMAP_COL_LAT + " double," + OPENSTREETMAP_COL_LON + " double," +
OPENSTREETMAP_COL_TAGS + " VARCHAR(2048)," + 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<OpenstreetmapPoint> cache = null; List<OpenstreetmapPoint> cache = null;
public OpenstreetmapsDbHelper(Context context) { public OpenstreetmapsDbHelper(Context context) {
@ -53,6 +57,9 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper {
if (oldVersion < 5) { if (oldVersion < 5) {
db.execSQL("ALTER TABLE " + OPENSTREETMAP_TABLE_NAME + " ADD " + OPENSTREETMAP_COL_CHANGED_TAGS + " TEXT"); 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<OpenstreetmapPoint> getOpenstreetmapPoints() { public List<OpenstreetmapPoint> getOpenstreetmapPoints() {
@ -66,7 +73,8 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper {
SQLiteDatabase db = getWritableDatabase(); SQLiteDatabase db = getWritableDatabase();
if (db != null) { if (db != null) {
StringBuilder tags = new StringBuilder(); StringBuilder tags = new StringBuilder();
Iterator<Entry<String, String>> eit = p.getEntity().getTags().entrySet().iterator(); Entity entity = p.getEntity();
Iterator<Entry<String, String>> eit = entity.getTags().entrySet().iterator();
while(eit.hasNext()) { while(eit.hasNext()) {
Entry<String, String> e = eit.next(); Entry<String, String> e = eit.next();
if(Algorithms.isEmpty(e.getKey()) || Algorithms.isEmpty(e.getValue())) { if(Algorithms.isEmpty(e.getKey()) || Algorithms.isEmpty(e.getValue())) {
@ -97,10 +105,12 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper {
OPENSTREETMAP_COL_TAGS + ", " + OPENSTREETMAP_COL_TAGS + ", " +
OPENSTREETMAP_COL_ACTION + ", " + OPENSTREETMAP_COL_ACTION + ", " +
OPENSTREETMAP_COL_COMMENT + ", " + OPENSTREETMAP_COL_COMMENT + ", " +
OPENSTREETMAP_COL_CHANGED_TAGS + ")" + OPENSTREETMAP_COL_CHANGED_TAGS + ", " +
" VALUES (?, ?, ?, ?, ?, ?, ?)", OPENSTREETMAP_COL_ENTITY_TYPE + ")" +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
new Object[]{p.getId(), p.getLatitude(), p.getLongitude(), tags.toString(), 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(); db.close();
checkOpenstreetmapPoints(); checkOpenstreetmapPoints();
return true; return true;
@ -134,27 +144,42 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper {
OPENSTREETMAP_COL_ACTION + "," + OPENSTREETMAP_COL_ACTION + "," +
OPENSTREETMAP_COL_COMMENT + "," + OPENSTREETMAP_COL_COMMENT + "," +
OPENSTREETMAP_COL_TAGS + "," + OPENSTREETMAP_COL_TAGS + "," +
OPENSTREETMAP_COL_CHANGED_TAGS + OPENSTREETMAP_COL_CHANGED_TAGS + "," +
OPENSTREETMAP_COL_ENTITY_TYPE +
" FROM " + OPENSTREETMAP_TABLE_NAME, null); " FROM " + OPENSTREETMAP_TABLE_NAME, null);
if (query.moveToFirst()) { if (query.moveToFirst()) {
do { do {
OpenstreetmapPoint p = new OpenstreetmapPoint(); OpenstreetmapPoint p = new OpenstreetmapPoint();
Node entity = new Node(query.getDouble(1), String entityType = query.getString(7);
query.getDouble(2), Entity entity = null;
query.getLong(0)); if (entityType != null && Entity.EntityType.valueOf(entityType) == Entity.EntityType.NODE) {
String tags = query.getString(5); entity = new Node(query.getDouble(1),
String[] split = tags.split("\\$\\$\\$"); query.getDouble(2),
for(int i=0; i<split.length - 1; i+= 2){ query.getLong(0));
entity.putTagNoLC(split[i].trim(), split[i+1].trim()); } else if (entityType != null && Entity.EntityType.valueOf(entityType) == Entity.EntityType.WAY) {
entity = new Way(query.getLong(0), null, query.getDouble(1),
query.getDouble(2));
} else if (entityType == null) {
entity = new Node(query.getDouble(1),
query.getDouble(2),
query.getLong(0));
} }
String changedTags = query.getString(6); if (entity != null) {
if (changedTags != null) { String tags = query.getString(5);
entity.setChangedTags(new HashSet<>(Arrays.asList(changedTags.split("\\$\\$\\$")))); 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()); } while (query.moveToNext());
} }
query.close(); query.close();

View file

@ -54,13 +54,7 @@ public class UploadOpenstreetmapPointAsyncTask
OpenstreetmapPoint p = (OpenstreetmapPoint) point; OpenstreetmapPoint p = (OpenstreetmapPoint) point;
EntityInfo entityInfo = null; EntityInfo entityInfo = null;
if (OsmPoint.Action.CREATE != p.getAction()) { if (OsmPoint.Action.CREATE != p.getAction()) {
Entity entity = p.getEntity(); entityInfo = remotepoi.loadEntity(p.getEntity());
if (entity instanceof Node) {
entityInfo = remotepoi.loadNode((Node) entity);
} else {
loadErrorsMap.put(point, "Entity is not node");
return loadErrorsMap;
}
} }
Entity n = remotepoi.commitEntityImpl(p.getAction(), p.getEntity(), entityInfo, Entity n = remotepoi.commitEntityImpl(p.getAction(), p.getEntity(), entityInfo,
p.getComment(), false, null); p.getComment(), false, null);