MATSIM
GeotoolsTransformation.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * GeotoolsTransformation.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.geometry.transformations;
22 
23 import org.geotools.api.referencing.FactoryException;
24 import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
25 import org.geotools.api.referencing.operation.MathTransform;
26 import org.geotools.api.referencing.operation.TransformException;
27 import org.geotools.geometry.jts.JTS;
28 import org.geotools.referencing.CRS;
29 import org.locationtech.jts.geom.Point;
30 import org.matsim.api.core.v01.Coord;
34 
41 
42  private MathTransform transform;
43 
55  public GeotoolsTransformation(final String from, final String to) {
56  CoordinateReferenceSystem sourceCRS = MGC.getCRS(from);
57  CoordinateReferenceSystem targetCRS = MGC.getCRS(to);
58 
59  try {
60  this.transform = CRS.findMathTransform(sourceCRS, targetCRS,true);
61  } catch (FactoryException e) {
62  throw new RuntimeException(e);
63  }
64  }
65 
66  @Override
67  public Coord transform(final Coord coord) {
68  Point p = null;
69  try {
70  p = (Point) JTS.transform(MGC.coord2Point(coord), this.transform);
71  } catch (TransformException e) {
72  throw new RuntimeException(e);
73  }
74  if(coord.hasZ()){
75  return CoordUtils.createCoord(p.getX(), p.getY(), coord.getZ());
76  } else{
77  return MGC.point2Coord(p);
78  }
79  }
80 
81 
82 
83 }
static Coord point2Coord(final Point point)
Definition: MGC.java:148
static Point coord2Point(final Coord coord)
Definition: MGC.java:131
static Coord createCoord(final Coordinate coordinate)
Definition: CoordUtils.java:41
static CoordinateReferenceSystem getCRS(final String wktOrAuthorityCodeOrShorthandName)
Definition: MGC.java:169