use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class PoiAssetLoader method loadPoisFromAssets.
/**
* Load the POIs from the pois.osm file located in the assets directory.
*
* @return the loaded POIs.
*/
public List<Poi> loadPoisFromAssets() {
try {
OsmDto read = simple.read(OsmDto.class, application.getAssets().open("pois.osm"));
List<Poi> pois = poiMapper.convertDtosToPois(read.getNodeDtoList());
pois.addAll(poiMapper.convertDtosToPois(read.getWayDtoList()));
return pois;
} catch (Exception e) {
Timber.e(e, "Error while loading POIS from assets");
throw new RuntimeException(e);
}
}
use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class OsmBackend method updatePoi.
/**
* {@inheritDoc}
*/
@Override
public UpdateResult updatePoi(final Poi poi, String transactionId) {
final OsmDto osmDto = new OsmDto();
if (poi.getWay()) {
WayDto nodeDto = poiMapper.convertPoiToWayDto(poi, transactionId);
osmDto.setWayDtoList(singletonList(nodeDto));
} else {
NodeDto nodeDto = poiMapper.convertPoiToNodeDto(poi, transactionId);
osmDto.setNodeDtoList(singletonList(nodeDto));
}
Call<ResponseBody> versionCall;
if (poi.getWay()) {
versionCall = osmRestClient.updateWay(poi.getBackendId(), osmDto);
} else {
versionCall = osmRestClient.updateNode(poi.getBackendId(), osmDto);
}
try {
Response<ResponseBody> response = versionCall.execute();
if (response.isSuccessful()) {
return new UpdateResult(ModificationStatus.SUCCESS, response.body().string());
} else {
if (response.code() == 400) {
Timber.e("Couldn't update node, conflicting version");
return new UpdateResult(ModificationStatus.FAILURE_CONFLICT, null);
} else if (response.code() == 404) {
Timber.e("Couldn't update node, no existing node found with the id " + poi.getId());
return new UpdateResult(ModificationStatus.FAILURE_NOT_EXISTING, null);
} else {
Timber.e("Couldn't update node");
return new UpdateResult(ModificationStatus.FAILURE_UNKNOWN, null);
}
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
return new UpdateResult(ModificationStatus.FAILURE_UNKNOWN, null);
}
use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class OsmBackend method addPoi.
/**
* {@inheritDoc}
*/
@Override
public CreationResult addPoi(final Poi poi, String transactionId) {
final OsmDto osmDto = new OsmDto();
if (poi.getWay()) {
WayDto nodeDto = poiMapper.convertPoiToWayDto(poi, transactionId);
osmDto.setWayDtoList(singletonList(nodeDto));
} else {
NodeDto nodeDto = poiMapper.convertPoiToNodeDto(poi, transactionId);
osmDto.setNodeDtoList(singletonList(nodeDto));
}
Call<ResponseBody> getCall;
if (poi.getWay()) {
getCall = osmRestClient.addWay(osmDto);
} else {
getCall = osmRestClient.addNode(osmDto);
}
try {
Response<ResponseBody> response = getCall.execute();
if (response.isSuccessful()) {
return new CreationResult(ModificationStatus.SUCCESS, response.body().string());
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
return new CreationResult(ModificationStatus.FAILURE_UNKNOWN, null);
}
use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class OsmBackend method getPoisDtosInBox.
/**
* {@inheritDoc}
*/
@Override
@NonNull
public List<OsmDto> getPoisDtosInBox(final Box box) throws NetworkException {
Timber.d("Requesting overpass for download");
List<OsmDto> osmDtos = new ArrayList<>();
poiTypes = poiManager.loadPoiTypes();
for (Map.Entry<Long, PoiType> entry : poiTypes.entrySet()) {
final PoiType poiTypeDto = entry.getValue();
if (poiTypeDto.getQuery() != null) {
OSMProxy.Result<OsmDto> result = osmProxy.proceed(new OSMProxy.NetworkAction<OsmDto>() {
@Override
public OsmDto proceed() {
// replace it by the coordinates of the current position
if (poiTypeDto.getQuery().contains(BBOX)) {
String format = poiTypeDto.getQuery().replace(BBOX, box.osmFormat());
try {
return overpassRestClient.sendRequest(format).execute().body();
} catch (IOException e) {
return null;
}
} else {
try {
return overpassRestClient.sendRequest(poiTypeDto.getQuery()).execute().body();
} catch (IOException e) {
return null;
}
}
}
});
if (result != null) {
OsmDto osmDto = result.getResult();
if (osmDto != null) {
osmDtos.add(osmDto);
} else {
throw new NetworkException();
}
poiTypes.remove(entry.getKey());
} else {
throw new NetworkException();
}
}
}
if (!poiTypes.isEmpty()) {
OSMProxy.Result<OsmDto> result = osmProxy.proceed(new OSMProxy.NetworkAction<OsmDto>() {
@Override
public OsmDto proceed() {
String request = generateOverpassRequest(box, poiTypes);
try {
return overpassRestClient.sendRequest(request).execute().body();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
});
if (result != null) {
OsmDto osmDto = result.getResult();
if (osmDto != null) {
osmDtos.add(osmDto);
} else {
throw new NetworkException();
}
} else {
throw new NetworkException();
}
}
return osmDtos;
}
use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class OsmBackend method deletePoi.
/**
* {@inheritDoc}
*/
@Override
public ModificationStatus deletePoi(final Poi poi, String transactionId) {
final OsmDto osmDto = new OsmDto();
if (poi.getWay()) {
WayDto nodeDto = poiMapper.convertPoiToWayDto(poi, transactionId);
osmDto.setWayDtoList(singletonList(nodeDto));
} else {
NodeDto nodeDto = poiMapper.convertPoiToNodeDto(poi, transactionId);
osmDto.setNodeDtoList(singletonList(nodeDto));
}
Call<ResponseBody> deleteCall;
if (poi.getWay()) {
deleteCall = osmRestClient.deleteWay(poi.getBackendId(), osmDto);
Timber.d("Deleted way %s", poi.getBackendId());
} else {
deleteCall = osmRestClient.deleteNode(poi.getBackendId(), osmDto);
Timber.d("Deleted node %s", poi.getBackendId());
}
poiManager.deletePoi(poi);
try {
Response<ResponseBody> response = deleteCall.execute();
if (response.isSuccessful()) {
return ModificationStatus.SUCCESS;
} else {
if (response.code() == 400) {
return ModificationStatus.FAILURE_CONFLICT;
} else if (response.code() == 404) {
// the point doesn't exist
return ModificationStatus.FAILURE_NOT_EXISTING;
} else if (response.code() == 410) {
// the poi was already deleted
return ModificationStatus.FAILURE_NOT_EXISTING;
}
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
return ModificationStatus.FAILURE_UNKNOWN;
}
Aggregations