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.util;
017:
018: import org.geotools.factory.Hints;
019:
020: import com.vividsolutions.jts.geom.Coordinate;
021: import com.vividsolutions.jts.geom.Envelope;
022: import com.vividsolutions.jts.geom.Geometry;
023: import com.vividsolutions.jts.geom.GeometryFactory;
024: import com.vividsolutions.jts.io.WKTReader;
025:
026: /**
027: * Converter factory performing converstions among geometric types.
028: * <p>
029: * Supported conversions:
030: * <ul>
031: * <li>{@link String} to {@link com.vividsolutions.jts.geom.Geometry}
032: * <li>{@link com.vividsolutions.jts.geom.Geometry} to {@link String}
033: * <li>{@link com.vividsolutions.jts.geom.Envelope} to {@link com.vividsolutions.jts.geom.Geometry}
034: * <li>{@link com.vividsolutions.jts.geom.Geometry} to {@link com.vividsolutions.jts.geom.Envelope}
035: * <li>
036: * </ul>
037: * </p>
038: * @author Justin Deoliveira, The Open Planning Project
039: * @since 2.4
040: */
041: public class GeometryConverterFactory implements ConverterFactory {
042:
043: public Converter createConverter(Class source, Class target,
044: Hints hints) {
045:
046: if (Geometry.class.isAssignableFrom(target)) {
047:
048: //String to Geometry
049: if (String.class.equals(source)) {
050: return new Converter() {
051: public Object convert(Object source, Class target)
052: throws Exception {
053: return new WKTReader().read((String) source);
054: }
055: };
056: }
057:
058: //Envelope to Geometry
059: if (Envelope.class.isAssignableFrom(source)) {
060: return new Converter() {
061: public Object convert(Object source, Class target)
062: throws Exception {
063: Envelope e = (Envelope) source;
064: GeometryFactory factory = new GeometryFactory();
065: return factory.createPolygon(factory
066: .createLinearRing(new Coordinate[] {
067: new Coordinate(e.getMinX(), e
068: .getMinY()),
069: new Coordinate(e.getMaxX(), e
070: .getMinY()),
071: new Coordinate(e.getMaxX(), e
072: .getMaxY()),
073: new Coordinate(e.getMinX(), e
074: .getMaxY()),
075: new Coordinate(e.getMinX(), e
076: .getMinY()) }), null);
077: }
078: };
079: }
080: }
081:
082: if (Geometry.class.isAssignableFrom(source)) {
083: //Geometry to envelope
084: if (Envelope.class.equals(target)) {
085: return new Converter() {
086: public Object convert(Object source, Class target)
087: throws Exception {
088: Geometry geometry = (Geometry) source;
089: return geometry.getEnvelopeInternal();
090: }
091: };
092: }
093:
094: //Geometry to String
095: if (String.class.equals(target)) {
096: return new Converter() {
097: public Object convert(Object source, Class target)
098: throws Exception {
099: Geometry geometry = (Geometry) source;
100: return geometry.toText();
101: }
102: };
103: }
104: }
105:
106: return null;
107: }
108: }
|