mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-21 19:41:23 +01:00
adding river data (id, width, increment)
added PHP script to add random details to cells.
This commit is contained in:
parent
607d573b83
commit
629b6d7c7c
2 changed files with 104 additions and 7 deletions
81
QGIS/add_random_points.php
Normal file
81
QGIS/add_random_points.php
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$max_deviation = 0.2;
|
||||||
|
$max_abs = 0.02;
|
||||||
|
$min_distance = 0.1;
|
||||||
|
|
||||||
|
$iterations = 4;
|
||||||
|
|
||||||
|
if ($argc < 2 ) {
|
||||||
|
exit( "Usage: add_random_points.php <filename.json>\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: This script created a few cases of self-intersection that must be fixed manually
|
||||||
|
// in QGIS: Vector -> Geometry Tools -> Check Validity
|
||||||
|
|
||||||
|
$cells = json_decode(file_get_contents($argv[1]), true);
|
||||||
|
|
||||||
|
|
||||||
|
for ($i=0; $i<$iterations; $i++) {
|
||||||
|
$lookup = array();
|
||||||
|
|
||||||
|
foreach ($cells['features'] as &$cell) {
|
||||||
|
$points = $cell['geometry']['coordinates'][0];
|
||||||
|
|
||||||
|
$prev = null;
|
||||||
|
$newpoints = array();
|
||||||
|
|
||||||
|
foreach ($points as $point) {
|
||||||
|
if ($prev) {
|
||||||
|
$distance = sqrt(pow($prev[0] - $point[0], 2) + pow($prev[1] - $point[1], 2));
|
||||||
|
if ($distance == 0) continue;
|
||||||
|
|
||||||
|
if ($distance > $min_distance) {
|
||||||
|
$id_a = $prev[0]."-".$prev[1];
|
||||||
|
$id_b = $point[0]."-".$point[1];
|
||||||
|
|
||||||
|
if (isset($lookup[$id_a."--".$id_b])) {
|
||||||
|
$newpoints[] = $lookup[$id_a."--".$id_b];
|
||||||
|
} elseif (isset($lookup[$id_b."--".$id_a])) {
|
||||||
|
$newpoints[] = $lookup[$id_b."--".$id_a];
|
||||||
|
} else {
|
||||||
|
$x = ($prev[0]+$point[0])/2.0;
|
||||||
|
$y = ($prev[1]+$point[1])/2.0;
|
||||||
|
|
||||||
|
$r = mt_rand() / mt_getrandmax(); // 0-1
|
||||||
|
$r = ($r+1) / 2; // 0.5 - 1.0
|
||||||
|
|
||||||
|
// if we define dx=x2-x1 and dy=y2-y1, then the normals are (-dy, dx) and (dy, -dx).
|
||||||
|
$dx = $point[0] - $x;
|
||||||
|
$dy = $point[1] - $y;
|
||||||
|
|
||||||
|
if (mt_rand() / mt_getrandmax() < 0.5) {
|
||||||
|
$x_off = -$dy;
|
||||||
|
$y_off = $dx;
|
||||||
|
} else {
|
||||||
|
$x_off = $dy;
|
||||||
|
$y_off = -$dx;
|
||||||
|
}
|
||||||
|
|
||||||
|
$x_off *= $r * $max_deviation;
|
||||||
|
$x_off = max(min($x_off, $max_abs), $max_abs*-1);
|
||||||
|
|
||||||
|
$y_off *= $r * $max_deviation;
|
||||||
|
$y_off = max(min($y_off, $max_abs), $max_abs*-1);
|
||||||
|
|
||||||
|
$p = array($x + $x_off, $y + $y_off);
|
||||||
|
$lookup[$id_a."--".$id_b] = $p;
|
||||||
|
$newpoints[] = $p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$newpoints[] = $point;
|
||||||
|
$prev = $point;
|
||||||
|
}
|
||||||
|
$cell['geometry']['coordinates'][0] = $newpoints;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode($cells);
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -3,9 +3,9 @@
|
||||||
|
|
||||||
// download map data as GeoJSON
|
// download map data as GeoJSON
|
||||||
function saveGeoJSON() {
|
function saveGeoJSON() {
|
||||||
saveGeoJSON_Cells();
|
//saveGeoJSON_Cells();
|
||||||
//saveGeoJSON_Roads();
|
saveGeoJSON_Roads();
|
||||||
saveGeoJSON_Rivers();
|
//saveGeoJSON_Rivers();
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveGeoJSON_Roads() {
|
function saveGeoJSON_Roads() {
|
||||||
|
|
@ -14,6 +14,8 @@ function saveGeoJSON_Roads() {
|
||||||
trails = routes.select("#trails");
|
trails = routes.select("#trails");
|
||||||
searoutes = routes.select("#searoutes");
|
searoutes = routes.select("#searoutes");
|
||||||
|
|
||||||
|
console.log(rivers._groups[0][0].childNodes);
|
||||||
|
|
||||||
console.log(typeof(roads));
|
console.log(typeof(roads));
|
||||||
console.log(roads);
|
console.log(roads);
|
||||||
|
|
||||||
|
|
@ -22,18 +24,32 @@ function saveGeoJSON_Roads() {
|
||||||
console.log(routes._groups[0][0]);
|
console.log(routes._groups[0][0]);
|
||||||
console.log(routes._groups[0][0].childNodes);
|
console.log(routes._groups[0][0].childNodes);
|
||||||
|
|
||||||
|
|
||||||
|
let data = "{ \"type\": \"FeatureCollection\", \"features\": [\n";
|
||||||
|
let id = 0;
|
||||||
|
|
||||||
|
// roads
|
||||||
|
routes._groups[0][0].childNodes.forEach(n => {
|
||||||
|
id++;
|
||||||
|
n.forEach(x => {
|
||||||
|
console.log(x);
|
||||||
|
});
|
||||||
|
data += JSON.stringify(getRiverPoints(n));
|
||||||
|
|
||||||
|
});
|
||||||
|
console.log(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveGeoJSON_Rivers() {
|
function saveGeoJSON_Rivers() {
|
||||||
let data = "{ \"type\": \"FeatureCollection\", \"features\": [\n";
|
let data = "{ \"type\": \"FeatureCollection\", \"features\": [\n";
|
||||||
let id = 0;
|
|
||||||
|
|
||||||
rivers._groups[0][0].childNodes.forEach(n => {
|
rivers._groups[0][0].childNodes.forEach(n => {
|
||||||
id++;
|
|
||||||
data += "{\n \"type\": \"Feature\",\n \"geometry\": { \"type\": \"LineString\", \"coordinates\": ";
|
data += "{\n \"type\": \"Feature\",\n \"geometry\": { \"type\": \"LineString\", \"coordinates\": ";
|
||||||
data += JSON.stringify(getRiverPoints(n));
|
data += JSON.stringify(getRiverPoints(n));
|
||||||
data += " },\n \"properties\": {\n";
|
data += " },\n \"properties\": {\n";
|
||||||
data += " \"id\": \""+id+"\"\n";
|
data += " \"id\": \""+n.id+"\",\n";
|
||||||
|
data += " \"width\": \""+n.dataset.width+"\",\n";
|
||||||
|
data += " \"increment\": \""+n.dataset.increment+"\"\n";
|
||||||
data +=" }\n},\n";
|
data +=" }\n},\n";
|
||||||
});
|
});
|
||||||
data = data.substring(0, data.length - 2)+"\n"; // remove trailing comma
|
data = data.substring(0, data.length - 2)+"\n"; // remove trailing comma
|
||||||
|
|
@ -52,7 +68,7 @@ function saveGeoJSON_Rivers() {
|
||||||
function getRiverPoints(node) {
|
function getRiverPoints(node) {
|
||||||
let points = [];
|
let points = [];
|
||||||
const l = node.getTotalLength() / 2; // half-length
|
const l = node.getTotalLength() / 2; // half-length
|
||||||
const increment = 0.5; // defines density of points
|
const increment = 0.25; // defines density of points
|
||||||
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);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue