Skip to content

Commit b2cedbf

Browse files
FranckLecuyeramarantTristan-WorkGH
authored andcommitted
Typescript migration (#123)
Signed-off-by: Franck LECUYER <[email protected]> Co-authored-by: Arnaud Marant <[email protected]> Co-authored-by: Tristan Chuine <[email protected]>
1 parent d806c92 commit b2cedbf

13 files changed

+1728
-1200
lines changed

demo/src/App.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import { useEffect, useRef } from 'react';
99
import { createTheme, StyledEngineProvider, ThemeProvider } from '@mui/material/styles';
1010
import { GeoData, NetworkMap, NetworkMapRef } from '../../src';
11-
import { Equipment } from '../../src/components/network-map-viewer/network/map-equipments';
1211
import { addNadToDemo, addSldToDemo } from './diagram-viewers/add-diagrams';
1312
import DemoMapEquipments from './map-viewer/demo-map-equipments';
1413

1514
import sposdata from './map-viewer/data/spos.json';
1615
import lposdata from './map-viewer/data/lpos.json';
1716
import smapdata from './map-viewer/data/smap.json';
1817
import lmapdata from './map-viewer/data/lmap.json';
18+
import { Equipment } from '../../src/components/network-map-viewer/utils/equipment-types';
1919

2020
export default function App() {
2121
const INITIAL_ZOOM = 9;

src/components/network-map-viewer/network/geo-data.js renamed to src/components/network-map-viewer/network/geo-data.ts

+51-35
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,49 @@
88
import { computeDestinationPoint, getGreatCircleBearing, getRhumbLineBearing } from 'geolib';
99
import cheapRuler from 'cheap-ruler';
1010
import { ArrowDirection } from './layers/arrow-layer';
11+
import { Line, LonLat } from '../utils/equipment-types';
12+
import { MapEquipments } from './map-equipments';
1113

12-
const substationPositionByIdIndexer = (map, substation) => {
14+
export type Coordinate = {
15+
lon: number;
16+
lat: number;
17+
};
18+
19+
export type SubstationPosition = {
20+
id: string;
21+
coordinate: Coordinate;
22+
};
23+
24+
export type LinePosition = {
25+
id: string;
26+
coordinates: Coordinate[];
27+
};
28+
29+
const substationPositionByIdIndexer = (map: Map<string, Coordinate>, substation: SubstationPosition) => {
1330
map.set(substation.id, substation.coordinate);
1431
return map;
1532
};
1633

17-
const linePositionByIdIndexer = (map, line) => {
34+
const linePositionByIdIndexer = (map: Map<string, Coordinate[]>, line: LinePosition) => {
1835
map.set(line.id, line.coordinates);
1936
return map;
2037
};
2138

2239
export class GeoData {
23-
substationPositionsById = new Map();
24-
25-
linePositionsById = new Map();
40+
substationPositionsById = new Map<string, Coordinate>();
41+
linePositionsById = new Map<string, Coordinate[]>();
2642

27-
constructor(substationPositionsById, linePositionsById) {
43+
constructor(substationPositionsById: Map<string, Coordinate>, linePositionsById: Map<string, Coordinate[]>) {
2844
this.substationPositionsById = substationPositionsById;
2945
this.linePositionsById = linePositionsById;
3046
}
3147

32-
setSubstationPositions(positions) {
48+
setSubstationPositions(positions: SubstationPosition[]) {
3349
// index positions by substation id
3450
this.substationPositionsById = positions.reduce(substationPositionByIdIndexer, new Map());
3551
}
3652

37-
updateSubstationPositions(substationIdsToUpdate, fetchedPositions) {
53+
updateSubstationPositions(substationIdsToUpdate: string[], fetchedPositions: SubstationPosition[]) {
3854
fetchedPositions.forEach((pos) => this.substationPositionsById.set(pos.id, pos.coordinate));
3955
// If a substation position is requested but not present in the fetched results, we delete its position.
4056
// It allows to cancel the position of a substation when the server can't situate it anymore after a network modification (for example a line deletion).
@@ -43,7 +59,7 @@ export class GeoData {
4359
.forEach((id) => this.substationPositionsById.delete(id));
4460
}
4561

46-
getSubstationPosition(substationId) {
62+
getSubstationPosition(substationId: string): LonLat {
4763
const position = this.substationPositionsById.get(substationId);
4864
if (!position) {
4965
console.warn(`Position not found for ${substationId}`);
@@ -52,12 +68,12 @@ export class GeoData {
5268
return [position.lon, position.lat];
5369
}
5470

55-
setLinePositions(positions) {
71+
setLinePositions(positions: LinePosition[]) {
5672
// index positions by line id
5773
this.linePositionsById = positions.reduce(linePositionByIdIndexer, new Map());
5874
}
5975

60-
updateLinePositions(lineIdsToUpdate, fetchedPositions) {
76+
updateLinePositions(lineIdsToUpdate: string[], fetchedPositions: LinePosition[]) {
6177
fetchedPositions.forEach((pos) => {
6278
this.linePositionsById.set(pos.id, pos.coordinates);
6379
});
@@ -72,7 +88,7 @@ export class GeoData {
7288
/**
7389
* Get line positions always ordered from side 1 to side 2.
7490
*/
75-
getLinePositions(network, line, detailed = true) {
91+
getLinePositions(network: MapEquipments, line: Line, detailed = true): LonLat[] {
7692
const voltageLevel1 = network.getVoltageLevel(line.voltageLevelId1);
7793
if (!voltageLevel1) {
7894
throw new Error(`Voltage level side 1 '${line.voltageLevelId1}' not found`);
@@ -101,7 +117,7 @@ export class GeoData {
101117
const linePositions = this.linePositionsById.get(line.id);
102118
// Is there any position for this line ?
103119
if (linePositions) {
104-
const positions = new Array(linePositions.length);
120+
const positions = new Array<LonLat>(linePositions.length);
105121

106122
for (const [index, position] of linePositions.entries()) {
107123
positions[index] = [position.lon, position.lat];
@@ -114,9 +130,9 @@ export class GeoData {
114130
return [substationPosition1, substationPosition2];
115131
}
116132

117-
getLineDistances(positions) {
133+
getLineDistances(positions: LonLat[]) {
118134
if (positions !== null && positions.length > 1) {
119-
let cumulativeDistanceArray = [0];
135+
const cumulativeDistanceArray = [0];
120136
let cumulativeDistance = 0;
121137
let segmentDistance;
122138
let ruler;
@@ -136,13 +152,13 @@ export class GeoData {
136152
* along with the remaining distance to travel on this segment to be at the exact wanted distance
137153
* (implemented using a binary search)
138154
*/
139-
findSegment(positions, cumulativeDistances, wantedDistance) {
155+
findSegment(positions: LonLat[], cumulativeDistances: number[], wantedDistance: number) {
140156
let lowerBound = 0;
141157
let upperBound = cumulativeDistances.length - 1;
142158
let middlePoint;
143159
while (lowerBound + 1 !== upperBound) {
144160
middlePoint = Math.floor((lowerBound + upperBound) / 2);
145-
let middlePointDistance = cumulativeDistances[middlePoint];
161+
const middlePointDistance = cumulativeDistances[middlePoint];
146162
if (middlePointDistance <= wantedDistance) {
147163
lowerBound = middlePoint;
148164
} else {
@@ -151,21 +167,21 @@ export class GeoData {
151167
}
152168
return {
153169
idx: lowerBound,
154-
segment: positions.slice(lowerBound, lowerBound + 2),
170+
segment: positions.slice(lowerBound, lowerBound + 2) as [LonLat, LonLat],
155171
remainingDistance: wantedDistance - cumulativeDistances[lowerBound],
156172
};
157173
}
158174

159175
labelDisplayPosition(
160-
positions,
161-
cumulativeDistances,
162-
arrowPosition,
163-
arrowDirection,
164-
lineParallelIndex,
165-
lineAngle,
166-
proximityAngle,
167-
distanceBetweenLines,
168-
proximityFactor
176+
positions: LonLat[],
177+
cumulativeDistances: number[],
178+
arrowPosition: number,
179+
arrowDirection: ArrowDirection,
180+
lineParallelIndex: number,
181+
lineAngle: number,
182+
proximityAngle: number,
183+
distanceBetweenLines: number,
184+
proximityFactor: number
169185
) {
170186
if (arrowPosition > 1 || arrowPosition < 0) {
171187
throw new Error('Proportional position value incorrect: ' + arrowPosition);
@@ -177,7 +193,7 @@ export class GeoData {
177193
) {
178194
return null;
179195
}
180-
let lineDistance = cumulativeDistances[cumulativeDistances.length - 1];
196+
const lineDistance = cumulativeDistances[cumulativeDistances.length - 1];
181197
let wantedDistance = lineDistance * arrowPosition;
182198

183199
if (cumulativeDistances.length === 2) {
@@ -187,7 +203,7 @@ export class GeoData {
187203
wantedDistance = wantedDistance - 2 * distanceBetweenLines * arrowPosition * proximityFactor;
188204
}
189205

190-
let goodSegment = this.findSegment(positions, cumulativeDistances, wantedDistance);
206+
const goodSegment = this.findSegment(positions, cumulativeDistances, wantedDistance);
191207

192208
// We don't have the exact same distance calculation as in the arrow shader, so take some margin:
193209
// we move the label a little bit on the flat side of the arrow so that at least it stays
@@ -206,9 +222,9 @@ export class GeoData {
206222
default:
207223
throw new Error('impossible');
208224
}
209-
let remainingDistance = goodSegment.remainingDistance * multiplier;
225+
const remainingDistance = goodSegment.remainingDistance * multiplier;
210226

211-
let angle = this.getMapAngle(goodSegment.segment[0], goodSegment.segment[1]);
227+
const angle = this.getMapAngle(goodSegment.segment[0], goodSegment.segment[1]);
212228
const neededOffset = this.getLabelOffset(angle, 20, arrowDirection);
213229

214230
const position = {
@@ -252,8 +268,8 @@ export class GeoData {
252268
return position;
253269
}
254270

255-
getLabelOffset(angle, offsetDistance, arrowDirection) {
256-
let radiantAngle = (-angle + 90) / (180 / Math.PI);
271+
getLabelOffset(angle: number, offsetDistance: number, arrowDirection: ArrowDirection): [number, number] {
272+
const radiantAngle = (-angle + 90) / (180 / Math.PI);
257273
let direction = 0;
258274
switch (arrowDirection) {
259275
case ArrowDirection.FROM_SIDE_2_TO_SIDE_1:
@@ -276,11 +292,11 @@ export class GeoData {
276292
}
277293

278294
//returns the angle between point1 and point2 in degrees [0-360)
279-
getMapAngle(point1, point2) {
295+
getMapAngle(point1: LonLat, point2: LonLat) {
280296
// We don't have the exact same angle calculation as in the arrow shader, and this
281297
// seems to give more approaching results
282298
let angle = getRhumbLineBearing(point1, point2);
283-
let angle2 = getGreatCircleBearing(point1, point2);
299+
const angle2 = getGreatCircleBearing(point1, point2);
284300
const coeff = 0.1;
285301
angle = coeff * angle + (1 - coeff) * angle2;
286302
return angle;

0 commit comments

Comments
 (0)