Commit 36f027c1 authored by Matija Obreza's avatar Matija Obreza

Tiles: Faster tiles

parent 875cea12
...@@ -19,6 +19,7 @@ package org.genesys2.server.service.impl; ...@@ -19,6 +19,7 @@ package org.genesys2.server.service.impl;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; 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.Accession;
import org.genesys2.server.model.genesys.QAccession; import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.QAccessionGeo; import org.genesys2.server.model.genesys.QAccessionGeo;
...@@ -74,26 +75,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -74,26 +75,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
QAccessionGeo qAccessionGeo = qAccession.accessionId.geo; QAccessionGeo qAccessionGeo = qAccession.accessionId.geo;
BooleanBuilder filt = new BooleanBuilder(); 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.isFulltextQuery()) {
if (filter != null) { if (filter != null) {
filt.and(filter.buildPredicate()); filt.and(filter.buildPredicate());
...@@ -108,14 +91,35 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -108,14 +91,35 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
filter.buildJpaQuery(query, qAccession); filter.buildJpaQuery(query, qAccession);
} }
query.where(filt); query.where(filt);
StopWatch stopWatch = StopWatch.createStarted();
List<Tuple> results = query.select(qAccessionGeo.longitude, qAccessionGeo.latitude).distinct().fetch(); 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() return results.stream()
.map(item -> new Double[]{ .map(item -> new Double[]{
item.get(qAccessionGeo.longitude), item.get(0, Double.class),
item.get(qAccessionGeo.latitude), item.get(1, Double.class),
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
} }
// 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 * .1);
LOG.debug("{} <= lat <= {} corr={}", latS, latN, diffLat * .1);
}
filt.and(qAccessionGeo.longitude.between(lonW - diffLon * .1, lonE + diffLon * .1)); // 10%
filt.and(qAccessionGeo.latitude.between(latS - diffLat * .1, latN + diffLat * .1)); // 10%
}
} }
...@@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys2.server.service.GenesysFilterService; import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.MappingService; import org.genesys2.server.service.MappingService;
import org.genesys2.server.service.filter.AccessionFilter; import org.genesys2.server.service.filter.AccessionFilter;
...@@ -84,6 +85,10 @@ public class MappingServiceImpl implements MappingService, InitializingBean { ...@@ -84,6 +85,10 @@ public class MappingServiceImpl implements MappingService, InitializingBean {
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics(); Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
BufferedImage accessionDot = zoomTemplates.get(zoomTemplates.size() > zoom ? zoom : zoomTemplates.size() - 1); 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 = null; List<Double[]> geoTiles = null;
try { try {
geoTiles = filterService.listGeoTile(filters, null, zoom, xtile, ytile); geoTiles = filterService.listGeoTile(filters, null, zoom, xtile, ytile);
...@@ -91,20 +96,35 @@ public class MappingServiceImpl implements MappingService, InitializingBean { ...@@ -91,20 +96,35 @@ public class MappingServiceImpl implements MappingService, InitializingBean {
LOG.error("Error occurred during search", e); LOG.error("Error occurred during search", e);
geoTiles = Collections.emptyList(); geoTiles = Collections.emptyList();
} }
stopWatch.split();
LOG.debug("Got {} geo in {}", geoTiles.size(), stopWatch.toSplitString());
AtomicInteger paints = new AtomicInteger(0); AtomicInteger paints = new AtomicInteger(0);
geoTiles.stream().map(item -> new TilePos(zoom, xtile, ytile, item)).distinct().forEach(item -> { AtomicInteger outsidesX = new AtomicInteger(0);
paints.incrementAndGet(); 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 // calculates the coordinate where the image is painted
int topLeftX = item.longitude - accessionDot.getWidth() / 2; int topLeftX = item.longitude - dotHalfW;
int topLeftY = item.latitude - accessionDot.getHeight() / 2; 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 // paints the image watermark
g2d.drawImage(accessionDot, topLeftX, topLeftY, null); g2d.drawImage(accessionDot, topLeftX, topLeftY, null);
}); });
stopWatch.split();
LOG.trace("zoom={} dot={}x{} paints={} of={}", zoom, accessionDot.getWidth(), accessionDot.getHeight(), paints.intValue(), geoTiles.size()); LOG.debug("Painted outX={} outY={} {} in {}", outsidesX.get(), outsidesY.get(), paints.get(), stopWatch.toSplitString());
try { try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 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