Skip to content

Commit 6409732

Browse files
authored
Updated the library to use ES6 style exports/imports
1 parent 285925b commit 6409732

File tree

5 files changed

+88
-98
lines changed

5 files changed

+88
-98
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
import './katapult-functions.js';
2-
import './katapult-geometry.js';
1+
import * as Functions from './katapult-functions.js';
2+
import * as Geometry from './katapult-geometry.js';
3+
4+
export const KatapultFunctions = { Geometry };
5+
export const KatapultGeometry = { Functions };

katapult-functions.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
//
2-
// Add your helper functions to the global k variable below.
3-
// And keep the functions in alphabetical order!
4-
//
5-
6-
// Global function access variable
7-
self.k = self.k || {};
8-
91
// Takes an array and calls a function to process each item in the array
102
// in batches of a given size. This can be helpful for when you need to process
113
// large amounts of data and must call asynchronous functions while processing
124
// @param {Array} list - full array of items to process
135
// @param {Number} batchSize - the max size of each batch of items
146
// @param {Function} processFunction - function to be called to process each item in the batch (must have parameter for item and for callback)
157
// @param {Function} callback - function that will be called when all items in the list array have been processed
16-
self.k.processListInBatch = function(list, batchSize, processFunction, callback) {
8+
export function ProcessListInBatch(list, batchSize, processFunction, callback) {
179

1810
// Call callback if the list is null or empty
1911
if (!list || list.length == 0) callback();
@@ -28,7 +20,7 @@ self.k.processListInBatch = function(list, batchSize, processFunction, callback)
2820
batch.forEach(function(item) {
2921
processFunction(item, function() {
3022
// Call the next batch when this is done
31-
if (--remaining == 0) k.processListInBatch(list, batchSize, processFunction, callback);
23+
if (--remaining == 0) ProcessListInBatch(list, batchSize, processFunction, callback);
3224
});
3325
});
3426
}

katapult-geometry.js

Lines changed: 72 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
11
import proj4 from 'proj4';
22

3-
/*global proj4*/
4-
window.addEventListener('load', function() {
5-
proj4.defs('EPSG:4326', "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ");
6-
});
7-
8-
self.k = self.k || {};
3+
proj4.defs('EPSG:4326', "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ");
94

105
//calculates distance between two lat/long points in meters
11-
self.k.calcDistance = function(lat1, lon1, lat2, lon2) {
12-
var p1 = self.k.latLongToXY(Number(lat1), Number(lon1));
13-
var p2 = self.k.latLongToXY(Number(lat2), Number(lon2), p1.srid);
14-
return self.k.calcDistanceXY(p1, p2);
6+
export function CalcDistance(lat1, lon1, lat2, lon2) {
7+
var p1 = LatLongToXY(Number(lat1), Number(lon1));
8+
var p2 = LatLongToXY(Number(lat2), Number(lon2), p1.srid);
9+
return CalcDistanceXY(p1, p2);
1510
};
16-
self.k.calcDistanceXY = function(p1, p2) {
11+
export function CalcDistanceXY(p1, p2) {
1712
return Math.sqrt(Math.pow(Number(p1.x) - Number(p2.x), 2) + Math.pow(Number(p1.y) - Number(p2.y), 2))
1813
};
19-
self.k.calcMidPoint = function(lat1, lon1, lat2, lon2) {
20-
var p1 = self.k.latLongToXY(Number(lat1), Number(lon1));
21-
var p2 = self.k.latLongToXY(Number(lat2), Number(lon2), p1.srid);
22-
var midpoint = self.k.calcMidPointXY(p1, p2);
14+
export function CalcMidPoint(lat1, lon1, lat2, lon2) {
15+
var p1 = LatLongToXY(Number(lat1), Number(lon1));
16+
var p2 = LatLongToXY(Number(lat2), Number(lon2), p1.srid);
17+
var midpoint = CalcMidPointXY(p1, p2);
2318
midpoint.srid = p1.srid;
24-
return self.k.xyToLatLong(midpoint);
19+
return XyToLatLong(midpoint);
2520
};
26-
self.k.calcMidPointXY = function(p1, p2) {
21+
export function CalcMidPointXY(p1, p2) {
2722
return {
2823
x: (Number(p1.x) + Number(p2.x)) / 2,
2924
y: (Number(p1.y) + Number(p2.y)) / 2
3025
}
3126
}
32-
self.k.calcBearing = function(lat1, lon1, lat2, lon2) {
33-
var p1 = self.k.latLongToXY(Number(lat1), Number(lon1));
34-
var p2 = self.k.latLongToXY(Number(lat2), Number(lon2), p1.srid);
35-
return self.k.calcBearingXY(p1, p2);
27+
export function CalcBearing(lat1, lon1, lat2, lon2) {
28+
var p1 = LatLongToXY(Number(lat1), Number(lon1));
29+
var p2 = LatLongToXY(Number(lat2), Number(lon2), p1.srid);
30+
return CalcBearingXY(p1, p2);
3631
};
37-
self.k.calcBearingXY = function(p1, p2) {
32+
export function CalcBearingXY(p1, p2) {
3833
var dy = Number(p2.y) - Number(p1.y);
3934
var dx = Number(p2.x) - Number(p1.x);
4035
var theta = Math.atan2(dy, dx); // range (-PI, PI]
@@ -43,27 +38,27 @@ self.k.calcBearingXY = function(p1, p2) {
4338
if (theta < 0) theta = 360 + theta; // range [0, 360)-
4439
return theta;
4540
};
46-
//Depricated, Use self.k.calcBearing
47-
self.k.calcBearingLL = function(lat1, lon1, lat2, lon2) {
48-
return self.k.calcBearing(Number(lat1), Number(lon1), Number(lat2), Number(lon2));
41+
//Depricated, Use CalcBearing
42+
export function CalcBearingLL(lat1, lon1, lat2, lon2) {
43+
return CalcBearing(Number(lat1), Number(lon1), Number(lat2), Number(lon2));
4944
};
5045
// calcuates shortest distance between point and line in meters
51-
self.k.calcDistanceToLine = function(pointLat, pointLon, linePoint1_Lat, linePoint1_Lon, linePoint2_Lat, linePoint2_Lon) {
52-
var p = self.k.latLongToXY(Number(pointLat), Number(pointLon));
53-
var p1 = self.k.latLongToXY(Number(linePoint1_Lat), Number(linePoint1_Lon), p.srid);
54-
var p2 = self.k.latLongToXY(Number(linePoint2_Lat), Number(linePoint2_Lon), p.srid);
55-
var pointOnLine = self.k.snapToLineXY(p, p1, p2);
46+
export function CalcDistanceToLine(pointLat, pointLon, linePoint1_Lat, linePoint1_Lon, linePoint2_Lat, linePoint2_Lon) {
47+
var p = LatLongToXY(Number(pointLat), Number(pointLon));
48+
var p1 = LatLongToXY(Number(linePoint1_Lat), Number(linePoint1_Lon), p.srid);
49+
var p2 = LatLongToXY(Number(linePoint2_Lat), Number(linePoint2_Lon), p.srid);
50+
var pointOnLine = SnapToLineXY(p, p1, p2);
5651
return Math.sqrt(pointOnLine.dx * pointOnLine.dx + pointOnLine.dy * pointOnLine.dy);
5752
};
58-
self.k.snapToLine = function(pointLat, pointLon, linePoint1_Lat, linePoint1_Lon, linePoint2_Lat, linePoint2_Lon, allowOverflow) {
59-
var p = self.k.latLongToXY(Number(pointLat), Number(pointLon));
60-
var p1 = self.k.latLongToXY(Number(linePoint1_Lat), Number(linePoint1_Lon), p.srid);
61-
var p2 = self.k.latLongToXY(Number(linePoint2_Lat), Number(linePoint2_Lon), p.srid);
62-
var pointOnLine = self.k.snapToLineXY(p, p1, p2, allowOverflow);
53+
export function SnapToLine(pointLat, pointLon, linePoint1_Lat, linePoint1_Lon, linePoint2_Lat, linePoint2_Lon, allowOverflow) {
54+
var p = LatLongToXY(Number(pointLat), Number(pointLon));
55+
var p1 = LatLongToXY(Number(linePoint1_Lat), Number(linePoint1_Lon), p.srid);
56+
var p2 = LatLongToXY(Number(linePoint2_Lat), Number(linePoint2_Lon), p.srid);
57+
var pointOnLine = SnapToLineXY(p, p1, p2, allowOverflow);
6358
pointOnLine.srid = p.srid;
64-
return self.k.xyToLatLong(pointOnLine);
59+
return XyToLatLong(pointOnLine);
6560
};
66-
self.k.snapToLineXY = function(p, p1, p2, allowOverflow) {
61+
export function SnapToLineXY(p, p1, p2, allowOverflow) {
6762
var x = Number(p1.x),
6863
y = Number(p1.y),
6964
dx = Number(p2.x) - x,
@@ -95,19 +90,19 @@ self.k.snapToLineXY = function(p, p1, p2, allowOverflow) {
9590
};
9691
};
9792
// Snap Point lat and long, Center Point lat and long, optional distance in meters, and optional bearing [0 - 360] with 0 at North
98-
self.k.snapPosition = function(pointLat, pointLon, centerLat, centerLon, distance_meters, bearing) {
99-
var center = self.k.latLongToXY(centerLat, centerLon);
100-
var point = self.k.latLongToXY(pointLat, pointLon, center.srid);
101-
var snapPoint = self.k.snapPositionXY(point, center, distance_meters, bearing);
93+
export function SnapPosition(pointLat, pointLon, centerLat, centerLon, distance_meters, bearing) {
94+
var center = LatLongToXY(centerLat, centerLon);
95+
var point = LatLongToXY(pointLat, pointLon, center.srid);
96+
var snapPoint = SnapPositionXY(point, center, distance_meters, bearing);
10297
snapPoint.srid = center.srid;
103-
return self.k.xyToLatLong(snapPoint);
98+
return XyToLatLong(snapPoint);
10499
};
105-
self.k.snapPositionXY = function(point, center, distance_meters, bearing) {
100+
export function SnapPositionXY(point, center, distance_meters, bearing) {
106101
if (distance_meters == null) {
107-
distance_meters = self.k.calcDistanceXY(center, point);
102+
distance_meters = CalcDistanceXY(center, point);
108103
}
109104
if (bearing == null) {
110-
bearing = self.k.calcBearingXY(center, point);
105+
bearing = CalcBearingXY(center, point);
111106
}
112107
var mathBearing = (90 - bearing) * Math.PI / 180;
113108
var utmX = distance_meters * Math.cos(mathBearing) + center.x;
@@ -117,14 +112,14 @@ self.k.snapPositionXY = function(point, center, distance_meters, bearing) {
117112
y:utmY
118113
};
119114
};
120-
self.k.snapToCircle = function(pointLat, pointLon, centerLat, centerLon, radius_meters) {
121-
var point = self.k.latLongToXY(Number(pointLat), Number(pointLon));
122-
var center = self.k.latLongToXY(Number(centerLat), Number(centerLon), point.srid);
123-
var pointOnCircle = self.k.snapToCircleXY(point, center, radius_meters);
115+
export function SnapToCircle(pointLat, pointLon, centerLat, centerLon, radius_meters) {
116+
var point = LatLongToXY(Number(pointLat), Number(pointLon));
117+
var center = LatLongToXY(Number(centerLat), Number(centerLon), point.srid);
118+
var pointOnCircle = SnapToCircleXY(point, center, radius_meters);
124119
pointOnCircle.srid = point.srid;
125-
return self.k.xyToLatLong(pointOnCircle);
120+
return XyToLatLong(pointOnCircle);
126121
};
127-
self.k.snapToCircleXY = function(point, center, radius) {
122+
export function SnapToCircleXY(point, center, radius) {
128123
// where P is the point, C is the center, and R is the radius:
129124
// V = (P - C); Answer = C + V / |V| * R;
130125
// where |V| is length of V.
@@ -138,7 +133,7 @@ self.k.snapToCircleXY = function(point, center, radius) {
138133
y:aY
139134
};
140135
};
141-
self.k.calcProj4 = function(lat, long) {
136+
export function CalcProj4(lat, long) {
142137
var zone = 1 + Math.floor((Number(long) + 180) / 6);
143138
var srid = 32600 + zone;
144139
var hemisphere = "";
@@ -157,12 +152,12 @@ self.k.calcProj4 = function(lat, long) {
157152
proj4: proj4String
158153
};
159154
};
160-
//self.k.latLongToXY(lat, long[, srid]) - pass lat and long and it will be projected into correct UTM zone
155+
//LatLongToXY(lat, long[, srid]) - pass lat and long and it will be projected into correct UTM zone
161156
// optionally pass srid to project into that coordinate system
162157
// returns {x:x, y:y, srid:srid}
163158
// TODO make it work with srid's that weren't previously calculated by the library
164-
self.k.latLongToXY = function(lat, long, srid) {
165-
srid = srid || self.k.calcProj4(lat, long).srid;
159+
export function LatLongToXY(lat, long, srid) {
160+
srid = srid || CalcProj4(lat, long).srid;
166161
var coords = {
167162
x: long,
168163
y: lat
@@ -174,11 +169,11 @@ self.k.latLongToXY = function(lat, long, srid) {
174169
transformed.srid = srid;
175170
return transformed;
176171
};
177-
//self.k.xyToLatLong(xOrPoint[, y, srid]) - pass a point {x:x, y:y, srid:srid} or x, y, srid
172+
//XyToLatLong(xOrPoint[, y, srid]) - pass a point {x:x, y:y, srid:srid} or x, y, srid
178173
// srid should be the epsg srid of the projected points
179174
// returns {lat:lat, long:long}
180175
// TODO make it work with srid's that weren't previously calculated by the library
181-
self.k.xyToLatLong = function(xOrPoint, y, srid) {
176+
export function XyToLatLong(xOrPoint, y, srid) {
182177
var point = xOrPoint;
183178
if (typeof xOrPoint != 'object') {
184179
point = {
@@ -194,7 +189,7 @@ self.k.xyToLatLong = function(xOrPoint, y, srid) {
194189
};
195190
};
196191
// round a number to a given decimal place
197-
self.k.round = function(value, exp) {
192+
export function Round(value, exp) {
198193
if (typeof exp === 'undefined' || +exp === 0)
199194
return Math.round(value);
200195

@@ -213,33 +208,33 @@ self.k.round = function(value, exp) {
213208
return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
214209
};
215210
// Given points p1 and p2, calculate the vector starting at p1 and ending at p2
216-
self.k.calculateVector = function (p1, p2) {
211+
export function CalculateVector (p1, p2) {
217212
return {
218213
x: p2.x - p1.x,
219214
y: p2.y - p1.y
220215
};
221216
};
222217
// Given two vectors v1 and v2, add vector v2 to v1
223-
self.k.addVectors = function(v1, v2) {
218+
export function AddVectors(v1, v2) {
224219
return {
225220
x: v1.x + v2.x,
226221
y: v1.y + v2.y
227222
};
228223
};
229224
// Given two vectors v1 and v2, subtract vector v2 from v1
230-
self.k.subtractVectors = function (v1, v2) {
225+
export function SubtractVectors (v1, v2) {
231226
return {
232227
x: v1.x - v2.x,
233228
y: v1.y - v2.y
234229
};
235230
};
236231
// Calculate the dot product of vectors v1 and v2
237-
self.k.dotProduct = function (v1, v2) {
232+
export function DotProduct (v1, v2) {
238233
return v1.x * v2.x + v1.y * v2.y;
239234
};
240235
// Calculate the unit vector of a vector
241-
self.k.calculateUnitVector = function (vector) {
242-
var norm = self.k.vectorNorm(vector);
236+
export function CalculateUnitVector (vector) {
237+
var norm = VectorNorm(vector);
243238
if (norm != 0) {
244239
return {
245240
x: vector.x / norm,
@@ -248,14 +243,14 @@ self.k.calculateUnitVector = function (vector) {
248243
}
249244
};
250245
// Calculate the norm of a vector
251-
self.k.vectorNorm = function (vector) {
246+
export function VectorNorm (vector) {
252247
return Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2));
253248
};
254249
// Project vector v1 onto v2
255-
self.k.projectVectors = function (v1, v2) {// projects v1 onto v2
256-
var norm = self.k.vectorNorm(v2);
250+
export function ProjectVectors (v1, v2) {// projects v1 onto v2
251+
var norm = VectorNorm(v2);
257252
if (norm != 0) {
258-
var r = self.k.dotProduct(v1, v2) / Math.pow(norm, 2);
253+
var r = DotProduct(v1, v2) / Math.pow(norm, 2);
259254
if (r > 0.99999999999 && r < 1.00000000001) {
260255
// fix rounding error
261256
return {
@@ -284,28 +279,28 @@ self.k.projectVectors = function (v1, v2) {// projects v1 onto v2
284279
}
285280
};
286281
// Scale a vector by c
287-
self.k.scaleVector = function (c, vector) {
282+
export function ScaleVector (c, vector) {
288283
return {
289284
x: c * vector.x,
290285
y: c * vector.y
291286
};
292287
};
293288
// Starting a point p, add move by a vector
294-
self.k.calculatePointFromVector = function (p, vector) {
289+
export function CalculatePointFromVector (p, vector) {
295290
return {
296291
x: p.x + vector.x,
297292
y: p.y + vector.y,
298293
srid: p.srid
299294
};
300295
};
301-
self.k.interpolate = function(lat1, lon1, lat2, lon2, percent) {
302-
var p1 = self.k.latLongToXY(Number(lat1), Number(lon1));
303-
var p2 = self.k.latLongToXY(Number(lat2), Number(lon2), p1.srid);
304-
var position = self.k.interpolateXY(p1, p2, percent);
296+
export function Interpolate(lat1, lon1, lat2, lon2, percent) {
297+
var p1 = LatLongToXY(Number(lat1), Number(lon1));
298+
var p2 = LatLongToXY(Number(lat2), Number(lon2), p1.srid);
299+
var position = InterpolateXY(p1, p2, percent);
305300
position.srid = p1.srid;
306-
return self.k.xyToLatLong(position);
301+
return XyToLatLong(position);
307302
}
308-
self.k.interpolateXY = function(p1, p2, percent) {
303+
export function InterpolateXY(p1, p2, percent) {
309304
return {
310305
x: Number(p1.x) + (Number(p2.x) - Number(p1.x)) * percent,
311306
y: Number(p1.y) + (Number(p2.y) - Number(p1.y)) * percent,
@@ -315,7 +310,7 @@ self.k.interpolateXY = function(p1, p2, percent) {
315310
// line intercept math by Paul Bourke http://paulbourke.net/geometry/pointlineplane/
316311
// Determine the intersection point of two line segments
317312
// Return FALSE if the lines don't intersect
318-
self.k.intersect = function(p1, p2, p3, p4) {
313+
export function Intersect(p1, p2, p3, p4) {
319314

320315
// Check if none of the lines are of length 0
321316
if ((p1.x === p2.x && p1.y === p2.y) || (p3.x === p4.x && p3.y === p4.y)) {

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "katapult-toolbox",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "Javascript GIS Mapping Calculations and misc useful helper functions",
55
"main": "index.js",
66
"scripts": {
@@ -17,6 +17,6 @@
1717
},
1818
"homepage": "https://github.com/KatapultDevelopment/katapult-toolbox#readme",
1919
"dependencies": {
20-
"proj4": "^2.6.1"
20+
"proj4": "^2.8.0"
2121
}
2222
}

0 commit comments

Comments
 (0)