Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions buildings.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function loadBuildings() {

function addBuilding(jsonFeature) {
return new Promise((resolve, reject) => {
var itemPos = tileposFromLatlon(latlonFromJSON(jsonFeature.geometry.coordinates[0][0]));
var itemPos = worldposFromLatlon(latlonFromJSON(jsonFeature.geometry.coordinates[0][0]));
var tags = jsonFeature.properties.tags ? jsonFeature.properties.tags : jsonFeature.properties;
var btype = tags.building;
if (tags.shelter == "yes") { btype = "shelter"; }
Expand Down Expand Up @@ -91,8 +91,8 @@ function addBuilding(jsonFeature) {
for (let way of jsonFeature.geometry.coordinates) {
let wayPoints = [];
for (let point of way) {
let tpos = tileposFromLatlon(latlonFromJSON(point));
let ppos = getRelativePositionFromTilepos(tpos, itemPos);
let tpos = worldposFromLatlon(latlonFromJSON(point));
let ppos = getRelativePositionFromWorldpos(tpos, itemPos);
wayPoints.push({x: ppos.x, y: ppos.z});
}
if (!outerPoints.length) {
Expand All @@ -115,7 +115,7 @@ function addBuilding(jsonFeature) {
}
item.setAttribute("geometry", buildingProperties);
item.setAttribute("material", {color: color});
item.setAttribute("position", getPositionFromTilepos(itemPos));
item.setAttribute("position", itemPos);
item.setAttribute("data-gpspos", jsonFeature.geometry.coordinates[0][0][1] + "/" + jsonFeature.geometry.coordinates[0][0][0]);
items.appendChild(item);
resolve();
Expand Down
31 changes: 23 additions & 8 deletions conversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ function tileposFromLatlon(latlon) {
y: ytilepos - baseTileID.y};
}

function worldposFromLatlon(latlon) {
if (!centerOffset) {
centerOffset = tileposFromLatlon(centerPos);
}
/* Get position x/z numbers from degree-based latitude/longitude values */
var n = Math.pow(2, tileZoom);
var lat_rad = latlon.latitude / 180 * Math.PI;
var xtilepos = n * ((latlon.longitude + 180) / 360);
var ytilepos = n * (1 - (Math.log(Math.tan(lat_rad) + 1 / Math.cos(lat_rad)) / Math.PI)) / 2;

return {
x: baseTileSize * (xtilepos - baseTileID.x - centerOffset.x),
y: 0,
z: baseTileSize * (ytilepos - baseTileID.y - centerOffset.y)
};
}

function tilesizeFromID(tileid) {
/* Get a tile size in meters from x/y tile numbers */
/* tileid is an object with x and y members telling the slippy map tile ID */
Expand Down Expand Up @@ -64,14 +81,12 @@ function getPositionFromTilepos(tilepos, offset) {
z: posresult.y};
}

function getRelativePositionFromTilepos(tilepos, reference) {
posresult = {
x: baseTileSize * (tilepos.x - reference.x),
y: baseTileSize * (tilepos.y - reference.y),
}
return {x: posresult.x,
y: 0,
z: posresult.y};
function getRelativePositionFromWorldpos(worldpos, reference) {
return {
x: worldpos.x - reference.x,
y: 0,
z: worldpos.z - reference.z,
};
}

function getPositionStringFromTilepos(tilepos, offset) {
Expand Down
5 changes: 4 additions & 1 deletion map.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ function getBoundingBoxString() {

function fetchFromOverpass(opQuery) {
return new Promise((resolve, reject) => {
fetch(overpassURL + "?data=" + encodeURIComponent(opQuery))
fetch(overpassURL, {
method: 'POST',
body: opQuery
})
.then((response) => {
if (response.ok) {
return response.text();
Expand Down
6 changes: 2 additions & 4 deletions trees.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ function loadTrees() {

function addTree(jsonFeature) {
return new Promise((resolve, reject) => {
var itemPos = tileposFromLatlon(latlonFromJSON(jsonFeature.geometry.coordinates));
var itemPos = worldposFromLatlon(latlonFromJSON(jsonFeature.geometry.coordinates));
var tags = jsonFeature.properties.tags ? jsonFeature.properties.tags : jsonFeature.properties;
var item = document.createElement("a-entity");
item.setAttribute("class", "tree");
item.setAttribute("data-reltilex", Math.floor(itemPos.x));
item.setAttribute("data-reltiley", Math.floor(itemPos.y));
var trunk = document.createElement("a-entity");
trunk.setAttribute("class", "trunk");
var crown = document.createElement("a-entity");
Expand Down Expand Up @@ -58,7 +56,7 @@ function addTree(jsonFeature) {
crown.setAttribute("material", {color: "#80ff80"});
crown.setAttribute("position", {x: 0, y: trunkHeight, z: 0});
}
item.setAttribute("position", getPositionFromTilepos(itemPos));
item.setAttribute("position", itemPos);
item.setAttribute("data-gpspos", jsonFeature.geometry.coordinates[1] + "/" + jsonFeature.geometry.coordinates[0]);
item.appendChild(trunk);
item.appendChild(crown);
Expand Down