Search in sources :

Example 1 with OsmDto

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);
    }
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) Poi(io.jawg.osmcontributor.model.entities.Poi)

Example 2 with OsmDto

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);
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) WayDto(io.jawg.osmcontributor.rest.dtos.osm.WayDto) IOException(java.io.IOException) NodeDto(io.jawg.osmcontributor.rest.dtos.osm.NodeDto) ResponseBody(okhttp3.ResponseBody)

Example 3 with OsmDto

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);
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) WayDto(io.jawg.osmcontributor.rest.dtos.osm.WayDto) IOException(java.io.IOException) NodeDto(io.jawg.osmcontributor.rest.dtos.osm.NodeDto) ResponseBody(okhttp3.ResponseBody)

Example 4 with OsmDto

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;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) ArrayList(java.util.ArrayList) PoiType(io.jawg.osmcontributor.model.entities.PoiType) IOException(java.io.IOException) Map(java.util.Map) NonNull(android.support.annotation.NonNull)

Example 5 with OsmDto

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;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) WayDto(io.jawg.osmcontributor.rest.dtos.osm.WayDto) IOException(java.io.IOException) NodeDto(io.jawg.osmcontributor.rest.dtos.osm.NodeDto) ResponseBody(okhttp3.ResponseBody)

Aggregations

OsmDto (io.jawg.osmcontributor.rest.dtos.osm.OsmDto)11 IOException (java.io.IOException)9 NodeDto (io.jawg.osmcontributor.rest.dtos.osm.NodeDto)5 WayDto (io.jawg.osmcontributor.rest.dtos.osm.WayDto)4 ResponseBody (okhttp3.ResponseBody)4 ArrayList (java.util.ArrayList)3 Poi (io.jawg.osmcontributor.model.entities.Poi)2 NoteDto (io.jawg.osmcontributor.rest.dtos.osm.NoteDto)2 NonNull (android.support.annotation.NonNull)1 Note (io.jawg.osmcontributor.model.entities.Note)1 PoiType (io.jawg.osmcontributor.model.entities.PoiType)1 ChangeSetDto (io.jawg.osmcontributor.rest.dtos.osm.ChangeSetDto)1 PermissionDto (io.jawg.osmcontributor.rest.dtos.osm.PermissionDto)1 TagDto (io.jawg.osmcontributor.rest.dtos.osm.TagDto)1 SyncConflictingNoteErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncConflictingNoteErrorEvent)1 SyncDownloadRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncDownloadRetrofitErrorEvent)1 SyncUploadNoteRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncUploadNoteRetrofitErrorEvent)1 SyncUploadRetrofitErrorEvent (io.jawg.osmcontributor.rest.events.error.SyncUploadRetrofitErrorEvent)1 OAuthRequest (io.jawg.osmcontributor.rest.security.OAuthRequest)1 Map (java.util.Map)1