Skip to content

Commit 6fb8d99

Browse files
author
matafokka
committed
Added dateline wrapping to the Rectangle Layer
1 parent 104cd87 commit 6fb8d99

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

SynthPolygonLayer/SynthPolygonLayer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ L.ALS.SynthPolygonLayer = L.ALS.SynthPolygonBaseLayer.extend({
105105
}
106106

107107
// Skip cloned layers
108-
if (layer.isCloned) {
108+
if (layer.isCloned)
109109
return;
110-
}
111110

112111
this.cloneLayerIfNeeded(layer);
113112

SynthRectangleBaseLayer/drawPaths.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ L.ALS.SynthRectangleBaseLayer.prototype.drawPathsWorker = function (isParallels)
6262

6363
lat = startLat, lng = startLng,
6464
turfPolygonCoordinates = turfPolygon.geometry.coordinates[0], // MathTools accepts coordinates of the polygon, not polygon itself
65-
number = 1, connectionLine = L.polyline([], connLineOptions),
65+
number = 1, connectionLine = new L.WrappedPolyline([], connLineOptions),
6666
prevLine, shouldDraw = true;
6767

6868
connectionLine.actualPaths = [];
@@ -133,7 +133,7 @@ L.ALS.SynthRectangleBaseLayer.prototype.drawPathsWorker = function (isParallels)
133133
secondPoint = endPoint;
134134
}
135135

136-
let line = L.polyline([], lineOptions); // Contains paths with turns, i.e. internal connections
136+
let line = new L.WrappedPolyline([], lineOptions); // Contains paths with turns, i.e. internal connections
137137

138138
for (let point of [firstPoint, secondPoint]) {
139139
// Add points to the path
@@ -147,7 +147,7 @@ L.ALS.SynthRectangleBaseLayer.prototype.drawPathsWorker = function (isParallels)
147147

148148
let labelId = L.ALS.Helpers.generateID();
149149
this.pathsLabelsIds.push(labelId);
150-
this.labelsGroup.addLabel(labelId, coord, number, L.LabelLayer.DefaultDisplayOptions[isParallels ? "Message" : "Error"]);
150+
this.labelsGroup.addLabel(labelId, L.latLng(coord).wrap(), number, L.LabelLayer.DefaultDisplayOptions[isParallels ? "Message" : "Error"]);
151151
number++;
152152
}
153153

SynthRectangleLayer/SynthRectangleLayer.js

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ L.ALS.SynthRectangleLayer = L.ALS.SynthRectangleBaseLayer.extend({
3434
let layersWereInvalidated = false;
3535

3636
this.polygonGroup.eachLayer((layer) => {
37+
// Remove a linked layer when a layer either original or cloned has been removed
38+
if (layer.linkedLayer && !this.polygonGroup.hasLayer(layer.linkedLayer)) {
39+
this.polygonGroup.removeLayer(layer);
40+
return;
41+
}
42+
43+
// Skip cloned layers
44+
if (layer.isCloned)
45+
return;
46+
47+
this.cloneLayerIfNeeded(layer);
48+
3749
// Limit polygon size by limiting total paths count
3850
let bounds = layer.getBounds(), topLeft = bounds.getNorthWest(),
3951
parallelsPathsCount = Math.ceil(this.getParallelOrMeridianLineLength(topLeft, bounds.getSouthWest()) / this.By) + 1,

WrappedPolyline.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* A class for displaying paths by parallels and meridians. Shouldn't be using for anything else.
3+
*
4+
* @class
5+
* @extends L.Polyline
6+
*/
7+
L.WrappedPolyline = L.Polyline.extend(/** @lends L.WrappedPolyline.prototype */{
8+
initialize: function (latlngs, options) {
9+
L.Util.setOptions(this, options);
10+
this.setLatLngs(latlngs);
11+
},
12+
13+
setLatLngs: function (latlngs) {
14+
this._originalLatLngs = [];
15+
16+
if (latlngs.length === 0) {
17+
L.Polyline.prototype.setLatLngs.call(this, this._originalLatLngs);
18+
return this;
19+
}
20+
21+
let segments = [this._originalLatLngs, []], moveBy = 0, isFirstIteration = true;
22+
23+
for (let segment of segments) {
24+
for (let i = 0; i < latlngs.length; i++) {
25+
let point = L.latLng(latlngs[i]).clone();
26+
27+
if (isFirstIteration && !moveBy) {
28+
if (point.lng < -180)
29+
moveBy = 360;
30+
else if (point.lng > 180)
31+
moveBy = -360;
32+
} else if (!isFirstIteration)
33+
point.lng += moveBy;
34+
35+
segment.push(point);
36+
}
37+
isFirstIteration = false;
38+
}
39+
40+
L.Polyline.prototype.setLatLngs.call(this, moveBy === 0 ? this._originalLatLngs : segments);
41+
return this;
42+
},
43+
44+
addLatLng: function (latLng) {
45+
return this.setLatLngs([...this._originalLatLngs, latLng]);
46+
},
47+
48+
getLatLngs: function () {
49+
return this._originalLatLngs;
50+
},
51+
52+
toGeoJSON: function (precision) {
53+
return new L.Polyline(this._originalLatLngs).toGeoJSON(precision);
54+
}
55+
});

main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ window.L = require("leaflet");
1717
L.GEODESIC_SEGMENTS = 500;
1818

1919
L.Geodesic = require("leaflet.geodesic").GeodesicLine;
20+
require("./WrappedPolyline.js");
2021
require("leaflet-draw");
2122
require("./DrawGeodesic.js");
2223
require("leaflet-advanced-layer-system");
@@ -132,8 +133,7 @@ let overlayLayer = new L.BlackOverlayLayer({
132133
// When drawing starts, hide notifications and black overlay, but add red datelines
133134

134135
let datelines = L.featureGroup();
135-
for (let sign of [1, -1]) {
136-
let lng = 180 * sign;
136+
for (let lng of [180, -180]) {
137137
datelines.addLayer(L.polyline([[90, lng], [-90, lng]], {
138138
color: "red",
139139
weight: 1,

0 commit comments

Comments
 (0)