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.district.network.ontology.DistrictNetworkResource;
12 public class ModelledCRS implements CRS {
14 private GeodeticCalculator calculator;
16 public ModelledCRS(ReadGraph graph, Resource type) throws DatabaseException, FactoryException {
17 String wkt = graph.getRelatedValue2(type, DistrictNetworkResource.getInstance(graph).HasSRTEXT, Bindings.STRING);
18 CoordinateReferenceSystem crs = org.geotools.referencing.CRS.parseWKT(wkt);
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();
32 // TODO: these only work with Spherical Mercator
33 public static double xToLongitude(double x) {
37 public static double yToLatitude(double y) {
38 double rad = Math.toRadians(y);
39 double sinh = Math.sinh(rad);
40 double atan = Math.atan(sinh);
41 double finald = Math.toDegrees(atan);
45 public static double longitudeToX(double lon) {
49 private static double asinh(double x) {
50 return Math.log(x + Math.sqrt(x*x + 1.0));
53 public static double latitudeToY(double lat) {
54 double frad = Math.toRadians(lat);
55 double ftan = Math.tan(frad);
56 double fsin = asinh(ftan);
57 double f = Math.toDegrees(fsin);