add ability to offline edit osm edits
This commit is contained in:
parent
30681c6f64
commit
c288c75a48
6 changed files with 72 additions and 48 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<String, String> 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
|
||||
|
|
|
@ -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<OpenstreetmapPoint> 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<OpenstreetmapPoint> getOpenstreetmapPoints() {
|
||||
|
@ -66,7 +73,8 @@ public class OpenstreetmapsDbHelper extends SQLiteOpenHelper {
|
|||
SQLiteDatabase db = getWritableDatabase();
|
||||
if (db != null) {
|
||||
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()) {
|
||||
Entry<String, String> 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<split.length - 1; i+= 2){
|
||||
entity.putTagNoLC(split[i].trim(), split[i+1].trim());
|
||||
String entityType = query.getString(7);
|
||||
Entity entity = null;
|
||||
if (entityType != null && Entity.EntityType.valueOf(entityType) == Entity.EntityType.NODE) {
|
||||
entity = new Node(query.getDouble(1),
|
||||
query.getDouble(2),
|
||||
query.getLong(0));
|
||||
} 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 (changedTags != null) {
|
||||
entity.setChangedTags(new HashSet<>(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();
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue