refactor getCoordinates functions

This commit is contained in:
Azgaar 2021-11-10 20:57:28 +03:00
parent 165d8e48be
commit 762ec2c841
4 changed files with 19 additions and 13 deletions

View file

@ -450,7 +450,7 @@ function saveGeoJSON_Rivers() {
function saveGeoJSON_Markers() { function saveGeoJSON_Markers() {
const features = pack.markers.map(marker => { const features = pack.markers.map(marker => {
const {i, type, icon, x, y, size, fill, stroke} = marker; const {i, type, icon, x, y, size, fill, stroke} = marker;
const coordinates = getQGIScoordinates(x, y); const coordinates = getCoordinates(x, y, 4);
const id = `marker${i}`; const id = `marker${i}`;
const note = notes.find(note => note.id === id); const note = notes.find(note => note.id === id);
const properties = {id, type, icon, ...note, size, fill, stroke}; const properties = {id, type, icon, ...note, size, fill, stroke};
@ -465,7 +465,7 @@ function saveGeoJSON_Markers() {
function getCellCoordinates(vertices) { function getCellCoordinates(vertices) {
const p = pack.vertices.p; const p = pack.vertices.p;
const coordinates = vertices.map(n => getQGIScoordinates(p[n][0], p[n][1])); const coordinates = vertices.map(n => getCoordinates(p[n][0], p[n][1], 2));
return [coordinates.concat([coordinates[0]])]; return [coordinates.concat([coordinates[0]])];
} }
@ -475,7 +475,7 @@ function getRoutePoints(node) {
const increment = l / Math.ceil(l / 2); const increment = l / Math.ceil(l / 2);
for (let i = 0; i <= l; i += increment) { for (let i = 0; i <= l; i += increment) {
const p = node.getPointAtLength(i); const p = node.getPointAtLength(i);
points.push(getQGIScoordinates(p.x, p.y)); points.push(getCoordinates(p.x, p.y, 4));
} }
return points; return points;
} }
@ -487,7 +487,7 @@ function getRiverPoints(node) {
for (let i = l, c = i; i >= 0; i -= increment, c += increment) { for (let i = l, c = i; i >= 0; i -= increment, c += increment) {
const p1 = node.getPointAtLength(i); const p1 = node.getPointAtLength(i);
const p2 = node.getPointAtLength(c); const p2 = node.getPointAtLength(c);
const [x, y] = getQGIScoordinates((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); const [x, y] = getCoordinates((p1.x + p2.x) / 2, (p1.y + p2.y) / 2, 4);
points.push([x, y]); points.push([x, y]);
} }
return points; return points;

View file

@ -456,7 +456,7 @@ function overviewBurgs() {
} }
function downloadBurgsData() { function downloadBurgsData() {
let data = `Id,Burg,Province,Province Full Name,State,State Full Name,Culture,Religion,Population,Longitude,Latitude,Elevation (${heightUnit.value}),Capital,Port,Citadel,Walls,Plaza,Temple,Shanty Town`; // headers let data = `Id,Burg,Province,Province Full Name,State,State Full Name,Culture,Religion,Population,Latitude,Longitude,Elevation (${heightUnit.value}),Capital,Port,Citadel,Walls,Plaza,Temple,Shanty Town`; // headers
if (options.showMFCGMap) data += `,City Generator Link`; if (options.showMFCGMap) data += `,City Generator Link`;
data += "\n"; data += "\n";
@ -475,8 +475,8 @@ function overviewBurgs() {
data += rn(b.population * populationRate * urbanization) + ","; data += rn(b.population * populationRate * urbanization) + ",";
// add geography data // add geography data
data += mapCoordinates.lonW + (b.x / graphWidth) * mapCoordinates.lonT + ","; data += getLatitude(b.y, 2) + ",";
data += mapCoordinates.latN - (b.y / graphHeight) * mapCoordinates.latT + ","; // this is inverted in QGIS otherwise data += getLongitude(b.x, 2) + ",";
data += parseInt(getHeight(pack.cells.h[b.cell])) + ","; data += parseInt(getHeight(pack.cells.h[b.cell])) + ",";
// add status data // add status data

View file

@ -228,8 +228,8 @@ function updateCellInfo(point, i, g) {
const x = (infoX.innerHTML = rn(point[0])); const x = (infoX.innerHTML = rn(point[0]));
const y = (infoY.innerHTML = rn(point[1])); const y = (infoY.innerHTML = rn(point[1]));
const f = cells.f[i]; const f = cells.f[i];
infoLat.innerHTML = toDMS(mapCoordinates.latN - (y / graphHeight) * mapCoordinates.latT, "lat"); infoLat.innerHTML = toDMS(getLatitude(y, 4), "lat");
infoLon.innerHTML = toDMS(mapCoordinates.lonW + (x / graphWidth) * mapCoordinates.lonT, "lon"); infoLon.innerHTML = toDMS(getLongitude(x, 4), "lon");
infoCell.innerHTML = i; infoCell.innerHTML = i;
const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value; const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value;

View file

@ -137,10 +137,16 @@ function generateDate(from = 100, to = 1000) {
return new Date(rand(from, to), rand(12), rand(31)).toLocaleDateString("en", {year: "numeric", month: "long", day: "numeric"}); return new Date(rand(from, to), rand(12), rand(31)).toLocaleDateString("en", {year: "numeric", month: "long", day: "numeric"});
} }
function getQGIScoordinates(x, y) { function getLongitude(x, decimals = 2) {
const cx = mapCoordinates.lonW + (x / graphWidth) * mapCoordinates.lonT; return rn(mapCoordinates.lonW + (x / graphWidth) * mapCoordinates.lonT, decimals);
const cy = mapCoordinates.latN - (y / graphHeight) * mapCoordinates.latT; // this is inverted in QGIS otherwise }
return [cx, cy];
function getLatitude(y, decimals = 2) {
return rn(mapCoordinates.latN - (y / graphHeight) * mapCoordinates.latT, decimals);
}
function getCoordinates(x, y, decimals = 2) {
return [getLongitude(x, decimals), getLatitude(y, decimals)];
} }
// prompt replacer (prompt does not work in Electron) // prompt replacer (prompt does not work in Electron)