use of net.osmand.plus.plugins.osmedit.helpers.OpenstreetmapUtil in project Osmand by osmandapp.
the class AddPOIAction method execute.
@Override
public void execute(@NonNull final MapActivity mapActivity) {
OsmandApplication app = mapActivity.getMyApplication();
OsmandSettings settings = app.getSettings();
OsmEditingPlugin plugin = OsmandPlugin.getPlugin(OsmEditingPlugin.class);
if (plugin == null)
return;
LatLon latLon = mapActivity.getMapView().getCurrentRotatedTileBox().getCenterLatLon();
Node node = new Node(latLon.getLatitude(), latLon.getLongitude(), -1);
node.replaceTags(getTagsFromParams());
EditPoiData editPoiData = new EditPoiData(node, mapActivity.getMyApplication());
if (Boolean.parseBoolean(getParams().get(KEY_DIALOG)) || editPoiData.hasEmptyValue()) {
Entity newEntity = editPoiData.getEntity();
EditPoiDialogFragment editPoiDialogFragment = EditPoiDialogFragment.createInstance(newEntity, true, getTagsFromParams());
editPoiDialogFragment.show(mapActivity.getSupportFragmentManager(), EditPoiDialogFragment.TAG);
} else {
OpenstreetmapUtil mOpenstreetmapUtil;
if (plugin.OFFLINE_EDITION.get() || !settings.isInternetConnectionAvailable(true)) {
mOpenstreetmapUtil = plugin.getPoiModificationLocalUtil();
} else {
mOpenstreetmapUtil = plugin.getPoiModificationRemoteUtil();
}
final boolean offlineEdit = mOpenstreetmapUtil instanceof OpenstreetmapLocalUtil;
Node newNode = new Node(node.getLatitude(), node.getLongitude(), node.getId());
Action action = newNode.getId() < 0 ? OsmPoint.Action.CREATE : OsmPoint.Action.MODIFY;
for (Map.Entry<String, String> tag : editPoiData.getTagValues().entrySet()) {
if (tag.getKey().equals(POI_TYPE_TAG)) {
final PoiType poiType = editPoiData.getAllTranslatedSubTypes().get(tag.getValue().trim().toLowerCase());
if (poiType != null) {
newNode.putTagNoLC(poiType.getEditOsmTag(), poiType.getEditOsmValue());
if (poiType.getOsmTag2() != null) {
newNode.putTagNoLC(poiType.getOsmTag2(), poiType.getOsmValue2());
}
if (poiType.getEditOsmTag2() != null) {
newNode.putTagNoLC(poiType.getEditOsmTag2(), poiType.getEditOsmValue2());
}
} else if (!Algorithms.isEmpty(tag.getValue())) {
PoiCategory category = editPoiData.getPoiCategory();
if (category != null) {
newNode.putTagNoLC(category.getDefaultTag(), tag.getValue());
}
}
if (offlineEdit && !Algorithms.isEmpty(tag.getValue())) {
newNode.putTagNoLC(tag.getKey(), tag.getValue());
}
} else if (!Algorithms.isEmpty(tag.getKey()) && !Algorithms.isEmpty(tag.getValue())) {
newNode.putTagNoLC(tag.getKey(), tag.getValue());
}
}
EditPoiDialogFragment.commitEntity(action, newNode, mOpenstreetmapUtil.getEntityInfo(newNode.getId()), "", false, result -> {
if (result != null) {
OsmEditingPlugin plugin1 = OsmandPlugin.getPlugin(OsmEditingPlugin.class);
if (plugin1 != null && offlineEdit) {
List<OpenstreetmapPoint> points = plugin1.getDBPOI().getOpenstreetmapPoints();
if (points.size() > 0) {
OsmPoint point = points.get(points.size() - 1);
mapActivity.getContextMenu().showOrUpdate(new LatLon(point.getLatitude(), point.getLongitude()), plugin1.getOsmEditsLayer(mapActivity).getObjectName(point), point);
}
}
mapActivity.getMapView().refreshMap(true);
}
return false;
}, mapActivity, mOpenstreetmapUtil, null);
}
}
use of net.osmand.plus.plugins.osmedit.helpers.OpenstreetmapUtil in project Osmand by osmandapp.
the class EditPoiDialogFragment method showEditInstance.
public static void showEditInstance(final MapObject mapObject, final AppCompatActivity activity) {
final OsmEditingPlugin plugin = OsmandPlugin.getPlugin(OsmEditingPlugin.class);
if (plugin == null) {
return;
}
final OsmandApplication app = ((OsmandApplication) activity.getApplication());
final OpenstreetmapUtil openstreetmapUtilToLoad = app.getSettings().isInternetConnectionAvailable(true) ? plugin.getPoiModificationRemoteUtil() : plugin.getPoiModificationLocalUtil();
new AsyncTask<Void, Void, Entity>() {
@Override
protected Entity doInBackground(Void... params) {
return openstreetmapUtilToLoad.loadEntity(mapObject);
}
@Override
protected void onPostExecute(Entity entity) {
if (entity != null) {
Entity existingOsmEditEntity = getExistingOsmEditEntity(plugin, entity.getId());
Entity entityToEdit = existingOsmEditEntity != null ? existingOsmEditEntity : entity;
EditPoiDialogFragment fragment = EditPoiDialogFragment.createInstance(entityToEdit, false);
fragment.show(activity.getSupportFragmentManager(), TAG);
} else {
Toast.makeText(activity, activity.getString(R.string.poi_cannot_be_found), Toast.LENGTH_LONG).show();
}
}
@Nullable
private Entity getExistingOsmEditEntity(@NonNull OsmEditingPlugin osmEditingPlugin, long entityId) {
List<OpenstreetmapPoint> osmEdits = osmEditingPlugin.getDBPOI().getOpenstreetmapPoints();
for (OpenstreetmapPoint osmEdit : osmEdits) {
if (osmEdit.getId() == entityId && osmEdit.getAction() == Action.MODIFY) {
return osmEdit.getEntity();
}
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Aggregations