Skip to content

Commit 88e6640

Browse files
committed
inline coordinate conversion functions instead of importing proj4js
1 parent 4b6de95 commit 88e6640

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

dash_cytoscape/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"cytoscape-spread": "^3.0.0",
4949
"cytoscape-svg": "0.4.0",
5050
"lodash": "^4.17.21",
51-
"proj4": "^2.9.2",
5251
"ramda": "^0.29.0",
5352
"react": ">=16",
5453
"react-cytoscapejs": "2.0.0",

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"cytoscape-spread": "^3.0.0",
4949
"cytoscape-svg": "0.4.0",
5050
"lodash": "^4.17.21",
51-
"proj4": "^2.9.2",
5251
"ramda": "^0.29.0",
5352
"react": ">=16",
5453
"react-cytoscapejs": "2.0.0",

src/lib/cyleaflet_clientside.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
import proj4 from 'proj4'
2-
3-
41
if (!window.dash_clientside) {
52
window.dash_clientside = {};
63
}
74

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
128
// - EPSG:3857 is the 'Web Mercator' projection used by many
139
// online mapping serveces including Leaflet
14-
var _proj4js_converter = proj4('EPSG:4326', 'EPSG:3857');
10+
// Reference: https://epsg.io/3857
1511

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
1617
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];
2122
}
2223

24+
// Convert EPSG:3857 to EPSG:4326
25+
// We also flip the sign of the y-value to match Cytoscape's coordinate system
2326
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;
2729
return [lon, lat];
2830
}
2931

30-
3132
window.dash_clientside.cyleaflet_utils = {
3233
};
3334

0 commit comments

Comments
 (0)