1 | package org.openstreetmap.gui.jmapviewer.tilesources;
|
---|
2 |
|
---|
3 | import java.util.Random;
|
---|
4 |
|
---|
5 | import org.openstreetmap.gui.jmapviewer.OsmMercator;
|
---|
6 |
|
---|
7 | public class ScanexTileSource extends AbstractTSMTileSource {
|
---|
8 | private static String API_KEY = "4018C5A9AECAD8868ED5DEB2E41D09F7";
|
---|
9 |
|
---|
10 | private enum ScanexLayer {
|
---|
11 | IRS("irs", "/TileSender.ashx?ModeKey=tile&MapName=F7B8CF651682420FA1749D894C8AD0F6&LayerName=BAC78D764F0443BD9AF93E7A998C9F5B"),
|
---|
12 | SPOT("spot", "/TileSender.ashx?ModeKey=tile&MapName=F7B8CF651682420FA1749D894C8AD0F6&LayerName=F51CE95441284AF6B2FC319B609C7DEC");
|
---|
13 |
|
---|
14 | private String name;
|
---|
15 | private String uri;
|
---|
16 |
|
---|
17 | ScanexLayer(String name, String uri) {
|
---|
18 | this.name = name;
|
---|
19 | this.uri = uri;
|
---|
20 | }
|
---|
21 | public String getName() {
|
---|
22 | return name;
|
---|
23 | }
|
---|
24 | public String getUri() {
|
---|
25 | return uri;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | /* IRS by default */
|
---|
30 | private ScanexLayer Layer = ScanexLayer.IRS;
|
---|
31 |
|
---|
32 | public ScanexTileSource(String url) {
|
---|
33 | super("Scanex" + url, "http://maps.kosmosnimki.ru");
|
---|
34 |
|
---|
35 | for (ScanexLayer layer : ScanexLayer.values()) {
|
---|
36 | if (url.equalsIgnoreCase(layer.getName())) {
|
---|
37 | this.Layer = layer;
|
---|
38 | break;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public int getMaxZoom() {
|
---|
45 | return 14;
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public String getExtension() {
|
---|
50 | return("jpeg");
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public String getTilePath(int zoom, int tilex, int tiley) {
|
---|
55 | int tmp = (int)Math.pow(2.0, zoom - 1);
|
---|
56 |
|
---|
57 | tilex = tilex - tmp;
|
---|
58 | tiley = tmp - tiley - 1;
|
---|
59 |
|
---|
60 | return this.Layer.getUri() + "&apikey=" + API_KEY + "&x=" + tilex + "&y=" + tiley + "&z=" + zoom;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public TileUpdate getTileUpdate() {
|
---|
64 | return TileUpdate.IfNoneMatch;
|
---|
65 | }
|
---|
66 |
|
---|
67 | private static double RADIUS_E = 6378137; /* radius of Earth at equator, m */
|
---|
68 | private static double EQUATOR = 40075016.68557849; /* equator length, m */
|
---|
69 | private static double E = 0.0818191908426; /* eccentricity of Earth's ellipsoid */
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public double latToTileY(double lat, int zoom) {
|
---|
73 | double tmp = Math.tan(Math.PI/4 * (1 + lat/90));
|
---|
74 | double pow = Math.pow(Math.tan(Math.PI/4 + Math.asin(E * Math.sin(Math.toRadians(lat)))/2), E);
|
---|
75 |
|
---|
76 | return (EQUATOR/2 - (RADIUS_E * Math.log(tmp/pow))) * Math.pow(2.0, zoom) / EQUATOR;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public double lonToTileX(double lon, int zoom) {
|
---|
81 | return (RADIUS_E * lon * Math.PI / (90*EQUATOR) + 1) * Math.pow(2.0, zoom - 1);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * To solve inverse formula latitude = f(y) we use
|
---|
86 | * Newton's method. We cache previous calculated latitude,
|
---|
87 | * because new one is usually close to the old one. In case
|
---|
88 | * if solution gets out of bounds, we reset to a new random
|
---|
89 | * value.
|
---|
90 | */
|
---|
91 | private double cached_lat = 0;
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public double tileYToLat(int y, int zoom) {
|
---|
95 | Random r= new Random();
|
---|
96 | double lat0, lat;
|
---|
97 |
|
---|
98 | lat = cached_lat;
|
---|
99 | do {
|
---|
100 | lat0 = lat;
|
---|
101 | lat = lat - Math.toDegrees(NextTerm(Math.toRadians(lat), y, zoom));
|
---|
102 | if (lat > OsmMercator.MAX_LAT || lat < OsmMercator.MIN_LAT) {
|
---|
103 | lat = OsmMercator.MIN_LAT +
|
---|
104 | (double )r.nextInt((int )(OsmMercator.MAX_LAT -
|
---|
105 | OsmMercator.MIN_LAT));
|
---|
106 | }
|
---|
107 | } while ((Math.abs(lat0 - lat) > 0.000001));
|
---|
108 |
|
---|
109 | cached_lat = lat;
|
---|
110 |
|
---|
111 | return (lat);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Next term in Newton's polynomial */
|
---|
115 | private double NextTerm(double lat, double y, int zoom) {
|
---|
116 | double sinl=Math.sin(lat);
|
---|
117 | double cosl=Math.cos(lat);
|
---|
118 | double ec, f, df;
|
---|
119 |
|
---|
120 | zoom = (int )Math.pow(2.0, zoom - 1);
|
---|
121 | ec = Math.exp((1 - y/zoom)*Math.PI);
|
---|
122 |
|
---|
123 | f = (Math.tan(Math.PI/4+lat/2) -
|
---|
124 | ec * Math.pow(Math.tan(Math.PI/4 + Math.asin(E * sinl)/2), E));
|
---|
125 | df = 1/(1 - sinl) - ec * E * cosl/((1 - E * sinl) *
|
---|
126 | (Math.sqrt (1 - E * E * sinl * sinl)));
|
---|
127 |
|
---|
128 | return (f/df);
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | public double tileXToLon(int x, int zoom) {
|
---|
133 | return (x / Math.pow(2.0, zoom - 1) - 1) * (90*EQUATOR) / RADIUS_E / Math.PI;
|
---|
134 | }
|
---|
135 | }
|
---|