Add support for editing MapObject

This commit is contained in:
Chumva 2018-09-05 19:17:16 +03:00
parent 08085f1efb
commit fed4db96e5
5 changed files with 101 additions and 43 deletions

View file

@ -51,6 +51,7 @@ import net.osmand.CallbackWithObject;
import net.osmand.PlatformUtil;
import net.osmand.data.Amenity;
import net.osmand.data.LatLon;
import net.osmand.data.MapObject;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.Entity;
@ -682,7 +683,7 @@ public class EditPoiDialogFragment extends BaseOsmAndDialogFragment {
return editPoiDialogFragment;
}
public static void showEditInstance(final Amenity amenity,
public static void showEditInstance(final MapObject mapObject,
final AppCompatActivity activity) {
final OsmandSettings settings = ((OsmandApplication) activity.getApplication())
.getSettings();
@ -697,7 +698,7 @@ public class EditPoiDialogFragment extends BaseOsmAndDialogFragment {
new AsyncTask<Void, Void, Entity>() {
@Override
protected Entity doInBackground(Void... params) {
return openstreetmapUtilToLoad.loadEntity(amenity);
return openstreetmapUtilToLoad.loadEntity(mapObject);
}
protected void onPostExecute(Entity entity) {

View file

@ -1,7 +1,11 @@
package net.osmand.plus.osmedit;
import net.osmand.NativeLibrary;
import net.osmand.PlatformUtil;
import net.osmand.data.Amenity;
import net.osmand.data.Building;
import net.osmand.data.LatLon;
import net.osmand.data.MapObject;
import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiType;
@ -77,46 +81,66 @@ public class OpenstreetmapLocalUtil implements OpenstreetmapUtil {
}
@Override
public Entity loadEntity(Amenity n) {
PoiType poiType = n.getType().getPoiTypeByKeyName(n.getSubType());
boolean isWay = n.getId() % 2 == 1; // check if amenity is a way
if (poiType == null) {
public Entity loadEntity(MapObject mapObject) {
boolean amenity = mapObject instanceof Amenity;
PoiType poiType = null;
if (amenity) {
poiType = ((Amenity) mapObject).getType().getPoiTypeByKeyName(((Amenity) mapObject).getSubType());
}
boolean isWay = mapObject.getId() % 2 == 1; // check if amenity is a way
if (poiType == null && amenity) {
return null;
}
long entityId = n.getId() >> 1;
// EntityId id = new Entity.EntityId(EntityType.NODE, entityId);
Entity entity;
if (isWay) {
entity = new Way(entityId, null, n.getLocation().getLatitude(), n.getLocation().getLongitude());
long entityId;
if (mapObject instanceof Amenity) {
entityId = mapObject.getId() >> 1;
} else {
entity = new Node(n.getLocation().getLatitude(),
n.getLocation().getLongitude(),
entityId);
}
entity.putTagNoLC(EditPoiData.POI_TYPE_TAG, poiType.getTranslation());
if(poiType.getOsmTag2() != null) {
entity.putTagNoLC(poiType.getOsmTag2(), poiType.getOsmValue2());
}
if(!Algorithms.isEmpty(n.getName())) {
entity.putTagNoLC(OSMTagKey.NAME.getValue(), n.getName());
}
if(!Algorithms.isEmpty(n.getOpeningHours())) {
entity.putTagNoLC(OSMTagKey.OPENING_HOURS.getValue(), n.getOpeningHours());
entityId = mapObject.getId() >> 7;
}
for (Map.Entry<String, String> entry : n.getAdditionalInfo().entrySet()) {
AbstractPoiType abstractPoi = MapPoiTypes.getDefault().getAnyPoiAdditionalTypeByKey(entry.getKey());
if (abstractPoi != null && abstractPoi instanceof PoiType) {
PoiType p = (PoiType) abstractPoi;
if (!p.isNotEditableOsm() && !Algorithms.isEmpty(p.getEditOsmTag())) {
entity.putTagNoLC(p.getEditOsmTag(), entry.getValue());
Entity entity;
LatLon loc = mapObject.getLocation();
if (loc == null) {
if (mapObject instanceof NativeLibrary.RenderedObject) {
loc = ((NativeLibrary.RenderedObject) mapObject).getLabelLatLon();
} else if (mapObject instanceof Building) {
loc = ((Building) mapObject).getLatLon2();
}
}
if (loc == null) {
return null;
}
if (isWay) {
entity = new Way(entityId, null, loc.getLatitude(), loc.getLongitude());
} else {
entity = new Node(loc.getLatitude(), loc.getLongitude(), entityId);
}
if (poiType != null) {
entity.putTagNoLC(EditPoiData.POI_TYPE_TAG, poiType.getTranslation());
if (poiType.getOsmTag2() != null) {
entity.putTagNoLC(poiType.getOsmTag2(), poiType.getOsmValue2());
}
}
if (!Algorithms.isEmpty(mapObject.getName())) {
entity.putTagNoLC(OSMTagKey.NAME.getValue(), mapObject.getName());
}
if (amenity) {
if (!Algorithms.isEmpty(((Amenity) mapObject).getOpeningHours())) {
entity.putTagNoLC(OSMTagKey.OPENING_HOURS.getValue(), ((Amenity) mapObject).getOpeningHours());
}
for (Map.Entry<String, String> entry : ((Amenity) mapObject).getAdditionalInfo().entrySet()) {
AbstractPoiType abstractPoi = MapPoiTypes.getDefault().getAnyPoiAdditionalTypeByKey(entry.getKey());
if (abstractPoi != null && abstractPoi instanceof PoiType) {
PoiType p = (PoiType) abstractPoi;
if (!p.isNotEditableOsm() && !Algorithms.isEmpty(p.getEditOsmTag())) {
entity.putTagNoLC(p.getEditOsmTag(), entry.getValue());
}
}
}
}
// check whether this is node (because id of node could be the same as relation)
if (entity instanceof Node && MapUtils.getDistance(entity.getLatLon(), n.getLocation()) < 50) {
if (entity instanceof Node && MapUtils.getDistance(entity.getLatLon(), mapObject.getLocation()) < 50) {
return entity;
} else if (entity instanceof Way) {
return entity;

View file

@ -3,9 +3,12 @@ package net.osmand.plus.osmedit;
import android.util.Xml;
import android.widget.Toast;
import net.osmand.NativeLibrary;
import net.osmand.PlatformUtil;
import net.osmand.data.Amenity;
import net.osmand.data.Building;
import net.osmand.data.LatLon;
import net.osmand.data.MapObject;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.Entity;
@ -409,9 +412,15 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
}
@Override
public Entity loadEntity(Amenity n) {
boolean isWay = n.getId() % 2 == 1;// check if amenity is a way
long entityId = n.getId() >> 1;
public Entity loadEntity(MapObject object) {
long objectId = object.getId();
boolean isWay = objectId % 2 == 1;// check if mapObject is a way
long entityId;
if (object instanceof Amenity) {
entityId = objectId >> 1;
} else {
entityId = objectId >> 7;
}
try {
String api = isWay ? "api/0.6/way/" : "api/0.6/node/";
String res = sendRequest(getSiteApi() + api + entityId, "GET", null,
@ -427,14 +436,32 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
if (entity != null) {
if (!isWay && entity instanceof Node) {
// check whether this is node (because id of node could be the same as relation)
if (MapUtils.getDistance(entity.getLatLon(), n.getLocation()) < 50) {
return replaceEditOsmTags(n, entity);
if (MapUtils.getDistance(entity.getLatLon(), object.getLocation()) < 50) {
if (object instanceof Amenity) {
return replaceEditOsmTags((Amenity) object, entity);
} else {
return entity;
}
}
} else if (isWay && entity instanceof Way) {
LatLon loc = n.getLocation();
LatLon loc = object.getLocation();
if (loc == null) {
if (object instanceof NativeLibrary.RenderedObject) {
loc = ((NativeLibrary.RenderedObject) object).getLabelLatLon();
} else if (object instanceof Building) {
loc = ((Building) object).getLatLon2();
}
}
if (loc == null) {
return null;
}
entity.setLatitude(loc.getLatitude());
entity.setLongitude(loc.getLongitude());
return replaceEditOsmTags(n, entity);
if (object instanceof Amenity) {
return replaceEditOsmTags((Amenity) object, entity);
} else {
return entity;
}
}
}
return null;

View file

@ -2,7 +2,7 @@ package net.osmand.plus.osmedit;
import android.support.annotation.Nullable;
import net.osmand.data.Amenity;
import net.osmand.data.MapObject;
import net.osmand.osm.edit.Entity;
import net.osmand.osm.edit.EntityInfo;
@ -16,5 +16,5 @@ public interface OpenstreetmapUtil {
void closeChangeSet();
Entity loadEntity(Amenity n);
Entity loadEntity(MapObject mapObject);
}

View file

@ -19,6 +19,7 @@ import android.widget.Toast;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.data.Amenity;
import net.osmand.data.MapObject;
import net.osmand.data.TransportStop;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.Entity;
@ -197,8 +198,11 @@ public class OsmEditingPlugin extends OsmandPlugin {
} else if (resId == R.string.context_menu_item_modify_note) {
modifyOsmNote(mapActivity, (OsmNotesPoint) selectedObj);
} else if (resId == R.string.poi_context_menu_modify) {
EditPoiDialogFragment.showEditInstance(selectedObj instanceof TransportStop ?
((TransportStop) selectedObj).getAmenity() : (Amenity) selectedObj, mapActivity);
if (selectedObj instanceof TransportStop) {
EditPoiDialogFragment.showEditInstance(((TransportStop) selectedObj).getAmenity(), mapActivity);
} else if (selectedObj instanceof MapObject) {
EditPoiDialogFragment.showEditInstance((MapObject) selectedObj, mapActivity);
}
} else if (resId == R.string.poi_context_menu_modify_osm_change) {
final Entity entity = ((OpenstreetmapPoint) selectedObj).getEntity();
EditPoiDialogFragment.createInstance(entity, false)
@ -217,6 +221,8 @@ public class OsmEditingPlugin extends OsmandPlugin {
}
final PoiType poiType = amenity.getType().getPoiTypeByKeyName(amenity.getSubType());
isEditable = !amenity.getType().isWiki() && poiType !=null && !poiType.isNotEditableOsm();
} else if (selectedObj instanceof MapObject) {
isEditable = true;
}
if (isEditable) {
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.poi_context_menu_modify, mapActivity)