001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.filter.v1_0;
017:
018: import javax.xml.namespace.QName;
019: import com.vividsolutions.jts.geom.Coordinate;
020: import com.vividsolutions.jts.geom.Envelope;
021: import com.vividsolutions.jts.geom.Geometry;
022: import com.vividsolutions.jts.geom.GeometryFactory;
023: import com.vividsolutions.jts.geom.Polygon;
024: import org.opengis.filter.FilterFactory2;
025: import org.opengis.filter.expression.Expression;
026: import org.opengis.filter.expression.Literal;
027: import org.opengis.filter.expression.PropertyName;
028: import org.geotools.geometry.jts.ReferencedEnvelope;
029: import org.geotools.gml2.bindings.GML;
030: import org.geotools.xml.Node;
031:
032: /**
033: * Filter parsing / encoding utility class.
034: *
035: * @author Justin Deoliveira, The Open Planning Project
036: *
037: */
038: public class OGCUtils {
039: /**
040: * Implementation of getProperty for {@link BinarySpatialOpTypeBinding} and
041: * {@link DistanceBufferTypeBinding}
042: *
043: * @param e1 First expression
044: * @param e2 Second expression
045: * @param name name of property
046: *
047: * @return the object for the property, or null
048: */
049: static Object property(Expression e1, Expression e2, QName name) {
050: if (OGC.PropertyName.equals(name)) {
051: if (e1 instanceof PropertyName) {
052: return e1;
053: } else if (e2 instanceof PropertyName) {
054: return e2;
055: }
056: }
057:
058: if (new QName(GML.NAMESPACE, "_Geometry").equals(name)) {
059: if (e1 instanceof Literal) {
060: Literal literal = (Literal) e1;
061:
062: if (literal.getValue() instanceof Geometry) {
063: return literal.getValue();
064: }
065: } else if (e2 instanceof Literal) {
066: Literal literal = (Literal) e2;
067:
068: if (literal.getValue() instanceof Geometry) {
069: return literal.getValue();
070: }
071: }
072: }
073:
074: if (new QName(GML.NAMESPACE, "Box").equals(name) /*filter 1.0*/
075: || new QName(GML.NAMESPACE, "Envelope").equals(name) /*filter 1.1*/) {
076: if (e1 instanceof Literal) {
077: Literal literal = (Literal) e1;
078:
079: if (literal.getValue() instanceof Envelope) {
080: return literal.getValue();
081: }
082: } else if (e2 instanceof Literal) {
083: Literal literal = (Literal) e2;
084:
085: if (literal.getValue() instanceof Envelope) {
086: return literal.getValue();
087: }
088: }
089: }
090:
091: return null;
092: }
093:
094: /**
095: * Returns a two element array of PropertyName, Literal ( Geometry )
096: *
097: * @param node The parse tree.
098: *
099: * @return A two element array of expressions for a BinarySpatialOp type.
100: */
101: static Expression[] spatial(Node node, FilterFactory2 ff,
102: GeometryFactory gf) {
103: PropertyName name = (PropertyName) node
104: .getChildValue(PropertyName.class);
105: Expression spatial = null;
106:
107: if (node.hasChild(Geometry.class)) {
108: spatial = ff.literal(node.getChildValue(Geometry.class));
109: } else if (node.hasChild(Envelope.class)) {
110: //JD: creating an envelope here would break a lot of our code, for instance alot of
111: // code that encodes a filter into sql will choke on this
112: Envelope envelope = (Envelope) node
113: .getChildValue(Envelope.class);
114: Polygon polygon = gf.createPolygon(gf
115: .createLinearRing(new Coordinate[] {
116: new Coordinate(envelope.getMinX(), envelope
117: .getMinY()),
118: new Coordinate(envelope.getMaxX(), envelope
119: .getMinY()),
120: new Coordinate(envelope.getMaxX(), envelope
121: .getMaxY()),
122: new Coordinate(envelope.getMinX(), envelope
123: .getMaxY()),
124: new Coordinate(envelope.getMinX(), envelope
125: .getMinY()) }), null);
126:
127: if (envelope instanceof ReferencedEnvelope) {
128: polygon.setUserData(((ReferencedEnvelope) envelope)
129: .getCoordinateReferenceSystem());
130: }
131:
132: spatial = ff.literal(polygon);
133: }
134:
135: return new Expression[] { name, spatial };
136: }
137: }
|