@tmlmobilidade/go-utils-geo 20260828.1636.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/geohash/geohash-decode.d.ts +2 -0
- package/dist/geohash/geohash-decode.js +4 -0
- package/dist/geohash/geohash-encode.d.ts +2 -0
- package/dist/geohash/geohash-encode.js +4 -0
- package/dist/geohash/index.d.ts +2 -0
- package/dist/geohash/index.js +2 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/measurements/distance-between-points.d.ts +21 -0
- package/dist/measurements/distance-between-points.js +35 -0
- package/dist/measurements/index.d.ts +2 -0
- package/dist/measurements/index.js +2 -0
- package/dist/measurements/interpolate.d.ts +22 -0
- package/dist/measurements/interpolate.js +40 -0
- package/dist/polylines/from-line-string-to-polyline.d.ts +7 -0
- package/dist/polylines/from-line-string-to-polyline.js +48 -0
- package/dist/polylines/from-polyline-to-line-string.d.ts +9 -0
- package/dist/polylines/from-polyline-to-line-string.js +19 -0
- package/dist/polylines/index.d.ts +2 -0
- package/dist/polylines/index.js +2 -0
- package/dist/transformers/chunk-line-by-distance.d.ts +13 -0
- package/dist/transformers/chunk-line-by-distance.js +54 -0
- package/dist/transformers/cut-line-at-length.d.ts +10 -0
- package/dist/transformers/cut-line-at-length.js +72 -0
- package/dist/transformers/index.d.ts +2 -0
- package/dist/transformers/index.js +2 -0
- package/package.json +50 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type GeoJsonPointGeometry, type GeoJsonPosition } from '@tmlmobilidade/go-types-geo';
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the distance between two points, in meters.
|
|
4
|
+
* This is a wrapper function around the `getDistanceBetweenPositions` function.
|
|
5
|
+
* @param pointA The first point.
|
|
6
|
+
* @param pointB The second point.
|
|
7
|
+
* @returns The distance between the two points, in meters.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getDistanceBetweenPoints(pointA: GeoJsonPointGeometry, pointB: GeoJsonPointGeometry): number;
|
|
10
|
+
/**
|
|
11
|
+
* Calculates the distance between two coordinate positions, in meters.
|
|
12
|
+
* This function uses the Haversine formula to calculate the distance
|
|
13
|
+
* between two points on the Earth's surface, given their latitude and longitude.
|
|
14
|
+
* The formula accounts for the curvature of the Earth.
|
|
15
|
+
* It is important to note that this function assumes the Earth is a perfect sphere,
|
|
16
|
+
* which is not entirely accurate, but it provides a good approximation for small distances.
|
|
17
|
+
* @param positionA The first position.
|
|
18
|
+
* @param positionB The second position.
|
|
19
|
+
* @returns The distance between the two points, in meters.
|
|
20
|
+
*/
|
|
21
|
+
export declare function getDistanceBetweenPositions(positionA: GeoJsonPosition, positionB: GeoJsonPosition): number;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { METERS_PER_DEGREE } from '@tmlmobilidade/go-types-geo';
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the distance between two points, in meters.
|
|
5
|
+
* This is a wrapper function around the `getDistanceBetweenPositions` function.
|
|
6
|
+
* @param pointA The first point.
|
|
7
|
+
* @param pointB The second point.
|
|
8
|
+
* @returns The distance between the two points, in meters.
|
|
9
|
+
*/
|
|
10
|
+
export function getDistanceBetweenPoints(pointA, pointB) {
|
|
11
|
+
return getDistanceBetweenPositions(pointA.coordinates, pointB.coordinates);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Calculates the distance between two coordinate positions, in meters.
|
|
15
|
+
* This function uses the Haversine formula to calculate the distance
|
|
16
|
+
* between two points on the Earth's surface, given their latitude and longitude.
|
|
17
|
+
* The formula accounts for the curvature of the Earth.
|
|
18
|
+
* It is important to note that this function assumes the Earth is a perfect sphere,
|
|
19
|
+
* which is not entirely accurate, but it provides a good approximation for small distances.
|
|
20
|
+
* @param positionA The first position.
|
|
21
|
+
* @param positionB The second position.
|
|
22
|
+
* @returns The distance between the two points, in meters.
|
|
23
|
+
*/
|
|
24
|
+
export function getDistanceBetweenPositions(positionA, positionB) {
|
|
25
|
+
// Extract coordinates from the points
|
|
26
|
+
const [lngA, latA] = positionA;
|
|
27
|
+
const [lngB, latB] = positionB;
|
|
28
|
+
// Calculate the average latitude
|
|
29
|
+
const averageLatitude = (latA + latB) / 2;
|
|
30
|
+
// Calculate the differences in longitude and latitude
|
|
31
|
+
const dx = (lngB - lngA) * METERS_PER_DEGREE * Math.cos((averageLatitude * Math.PI) / 180);
|
|
32
|
+
const dy = (latB - latA) * METERS_PER_DEGREE;
|
|
33
|
+
// Calculate the distance using the Pythagorean theorem
|
|
34
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
35
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type GeoJsonPointGeometry, type GeoJsonPosition } from '@tmlmobilidade/go-types-geo';
|
|
2
|
+
/**
|
|
3
|
+
* Interpolates between two points at a given ratio (0..1).
|
|
4
|
+
* This is a wrapper function around the `interpolatePosition` function.
|
|
5
|
+
* @param pointA The first point.
|
|
6
|
+
* @param pointB The second point.
|
|
7
|
+
* @param ratio The ratio at which to interpolate (0 = pointA, 1 = pointB).
|
|
8
|
+
* @returns The interpolated point.
|
|
9
|
+
*/
|
|
10
|
+
export declare function interpolatePoints(pointA: GeoJsonPointGeometry, pointB: GeoJsonPointGeometry, ratio: number): GeoJsonPointGeometry;
|
|
11
|
+
/**
|
|
12
|
+
* Linearly interpolates between two positions at a given ratio (0..1).
|
|
13
|
+
* This function is useful for calculating intermediate points
|
|
14
|
+
* along a line segment defined by two positions.
|
|
15
|
+
* @param positionA The first position.
|
|
16
|
+
* @param positionB The second position.
|
|
17
|
+
* @param ratio The ratio at which to interpolate (0 = positionA, 1 = positionB).
|
|
18
|
+
* A ratio of 0.5 would give the midpoint between the two positions.
|
|
19
|
+
* A ratio of 0.25 would give a point closer to positionA.
|
|
20
|
+
* @returns The interpolated position.
|
|
21
|
+
*/
|
|
22
|
+
export declare function interpolatePositions(positionA: GeoJsonPosition, positionB: GeoJsonPosition, ratio: number): GeoJsonPosition;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { GeoJsonPointGeometrySchema, LatitudeSchema, LongitudeSchema } from '@tmlmobilidade/go-types-geo';
|
|
3
|
+
/**
|
|
4
|
+
* Interpolates between two points at a given ratio (0..1).
|
|
5
|
+
* This is a wrapper function around the `interpolatePosition` function.
|
|
6
|
+
* @param pointA The first point.
|
|
7
|
+
* @param pointB The second point.
|
|
8
|
+
* @param ratio The ratio at which to interpolate (0 = pointA, 1 = pointB).
|
|
9
|
+
* @returns The interpolated point.
|
|
10
|
+
*/
|
|
11
|
+
export function interpolatePoints(pointA, pointB, ratio) {
|
|
12
|
+
const result = interpolatePositions(pointA.coordinates, pointB.coordinates, ratio);
|
|
13
|
+
return GeoJsonPointGeometrySchema.parse({
|
|
14
|
+
coordinates: result,
|
|
15
|
+
type: 'Point',
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Linearly interpolates between two positions at a given ratio (0..1).
|
|
20
|
+
* This function is useful for calculating intermediate points
|
|
21
|
+
* along a line segment defined by two positions.
|
|
22
|
+
* @param positionA The first position.
|
|
23
|
+
* @param positionB The second position.
|
|
24
|
+
* @param ratio The ratio at which to interpolate (0 = positionA, 1 = positionB).
|
|
25
|
+
* A ratio of 0.5 would give the midpoint between the two positions.
|
|
26
|
+
* A ratio of 0.25 would give a point closer to positionA.
|
|
27
|
+
* @returns The interpolated position.
|
|
28
|
+
*/
|
|
29
|
+
export function interpolatePositions(positionA, positionB, ratio) {
|
|
30
|
+
// Extract coordinates from the points
|
|
31
|
+
const lng = LongitudeSchema.parse(positionA[0] + (positionB[0] - positionA[0]) * ratio);
|
|
32
|
+
const lat = LatitudeSchema.parse(positionA[1] + (positionB[1] - positionA[1]) * ratio);
|
|
33
|
+
// Preserve elevation if present
|
|
34
|
+
if (positionA.length === 3 && positionB.length === 3) {
|
|
35
|
+
const alt = positionA[2] + (positionB[2] - positionA[2]) * ratio;
|
|
36
|
+
return [lng, lat, alt];
|
|
37
|
+
}
|
|
38
|
+
// Return the interpolated position
|
|
39
|
+
return [lng, lat];
|
|
40
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type EncodedPolyline, type GeoJsonLineStringGeometry } from '@tmlmobilidade/go-types-geo';
|
|
2
|
+
/**
|
|
3
|
+
* Decode a polyline from an encoded string into a GeoJSON LineString.
|
|
4
|
+
* @param value The polyline to encode.
|
|
5
|
+
* @returns The encoded polyline.
|
|
6
|
+
*/
|
|
7
|
+
export declare function fromGeoJsonLineStringToEncodedPolyline(lineString: GeoJsonLineStringGeometry): EncodedPolyline;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { EncodedPolylineSchema, GeoJsonLineStringGeometrySchema } from '@tmlmobilidade/go-types-geo';
|
|
3
|
+
/**
|
|
4
|
+
* Decode a polyline from an encoded string into a GeoJSON LineString.
|
|
5
|
+
* @param value The polyline to encode.
|
|
6
|
+
* @returns The encoded polyline.
|
|
7
|
+
*/
|
|
8
|
+
export function fromGeoJsonLineStringToEncodedPolyline(lineString) {
|
|
9
|
+
// Validate the GeoJSON LineString.
|
|
10
|
+
const validatedLineString = GeoJsonLineStringGeometrySchema.parse(lineString);
|
|
11
|
+
// Initialize the output string.
|
|
12
|
+
let result = '';
|
|
13
|
+
// Initialize the previous latitude and longitude values.
|
|
14
|
+
let previousLat = 0;
|
|
15
|
+
let previousLng = 0;
|
|
16
|
+
// Define a function to encode a value.
|
|
17
|
+
function encodeValue(value) {
|
|
18
|
+
// Initialize the output string.
|
|
19
|
+
let encoded = '';
|
|
20
|
+
// If the value is negative, negate it.
|
|
21
|
+
value = value < 0 ? ~(value << 1) : value << 1;
|
|
22
|
+
// Loop until the value is less than 0x20.
|
|
23
|
+
while (value >= 0x20) {
|
|
24
|
+
// Add the encoded value to the output string.
|
|
25
|
+
encoded += String.fromCharCode((0x20 | (value & 0x1f)) + 63);
|
|
26
|
+
// Shift the value by 5 bits.
|
|
27
|
+
value >>= 5;
|
|
28
|
+
}
|
|
29
|
+
// Add the final encoded value to the output string.
|
|
30
|
+
encoded += String.fromCharCode(value + 63);
|
|
31
|
+
// Return the encoded value.
|
|
32
|
+
return encoded;
|
|
33
|
+
}
|
|
34
|
+
// Loop through the coordinates of the polyline.
|
|
35
|
+
for (const [lng, lat] of validatedLineString.coordinates) {
|
|
36
|
+
// Convert the latitude and longitude values to E5 format.
|
|
37
|
+
const latE6 = Math.round(lat * 1e6);
|
|
38
|
+
const lngE6 = Math.round(lng * 1e6);
|
|
39
|
+
// Encode the latitude and longitude values.
|
|
40
|
+
result += encodeValue(latE6 - previousLat);
|
|
41
|
+
result += encodeValue(lngE6 - previousLng);
|
|
42
|
+
// Update the previous latitude and longitude values.
|
|
43
|
+
previousLat = latE6;
|
|
44
|
+
previousLng = lngE6;
|
|
45
|
+
}
|
|
46
|
+
// Validate and return the encoded polyline.
|
|
47
|
+
return EncodedPolylineSchema.parse(result);
|
|
48
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type EncodedPolyline, type GeoJsonLineStringGeometry } from '@tmlmobilidade/go-types-geo';
|
|
2
|
+
/**
|
|
3
|
+
* Decode a polyline from an encoded string into a GeoJSON LineString.
|
|
4
|
+
* @param encoded The encoded polyline to decode.
|
|
5
|
+
* @param onPoint A callback function that receives the latitude
|
|
6
|
+
* and longitude values for each decoded coordinate.
|
|
7
|
+
* @throws An error if the input is malformed.
|
|
8
|
+
*/
|
|
9
|
+
export declare function fromEncodedPolylineToGeoJsonLineString(value: EncodedPolyline): GeoJsonLineStringGeometry;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { GeoJsonLineStringGeometrySchema, parseEncodedPolyline } from '@tmlmobilidade/go-types-geo';
|
|
3
|
+
/**
|
|
4
|
+
* Decode a polyline from an encoded string into a GeoJSON LineString.
|
|
5
|
+
* @param encoded The encoded polyline to decode.
|
|
6
|
+
* @param onPoint A callback function that receives the latitude
|
|
7
|
+
* and longitude values for each decoded coordinate.
|
|
8
|
+
* @throws An error if the input is malformed.
|
|
9
|
+
*/
|
|
10
|
+
export function fromEncodedPolylineToGeoJsonLineString(value) {
|
|
11
|
+
// Initialize the coordinates array.
|
|
12
|
+
const result = { coordinates: [], type: 'LineString' };
|
|
13
|
+
// Parse the encoded polyline.
|
|
14
|
+
parseEncodedPolyline(value, (decodedLatitude, decodedLongitude) => {
|
|
15
|
+
result.coordinates.push([decodedLongitude, decodedLatitude]);
|
|
16
|
+
});
|
|
17
|
+
// Return the decoded polyline.
|
|
18
|
+
return GeoJsonLineStringGeometrySchema.parse(result);
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type GeoJsonLineStringGeometry } from '@tmlmobilidade/go-types-geo';
|
|
2
|
+
/**
|
|
3
|
+
* Resamples a GeoJSON LineString into equidistant points by walking the full
|
|
4
|
+
* cumulative distance of the polyline. This function accumulates distance from
|
|
5
|
+
* the start of the line to each vertex, and places nodes at exact `segmentLength` intervals,
|
|
6
|
+
* interpolating between vertices. This means that on long "straight" segments, the output will have
|
|
7
|
+
* more points than on curves, and on short "curved" segments, the output will have fewer points,
|
|
8
|
+
* maintaining the same overall length of the line and of each chunked segment.
|
|
9
|
+
* @param inputLineString The LineString to resample.
|
|
10
|
+
* @param segmentLength The target distance between consecutive output points, in meters.
|
|
11
|
+
* @returns A GeoJSON LineString with equidistant coordinates along the original path.
|
|
12
|
+
*/
|
|
13
|
+
export declare function chunkLineStringByDistance(inputLineString: GeoJsonLineStringGeometry, segmentLength: number): GeoJsonLineStringGeometry;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { getDistanceBetweenPositions } from '../measurements/distance-between-points.js';
|
|
3
|
+
import { interpolatePositions } from '../measurements/interpolate.js';
|
|
4
|
+
import { GeoJsonLineStringGeometrySchema } from '@tmlmobilidade/go-types-geo';
|
|
5
|
+
/**
|
|
6
|
+
* Resamples a GeoJSON LineString into equidistant points by walking the full
|
|
7
|
+
* cumulative distance of the polyline. This function accumulates distance from
|
|
8
|
+
* the start of the line to each vertex, and places nodes at exact `segmentLength` intervals,
|
|
9
|
+
* interpolating between vertices. This means that on long "straight" segments, the output will have
|
|
10
|
+
* more points than on curves, and on short "curved" segments, the output will have fewer points,
|
|
11
|
+
* maintaining the same overall length of the line and of each chunked segment.
|
|
12
|
+
* @param inputLineString The LineString to resample.
|
|
13
|
+
* @param segmentLength The target distance between consecutive output points, in meters.
|
|
14
|
+
* @returns A GeoJSON LineString with equidistant coordinates along the original path.
|
|
15
|
+
*/
|
|
16
|
+
export function chunkLineStringByDistance(inputLineString, segmentLength) {
|
|
17
|
+
//
|
|
18
|
+
if (inputLineString.coordinates.length < 2)
|
|
19
|
+
throw new Error('LineString must have at least 2 coordinates.');
|
|
20
|
+
//
|
|
21
|
+
// Pre-compute cumulative distances at each original vertex
|
|
22
|
+
const cumDist = [0];
|
|
23
|
+
for (let i = 0; i < inputLineString.coordinates.length - 1; i++) {
|
|
24
|
+
cumDist.push(cumDist[i] + getDistanceBetweenPositions(inputLineString.coordinates[i], inputLineString.coordinates[i + 1]));
|
|
25
|
+
}
|
|
26
|
+
//
|
|
27
|
+
// If the total length is 0, then return the input line string as is.
|
|
28
|
+
const totalLength = cumDist[cumDist.length - 1];
|
|
29
|
+
if (totalLength === 0)
|
|
30
|
+
return inputLineString;
|
|
31
|
+
const result = inputLineString.coordinates;
|
|
32
|
+
//
|
|
33
|
+
// Walk the polyline placing a node every segmentLength meters
|
|
34
|
+
let segmentIndex = 0;
|
|
35
|
+
const nodeCount = Math.floor(totalLength / segmentLength);
|
|
36
|
+
for (let n = 1; n <= nodeCount; n++) {
|
|
37
|
+
// Calculate the target distance for the current node
|
|
38
|
+
const targetDist = n * segmentLength;
|
|
39
|
+
// Advance segmentIndex to the segment that contains targetDist
|
|
40
|
+
while (segmentIndex < inputLineString.coordinates.length - 2 && cumDist[segmentIndex + 1] < targetDist) {
|
|
41
|
+
segmentIndex++;
|
|
42
|
+
}
|
|
43
|
+
// Calculate the start and end of the segment
|
|
44
|
+
const segStart = cumDist[segmentIndex];
|
|
45
|
+
const segEnd = cumDist[segmentIndex + 1];
|
|
46
|
+
const ratio = segEnd > segStart ? (targetDist - segStart) / (segEnd - segStart) : 0;
|
|
47
|
+
// Interpolate the position of the node between the start and end of the segment
|
|
48
|
+
result.push(interpolatePositions(inputLineString.coordinates[segmentIndex], inputLineString.coordinates[segmentIndex + 1], ratio));
|
|
49
|
+
}
|
|
50
|
+
return GeoJsonLineStringGeometrySchema.parse({
|
|
51
|
+
coordinates: result,
|
|
52
|
+
type: 'LineString',
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type GeoJsonLineStringGeometry } from '@tmlmobilidade/go-types-geo';
|
|
2
|
+
/**
|
|
3
|
+
* Cuts a LineString at a specified length.
|
|
4
|
+
* If the line is shorter than the specified length, it returns the entire line.
|
|
5
|
+
* @param inputLineString The LineString to cut.
|
|
6
|
+
* @param length The length at which to cut the line, in meters, from the start of the line.
|
|
7
|
+
* @param direction The direction in which to cut the line. If 'forward', it cuts from the start of the line.
|
|
8
|
+
* @returns A new LineString that is cut at the specified length.
|
|
9
|
+
*/
|
|
10
|
+
export declare function cutLineStringAtLength(inputLineString: GeoJsonLineStringGeometry, length: number, direction?: 'forward' | 'reversed'): GeoJsonLineStringGeometry | null;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { getDistanceBetweenPositions } from '../measurements/distance-between-points.js';
|
|
3
|
+
import { interpolatePositions } from '../measurements/interpolate.js';
|
|
4
|
+
import { GeoJsonLineStringGeometrySchema } from '@tmlmobilidade/go-types-geo';
|
|
5
|
+
/**
|
|
6
|
+
* Cuts a LineString at a specified length.
|
|
7
|
+
* If the line is shorter than the specified length, it returns the entire line.
|
|
8
|
+
* @param inputLineString The LineString to cut.
|
|
9
|
+
* @param length The length at which to cut the line, in meters, from the start of the line.
|
|
10
|
+
* @param direction The direction in which to cut the line. If 'forward', it cuts from the start of the line.
|
|
11
|
+
* @returns A new LineString that is cut at the specified length.
|
|
12
|
+
*/
|
|
13
|
+
export function cutLineStringAtLength(inputLineString, length, direction = 'forward') {
|
|
14
|
+
//
|
|
15
|
+
//
|
|
16
|
+
// Return the input line string if it is empty,
|
|
17
|
+
// or null if the length is less than or equal to 0.
|
|
18
|
+
if (inputLineString.coordinates.length < 2)
|
|
19
|
+
throw new Error('LineString must have at least 2 coordinates.');
|
|
20
|
+
if (length <= 0)
|
|
21
|
+
throw new Error('Length must be greater than 0.');
|
|
22
|
+
//
|
|
23
|
+
// Reverse the line if the direction is 'reversed'
|
|
24
|
+
if (direction === 'reversed') {
|
|
25
|
+
inputLineString.coordinates = inputLineString.coordinates.slice().reverse();
|
|
26
|
+
}
|
|
27
|
+
//
|
|
28
|
+
// Hold the cumulative distance between points
|
|
29
|
+
// and the coordinates of the new line
|
|
30
|
+
let cumulativeLength = 0;
|
|
31
|
+
const newLineStringCoordinates = [];
|
|
32
|
+
//
|
|
33
|
+
// Loop through the coordinates of the line
|
|
34
|
+
for (let i = 0; i < inputLineString.coordinates.length - 1; i++) {
|
|
35
|
+
// Get the coordinates of the current and the next point
|
|
36
|
+
const coordA = inputLineString.coordinates[i];
|
|
37
|
+
const coordB = inputLineString.coordinates[i + 1];
|
|
38
|
+
// Calculate the distance between the two points
|
|
39
|
+
const segmentLength = getDistanceBetweenPositions(coordA, coordB);
|
|
40
|
+
// If the current segment length plus the cumulative length
|
|
41
|
+
// extends the desired length of the resulting string
|
|
42
|
+
// then the line should be cut at the interpolation point
|
|
43
|
+
if (cumulativeLength + segmentLength >= length) {
|
|
44
|
+
const remainingLength = length - cumulativeLength;
|
|
45
|
+
const relativePositionOnSegment = remainingLength / segmentLength;
|
|
46
|
+
const interpolated = interpolatePositions(coordA, coordB, relativePositionOnSegment);
|
|
47
|
+
newLineStringCoordinates.push(coordA, interpolated);
|
|
48
|
+
return GeoJsonLineStringGeometrySchema.parse({
|
|
49
|
+
coordinates: newLineStringCoordinates,
|
|
50
|
+
type: 'LineString',
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
// Add the length of the current segment to the
|
|
54
|
+
// cumulative length of the line up until this point
|
|
55
|
+
cumulativeLength += segmentLength;
|
|
56
|
+
// If the cumulative length is already greater than the desired length
|
|
57
|
+
// then the line should be cut at this point. Break the loop now.
|
|
58
|
+
if (cumulativeLength > length)
|
|
59
|
+
break;
|
|
60
|
+
// If the cumulative length is not yet up to the desired length
|
|
61
|
+
// then the current point should be added to the new line
|
|
62
|
+
newLineStringCoordinates.push(coordA);
|
|
63
|
+
}
|
|
64
|
+
//
|
|
65
|
+
// If the entire line is shorter than the target length,
|
|
66
|
+
// then return the full line string as is.
|
|
67
|
+
newLineStringCoordinates.push(inputLineString.coordinates[inputLineString.coordinates.length - 1]);
|
|
68
|
+
return GeoJsonLineStringGeometrySchema.parse({
|
|
69
|
+
coordinates: newLineStringCoordinates,
|
|
70
|
+
type: 'LineString',
|
|
71
|
+
});
|
|
72
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tmlmobilidade/go-utils-geo",
|
|
3
|
+
"version": "20260828.1636.54",
|
|
4
|
+
"author": {
|
|
5
|
+
"email": "iso@tmlmobilidade.pt",
|
|
6
|
+
"name": "TML-ISO"
|
|
7
|
+
},
|
|
8
|
+
"license": "AGPL-3.0-or-later",
|
|
9
|
+
"homepage": "https://go.tmlmobilidade.pt",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/tmlmobilidade/go/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/tmlmobilidade/go.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"public transit",
|
|
19
|
+
"tml",
|
|
20
|
+
"transportes metropolitanos de lisboa",
|
|
21
|
+
"go"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc && resolve-tspaths",
|
|
34
|
+
"lint": "eslint ./src/ && tsc --noEmit",
|
|
35
|
+
"lint:fix": "eslint ./src/ --fix",
|
|
36
|
+
"watch": "tsc-watch --onSuccess 'resolve-tspaths'"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@tmlmobilidade/go-types-geo": "*",
|
|
40
|
+
"ngeohash": "0.6.4"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@tmlmobilidade/go-utils-tsconfig": "*",
|
|
44
|
+
"@types/ngeohash": "0.6.8",
|
|
45
|
+
"@types/node": "26.1.2",
|
|
46
|
+
"resolve-tspaths": "0.8.23",
|
|
47
|
+
"tsc-watch": "7.2.1",
|
|
48
|
+
"typescript": "6.0.3"
|
|
49
|
+
}
|
|
50
|
+
}
|