@hastehaul/common 2.14.0 → 2.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,11 +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<{
11
+ export declare function isPointInGeoJSON(latLng: number[], fileName: string): Promise<{
12
12
  isInside: boolean;
13
13
  properties: any;
14
14
  } | {
15
15
  isInside: null;
16
16
  properties: null;
17
17
  }>;
18
+ export declare function clearGeoJsonCache(): void;
18
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,37 +61,45 @@ function loadAsset(relativePath) {
59
61
  }
60
62
  });
61
63
  }
62
- // export async function isPointInGeoJSONw(latLng: string | any[], fileName: string) {
64
+ // export async function isPointInGeoJSON(latLng: string | any[], fileName: string) {
63
65
  // try {
64
- // if(latLng.length === 0) return false
66
+ // if (!latLng || latLng.length === 0) return { isInside: false, properties: null };
65
67
  // const geoJsonData = await loadAsset(fileName);
66
68
  // const geojson = JSON.parse(geoJsonData!);
67
69
  // const points = point([latLng[1], latLng[0]]);
68
- // const isInside = geojson.features.some((feature: { geometry: any; }) => {
70
+ // for (const feature of geojson.features) {
69
71
  // const geometry = feature.geometry;
70
72
  // if (geometry.type === 'Polygon') {
71
- // return booleanPointInPolygon(points, polygon(geometry.coordinates));
73
+ // if (booleanPointInPolygon(points, polygon(geometry.coordinates))) {
74
+ // return { isInside: true, properties: feature.properties };
75
+ // }
72
76
  // } else if (geometry.type === 'MultiPolygon') {
73
- // return geometry.coordinates.some((poly: Position[][]) => booleanPointInPolygon(points, polygon(poly)));
77
+ // if (geometry.coordinates.some((poly: Position[][]) => booleanPointInPolygon(points, polygon(poly)))) {
78
+ // return { isInside: true, properties: feature.properties };
79
+ // }
74
80
  // }
75
- // return false;
76
- // });
77
- // return isInside;
81
+ // }
82
+ // return { isInside: false, properties: null };
78
83
  // } catch (error) {
79
84
  // console.error(`Error checking point in GeoJSON: ${error}`);
80
- // return null;
85
+ // return { isInside: null, properties: null };
81
86
  // }
82
87
  // }
88
+ let cachedGeoJsonData = null;
83
89
  function isPointInGeoJSON(latLng, fileName) {
84
90
  return __awaiter(this, void 0, void 0, function* () {
85
91
  try {
86
92
  if (!latLng || latLng.length === 0)
87
93
  return { isInside: false, properties: null };
88
- const geoJsonData = yield loadAsset(fileName);
89
- const geojson = JSON.parse(geoJsonData);
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
+ // !NB points take the form longitude, latitude
90
100
  const points = (0, helpers_1.point)([latLng[1], latLng[0]]);
91
- for (const feature of geojson.features) {
92
- const geometry = feature.geometry;
101
+ for (const feature of cachedGeoJsonData.features) {
102
+ const { geometry } = feature;
93
103
  if (geometry.type === 'Polygon') {
94
104
  if ((0, boolean_point_in_polygon_1.default)(points, (0, helpers_1.polygon)(geometry.coordinates))) {
95
105
  return { isInside: true, properties: feature.properties };
@@ -100,6 +110,16 @@ function isPointInGeoJSON(latLng, fileName) {
100
110
  return { isInside: true, properties: feature.properties };
101
111
  }
102
112
  }
113
+ else if (geometry.type === 'LineString') {
114
+ if ((0, boolean_point_on_line_1.default)(points, (0, helpers_1.lineString)(geometry.coordinates))) {
115
+ return { isInside: true, properties: feature.properties };
116
+ }
117
+ }
118
+ else if (geometry.type === 'MultiLineString') {
119
+ if (geometry.coordinates.some((line) => (0, boolean_point_on_line_1.default)(points, (0, helpers_1.lineString)(line)))) {
120
+ return { isInside: true, properties: feature.properties };
121
+ }
122
+ }
103
123
  }
104
124
  return { isInside: false, properties: null };
105
125
  }
@@ -109,3 +129,6 @@ function isPointInGeoJSON(latLng, fileName) {
109
129
  }
110
130
  });
111
131
  }
132
+ function clearGeoJsonCache() {
133
+ cachedGeoJsonData = null;
134
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.14.0",
3
+ "version": "2.16.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
  }