@tmlmobilidade/utils 20250628.1632.4 → 20250628.1640.52

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.
@@ -0,0 +1,17 @@
1
+ import { type Feature, type Point, type Polygon } from 'geojson';
2
+ /**
3
+ * Create a geofence around a given point with a given radius in meters (default is 50 meters).
4
+ * @param point A GeoJSON Point representation of the point to create the geofence around.
5
+ * @param radius The distance in meters to calculate the geofence radius. Default is 50 meters.
6
+ * @param steps The number of steps to use for the buffer. Default is 16.
7
+ * @returns The GeoJSON Feature of a Polygon.
8
+ */
9
+ export declare function getGeofenceOnPoint(point: Feature<Point>, radius?: number, steps?: number): Feature<Polygon>;
10
+ /**
11
+ * Create a geofence around a given position with a given radius in meters (default is 50 meters).
12
+ * @param position A tuple representing the coordinates of the point to create the geofence around.
13
+ * @param radius The distance in meters to calculate the geofence radius. Default is 50 meters.
14
+ * @param steps The number of steps to use for the buffer. Default is 16.
15
+ * @returns The GeoJSON Feature of a Polygon.
16
+ */
17
+ export declare function getGeofenceOnPosition(position: [number, number], radius?: number, steps?: number): Feature<Polygon>;
@@ -0,0 +1,46 @@
1
+ /* * */
2
+ import { EARTH_RADIUS } from './constants.js';
3
+ import { toFeatureFromObject, toPointFromPositions } from './conversions.js';
4
+ import { polygon } from '@turf/helpers';
5
+ /**
6
+ * Create a geofence around a given point with a given radius in meters (default is 50 meters).
7
+ * @param point A GeoJSON Point representation of the point to create the geofence around.
8
+ * @param radius The distance in meters to calculate the geofence radius. Default is 50 meters.
9
+ * @param steps The number of steps to use for the buffer. Default is 16.
10
+ * @returns The GeoJSON Feature of a Polygon.
11
+ */
12
+ export function getGeofenceOnPoint(point, radius = 50, steps = 16) {
13
+ // Extract the center coordinates from the point
14
+ const [centerLon, centerLat] = point.geometry.coordinates;
15
+ // Set the angle size based on the number of steps
16
+ const angleStep = (2 * Math.PI) / steps;
17
+ // Set an empty array to hold the coordinates of the polygon vertices
18
+ const coords = [];
19
+ // Calculate the coordinates of the polygon vertices
20
+ for (let i = 0; i < steps; i++) {
21
+ const angle = i * angleStep;
22
+ const dx = radius * Math.cos(angle);
23
+ const dy = radius * Math.sin(angle);
24
+ const newLat = centerLat + (dy / EARTH_RADIUS) * (180 / Math.PI);
25
+ const newLng = centerLon + (dx / (EARTH_RADIUS * Math.cos((centerLat * Math.PI) / 180))) * (180 / Math.PI);
26
+ coords.push([newLng, newLat]);
27
+ }
28
+ // Close the polygon by adding the first coordinate to the end
29
+ coords.push(coords[0]);
30
+ // Return the polygon feature
31
+ return polygon([coords]);
32
+ }
33
+ /**
34
+ * Create a geofence around a given position with a given radius in meters (default is 50 meters).
35
+ * @param position A tuple representing the coordinates of the point to create the geofence around.
36
+ * @param radius The distance in meters to calculate the geofence radius. Default is 50 meters.
37
+ * @param steps The number of steps to use for the buffer. Default is 16.
38
+ * @returns The GeoJSON Feature of a Polygon.
39
+ */
40
+ export function getGeofenceOnPosition(position, radius = 50, steps = 16) {
41
+ // Create a point feature from the coordinates
42
+ const newPoint = toPointFromPositions(position);
43
+ const newFeature = toFeatureFromObject(newPoint);
44
+ // Call the getGeofenceOnPoint function to create the geofence
45
+ return getGeofenceOnPoint(newFeature, radius, steps);
46
+ }
@@ -2,4 +2,5 @@ export * from './chunk-line.js';
2
2
  export * from './constants.js';
3
3
  export * from './conversions.js';
4
4
  export * from './cut-line-at-length.js';
5
+ export * from './get-geofence-on-point.js';
5
6
  export * from './measurements.js';
@@ -2,4 +2,5 @@ export * from './chunk-line.js';
2
2
  export * from './constants.js';
3
3
  export * from './conversions.js';
4
4
  export * from './cut-line-at-length.js';
5
+ export * from './get-geofence-on-point.js';
5
6
  export * from './measurements.js';
@@ -0,0 +1,9 @@
1
+ import { type Feature, type Point, type Polygon, type Position } from 'geojson';
2
+ /**
3
+ * Check if a point is inside a polygon using the ray-casting algorithm.
4
+ * @param point A GeoJSON Point representation of the point to check.
5
+ * @param geofence A GeoJSON Polygon representation of the geofence.
6
+ * @returns A boolean indicating if the point is inside the polygon.
7
+ * @see https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
8
+ */
9
+ export declare function isPointInPolygon(point: Feature<Point> | Position, polygon: Feature<Polygon>): boolean;
@@ -0,0 +1,37 @@
1
+ /* * */
2
+ /**
3
+ * Check if a point is inside a polygon using the ray-casting algorithm.
4
+ * @param point A GeoJSON Point representation of the point to check.
5
+ * @param geofence A GeoJSON Polygon representation of the geofence.
6
+ * @returns A boolean indicating if the point is inside the polygon.
7
+ * @see https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
8
+ */
9
+ export function isPointInPolygon(point, polygon) {
10
+ //
11
+ const pt = Array.isArray(point) ? point : point.geometry.coordinates;
12
+ const [x, y] = pt;
13
+ const ring = polygon.geometry.coordinates[0]; // Outer ring only
14
+ let inside = false;
15
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
16
+ const xi = ring[i][0], yi = ring[i][1];
17
+ const xj = ring[j][0], yj = ring[j][1];
18
+ const intersectsVertically = (yi > y) !== (yj > y);
19
+ const edgeSlope = (xj - xi) / (yj - yi + 1e-10); // slope of the edge
20
+ const xIntersect = edgeSlope * (y - yi) + xi;
21
+ const isToRight = x < xIntersect;
22
+ const intersects = intersectsVertically && isToRight;
23
+ if (intersects)
24
+ inside = !inside;
25
+ }
26
+ return inside;
27
+ }
28
+ // /**
29
+ // * Check if a point is inside a geofence polygon.
30
+ // * @param point A GeoJSON Point representation of the point to check.
31
+ // * @param geofence A GeoJSON Polygon representation of the geofence.
32
+ // * @returns A boolean indicating if the point is inside the geofence polygon.
33
+ // */
34
+ // export function isInsideGeofence(point: Position, geofence: Feature<Polygon>): boolean {
35
+ // // Check if the point is inside the polygon
36
+ // return isPointInPolygon(point, geofence);
37
+ // }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/utils",
3
- "version": "20250628.1632.4",
3
+ "version": "20250628.1640.52",
4
4
  "author": "João de Vasconcelos & Jusi Monteiro",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "homepage": "https://github.com/tmlmobilidade/services#readme",