]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/ModelledCRS.java
Final push of district changes
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / ModelledCRS.java
1 package org.simantics.district.network;
2
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;
11
12 public class ModelledCRS implements CRS {
13
14     private GeodeticCalculator calculator;
15
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);
19         
20         this.calculator = new GeodeticCalculator(crs);
21     }
22     
23     @Override
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();
28         return dist;
29     }
30
31     
32     // TODO: these only work with Spherical Mercator
33     public static double xToLongitude(double x) {
34         return x;
35     }
36     
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);
42         return finald;
43     }
44     
45     public static double longitudeToX(double lon) {
46         return lon;
47     }
48     
49     private static double asinh(double x) {
50         return Math.log(x + Math.sqrt(x*x + 1.0));
51     }
52     
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);
58         return f;
59     }
60 }