tim@455: package de.intevation.gnv.math;
tim@455:
sascha@779: import com.vividsolutions.jts.geom.Coordinate;
sascha@779:
sascha@779: import de.intevation.gnv.geobackend.base.Result;
sascha@779:
sascha@779: import de.intevation.gnv.geobackend.base.query.QueryExecutor;
sascha@779: import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
sascha@779:
sascha@779: import de.intevation.gnv.geobackend.base.query.exception.QueryException;
sascha@779:
sascha@779: import de.intevation.gnv.geobackend.sde.datasources.RasterObject;
sascha@779:
sascha@779: import de.intevation.gnv.utils.WKTUtils;
sascha@779:
sascha@779: import java.lang.ref.SoftReference;
sascha@779:
tim@457: import java.util.ArrayList;
tim@455: import java.util.Collection;
tim@455:
tim@455: import org.apache.log4j.Logger;
tim@455:
tim@455: /**
sascha@780: * @author Tim Englich
sascha@780: * @author Sascha L. Teichmann
tim@455: */
tim@455: public class QueriedXYDepth implements XYDepth {
tim@455:
tim@455: /**
tim@455: * the logger, used to log exceptions and additonaly information
tim@455: */
tim@455: private static Logger log = Logger.getLogger(QueriedXYDepth.class);
sascha@778:
sascha@458: private static final String queryID = "rasterQuery";
sascha@778:
sascha@458: private QueryExecutor queryExecutor;
sascha@778:
sascha@458: private ArrayList> rasterData;
sascha@462:
sascha@462: private RasterObject last;
sascha@463:
sascha@463: private int interpolation;
sascha@778:
tim@455: public QueriedXYDepth() {
sascha@463: this(RasterObject.BILINEAR);
sascha@463: }
sascha@463:
sascha@463: public QueriedXYDepth(int interpolation) {
sascha@463: this.interpolation = interpolation;
sascha@458: rasterData = new ArrayList>();
sascha@458: queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor();
tim@455: }
tim@455:
tim@455: /**
tim@455: * @see de.intevation.gnv.math.XYDepth#depth(com.vividsolutions.jts.geom.Coordinate)
tim@455: */
tim@455: public double depth(Coordinate coordinate) {
tim@457: double resultValue = Double.NaN;
sascha@778:
sascha@458: RasterObject ro = getRasterObject(coordinate);
sascha@778:
sascha@458: if (ro == null) {
tim@457: try {
sascha@801: String[] filterValues =
sascha@801: new String[] { WKTUtils.toWKT(coordinate) };
sascha@801: Collection result = queryExecutor.executeQuery(
sascha@801: queryID, filterValues);
sascha@458: for (Result row: result) {
sascha@458: if ((ro = (RasterObject)row.getObject(0)) != null) {
sascha@801: rasterData.add(
sascha@801: new SoftReference(last = ro));
sascha@458: }
sascha@458: break;
sascha@458: }
tim@457: } catch (QueryException e) {
sascha@458: log.error(e, e);
tim@457: }
tim@457: }
sascha@458: return ro != null
sascha@463: ? ro.getValue(coordinate, interpolation)
sascha@778: : Double.NaN;
tim@455: }
sascha@778:
sascha@463: private RasterObject getRasterObject(Coordinate coordinate) {
sascha@462: if (last != null && last.contains(coordinate)) {
sascha@462: return last;
sascha@462: }
sascha@458: for (int i = rasterData.size()-1; i >= 0; --i) {
sascha@458: SoftReference ref = rasterData.get(i);
sascha@458: RasterObject ro = ref.get();
sascha@462: if (ro != null && ro.contains(coordinate)) {
sascha@462: return last = ro;
tim@457: }
tim@457: }
tim@457: return null;
tim@457: }
sascha@801: }