|
1 | | -import proj4 from 'proj4' |
2 | | - |
3 | | - |
4 | 1 | if (!window.dash_clientside) { |
5 | 2 | window.dash_clientside = {}; |
6 | 3 | } |
7 | 4 |
|
8 | | -// Proj4 coordinate conversion object |
9 | | -// object with 2 methods: `forward` converts from lat/lon to x/y, |
10 | | -// and `inverse` converts from x/y to lat/lon. |
11 | | -// - EPSG:4326 is the 'standard' lat/lon coordinate system, |
| 5 | +// Functions to convert lat/lon used by Leaflet to x/y used by Cytoscape. |
| 6 | +// - EPSG:4326 is the 'standard' (Mercator) grid coordinate system, |
| 7 | +// which maps nicely to Cytoscape's x/y grid |
12 | 8 | // - EPSG:3857 is the 'Web Mercator' projection used by many |
13 | 9 | // online mapping serveces including Leaflet |
14 | | -var _proj4js_converter = proj4('EPSG:4326', 'EPSG:3857'); |
| 10 | +// Reference: https://epsg.io/3857 |
15 | 11 |
|
| 12 | +// Conversion factor based on EPSG:3857 bounds |
| 13 | +var conversion_factor = 20037508.34; |
| 14 | + |
| 15 | +// Convert EPSG:4326 to EPSG:3857 |
| 16 | +// We also flip the sign of the y-value to match Cytoscape's coordinate system |
16 | 17 | function lonLatToXY(lon, lat) { |
17 | | - var xy = _proj4js_converter.forward([lon, lat]); |
18 | | - var x = xy[0]; |
19 | | - var y = -xy[1]; |
20 | | - return [x, y]; |
| 18 | + var x = lon * conversion_factor / 180; |
| 19 | + var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180); |
| 20 | + y = y * conversion_factor / 180; |
| 21 | + return [x, -y]; |
21 | 22 | } |
22 | 23 |
|
| 24 | +// Convert EPSG:3857 to EPSG:4326 |
| 25 | +// We also flip the sign of the y-value to match Cytoscape's coordinate system |
23 | 26 | function xYToLonLat(x, y) { |
24 | | - var lonlat = _proj4js_converter.inverse([x, -y]); |
25 | | - var lon = lonlat[0]; |
26 | | - var lat = lonlat[1]; |
| 27 | + var lon = x * 180 / conversion_factor; |
| 28 | + var lat = Math.atan(Math.exp(-y * Math.PI / conversion_factor)) * 360 / Math.PI - 90; |
27 | 29 | return [lon, lat]; |
28 | 30 | } |
29 | 31 |
|
30 | | - |
31 | 32 | window.dash_clientside.cyleaflet_utils = { |
32 | 33 | }; |
33 | 34 |
|
|
0 commit comments