Commit f480faea authored by Matija Obreza's avatar Matija Obreza

Merge branch '19-auto-center-accessions-map' into 'master'

Resolve "Auto-center accessions map"

Closes #19

See merge request !10
parents cf377060 0ed5f159
......@@ -43,4 +43,6 @@ public interface ElasticService {
List<String> autocompleteSearch(String query) throws SearchException;
void regenerateAccessionSequentialNumber();
String getBoundingBox(AppliedFilters filters);
}
......@@ -41,6 +41,4 @@ public interface GenesysFilterService {
void listGeoTile(boolean distinct, AppliedFilters filters, Integer limit, int zoom, int xtile, int ytile, RowCallbackHandler rowHandler);
void listGeoForBoundingBox(AppliedFilters filters, RowCallbackHandler rowHandler);
}
......@@ -68,15 +68,6 @@ public interface MappingService {
*/
byte[] getTile(AppliedFilters filters, int zoom, int xtile, int ytile);
/**
* Gets coordinates of bounding box
*
* @param filters
* the filters
* @return coordinates
*/
String getBoundingBox(AppliedFilters filters);
/**
* Coordinate utilities
*/
......
......@@ -26,14 +26,23 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.AndFilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.OrFilterBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
......@@ -74,6 +83,8 @@ import org.springframework.stereotype.Service;
@Service
public class ElasticsearchSearchServiceImpl implements ElasticService, InitializingBean {
private final ObjectMapper mapper = new ObjectMapper();
private static final Log LOG = LogFactory.getLog(ElasticsearchSearchServiceImpl.class);
@Autowired
......@@ -147,6 +158,41 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
}
}
@Override
public String getBoundingBox(final AppliedFilters filters) {
AndFilterBuilder filterBuilder = getFilterBuilder(filters);
final FilterAggregationBuilder builder = AggregationBuilders
.filter("agg").filter(filterBuilder)
.subAggregation(AggregationBuilders.min("min_longitude").field("geo.longitude"))
.subAggregation(AggregationBuilders.max("max_longitude").field("geo.longitude"))
.subAggregation(AggregationBuilders.min("min_latitude").field("geo.latitude"))
.subAggregation(AggregationBuilders.max("max_latitude").field("geo.latitude"));
SearchQuery searchQuery =
new NativeSearchQueryBuilder()
.withIndices(IndexAliasConstants.INDEXALIAS_PASSPORT_WRITE)
.withTypes(IndexAliasConstants.PASSPORT_TYPE)
.addAggregation(builder)
.build();
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, SearchResponse::getAggregations);
Filter agg = aggregations.get("agg");
InternalMin minLatitude = agg.getAggregations().get("min_latitude");
InternalMin minLongitude = agg.getAggregations().get("min_longitude");
InternalMax maxLatitude = agg.getAggregations().get("max_latitude");
InternalMax maxLongitude = agg.getAggregations().get("max_longitude");
final ObjectNode geoJson = mapper.createObjectNode();
geoJson.put("west", String.valueOf(minLatitude.getValue()));
geoJson.put("south", String.valueOf(minLongitude.getValue()));
geoJson.put("east", String.valueOf(maxLatitude.getValue()));
geoJson.put("north",String.valueOf(maxLongitude.getValue()));
return geoJson.toString();
}
@Override
public TermResult termStatistics(AppliedFilters appliedFilters, String term, int size) throws SearchException {
......
......@@ -426,28 +426,4 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}, rowHandler);
}
@Override
public void listGeoForBoundingBox(final AppliedFilters filters, final RowCallbackHandler rowHandler) {
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accessiongeo", "geo");
if (!filters.isEmpty()) {
// Join accession table when we have filters or looking at details
directQuery.innerJoin("accession", "a", "a.id=geo.accessionId");
}
if (!filters.isEmpty()) {
directQuery.join(filters);
directQuery.filter(filters, methodId -> methodRepository.findOne(methodId));
}
this.jdbcTemplate.query(connection -> {
final PreparedStatement stmt = connection.prepareStatement(
directQuery.getQuery("Max(geo.longitude), Min(geo.longitude), Max(geo.latitude), Min(geo.latitude)"));
// Set mysql JConnector to stream results
stmt.setFetchSize(Integer.MIN_VALUE);
new ArgumentPreparedStatementSetter(directQuery.getParameters()).setValues(stmt);
return stmt;
}, rowHandler);
}
}
......@@ -222,19 +222,4 @@ public class MappingServiceImpl implements MappingService {
throw new RuntimeException("Could not render image", e);
}
}
public String getBoundingBox(final AppliedFilters filters) {
final String[] result = {""};
filterService.listGeoForBoundingBox(filters, resultSet -> {
final ObjectNode geoJson = mapper.createObjectNode();
geoJson.put("west", String.valueOf(resultSet.getDouble("Min(geo.latitude)")));
geoJson.put("south", String.valueOf(resultSet.getDouble("Min(geo.longitude)")));
geoJson.put("east", String.valueOf(resultSet.getDouble("Max(geo.latitude)")));
geoJson.put("north", String.valueOf(resultSet.getDouble("Max(geo.longitude)")));
result[0] = geoJson.toString();
});
return result[0];
}
}
......@@ -811,7 +811,7 @@ public class ExplorerController extends BaseController implements InitializingBe
public String getBoundingBox(@RequestParam(value = "filter", required = true) String jsonFilter) throws IOException {
AppliedFilters appliedFilters = mapper.readValue(jsonFilter, AppliedFilters.class);
return mappingService.getBoundingBox(appliedFilters);
return elasticService.getBoundingBox(appliedFilters);
}
@RequestMapping(value = "/explore/tile/{zoom}/{x}/{y}", produces = MediaType.IMAGE_PNG_VALUE)
......
......@@ -423,7 +423,7 @@
$(this).remove();
$('input[i-key="' + key + '"][value="' + value + '"]').prop('checked', false);
if (jsonData["crops"].length === 0) {
if (jsonData["crops"] !== undefined && jsonData["crops"].length === 0) {
delete jsonData["crops"];
$("#panel_additional_id").hide();
}
......
......@@ -313,7 +313,7 @@
$(this).remove();
$('input[i-key=" + normKey + "][value=" + value + "]').prop('checked', false);
if (jsonData["crops"].length === 0) {
if (jsonData["crops"] !== undefined && jsonData["crops"].length === 0) {
delete jsonData["crops"];
$("#panel_additional_id").hide();
}
......
......@@ -277,21 +277,6 @@
jsonData[key] = [];
});
$("body").on("click", ".filtval", function (event) {
event.preventDefault();
var key = $(this).attr("i-key");
var normKey = GenesysFilter.normKey(key);
var value = $(this).attr("x-key").replace(normKey, "");
if (value == "null") value = null;
GenesysFilterUtil.removeValue(value, key, jsonData);
applyFilters();
BrowseUtil.applySuggestions(jsonData, messages);
$(this).remove();
$('input[i-key=" + normKey + "][value=" + value + "]').prop('checked', false);
});
$("#menuShareLink").on("click", function () {
if ($('#shortLink').val() === '') {
$.ajax({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment