def _count_suburb_by_age_group(tree_data: List[Dict[str, Any]], use_prediction: bool) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        suburb_name = clean_key(tree["geo_info"]["suburb"])
        if counted_trees.get(suburb_name) is None:
            counted_trees[suburb_name]: Dict[str, Any] = {}
        
        age_group = tree["tree_age"]["age_group_2020"]
        if age_group is None:
            if use_prediction is True:
                try:
                    if tree["predictions"]["by_radius_prediction"]["age_group"]["probability"] >= MIN_PROBABILITY:
                        age_group = tree["predictions"]["by_radius_prediction"]["age_group"]["prediction"]
                except:
                    pass
        
        age_group = clean_key(age_group)
        if counted_trees[suburb_name].get(age_group) is None:
            counted_trees[suburb_name][age_group]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[suburb_name][age_group]["absolute"] += 1

    for suburb, suburb_vals in counted_trees.items():
        all_suburb_trees = sum([x["absolute"] for x in suburb_vals.values()])
        for agegroup, vals in suburb_vals.items():
            counted_trees[suburb][agegroup]["percentage"] = round(vals["absolute"]/all_suburb_trees, 2)
    
    return counted_trees
Esempio n. 2
0
def _count_district_by_genus(tree_data: List[Dict[str, Any]], use_prediction: bool) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        district_name = clean_key(tree["geo_info"]["district"])
        if counted_trees.get(district_name) is None:
            counted_trees[district_name]: Dict[str, Any] = {}
        
        genus_name = tree["tree_taxonomy"]["genus"]
        if genus_name is None:
            if use_prediction is True:
                try:
                    if tree["predictions"]["by_radius_prediction"]["genus"]["probability"] >= MIN_PROBABILITY:
                        genus_name = tree["predictions"]["by_radius_prediction"]["genus"]["prediction"]
                except:
                    pass
        genus_name = clean_key(genus_name)
        
        if counted_trees[district_name].get(genus_name) is None:
            counted_trees[district_name][genus_name]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[district_name][genus_name]["absolute"] += 1

    for district, district_vals in counted_trees.items():
        all_district_trees = sum([x["absolute"] for x in district_vals.values()])
        for genus, vals in district_vals.items():
            counted_trees[district][genus]["percentage"] = round(vals["absolute"]/all_district_trees, 2)
    
    return counted_trees
