mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
fix: #1114 - saveGeoJSON_Routes
This commit is contained in:
parent
33fbfc2e48
commit
bf41ad1b70
3 changed files with 40 additions and 52 deletions
10
index.html
10
index.html
|
|
@ -5890,10 +5890,10 @@
|
||||||
|
|
||||||
<div style="margin: 1em 0 0.3em; font-weight: bold">Export to GeoJSON</div>
|
<div style="margin: 1em 0 0.3em; font-weight: bold">Export to GeoJSON</div>
|
||||||
<div>
|
<div>
|
||||||
<button onclick="saveGeoJSON_Cells()" data-tip="Download cells data in GeoJSON format">cells</button>
|
<button onclick="saveGeoJsonCells()" data-tip="Download cells data in GeoJSON format">cells</button>
|
||||||
<button onclick="saveGeoJSON_Routes()" data-tip="Download routes data in GeoJSON format">routes</button>
|
<button onclick="saveGeoJsonRoutes()" data-tip="Download routes data in GeoJSON format">routes</button>
|
||||||
<button onclick="saveGeoJSON_Rivers()" data-tip="Download rivers data in GeoJSON format">rivers</button>
|
<button onclick="saveGeoJsonRivers()" data-tip="Download rivers data in GeoJSON format">rivers</button>
|
||||||
<button onclick="saveGeoJSON_Markers()" data-tip="Download markers data in GeoJSON format">markers</button>
|
<button onclick="saveGeoJsonMarkers()" data-tip="Download markers data in GeoJSON format">markers</button>
|
||||||
</div>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
GeoJSON format is used in GIS tools such as QGIS. Check out
|
GeoJSON format is used in GIS tools such as QGIS. Check out
|
||||||
|
|
@ -8063,6 +8063,6 @@
|
||||||
<script defer src="modules/io/save.js?v=1.99.00"></script>
|
<script defer src="modules/io/save.js?v=1.99.00"></script>
|
||||||
<script defer src="modules/io/load.js?v=1.99.07"></script>
|
<script defer src="modules/io/load.js?v=1.99.07"></script>
|
||||||
<script defer src="modules/io/cloud.js?v=1.99.00"></script>
|
<script defer src="modules/io/cloud.js?v=1.99.00"></script>
|
||||||
<script defer src="modules/io/export.js?v=1.99.00"></script>
|
<script defer src="modules/io/export.js?v=1.99.13"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -440,14 +440,24 @@ function inlineStyle(clone) {
|
||||||
emptyG.remove();
|
emptyG.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveGeoJSON_Cells() {
|
function saveGeoJsonCells() {
|
||||||
|
const {cells, vertices} = pack;
|
||||||
const json = {type: "FeatureCollection", features: []};
|
const json = {type: "FeatureCollection", features: []};
|
||||||
const cells = pack.cells;
|
|
||||||
const getPopulation = i => {
|
const getPopulation = i => {
|
||||||
const [r, u] = getCellPopulation(i);
|
const [r, u] = getCellPopulation(i);
|
||||||
return rn(r + u);
|
return rn(r + u);
|
||||||
};
|
};
|
||||||
const getHeight = i => parseInt(getFriendlyHeight([cells.p[i][0], cells.p[i][1]]));
|
|
||||||
|
const getHeight = i => parseInt(getFriendlyHeight([...cells.p[i]]));
|
||||||
|
|
||||||
|
function getCellCoordinates(cellVertices) {
|
||||||
|
const coordinates = cellVertices.map(vertex => {
|
||||||
|
const [x, y] = vertices.p[vertex];
|
||||||
|
return getCoordinates(x, y, 4);
|
||||||
|
});
|
||||||
|
return [...coordinates, coordinates[0]];
|
||||||
|
}
|
||||||
|
|
||||||
cells.i.forEach(i => {
|
cells.i.forEach(i => {
|
||||||
const coordinates = getCellCoordinates(cells.v[i]);
|
const coordinates = getCellCoordinates(cells.v[i]);
|
||||||
|
|
@ -470,20 +480,14 @@ function saveGeoJSON_Cells() {
|
||||||
downloadFile(JSON.stringify(json), fileName, "application/json");
|
downloadFile(JSON.stringify(json), fileName, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveGeoJSON_Routes() {
|
function saveGeoJsonRoutes() {
|
||||||
const {cells, burgs} = pack;
|
const features = pack.routes.map(({i, points, group, name = null}) => {
|
||||||
let points = cells.p.map(([x, y], cellId) => {
|
const coordinates = points.map(([x, y]) => getCoordinates(x, y, 4));
|
||||||
const burgId = cells.burg[cellId];
|
const id = `route${i}`;
|
||||||
if (burgId) return [burgs[burgId].x, burgs[burgId].y];
|
|
||||||
return [x, y];
|
|
||||||
});
|
|
||||||
|
|
||||||
const features = pack.routes.map(route => {
|
|
||||||
const coordinates = route.points || getRoutePoints(route, points);
|
|
||||||
return {
|
return {
|
||||||
type: "Feature",
|
type: "Feature",
|
||||||
geometry: {type: "LineString", coordinates},
|
geometry: {type: "LineString", coordinates},
|
||||||
properties: {id: route.id, group: route.group}
|
properties: {id, group, name}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
const json = {type: "FeatureCollection", features};
|
const json = {type: "FeatureCollection", features};
|
||||||
|
|
@ -492,24 +496,27 @@ function saveGeoJSON_Routes() {
|
||||||
downloadFile(JSON.stringify(json), fileName, "application/json");
|
downloadFile(JSON.stringify(json), fileName, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveGeoJSON_Rivers() {
|
function saveGeoJsonRivers() {
|
||||||
const json = {type: "FeatureCollection", features: []};
|
const features = pack.rivers.map(
|
||||||
|
({i, cells, points, source, mouth, parent, basin, widthFactor, sourceWidth, discharge, name, type}) => {
|
||||||
rivers.selectAll("path").each(function () {
|
if (!cells || cells.length < 2) return;
|
||||||
const river = pack.rivers.find(r => r.i === +this.id.slice(5));
|
const meanderedPoints = Rivers.addMeandering(cells, points);
|
||||||
if (!river) return;
|
const coordinates = meanderedPoints.map(([x, y]) => getCoordinates(x, y, 4));
|
||||||
|
const id = `river${i}`;
|
||||||
const coordinates = getRiverPoints(this);
|
return {
|
||||||
const properties = {...river, id: this.id};
|
type: "Feature",
|
||||||
const feature = {type: "Feature", geometry: {type: "LineString", coordinates}, properties};
|
geometry: {type: "LineString", coordinates},
|
||||||
json.features.push(feature);
|
properties: {id, source, mouth, parent, basin, widthFactor, sourceWidth, discharge, name, type}
|
||||||
});
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const json = {type: "FeatureCollection", features};
|
||||||
|
|
||||||
const fileName = getFileName("Rivers") + ".geojson";
|
const fileName = getFileName("Rivers") + ".geojson";
|
||||||
downloadFile(JSON.stringify(json), fileName, "application/json");
|
downloadFile(JSON.stringify(json), fileName, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveGeoJSON_Markers() {
|
function saveGeoJsonMarkers() {
|
||||||
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 = getCoordinates(x, y, 4);
|
const coordinates = getCoordinates(x, y, 4);
|
||||||
|
|
@ -524,22 +531,3 @@ function saveGeoJSON_Markers() {
|
||||||
const fileName = getFileName("Markers") + ".geojson";
|
const fileName = getFileName("Markers") + ".geojson";
|
||||||
downloadFile(JSON.stringify(json), fileName, "application/json");
|
downloadFile(JSON.stringify(json), fileName, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCellCoordinates(vertices) {
|
|
||||||
const p = pack.vertices.p;
|
|
||||||
const coordinates = vertices.map(n => getCoordinates(p[n][0], p[n][1], 2));
|
|
||||||
return [coordinates.concat([coordinates[0]])];
|
|
||||||
}
|
|
||||||
|
|
||||||
function getRiverPoints(node) {
|
|
||||||
let points = [];
|
|
||||||
const l = node.getTotalLength() / 2; // half-length
|
|
||||||
const increment = 0.25; // defines density of points
|
|
||||||
for (let i = l, c = i; i >= 0; i -= increment, c += increment) {
|
|
||||||
const p1 = node.getPointAtLength(i);
|
|
||||||
const p2 = node.getPointAtLength(c);
|
|
||||||
const [x, y] = getCoordinates((p1.x + p2.x) / 2, (p1.y + p2.y) / 2, 4);
|
|
||||||
points.push([x, y]);
|
|
||||||
}
|
|
||||||
return points;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
// version and caching control
|
// version and caching control
|
||||||
const version = "1.99.12"; // generator version, update each time
|
const version = "1.99.13"; // generator version, update each time
|
||||||
|
|
||||||
{
|
{
|
||||||
document.title += " v" + version;
|
document.title += " v" + version;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue