sascha@514: package de.intevation.gnv.math; sascha@514: sascha@514: import com.vividsolutions.jts.geom.Coordinate; sascha@514: import com.vividsolutions.jts.geom.Envelope; sascha@514: import com.vividsolutions.jts.geom.Geometry; sascha@514: import com.vividsolutions.jts.geom.GeometryFactory; sascha@514: import com.vividsolutions.jts.geom.LinearRing; sascha@514: import com.vividsolutions.jts.geom.Point; sascha@514: import com.vividsolutions.jts.geom.Polygon; sascha@514: sascha@514: import com.vividsolutions.jts.index.ItemVisitor; sascha@514: sascha@514: import java.io.Serializable; sascha@514: sascha@514: import java.util.ArrayList; sascha@514: import java.util.HashMap; sascha@514: import java.util.List; sascha@514: sascha@517: import org.apache.log4j.Logger; sascha@517: sascha@514: /** sascha@514: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) sascha@514: */ sascha@514: public class GridCell sascha@514: implements Serializable sascha@514: { sascha@517: private static Logger log = Logger.getLogger(GridCell.class); sascha@517: sascha@514: public static final class CellFinder sascha@514: implements ItemVisitor sascha@514: { sascha@514: public GridCell found; sascha@514: sascha@514: protected Point point; sascha@514: sascha@514: public CellFinder() { sascha@514: } sascha@514: sascha@514: public void prepare(Coordinate center) { sascha@514: found = null; sascha@514: point = GEOMETRY_FACTORY.createPoint(center); sascha@514: } sascha@514: sascha@514: public void visitItem(Object item) { sascha@514: if (found == null) { sascha@514: GridCell cell = (GridCell)item; sascha@514: if (cell.contains(point)) { sascha@514: found = cell; sascha@514: } sascha@514: } sascha@514: } sascha@514: } // class CellFinder sascha@514: sascha@514: public Point2d p1; sascha@514: public Point2d p2; sascha@514: public Point2d p3; sascha@514: public Point2d p4; sascha@514: sascha@514: protected Polygon polygon; sascha@514: sascha@514: public static final GeometryFactory GEOMETRY_FACTORY sascha@514: = new GeometryFactory(); sascha@514: sascha@514: public GridCell() { sascha@514: } sascha@514: sascha@514: public GridCell(Point2d p1, Point2d p2, Point2d p3, Point2d p4) { sascha@514: this.p1 = p1; sascha@514: this.p2 = p2; sascha@514: this.p3 = p3; sascha@514: this.p4 = p4; sascha@514: createPolygon(); sascha@514: } sascha@514: sascha@514: protected void createPolygon() { sascha@514: LinearRing shell = GEOMETRY_FACTORY sascha@514: .createLinearRing(new Coordinate [] { p1, p2, p3, p4, p1 }); sascha@514: polygon = GEOMETRY_FACTORY.createPolygon(shell, null); sascha@514: } sascha@514: sascha@514: public Envelope getEnvelope() { sascha@514: return polygon.getEnvelopeInternal(); sascha@514: } sascha@514: sascha@514: public boolean contains(Geometry coord) { sascha@514: return polygon.contains(coord); sascha@514: } sascha@514: sascha@514: public static List pointsToGridCells( sascha@514: List points sascha@514: ) { sascha@517: return pointsToGridCells(points, null); sascha@517: } sascha@517: sascha@517: public static List pointsToGridCells( sascha@517: List points, sascha@517: Envelope relevantArea sascha@517: ) { sascha@514: int minI = Integer.MAX_VALUE; sascha@514: int maxI = Integer.MIN_VALUE; sascha@514: int minJ = Integer.MAX_VALUE; sascha@514: int maxJ = Integer.MIN_VALUE; sascha@514: sascha@514: HashMap> rows = sascha@514: new HashMap>(); sascha@514: sascha@517: int culled = 0; sascha@517: sascha@514: for (Point2d p: points) { sascha@514: sascha@517: if (relevantArea != null && !relevantArea.contains(p.x, p.y)) { sascha@517: ++culled; sascha@517: continue; sascha@517: } sascha@517: sascha@514: if (p.i < minI) minI = p.i; sascha@514: if (p.i > maxI) maxI = p.i; sascha@514: if (p.j < minJ) minJ = p.j; sascha@514: if (p.j > maxJ) maxJ = p.j; sascha@514: sascha@514: HashMap row = rows.get(p.i); sascha@514: sascha@514: if (row == null) { sascha@514: rows.put(p.i, row = new HashMap()); sascha@514: } sascha@514: sascha@514: row.put(p.j, p); sascha@514: } sascha@514: sascha@514: ArrayList cells = new ArrayList(points.size()); sascha@514: sascha@514: for (int i = minI + 1; i <= maxI; ++i) { sascha@514: HashMap row1 = rows.get(i-1); sascha@514: HashMap row2 = rows.get(i); sascha@514: if (row1 != null && row2 != null) { sascha@514: for (int j = minJ + 1; j < maxJ; ++j) { sascha@514: Point2d p1 = row1.get(j-1); sascha@514: Point2d p2 = row1.get(j); sascha@514: Point2d p3 = row2.get(j); sascha@514: Point2d p4 = row2.get(j-1); sascha@514: sascha@514: if (p1 != null && p2 != null && p3 != null && p4 != null) { sascha@514: cells.add(new GridCell(p1, p2, p3, p4)); sascha@514: } sascha@514: } // for all columns in row sascha@514: } sascha@514: } // for all rows sascha@514: sascha@519: if (log.isDebugEnabled()) { sascha@519: log.debug("culled points: " + culled); sascha@519: log.debug("min/max i: " + minI + " / " + maxI); sascha@519: log.debug("min/max j: " + minJ + " / " + maxJ); sascha@519: log.debug("cells found: " + cells.size()); sascha@519: } sascha@519: sascha@514: return cells; sascha@514: } sascha@514: } sascha@514: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :