01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.filter;
17:
18: import com.vividsolutions.jts.geom.Coordinate;
19: import com.vividsolutions.jts.geom.Envelope;
20: import com.vividsolutions.jts.geom.GeometryFactory;
21: import com.vividsolutions.jts.geom.LinearRing;
22: import com.vividsolutions.jts.geom.TopologyException;
23:
24: /**
25: * Implements a Bounding Box expression.
26: * <p>
27: * Please note this is exactly the same as doing:
28: * <code>
29: * filterFactory.literal( JTS.toGeometry( bounds ) );
30: * </code>
31: *
32: * @author Ian Turton, CCG
33: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/filter/BBoxExpressionImpl.java $
34: * @version $Id: BBoxExpressionImpl.java 25630 2007-05-24 20:15:52Z jgarnett $
35: */
36: public class BBoxExpressionImpl extends
37: org.geotools.filter.LiteralExpressionImpl implements
38: BBoxExpression {
39: /** Factory for creating geometries */
40: private GeometryFactory gfac = new GeometryFactory();
41:
42: /**
43: * Creates a new instance of BBoxExpression
44: *
45: * @throws IllegalFilterException if there are problems.
46: */
47: protected BBoxExpressionImpl() throws IllegalFilterException {
48: this (new Envelope());
49: }
50:
51: /**
52: * Creates a new instance of BBoxExpression, with an initial box.
53: *
54: * @param env the envelope to set as the box.
55: *
56: * @throws IllegalFilterException if there are problems.
57: */
58: protected BBoxExpressionImpl(Envelope env)
59: throws IllegalFilterException {
60: expressionType = DefaultExpression.LITERAL_GEOMETRY;
61: setBounds(env);
62: }
63:
64: /**
65: * Set the bbox for this expression
66: *
67: * @param env The envelope to set as the bounds.
68: *
69: * @throws IllegalFilterException If the box can not be created.
70: *
71: * @task HACK: currently sets the SRID to null, which can cause problems
72: * with JTS when it comes to doing spatial tests
73: */
74: public final void setBounds(Envelope env)
75: throws IllegalFilterException {
76: Coordinate[] coords = new Coordinate[5];
77: coords[0] = new Coordinate(env.getMinX(), env.getMinY());
78: coords[1] = new Coordinate(env.getMinX(), env.getMaxY());
79: coords[2] = new Coordinate(env.getMaxX(), env.getMaxY());
80: coords[3] = new Coordinate(env.getMaxX(), env.getMinY());
81: coords[4] = new Coordinate(env.getMinX(), env.getMinY());
82:
83: LinearRing ring = null;
84:
85: try {
86: ring = gfac.createLinearRing(coords);
87: } catch (TopologyException tex) {
88: throw new IllegalFilterException(tex.toString());
89: }
90:
91: super.setLiteral(gfac.createPolygon(ring, null));
92: }
93: }
|