Search in sources :

Example 1 with PoiType

use of io.jawg.osmcontributor.model.entities.PoiType in project osm-contributor by jawg.

the class OsmBackend method generateOverpassRequest.

/**
 * Generate a String corresponding to an Overpass request
 * which will call all the POI given by the map.
 *
 * @param box      The coordinates of each direction of the map
 * @param poiTypes Map of poiTypes depending the preset loaded
 * @return A String of the request.
 */
private String generateOverpassRequest(Box box, Map<Long, PoiType> poiTypes) {
    StringBuilder cmplReq = new StringBuilder("(");
    if (poiTypes.size() > 15) {
        List<String> types = poiManager.loadPoiTypeKeysWithDefaultValues();
        // we've got lots of pois, overpath will struggle with the finer request, download all the pois in the box
        for (String type : OBJECT_TYPES) {
            for (String key : types) {
                cmplReq.append(type).append("[\"").append(key).append("\"]").append(box.osmFormat());
            }
        }
    } else {
        // Download all the pois in the box who are of one of the PoiType contained in the database
        for (String type : OBJECT_TYPES) {
            // For each poiTypes, add the corresponding part to the request
            for (PoiType poiTypeDto : poiTypes.values()) {
                // Check for tags who have a value and add a ["key"~"value"] string to the request
                boolean valid = false;
                for (PoiTypeTag poiTypeTag : poiTypeDto.getTags()) {
                    if (poiTypeTag.getValue() != null) {
                        if (!valid) {
                            cmplReq.append(type);
                        }
                        valid = true;
                        cmplReq.append("[\"").append(poiTypeTag.getKey()).append("\"~\"").append(poiTypeTag.getValue()).append("\"]");
                    }
                }
                // If there was at least one tag with a value, add the box coordinates to the request
                if (valid) {
                    cmplReq.append(box.osmFormat());
                }
            }
        }
    }
    cmplReq.append(");out meta center;");
    return cmplReq.toString();
}
Also used : PoiType(io.jawg.osmcontributor.model.entities.PoiType) PoiTypeTag(io.jawg.osmcontributor.model.entities.PoiTypeTag)

Example 2 with PoiType

use of io.jawg.osmcontributor.model.entities.PoiType 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 3 with PoiType

use of io.jawg.osmcontributor.model.entities.PoiType in project osm-contributor by jawg.

the class PoiManager method updatePoiTypes.

/**
 * Update the PoiTypes in the database with the given List of PoiTypes.
 *
 * @param newPoiTypes The PoiTypes to update.
 */
public void updatePoiTypes(List<PoiType> newPoiTypes) {
    for (PoiType newPoiType : newPoiTypes) {
        PoiType byBackendId = poiTypeDao.findByBackendId(newPoiType.getBackendId());
        if (byBackendId != null) {
            newPoiType.setId(byBackendId.getId());
        }
        savePoiType(newPoiType);
    }
}
Also used : PleaseLoadLastUsedPoiType(io.jawg.osmcontributor.ui.events.map.PleaseLoadLastUsedPoiType) PoiType(io.jawg.osmcontributor.model.entities.PoiType)

Example 4 with PoiType

use of io.jawg.osmcontributor.model.entities.PoiType in project osm-contributor by jawg.

the class PoiManager method updatePoiTypeLastUse.

/**
 * Update the date of last use of a PoiType.
 *
 * @param id The id of the PoiType to update.
 */
public void updatePoiTypeLastUse(long id) {
    PoiType poiType = poiTypeDao.queryForId(id);
    if (poiType != null) {
        poiType.setLastUse(new DateTime());
        Timber.d("Update date of : %s", poiType);
        poiTypeDao.createOrUpdate(poiType);
    }
}
Also used : PleaseLoadLastUsedPoiType(io.jawg.osmcontributor.ui.events.map.PleaseLoadLastUsedPoiType) PoiType(io.jawg.osmcontributor.model.entities.PoiType) DateTime(org.joda.time.DateTime)

Example 5 with PoiType

use of io.jawg.osmcontributor.model.entities.PoiType in project osm-contributor by jawg.

the class PoiManagerTest method getPoiType.

private PoiType getPoiType() {
    PoiType poiType = new PoiType();
    PoiTypeTag poiTypeTag = new PoiTypeTag();
    poiTypeTag.setKey("toto");
    poiTypeTag.setOrdinal(0);
    poiTypeTag.setMandatory(true);
    poiType.setTags(Collections.singletonList(poiTypeTag));
    poiType.setName("PoiType");
    poiType.setTechnicalName("technical=name");
    poiType.setLastUse(new DateTime());
    poiType.setKeyWords("");
    return poiType;
}
Also used : PoiType(io.jawg.osmcontributor.model.entities.PoiType) PoiTypeTag(io.jawg.osmcontributor.model.entities.PoiTypeTag) DateTime(org.joda.time.DateTime)

Aggregations

PoiType (io.jawg.osmcontributor.model.entities.PoiType)28 PoiTypeTag (io.jawg.osmcontributor.model.entities.PoiTypeTag)9 Subscribe (org.greenrobot.eventbus.Subscribe)9 ArrayList (java.util.ArrayList)7 PleaseLoadLastUsedPoiType (io.jawg.osmcontributor.ui.events.map.PleaseLoadLastUsedPoiType)5 View (android.view.View)3 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 BindView (butterknife.BindView)2 Poi (io.jawg.osmcontributor.model.entities.Poi)2 PoiTag (io.jawg.osmcontributor.model.entities.PoiTag)2 PoiTypesLoaded (io.jawg.osmcontributor.model.events.PoiTypesLoaded)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DateTime (org.joda.time.DateTime)2 NonNull (android.support.annotation.NonNull)1 Snackbar (android.support.design.widget.Snackbar)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 ListView (android.widget.ListView)1