001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.coordsys;
034:
035: import com.vividsolutions.jts.geom.Coordinate;
036: import com.vividsolutions.jts.geom.CoordinateFilter;
037: import com.vividsolutions.jts.geom.Geometry;
038: import com.vividsolutions.jump.feature.Feature;
039:
040: /**
041: * The source and destination coordinate reference systems must have
042: * the same datum (for example, WGS 84).
043: */
044: public class Reprojector {
045: private static Reprojector instance = new Reprojector();
046:
047: private Reprojector() {
048: }
049:
050: public static Reprojector instance() {
051: return instance;
052: }
053:
054: public boolean wouldChangeValues(CoordinateSystem source,
055: CoordinateSystem destination) {
056: if (source == CoordinateSystem.UNSPECIFIED) {
057: return false;
058: }
059:
060: if (destination == CoordinateSystem.UNSPECIFIED) {
061: return false;
062: }
063:
064: if (source == destination) {
065: return false;
066: }
067:
068: return true;
069: }
070:
071: public void reproject(Coordinate coordinate,
072: CoordinateSystem source, CoordinateSystem destination) {
073: if (!wouldChangeValues(source, destination)) {
074: return;
075: }
076:
077: Planar result = destination.getProjection().asPlanar(
078: source.getProjection().asGeographic(
079: new Planar(coordinate.x, coordinate.y),
080: new Geographic()), new Planar());
081: coordinate.x = result.x;
082: coordinate.y = result.y;
083: }
084:
085: public void reproject(Geometry geometry,
086: final CoordinateSystem source,
087: final CoordinateSystem destination) {
088: if (!wouldChangeValues(source, destination)) {
089: return;
090: }
091:
092: geometry.apply(new CoordinateFilter() {
093: public void filter(Coordinate coord) {
094: reproject(coord, source, destination);
095: }
096: });
097: geometry.setSRID(destination.getEPSGCode());
098: geometry.geometryChanged();
099: }
100: }
|