Skip to content

Commit 2130d35

Browse files
authored
add more functions
1 parent 075d91a commit 2130d35

File tree

1 file changed

+112
-4
lines changed

1 file changed

+112
-4
lines changed

katapult-geometry.html

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<script>
33
/*global proj4*/
44
window.addEventListener('load', function() {
5-
proj4.defs('4326', "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ");
5+
proj4.defs('EPSG:4326', "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ");
66
});
77
var k = k || {};
88

@@ -93,6 +93,29 @@
9393
dy: dy
9494
};
9595
};
96+
// Snap Point lat and long, Center Point lat and long, optional distance in meters, and optional bearing [0 - 360] with 0 at North
97+
k.snapPosition = function(pointLat, pointLon, centerLat, centerLon, distance_meters, bearing) {
98+
var center = k.latLongToXY(centerLat, centerLon);
99+
var point = k.latLongToXY(pointLat, pointLon, center.srid);
100+
var snapPoint = k.snapPositionXY(point, center, distance_meters, bearing);
101+
snapPoint.srid = center.srid;
102+
return k.xyToLatLong(snapPoint);
103+
};
104+
k.snapPositionXY = function(point, center, distance_meters, bearing) {
105+
if (distance_meters == null) {
106+
distance_meters = k.calcDistanceXY(center, point);
107+
}
108+
if (bearing == null) {
109+
bearing = k.calcBearingXY(center, point);
110+
}
111+
var mathBearing = (90 - bearing) * Math.PI / 180;
112+
var utmX = distance_meters * Math.cos(mathBearing) + center.x;
113+
var utmY = distance_meters * Math.sin(mathBearing) + center.y;
114+
return {
115+
x:utmX,
116+
y:utmY
117+
};
118+
};
96119
k.snapToCircle = function(pointLat, pointLon, centerLat, centerLon, radius_meters) {
97120
var point = k.latLongToXY(Number(pointLat), Number(pointLon));
98121
var center = k.latLongToXY(Number(centerLat), Number(centerLon), point.srid);
@@ -144,7 +167,7 @@
144167
y: lat
145168
};
146169
// run proj4 transform - proj4(fromProjection[, toProjection, coordinates])
147-
var transformed = proj4(proj4('4326'), proj4(srid), coords);
170+
var transformed = proj4(proj4('EPSG:4326'), proj4(srid), coords);
148171
transformed.x = transformed.x;
149172
transformed.y = transformed.y;
150173
transformed.srid = srid;
@@ -163,8 +186,7 @@
163186
srid: srid
164187
};
165188
}
166-
console.log('xyToLatLong', point);
167-
var transformed = proj4(proj4(point.srid), proj4('4326'), point);
189+
var transformed = proj4(proj4(point.srid), proj4('EPSG:4326'), point);
168190
return {
169191
lat: transformed.y,
170192
long: transformed.x
@@ -189,4 +211,90 @@
189211
value = value.toString().split('e');
190212
return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
191213
};
214+
// Given points p1 and p2, calculate the vector starting at p1 and ending at p2
215+
k.calculateVector = function (p1, p2) {
216+
return {
217+
x: p2.x - p1.x,
218+
y: p2.y - p1.y
219+
};
220+
};
221+
// Given two vectors v1 and v2, add vector v2 to v1
222+
k.addVectors = function(v1, v2) {
223+
return {
224+
x: v1.x + v2.x,
225+
y: v1.y + v2.y
226+
};
227+
};
228+
// Given two vectors v1 and v2, subtract vector v2 from v1
229+
k.subtractVectors = function (v1, v2) {
230+
return {
231+
x: v1.x - v2.x,
232+
y: v1.y - v2.y
233+
};
234+
};
235+
// Calculate the dot product of vectors v1 and v2
236+
k.dotProduct = function (v1, v2) {
237+
return v1.x * v2.x + v1.y * v2.y;
238+
};
239+
// Calculate the unit vector of a vector
240+
k.calculateUnitVector = function (vector) {
241+
var norm = k.vectorNorm(vector);
242+
if (norm != 0) {
243+
return {
244+
x: vector.x / norm,
245+
y: vector.y / norm
246+
};
247+
}
248+
};
249+
// Calculate the norm of a vector
250+
k.vectorNorm = function (vector) {
251+
return Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2));
252+
};
253+
// Project vector v1 onto v2
254+
k.projectVectors = function (v1, v2) {// projects v1 onto v2
255+
var norm = k.vectorNorm(v2);
256+
if (norm != 0) {
257+
var r = k.dotProduct(v1, v2) / Math.pow(norm, 2);
258+
if (r > 0.99999999999 && r < 1.00000000001) {
259+
// fix rounding error
260+
return {
261+
x: v2.x,
262+
y: v2.y
263+
};
264+
}
265+
else if (r > -1e-11 && r < 1e-11) {
266+
// fix rounding error
267+
return {
268+
x: 0,
269+
y: 0
270+
};
271+
}
272+
else {
273+
return {
274+
x: r * v2.x,
275+
y: r * v2.y
276+
};
277+
}
278+
} else {
279+
return {
280+
x: v1.x,
281+
y: v1.y
282+
};
283+
}
284+
};
285+
// Scale a vector by c
286+
k.scaleVector = function (c, vector) {
287+
return {
288+
x: c * vector.x,
289+
y: c * vector.y
290+
};
291+
};
292+
// Starting a point p, add move by a vector
293+
k.calculatePointFromVector = function (p, vector) {
294+
return {
295+
x: p.x + vector.x,
296+
y: p.y + vector.y,
297+
srid: p.srid
298+
};
299+
};
192300
</script>

0 commit comments

Comments
 (0)