1 package org.simantics.district.network;
3 import org.geotools.referencing.GeodeticCalculator;
4 import org.opengis.referencing.FactoryException;
5 import org.opengis.referencing.crs.CoordinateReferenceSystem;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.layer0.Layer0;
12 public class ModelledCRS implements CRS {
14 private GeodeticCalculator calculator;
16 public ModelledCRS(ReadGraph graph, Resource type) throws DatabaseException, FactoryException {
17 String code = graph.getRelatedValue2(type, Layer0.getInstance(graph).HasLabel, Bindings.STRING);
18 CoordinateReferenceSystem crs = org.geotools.referencing.CRS.decode(code);
20 this.calculator = new GeodeticCalculator(crs);
24 public double calculateDistance(double[] start, double[] end) {
25 calculator.setStartingGeographicPoint(start[0], start[1]);
26 calculator.setDestinationGeographicPoint(end[0], end[1]);
27 double dist = calculator.getOrthodromicDistance();
31 // TODO: these only work with Spherical Mercator
32 public static double xToLongitude(double x) {
36 public static double yToLatitude(double y) {
37 double rad = Math.toRadians(y);
38 double sinh = Math.sinh(rad);
39 double atan = Math.atan(sinh);
40 double finald = Math.toDegrees(atan);
44 public static double longitudeToX(double lon) {
48 private static double asinh(double x) {
49 return Math.log(x + Math.sqrt(x*x + 1.0));
52 public static double latitudeToY(double lat) {
53 double frad = Math.toRadians(lat);
54 double ftan = Math.tan(frad);
55 double fsin = asinh(ftan);
56 double f = Math.toDegrees(fsin);