@hastehaul/common 2.13.0 → 2.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,5 +8,12 @@ export declare function formatDatetimeToHumanReadable(datetime: Date, format?: s
8
8
  export declare function generateIdentifier(messageContent: string): string;
9
9
  export declare function getValueFromSettings(settings: Setting[], key: string, envVar?: string, defaultValue?: any): any;
10
10
  export declare function loadAsset(relativePath: string): Promise<string | null>;
11
- export declare function isPointInGeoJSON(latLng: string | any[], fileName: string): Promise<any>;
11
+ export declare function isPointInGeoJSON(latLng: number[], fileName: string): Promise<{
12
+ isInside: boolean;
13
+ properties: any;
14
+ } | {
15
+ isInside: null;
16
+ properties: null;
17
+ }>;
18
+ export declare function clearGeoJsonCache(): void;
12
19
  export {};
@@ -19,10 +19,12 @@ exports.generateIdentifier = generateIdentifier;
19
19
  exports.getValueFromSettings = getValueFromSettings;
20
20
  exports.loadAsset = loadAsset;
21
21
  exports.isPointInGeoJSON = isPointInGeoJSON;
22
+ exports.clearGeoJsonCache = clearGeoJsonCache;
22
23
  const boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
23
24
  const helpers_1 = require("@turf/helpers");
24
25
  const moment_1 = __importDefault(require("moment"));
25
26
  const fs_1 = require("fs");
27
+ const boolean_point_on_line_1 = __importDefault(require("@turf/boolean-point-on-line"));
26
28
  function hasElapsed(expirationDate) {
27
29
  const currentDate = (0, moment_1.default)();
28
30
  const targetDate = (0, moment_1.default)(expirationDate);
@@ -59,29 +61,73 @@ function loadAsset(relativePath) {
59
61
  }
60
62
  });
61
63
  }
64
+ // export async function isPointInGeoJSON(latLng: string | any[], fileName: string) {
65
+ // try {
66
+ // if (!latLng || latLng.length === 0) return { isInside: false, properties: null };
67
+ // const geoJsonData = await loadAsset(fileName);
68
+ // const geojson = JSON.parse(geoJsonData!);
69
+ // const points = point([latLng[1], latLng[0]]);
70
+ // for (const feature of geojson.features) {
71
+ // const geometry = feature.geometry;
72
+ // if (geometry.type === 'Polygon') {
73
+ // if (booleanPointInPolygon(points, polygon(geometry.coordinates))) {
74
+ // return { isInside: true, properties: feature.properties };
75
+ // }
76
+ // } else if (geometry.type === 'MultiPolygon') {
77
+ // if (geometry.coordinates.some((poly: Position[][]) => booleanPointInPolygon(points, polygon(poly)))) {
78
+ // return { isInside: true, properties: feature.properties };
79
+ // }
80
+ // }
81
+ // }
82
+ // return { isInside: false, properties: null };
83
+ // } catch (error) {
84
+ // console.error(`Error checking point in GeoJSON: ${error}`);
85
+ // return { isInside: null, properties: null };
86
+ // }
87
+ // }
88
+ let cachedGeoJsonData = null;
62
89
  function isPointInGeoJSON(latLng, fileName) {
63
90
  return __awaiter(this, void 0, void 0, function* () {
64
91
  try {
65
- if (latLng.length === 0)
66
- return false;
67
- const geoJsonData = yield loadAsset(fileName);
68
- const geojson = JSON.parse(geoJsonData);
69
- const points = (0, helpers_1.point)([latLng[1], latLng[0]]);
70
- const isInside = geojson.features.some((feature) => {
71
- const geometry = feature.geometry;
92
+ if (!latLng || latLng.length === 0)
93
+ return { isInside: false, properties: null };
94
+ // Load and parse GeoJSON data only if not already cached
95
+ if (!cachedGeoJsonData) {
96
+ const geoJsonData = yield loadAsset(fileName);
97
+ cachedGeoJsonData = JSON.parse(geoJsonData);
98
+ }
99
+ const points = (0, helpers_1.point)([latLng[0], latLng[1]]);
100
+ for (const feature of cachedGeoJsonData.features) {
101
+ const { geometry } = feature;
72
102
  if (geometry.type === 'Polygon') {
73
- return (0, boolean_point_in_polygon_1.default)(points, (0, helpers_1.polygon)(geometry.coordinates));
103
+ if ((0, boolean_point_in_polygon_1.default)(points, (0, helpers_1.polygon)(geometry.coordinates))) {
104
+ return { isInside: true, properties: feature.properties };
105
+ }
74
106
  }
75
107
  else if (geometry.type === 'MultiPolygon') {
76
- return geometry.coordinates.some((poly) => (0, boolean_point_in_polygon_1.default)(points, (0, helpers_1.polygon)(poly)));
108
+ if (geometry.coordinates.some((poly) => (0, boolean_point_in_polygon_1.default)(points, (0, helpers_1.polygon)(poly)))) {
109
+ return { isInside: true, properties: feature.properties };
110
+ }
77
111
  }
78
- return false;
79
- });
80
- return isInside;
112
+ else if (geometry.type === 'LineString') {
113
+ if ((0, boolean_point_on_line_1.default)(points, (0, helpers_1.lineString)(geometry.coordinates))) {
114
+ return { isInside: true, properties: feature.properties };
115
+ }
116
+ }
117
+ else if (geometry.type === 'MultiLineString') {
118
+ if (geometry.coordinates.some((line) => (0, boolean_point_on_line_1.default)(points, (0, helpers_1.lineString)(line)))) {
119
+ return { isInside: true, properties: feature.properties };
120
+ }
121
+ }
122
+ }
123
+ return { isInside: false, properties: null };
81
124
  }
82
125
  catch (error) {
83
126
  console.error(`Error checking point in GeoJSON: ${error}`);
84
- return null;
127
+ return { isInside: null, properties: null };
85
128
  }
86
129
  });
87
130
  }
131
+ function clearGeoJsonCache() {
132
+ cachedGeoJsonData = null;
133
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.13.0",
3
+ "version": "2.15.0",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -25,6 +25,11 @@
25
25
  "typescript": "^5.1.6"
26
26
  },
27
27
  "dependencies": {
28
+ "@turf/boolean-point-in-polygon": "^6.5.0",
29
+ "@turf/boolean-point-on-line": "^7.0.0",
30
+ "@turf/helpers": "^6.5.0",
31
+ "@turf/meta": "^6.5.0",
32
+ "@turf/points-within-polygon": "^6.5.0",
28
33
  "@vsky/accesscontrol": "^3.0.14",
29
34
  "bullmq": "^5.8.3",
30
35
  "express": "^4.18.2",
@@ -32,10 +37,6 @@
32
37
  "jsonwebtoken": "^9.0.1",
33
38
  "moment": "^2.29.4",
34
39
  "nats": "^2.17.0",
35
- "socket.io": "^4.7.1",
36
- "@turf/boolean-point-in-polygon": "^6.5.0",
37
- "@turf/helpers": "^6.5.0",
38
- "@turf/meta": "^6.5.0",
39
- "@turf/points-within-polygon": "^6.5.0"
40
+ "socket.io": "^4.7.1"
40
41
  }
41
42
  }