]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/ModelledCRS.java
Enhancements to district functionalities and code
[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     // TODO: these only work with Spherical Mercator
32     public static double xToLongitude(double x) {
33         return x;
34     }
35
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);
41         return finald;
42     }
43
44     public static double longitudeToX(double lon) {
45         return lon;
46     }
47     
48     private static double asinh(double x) {
49         return Math.log(x + Math.sqrt(x*x + 1.0));
50     }
51     
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);
57         return f;
58     }
59 }