BangleApps/apps/golfview/custom.html

123 lines
4.3 KiB
HTML
Raw Normal View History

<html>
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
2022-04-03 05:52:47 +00:00
<script src="https://cdn.jsdelivr.net/gh/mourner/simplify-js@1.2.4/simplify.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
2022-04-03 19:58:49 +00:00
<p id="status">No course</p>
2022-04-05 18:18:21 +00:00
<div>
<button id="upload" class="btn btn-primary" disabled="true">Upload to Device</button>
<button id="download" class="btn btn-primary" disabled="true">Download Course</button>
</div>
<script src="../../core/lib/customize.js"></script>
<script src="./maptools.js"></script>
<script>
const url = "https://overpass-api.de/api/interpreter";
2022-04-07 19:56:53 +00:00
let query = `[bbox:38.4829,-121.8694,38.7110,-121.6215][out:json][timeout:5];way["name"="Davis Golf Course"];map_to_area ->.golfcourse;way["golf"="hole"](area.golfcourse)->.holes;(relation["golf"="fairway"](area.golfcourse);way["golf"~"^(green|tee|water_hazard|bunker|fairway)"](area.golfcourse);)->.features;.holes out geom;.features out geom;`;
let course_input = null;
function processFeatures(course_verbose) {
let course_processed = {
holes: {}
};
for (let i = 0; i < course_verbose.length; i++) {
const element = course_verbose[i];
2022-04-07 19:56:53 +00:00
if (element.tags.golf === "hole") {
// if we find a high-level hole feature
2022-04-07 19:56:53 +00:00
// todo check if hole exists
let current_hole = parseInt(element.tags.ref); //subsequent way features should be applied to the current hole
let tees = []
Object.keys(element.tags).forEach((key) => {
if (key.includes("dist")) {
tees.push(Math.round(element.tags[key]));
}
2022-04-07 19:56:53 +00:00
})
var hole = {
hole_number: current_hole,
handicap: parseInt(element.tags.handicap),
par: parseInt(element.tags.par),
nodesXY: preprocessCoords(element.geometry, element.geometry[0]),
tees: tees.sort(),
way: element.geometry,
features: [],
angle: 0,
}
2022-04-07 19:56:53 +00:00
hole.angle = angle(hole.nodesXY[0], hole.nodesXY[hole.nodesXY.length - 1])
course_processed.holes[current_hole.toString()] = hole;
}
2022-04-07 20:20:56 +00:00
else {
2022-04-07 19:56:53 +00:00
if (!("ref" in element.tags)) continue;
2022-04-07 20:20:56 +00:00
if (element.type == "relation") {
for (member of element.members) {
if (member.role === "outer") break; // only use the outer because it is overwritten anyway
}
Object.assign(element, { "geometry": member.geometry });
}
// if we find a feature add it to the corresponding hole
2022-04-07 19:56:53 +00:00
let active_holes = element.tags.ref.split(";"); // a feature can be on more than one hole
for (feature_hole of active_holes) {
let new_feature = {
2022-04-07 19:56:53 +00:00
nodes: preprocessCoords(element.geometry, course_processed.holes[feature_hole].way[0]),
type: element.tags.golf,
id: element.id,
}
2022-04-07 19:56:53 +00:00
course_processed.holes[feature_hole].features.push(new_feature);
}
}
}
return course_processed;
}
2022-04-07 19:56:53 +00:00
function preprocessCoords(coord_array, origin) {
let many_points = arraytoXY(coord_array, origin);
let less_points = simplify(many_points, 2, true); // from simplify-js
2022-04-07 19:56:53 +00:00
// convert to int to save some memory
less_points = less_points.map(function (pt) {
return { x: Math.round(pt.x), y: Math.round(pt.y) }
});
return less_points;
}
2022-04-05 20:31:24 +00:00
var courses = [];
2022-04-05 19:59:09 +00:00
var course_name = "Davis";
2022-04-05 18:18:21 +00:00
$("#upload").click(function () {
sendCustomizedApp({
2022-04-05 20:31:24 +00:00
storage: courses
2022-04-05 18:18:21 +00:00
});
});
$("#download").click(function () {
2022-04-07 20:29:54 +00:00
downloadObjectAsJSON(courses[0].content, "golfcourse-" + course_name);
2022-04-05 18:18:21 +00:00
});
// download info from the course
$.post(url, query, function (result) {
course_input = result;
2022-04-04 03:58:44 +00:00
console.log(course_input);
out = processFeatures(course_input.elements);
console.log(out);
2022-04-05 20:31:24 +00:00
courses.push({
2022-04-05 19:59:09 +00:00
name: "golfcourse-" + course_name + ".json",
content: JSON.parse(JSON.stringify(out)) // deep copy
2022-04-05 20:31:24 +00:00
});
2022-04-03 19:58:49 +00:00
$('#status').text("Course retrieved!");
$('#upload').attr("disabled", false);
2022-04-05 18:18:21 +00:00
$('#download').attr("disabled", false);
})
</script>
</body>
</html>