Esempio n. 3
0
def _count_object_type_by_suburb(
        tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        object_type = tree["base_info"]["object_type"]
        object_type = clean_key(object_type)

        if counted_trees.get(object_type) is None:
            counted_trees[object_type]: Dict[str, Any] = {}

        suburb_name = clean_key(tree["geo_info"]["suburb"])

        if counted_trees[object_type].get(suburb_name) is None:
            counted_trees[object_type][suburb_name]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[object_type][suburb_name]["absolute"] += 1

    for object_type, object_type_vals in counted_trees.items():
        all_object_type_trees = sum(
            [x["absolute"] for x in object_type_vals.values()])
        for suburb, vals in object_type_vals.items():
            counted_trees[object_type][suburb]["percentage"] = round(
                vals["absolute"] / all_object_type_trees, 2)

    return counted_trees
def _count_trees(tree_data: List[Dict[str, Any]],
                 use_prediction: bool) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        age_group = tree["tree_age"]["age_group_2020"]
        if age_group is None:
            if use_prediction is True:
                try:
                    if tree["predictions"]["by_radius_prediction"][
                            "age_group"]["probability"] >= MIN_PROBABILITY:
                        age_group = tree["predictions"][
                            "by_radius_prediction"]["age_group"]["prediction"]
                except:
                    pass

        age_group = clean_key(age_group)

        if counted_trees.get(age_group) is None:
            counted_trees[age_group]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[age_group]["absolute"] += 1

    for age_group, vals in counted_trees.items():
        counted_trees[age_group]["percentage"] = round(
            vals["absolute"] / len(tree_data), 2)

    return counted_trees
def _location_type(tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    location_types_count: Dict[str, int] = {}

    for tree in tree_data:
        location_types = tree["tree_location_type"]

        if location_types is None:
            location_type = clean_key(location_types)
            if location_types_count.get(location_type) is None:
                location_types_count[location_type] = 0
            location_types_count[location_type] += 1
            continue

        is_highway = False
        if len(location_types) == 1 and "highway" in location_types:
            is_highway = True

        use_green_spaces_agriculture = True
        if len(
                location_types
        ) > 1 and "green_spaces_leisure" in location_types and "green_spaces_agriculture" in location_types:
            use_green_spaces_agriculture = False

        for location_type in location_types:
            if location_type == "highway" and is_highway is False:
                continue
            if location_type == "green_spaces_agriculture" and use_green_spaces_agriculture is False:
                continue

            if location_types_count.get(location_type) is None:
                location_types_count[location_type] = 0
            location_types_count[location_type] += 1

    return location_types_count
Esempio n. 6
0
def _count_neighbours_radius_50(tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    '''
    TODO: include this attribute in base data again
    '''
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        district_name = clean_key(tree["geo_info"]["district"])

        if counted_trees.get(district_name) is None:
            counted_trees[district_name]: Dict[str, Any] = {
                "min": None,
                "max": None,
                "mean": None,
                "median": None,
                "tmp_list": []
            }
        
        num_neighbours = tree["num_neighbours_radius_50"]  # not included in base data right now
        if num_neighbours is None:
            continue
        
        counted_trees[district_name]["tmp_list"].append(num_neighbours)
    
    for district, vals in counted_trees.items():
        counted_trees[district]["min"] = min(vals["tmp_list"])
        counted_trees[district]["max"] = max(vals["tmp_list"])
        counted_trees[district]["mean"] = round(mean(vals["tmp_list"]))
        counted_trees[district]["median"] = median(vals["tmp_list"])

        del counted_trees[district]["tmp_list"]
    
    return counted_trees
def _count_trees(tree_data: List[Dict[str, Any]],
                 use_prediction: bool) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        genus_name = tree["tree_taxonomy"]["genus"]
        if genus_name is None:
            if use_prediction is True:
                try:
                    if tree["predictions"]["by_radius_prediction"]["genus"][
                            "probability"] >= MIN_PROBABILITY:
                        genus_name = tree["predictions"][
                            "by_radius_prediction"]["genus"]["prediction"]
                except:
                    pass
        genus_name = clean_key(genus_name)

        if counted_trees.get(genus_name) is None:
            counted_trees[genus_name]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[genus_name]["absolute"] += 1

    for genus, vals in counted_trees.items():
        counted_trees[genus]["percentage"] = round(
            vals["absolute"] / len(tree_data), 2)

    return counted_trees
Esempio n. 8
0
def _genus_species_name_german_tree(
        tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    names_tree: Dict[str, int] = {}
    for tree in tree_data:
        genus_name = tree["tree_taxonomy"]["genus"]
        if genus_name is None:
            continue

        if names_tree.get(genus_name) is None:
            names_tree[genus_name]: Dict[str, List[str]] = {}

        species_name = clean_key(tree["tree_taxonomy"]["species"])
        if names_tree[genus_name].get(species_name) is None:
            names_tree[genus_name][species_name] = []

        name_german_list = tree["tree_taxonomy"]["name_german"]
        if name_german_list is None:
            continue

        for name_german in name_german_list:
            if name_german not in names_tree[genus_name][species_name]:
                names_tree[genus_name][species_name].append(name_german)

    # ***
    # default for empty values: null
    for genus_key, genus_vals in names_tree.items():
        for species_key, species_vals in genus_vals.items():
            if len(species_vals) == 0:
                names_tree[genus_key][species_key] = None

    return names_tree
Esempio n. 9
0
def _suburb_data_completeness(
        tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    completeness_in_suburb: Dict[str, List[str]] = {}
    for tree in tree_data:
        suburb_name = clean_key(tree["geo_info"]["suburb"])

        # if suburb_name is None:
        #     continue

        if completeness_in_suburb.get(suburb_name) is None:
            completeness_in_suburb[suburb_name]: Dict[str, Any] = {
                "mean": None,
                "median": None,
                "min": None,
                "max": None,
                "tmp_list": []
            }

        dataset_completeness = tree["dataset_completeness"]
        completeness_in_suburb[suburb_name]["tmp_list"].append(
            dataset_completeness)

    for suburb, vals in completeness_in_suburb.items():
        completeness_in_suburb[suburb]["min"] = min(vals["tmp_list"])
        completeness_in_suburb[suburb]["max"] = max(vals["tmp_list"])
        completeness_in_suburb[suburb]["mean"] = round(mean(vals["tmp_list"]),
                                                       2)
        completeness_in_suburb[suburb]["median"] = median(vals["tmp_list"])

        del completeness_in_suburb[suburb]["tmp_list"]

    return completeness_in_suburb
def _count_age_group_by_genus(tree_data: List[Dict[str, Any]],
                              use_prediction: bool) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        age_group = tree["tree_age"]["age_group_2020"]
        if age_group is None:
            if use_prediction is True:
                try:
                    if tree["predictions"]["by_radius_prediction"][
                            "age_group"]["probability"] >= MIN_PROBABILITY:
                        age_group = tree["predictions"][
                            "by_radius_prediction"]["age_group"]["prediction"]
                except:
                    pass

        age_group = clean_key(age_group)

        if counted_trees.get(age_group) is None:
            counted_trees[age_group]: Dict[str, Any] = {}

        genus_name = tree["tree_taxonomy"]["genus"]
        if genus_name is None:
            if use_prediction is True:
                try:
                    genus_name = tree["predictions"]["by_radius_prediction"][
                        "genus"]
                except:
                    pass
        genus_name = clean_key(genus_name)

        if counted_trees[age_group].get(genus_name) is None:
            counted_trees[age_group][genus_name]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[age_group][genus_name]["absolute"] += 1

    for agegroup, agegroup_vals in counted_trees.items():
        all_agegroup_trees = sum(
            [x["absolute"] for x in agegroup_vals.values()])
        for genus, vals in agegroup_vals.items():
            counted_trees[agegroup][genus]["percentage"] = round(
                vals["absolute"] / all_agegroup_trees, 2)

    return counted_trees
Esempio n. 11
0
def _count_district_by_object_type(tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        district_name = clean_key(tree["geo_info"]["district"])
        if counted_trees.get(district_name) is None:
            counted_trees[district_name]: Dict[str, Any] = {}
        
        object_type = tree["base_info"]["object_type"]
        object_type = clean_key(object_type)
        
        if counted_trees[district_name].get(object_type) is None:
            counted_trees[district_name][object_type]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[district_name][object_type]["absolute"] += 1

    for district, district_vals in counted_trees.items():
        all_district_trees = sum([x["absolute"] for x in district_vals.values()])
        for object_type, vals in district_vals.items():
            counted_trees[district][object_type]["percentage"] = round(vals["absolute"]/all_district_trees, 2)
    
    return counted_trees
Esempio n. 12
0
def _count_trees(tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        district_name = clean_key(tree["geo_info"]["district"])
        if counted_trees.get(district_name) is None:
            counted_trees[district_name]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[district_name]["absolute"] += 1

    for district, vals in counted_trees.items():
        counted_trees[district]["percentage"] = round(vals["absolute"]/len(tree_data), 2)
    
    return counted_trees
Esempio n. 13
0
def _count_trees(tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    counted_trees: Dict[str, int] = {}
    for tree in tree_data:
        object_type = tree["base_info"]["object_type"]
        object_type = clean_key(object_type)

        if counted_trees.get(object_type) is None:
            counted_trees[object_type]: Dict[str, Any] = {
                "absolute": 0,
                "percentage": 0
            }
        counted_trees[object_type]["absolute"] += 1

    for object_type, vals in counted_trees.items():
        counted_trees[object_type]["percentage"] = round(
            vals["absolute"] / len(tree_data), 2)

    return counted_trees
def _osm_keys(tree_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    osm_keys_count: Dict[str, int] = {}

    for tree in tree_data:
        location_types = tree["tree_location_type"]

        if location_types is None:
            location_type = clean_key(location_types)
            if osm_keys_count.get(location_type) is None:
                osm_keys_count[location_type] = 0
            osm_keys_count[location_type] += 1
            continue

        is_highway = False
        if len(location_types) == 1 and "highway" in location_types:
            is_highway = True

        use_green_spaces_agriculture = True
        if len(
                location_types
        ) > 1 and "green_spaces_leisure" in location_types and "green_spaces_agriculture" in location_types:
            use_green_spaces_agriculture = False

        for location_type, osm_node in location_types.items():
            if location_type == "highway" and is_highway is False:
                continue
            if location_type == "green_spaces_agriculture" and use_green_spaces_agriculture is False:
                continue

            if is_highway is True:
                if osm_keys_count.get(location_type) is None:
                    osm_keys_count[location_type] = 0
                osm_keys_count[location_type] += 1
            else:
                osm_key = osm_node["type"]
                if osm_keys_count.get(osm_key) is None:
                    osm_keys_count[osm_key] = 0
                osm_keys_count[osm_key] += 1

    return osm_keys_count