Commit 2ffd890f authored by Eric Duminil's avatar Eric Duminil
Browse files

Allow random parameters

parent d548093b
......@@ -11,6 +11,7 @@ from pathlib import Path
from collections import namedtuple, Counter
import re
import math
import random
import folium
import matplotlib.pyplot as plt
......@@ -45,7 +46,6 @@ WKT = "POLYGON((9.942183 49.804010, 9.942301 49.802740, 9.945477 49.802698, 9.94
# TREES #
########################
# TODO: Pseudoacacia oder Robinia. 50% 50%?
# TODO: Add DreSo Table info.
# TODO: Select random streets
......@@ -61,11 +61,20 @@ TREE_DISTANCE = 25 # [m]
# Unless there's already another tree closer than MIN_DISTANCE away:
MIN_DISTANCE = 10 # [m]
# Tree properties, which will be used for every added tree:
TREE_SPECIES = 'Acer platanoides' # Should be known by Tree-DB
# Tree properties, which will be used for added trees.
# If parameter is a dictionary, a key will be picked randomly for each tree, using values as weight.
# If parameter is a list, a key will be picked randomly for each tree
# parameter will be used if it is a string or a number
TREE_SPECIES = {
# Keys should be known by Tree-DB.
# Values should be probabilities, adding to 1.0
'Robinia pseudoacacia': 0.5,
'Acer platanoides': 0.5
}
TREE_CROWN_DIAMETER = 8 # [m]
TREE_HEIGHT = 12 # [m]
TREE_HEIGHT = [8, 10, 12] # [m]
TREE_TRUNK_DIAMETER = 90 # [cm]
########################
......@@ -100,6 +109,14 @@ OUTPUT_DIR = SCRIPT_DIR / 'output'
Bounds = namedtuple("Bounds", "W S E N")
def pick_from(some_object):
if isinstance(some_object, dict):
return random.choices(list(some_object), weights=some_object.values(), k=1)[0]
if isinstance(some_object, list):
return random.choice(some_object)
return some_object
def load_region(wkt_polygon: str):
region = wkt.loads(wkt_polygon)
bounds = Bounds(*region.bounds)
......@@ -225,14 +242,18 @@ def place_trees(forest: Forest, ways: list, region: str, to_local, tree_distance
for potential_tree in potential_trees:
x = potential_tree.x
y = potential_tree.y
tree_species = pick_from(TREE_SPECIES)
diameter = pick_from(TREE_CROWN_DIAMETER)
height = pick_from(TREE_HEIGHT)
trunk_diameter = pick_from(TREE_TRUNK_DIAMETER)
if local_region.contains(geometry.Point(x, y)):
forest.add_tree_if_possible(min_distance_2, x, y,
color='#DFFF00',
type='Fake Tree',
description=TREE_SPECIES,
diameter=TREE_CROWN_DIAMETER, # [m]
height=TREE_HEIGHT, # [m]
trunk_diameter=TREE_TRUNK_DIAMETER, # [cm]
description=tree_species,
diameter=diameter, # [m]
height=height, # [m]
trunk_diameter=trunk_diameter, # [cm]
source='add_trees.py'
)
......@@ -331,6 +352,8 @@ def main(wkt_polygon: str, epsg_id: int, tree_distance: float, min_distance: flo
region, bounds = load_region(wkt_polygon)
ways = get_osm_roads(bounds)
random.seed(get_basename(bounds))
if import_tree_shp:
existing_forest = get_existing_forest(import_tree_shp)
else:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment