Commit 92b2d045 authored by Matija Obreza's avatar Matija Obreza

Tiles: Faster tiles using smaller margins

- Return default bounds if only historic or no filters are applied
parent da7e44fa
......@@ -70,6 +70,7 @@ import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
......@@ -546,7 +547,15 @@ public class AccessionServiceImpl implements AccessionService {
@Override
public Number[][] getGeoBounds(AccessionFilter filter) {
if (filter.isFulltextQuery()) {
List<Predicate> predicates = filter.collectPredicates();
if (LOG.isDebugEnabled()) {
LOG.debug("{} predicates", predicates.size());
predicates.forEach(predicate -> {
LOG.debug("Predicate: {}", predicate);
});
}
// geo.lat.notNull() & geo.long.notNull()
if (filter.isFulltextQuery() || predicates.size() <= 2 || (filter.historic != null && predicates.size() == 3)) {
return AccessionService.DEFAULT_GEOBOUNDS;
}
......
......@@ -19,6 +19,7 @@ package org.genesys2.server.service.impl;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.QAccessionGeo;
......@@ -74,26 +75,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
QAccessionGeo qAccessionGeo = qAccession.accessionId.geo;
BooleanBuilder filt = new BooleanBuilder();
tileQuery(filt, qAccessionGeo, zoom, xtile, ytile);
// The tile query
{
final double lonW = CoordUtil.tileToLon(zoom, xtile);
final double lonE = CoordUtil.tileToLon(zoom, xtile + 1);
final double diffLon = lonE - lonW;
final double latN = CoordUtil.tileToLat(zoom, ytile);
final double latS = CoordUtil.tileToLat(zoom, ytile + 1);
final double diffLat = latN - latS;
if (LOG.isDebugEnabled()) {
LOG.debug("{} <= lon <= {} corr={}", lonW, lonE, diffLon * .2);
LOG.debug("{} <= lat <= {} corr={}", latS, latN, diffLat * .2);
}
filt.and(qAccessionGeo.longitude.between(lonW - zoom * diffLon * .2, lonE + zoom * diffLon * .2));
filt.and(qAccessionGeo.latitude.between(latS - zoom * diffLat * .2, latN + zoom * diffLat * .2));
}
if (filter.isFulltextQuery()) {
if (filter != null) {
filt.and(filter.buildPredicate());
......@@ -108,7 +91,10 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
filter.buildJpaQuery(query, qAccession);
}
query.where(filt);
StopWatch stopWatch = StopWatch.createStarted();
List<Tuple> results = query.select(qAccessionGeo.longitude, qAccessionGeo.latitude).distinct().fetch();
stopWatch.split();
LOG.debug("Got {} coordinates in {}", results.size(), stopWatch.toSplitString());
return results.stream()
.map(item -> new Double[]{
item.get(qAccessionGeo.longitude),
......@@ -118,4 +104,22 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
// The tile query
private void tileQuery(BooleanBuilder filt, QAccessionGeo qAccessionGeo, int zoom, int xtile, int ytile) {
final double lonW = CoordUtil.tileToLon(zoom, xtile);
final double lonE = CoordUtil.tileToLon(zoom, xtile + 1);
final double diffLon = lonE - lonW;
final double latN = CoordUtil.tileToLat(zoom, ytile);
final double latS = CoordUtil.tileToLat(zoom, ytile + 1);
final double diffLat = latN - latS;
if (LOG.isDebugEnabled()) {
LOG.debug("{} <= lon <= {} corr={}", lonW, lonE, diffLon * .05);
LOG.debug("{} <= lat <= {} corr={}", latS, latN, diffLat * .05);
}
filt.and(qAccessionGeo.longitude.between(lonW - diffLon * .05, lonE + diffLon * .05)); // 5%
filt.and(qAccessionGeo.latitude.between(latS - diffLat * .05, latN + diffLat * .05)); // 5%
}
}
......@@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import javax.imageio.ImageIO;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.MappingService;
import org.genesys2.server.service.filter.AccessionFilter;
......@@ -83,21 +84,40 @@ public class MappingServiceImpl implements MappingService, InitializingBean {
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
BufferedImage accessionDot = zoomTemplates.get(zoomTemplates.size() > zoom ? zoom : zoomTemplates.size() - 1);
int dotHalfW = accessionDot.getWidth() / 2;
int dotHalfH = accessionDot.getHeight() / 2;
StopWatch stopWatch = StopWatch.createStarted();
List<Double[]> geoTiles = filterService.listGeoTile(filters, null, zoom, xtile, ytile);
stopWatch.split();
LOG.debug("Got {} geo in {}", geoTiles.size(), stopWatch.toSplitString());
AtomicInteger paints = new AtomicInteger(0);
geoTiles.stream().map(item -> new TilePos(zoom, xtile, ytile, item)).distinct().forEach(item -> {
paints.incrementAndGet();
AtomicInteger outsidesX = new AtomicInteger(0);
AtomicInteger outsidesY = new AtomicInteger(0);
geoTiles.parallelStream().map(item -> new TilePos(zoom, xtile, ytile, item)).distinct().forEach(item -> {
// calculates the coordinate where the image is painted
int topLeftX = item.longitude - accessionDot.getWidth() / 2;
int topLeftY = item.latitude - accessionDot.getHeight() / 2;
int topLeftX = item.longitude - dotHalfW;
int topLeftY = item.latitude - dotHalfH;
if (LOG.isDebugEnabled()) {
paints.incrementAndGet();
if (topLeftX < -dotHalfW || topLeftX > 255 + dotHalfW) {
LOG.trace("Longitude {},{} outside 0 - 255", item.longitude, item.latitude);
outsidesX.incrementAndGet();
}
if (topLeftY < -dotHalfH || topLeftY > 255 + dotHalfH) {
LOG.trace("Latitude {},{} outside 0 - 255", item.longitude, item.latitude);
outsidesY.incrementAndGet();
}
}
// paints the image watermark
g2d.drawImage(accessionDot, topLeftX, topLeftY, null);
});
LOG.trace("zoom={} dot={}x{} paints={} of={}", zoom, accessionDot.getWidth(), accessionDot.getHeight(), paints.intValue(), geoTiles.size());
stopWatch.split();
LOG.debug("Painted outX={} outY={} {} in {}", outsidesX.get(), outsidesY.get(), paints.get(), stopWatch.toSplitString());
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
......
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