001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.feature.type;
017:
018: import org.geotools.feature.DefaultAttributeType;
019: import org.geotools.feature.IllegalAttributeException;
020: import org.geotools.geometry.jts.JTS;
021: import org.opengis.filter.Filter;
022: import org.geotools.referencing.crs.DefaultGeocentricCRS;
023: import org.opengis.referencing.crs.CoordinateReferenceSystem;
024:
025: import com.vividsolutions.jts.geom.Coordinate;
026: import com.vividsolutions.jts.geom.Envelope;
027: import com.vividsolutions.jts.geom.Geometry;
028: import com.vividsolutions.jts.geom.GeometryCollection;
029: import com.vividsolutions.jts.geom.GeometryFactory;
030: import com.vividsolutions.jts.geom.LinearRing;
031: import com.vividsolutions.jts.geom.PrecisionModel;
032: import com.vividsolutions.jts.io.WKTReader;
033:
034: /**
035: * Provides ...TODO summary sentence
036: * <p>
037: * TODO Description
038: * </p><p>
039: * </p><p>
040: * Example Use:<pre><code>
041: * GeometryAttributeType x = new GeometryAttributeType( ... );
042: * TODO code example
043: * </code></pre>
044: * </p>
045: * @author Leprosy
046: * @since 0.3
047: * TODO: test wkt geometry parse.
048: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/type/GeometricAttributeType.java $
049: */
050: public class GeometricAttributeType extends DefaultAttributeType
051: implements org.geotools.feature.GeometryAttributeType {
052: /** CoordianteSystem used by this GeometryAttributeType */
053: protected CoordinateReferenceSystem coordinateSystem;
054: protected GeometryFactory geometryFactory;
055: private Filter filter;
056:
057: public GeometricAttributeType(String name, Class type,
058: boolean nillable, int min, int max, Object defaultValue,
059: CoordinateReferenceSystem cs, Filter filter) {
060: super (name, type, nillable, min, max, defaultValue);
061: this .filter = filter;
062: coordinateSystem = cs;
063: geometryFactory = cs == null ? CSGeometryFactory.DEFAULT
064: : new CSGeometryFactory(cs);
065:
066: /*
067: coordinateSystem = (cs != null) ? cs : LocalCoordinateSystem.CARTESIAN;
068: geometryFactory = (cs == LocalCoordinateSystem.CARTESIAN)
069: ? CSGeometryFactory.DEFAULT : new CSGeometryFactory(cs);
070: */
071: }
072:
073: public GeometricAttributeType(String name, Class type,
074: boolean nillable, Object defaultValue,
075: CoordinateReferenceSystem cs, Filter filter) {
076: this (name, type, nillable, 1, 1, defaultValue, cs, filter);
077: }
078:
079: public GeometricAttributeType(GeometricAttributeType copy,
080: CoordinateReferenceSystem override) {
081: super (copy);
082: coordinateSystem = copy.getCoordinateSystem();
083:
084: if (override != null) {
085: coordinateSystem = override;
086: }
087:
088: if (coordinateSystem == null) {
089: coordinateSystem = DefaultGeocentricCRS.CARTESIAN;
090: }
091: geometryFactory = (coordinateSystem == DefaultGeocentricCRS.CARTESIAN) ? CSGeometryFactory.DEFAULT
092: : new CSGeometryFactory(coordinateSystem);
093: }
094:
095: public CoordinateReferenceSystem getCoordinateSystem() {
096: return coordinateSystem;
097: }
098:
099: public GeometryFactory getGeometryFactory() {
100: return geometryFactory;
101: }
102:
103: public Object parse(Object value) throws IllegalArgumentException {
104: if (value == null) {
105: return value;
106: }
107:
108: if (value instanceof Geometry) {
109: return value;
110: }
111:
112: if (value instanceof Envelope) {
113: //convert enevelope to polygon
114: return JTS.toGeometry((Envelope) value);
115: }
116:
117: if (value instanceof String) {
118: String wkt = (String) value;
119: WKTReader reader = new WKTReader();
120: try {
121: return reader.read(wkt);
122: } catch (com.vividsolutions.jts.io.ParseException pe) {
123: throw new IllegalArgumentException(
124: "Could not parse the " + "string: " + wkt
125: + " to well known text");
126: }
127: }
128: // consider wkb/gml support?
129: throw new IllegalArgumentException(
130: "AttributeGT.Geometric cannot parse " + value);
131: }
132:
133: public Object duplicate(Object o) throws IllegalAttributeException {
134: if (o == null)
135: return o;
136: if (o instanceof Geometry) {
137: return ((Geometry) o).clone();
138: }
139: throw new IllegalAttributeException("Cannot duplicate "
140: + o.getClass().getName());
141: }
142:
143: /* (non-Javadoc)
144: * @see org.geotools.feature.PrimativeAttributeType#getRestriction()
145: */
146: public Filter getRestriction() {
147: return filter;
148: }
149: }
150:
151: /**
152: * Helper class used to force CS information on JTS Geometry
153: */
154: class CSGeometryFactory extends GeometryFactory {
155:
156: static public GeometryFactory DEFAULT = new GeometryFactory();
157: static public PrecisionModel DEFAULT_PRECISON_MODEL = new PrecisionModel();
158:
159: public CSGeometryFactory(CoordinateReferenceSystem cs) {
160: super (toPrecisionModel(cs), toSRID(cs));
161: }
162:
163: public GeometryCollection createGeometryCollection(
164: Geometry[] geometries) {
165: GeometryCollection gc = super
166: .createGeometryCollection(geometries);
167:
168: // JTS14
169: //gc.setUserData( cs );
170: return gc;
171: }
172:
173: public LinearRing createLinearRing(Coordinate[] coordinates) {
174: LinearRing lr = super .createLinearRing(coordinates);
175:
176: // JTS14
177: //gc.setUserData( cs );
178: return lr;
179: }
180:
181: //
182: // And so on
183: // Utility Functions
184: private static int toSRID(CoordinateReferenceSystem cs) {
185: if ((cs == null) || (cs == DefaultGeocentricCRS.CARTESIAN)) {
186: return 0;
187: }
188:
189: // not sure how to tell SRID from CoordinateSystem?
190: return 0;
191: }
192:
193: private static PrecisionModel toPrecisionModel(
194: CoordinateReferenceSystem cs) {
195: if ((cs == null) || (cs == DefaultGeocentricCRS.CARTESIAN)) {
196: return DEFAULT_PRECISON_MODEL;
197: }
198:
199: return DEFAULT_PRECISON_MODEL;
200: }
201: }
|