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();
}
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;
}
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);
}
}
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);
}
}
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;
}
Aggregations