From 629b6d7c7c51c48814f00acb320b9582d4c15a9c Mon Sep 17 00:00:00 2001 From: Tom Vogt Date: Sat, 7 Sep 2019 09:19:38 +0200 Subject: [PATCH] adding river data (id, width, increment) added PHP script to add random details to cells. --- QGIS/add_random_points.php | 81 ++++++++++++++++++++++++++++++++++++++ modules/save-and-load.js | 30 ++++++++++---- 2 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 QGIS/add_random_points.php diff --git a/QGIS/add_random_points.php b/QGIS/add_random_points.php new file mode 100644 index 00000000..71bb532b --- /dev/null +++ b/QGIS/add_random_points.php @@ -0,0 +1,81 @@ +\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); + +?> diff --git a/modules/save-and-load.js b/modules/save-and-load.js index de08d9a1..385a9dda 100644 --- a/modules/save-and-load.js +++ b/modules/save-and-load.js @@ -3,9 +3,9 @@ // download map data as GeoJSON function saveGeoJSON() { - saveGeoJSON_Cells(); - //saveGeoJSON_Roads(); - saveGeoJSON_Rivers(); + //saveGeoJSON_Cells(); + saveGeoJSON_Roads(); + //saveGeoJSON_Rivers(); } function saveGeoJSON_Roads() { @@ -14,6 +14,8 @@ function saveGeoJSON_Roads() { trails = routes.select("#trails"); searoutes = routes.select("#searoutes"); + console.log(rivers._groups[0][0].childNodes); + console.log(typeof(roads)); console.log(roads); @@ -22,18 +24,32 @@ function saveGeoJSON_Roads() { console.log(routes._groups[0][0]); 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() { let data = "{ \"type\": \"FeatureCollection\", \"features\": [\n"; - let id = 0; rivers._groups[0][0].childNodes.forEach(n => { - id++; data += "{\n \"type\": \"Feature\",\n \"geometry\": { \"type\": \"LineString\", \"coordinates\": "; data += JSON.stringify(getRiverPoints(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 = data.substring(0, data.length - 2)+"\n"; // remove trailing comma @@ -52,7 +68,7 @@ function saveGeoJSON_Rivers() { function getRiverPoints(node) { let points = []; 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) { const p1 = node.getPointAtLength(i); const p2 = node.getPointAtLength(c);