Skip to content

Commit

Permalink
rules seems ok, maybe there's something with data
Browse files Browse the repository at this point in the history
  • Loading branch information
kauevestena committed Oct 7, 2024
1 parent 1d2e2ff commit b99dec8
Show file tree
Hide file tree
Showing 7 changed files with 1,229 additions and 49 deletions.
Binary file added assets/map_symbols/age.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/map_symbols/n_revs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,45 @@
},
}

# for the map:
numeric_themes = {
"age": {
"name": "DQ - Data Age (Y)",
"invalid_color": "#808080",
"invalid_threshold": 0,
"invalid_operator": "<",
"invalid_value_above": False,
"default_color": "#fde725",
"default_value": 0,
"n_digits": 2,
"color_dict": {
"2": "#7ad151",
"4": "#22a884",
"6": "#2a788e",
"8": "#414487",
"10": "#440154",
},
},
"n_revs": {
"name": "DQ - N. of Revs.",
"invalid_color": "#808080",
"invalid_threshold": 0,
"invalid_operator": "<",
"invalid_value_above": False,
"default_color": "#1d1147",
"default_value": 0,
"n_digits": 2,
"color_dict": {
"5": "#51127c",
"10": "#832681",
"15": "#b73779",
"20": "#e75263",
"25": "#fc8961",
"30": "#fec488",
},
},
}

layernames = [key for key in fields_values_properties]


Expand Down
22 changes: 22 additions & 0 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,3 +922,25 @@ def create_date_age(row):
return -1

return (datetime.today() - rev).days / 365.25


def get_spaces(number, max_digits=2):
total_length = max_digits + 1 # +1 for consistent spacing
num_digits = len(str(number))
num_spaces = total_length - num_digits
return " " * num_spaces


def get_formatted_interval_string(n1, n2, max_digits=2):
spaces1 = get_spaces(n1, max_digits)
spaces2 = get_spaces(n2, max_digits)

# # a small correction in case of spaces1 and spaces2 are not equal
# # not sure if works for max_digits>2
# if len(spaces1) != len(spaces2):
# spaces2 += " "

if spaces1 == " ":
return f" {n1}{spaces1}-{spaces2}{n2}"
else:
return f"{n1}{spaces1}-{spaces2}{n2}"
19 changes: 15 additions & 4 deletions webmap/create_webmap_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,25 @@
"traffic_calming": "#63636366",
}

# adding the simple styles:
for attribute in interest_attributes:
color_dict = get_color_dict(attribute, attribute_layers.get(attribute, "sidewalks"))
color_schema = create_maplibre_color_schema(
color_dict, attribute, different_else_color.get(attribute, "gray")
)
# color_schema = create_maplibre_color_schema(
# color_dict, attribute, different_else_color.get(attribute, "gray")
# )

params["styles"][attribute] = create_simple_map_style(
interest_attributes[attribute], color_schema, color_dict, attribute
name=interest_attributes[attribute],
# color_schema=color_schema,
color_dict=color_dict,
attribute_name=attribute,
else_color=different_else_color.get(attribute, "gray"),
)

# Now the numerical styles:
for fieldname in numeric_themes:
params["styles"][fieldname] = create_simple_numeric_style(
attribute_name=fieldname, sources=MAP_SOURCES, **numeric_themes[fieldname]
)

# reading the base html
Expand Down
192 changes: 147 additions & 45 deletions webmap/webmap_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ def get_sources(terrain_url=None, only_urls=False):
MAP_SOURCES = get_sources()["sources"]


def intialize_style_dict(name, sources=MAP_SOURCES):
style_dict = deepcopy(mapstyle_basedict)

style_dict["sources"] = sources

style_dict["name"] = name

style_dict["layers"].extend(deepcopy(immutable_layers))

return style_dict


def initialize_layer_dict(layername):
layer_type = layertypes_dict[layername]

layer_dict = deepcopy(layertypes_basedict[layer_type])

# now we can set the id and source:
layer_dict["id"] = layername
layer_dict["source"] = f"oswm_pmtiles_{layername}"
layer_dict["source-layer"] = layername

return layer_dict, layer_type


def sort_keys_by_order(input_dict, order_list):
ordered_keys = []
remaining_keys = list(input_dict.keys())
Expand Down Expand Up @@ -211,26 +236,13 @@ def create_base_style(sources=MAP_SOURCES, name="Footway Categories"):

custom_legend_widths = {"kerbs": 8}

style_dict = deepcopy(mapstyle_basedict)

style_dict["sources"] = sources

style_dict["name"] = name

style_dict["layers"].extend(deepcopy(immutable_layers))
style_dict = intialize_style_dict(name, sources)

# declaring the legend:
style_legend = StandaloneLegend()

for layername in ordered_map_layers:
layer_type = layertypes_dict[layername]

layer_dict = deepcopy(layertypes_basedict[layer_type])

# now we can set the id and source:
layer_dict["id"] = layername
layer_dict["source"] = f"oswm_pmtiles_{layername}"
layer_dict["source-layer"] = layername
layer_dict, layer_type = initialize_layer_dict(layername)

if layername in custom_layer_colors:
layer_dict["paint"]["line-color"] = custom_layer_colors[layername]
Expand Down Expand Up @@ -263,20 +275,15 @@ def create_base_style(sources=MAP_SOURCES, name="Footway Categories"):

def create_simple_map_style(
name,
color_schema,
color_dict,
filename,
attribute_name,
else_color="gray",
sources=MAP_SOURCES,
generate_shadow_layers=False,
generate_shadow_layers=False, # DEPRECATED
):
style_dict = intialize_style_dict(name, sources)

style_dict = deepcopy(mapstyle_basedict)

style_dict["sources"] = sources

style_dict["name"] = name

style_dict["layers"].extend(deepcopy(immutable_layers))
color_schema = create_maplibre_color_schema(color_dict, attribute_name, else_color)

# creating "shadow layers" for line layers only:
if generate_shadow_layers:
Expand All @@ -292,14 +299,7 @@ def create_simple_map_style(
style_dict["layers"].append(layer_dict)

for layername in ordered_map_layers:
layer_type = layertypes_dict[layername]

layer_dict = deepcopy(layertypes_basedict[layer_type])

# now we can set the id and source:
layer_dict["id"] = layername
layer_dict["source"] = f"oswm_pmtiles_{layername}"
layer_dict["source-layer"] = layername
layer_dict, layer_type = initialize_layer_dict(layername)

# layer_type = layertypes_dict[layername]

Expand All @@ -320,7 +320,7 @@ def create_simple_map_style(

style_legend.add_line(label="other", color=color_schema[-1], **custom_line_args)

style_legend.export(os.path.join(map_symbols_assets_path, f"{filename}.png"))
style_legend.export(os.path.join(map_symbols_assets_path, f"{attribute_name}.png"))

return style_dict

Expand Down Expand Up @@ -360,13 +360,7 @@ def create_crossings_kerbs_style(
else_color="#63636380",
):

style_dict = deepcopy(mapstyle_basedict)

style_dict["sources"] = sources

style_dict["name"] = name

style_dict["layers"].extend(deepcopy(immutable_layers))
style_dict = intialize_style_dict(name, sources)

interest_layers = {
# layername : tag key
Expand Down Expand Up @@ -437,10 +431,118 @@ def create_crossings_kerbs_style(
return style_dict


def create_age_style():
# TODO
# style picker: https://waldyrious.net/viridis-palette-generator/
pass
def create_simple_numeric_style(
name,
color_dict,
attribute_name,
default_color,
default_value=0,
invalid_color="#808080",
invalid_threshold=0,
invalid_operator="<",
n_digits=2,
sources=MAP_SOURCES,
invalid_value_above=False,
):
"""
Creates a simple style for numeric values, where values in the color_dict are mapped to their corresponding colors.
:param name: name of the style
:param color_dict: a dictionary mapping each value to its corresponding color
:param attribute_name: name of the attribute to style
:param default_color: default color
:param invalid_color: color for invalid values
:param invalid_threshold: threshold for invalid values
:param invalid_operator: operator for invalid values
:param sources: sources for the style
:param invalid_value_above: if True, values above the threshold are invalid
:return: the style dictionary
To generate discretized good styles, use: https://waldyrious.net/viridis-palette-generator/
"""
style_dict = intialize_style_dict(name, sources)

color_schema = [
"case",
[invalid_operator, ["get", attribute_name], invalid_threshold],
invalid_color, #
[
"step",
["get", attribute_name],
default_color,
# 2,
# "#7CFC00", # // Age >= 2
# 4,
# "#ADFF2F", # // Age >= 4
# 6,
# "#FFD700", # // Age >= 6
# 8,
# "#FF8C00", # // Age >= 8
# 10,
# "#FF0000", # // Age >= 10
],
]

# to make it easier to read, we sort the keys:
sorted_keys = list(sorted(color_dict.keys(), key=float))

for key in sorted_keys:
value = color_dict[key]
color_schema[3].append(float(key))
color_schema[3].append(value)

for layername in ordered_map_layers:
# seems that all layers will have the same style:
layer_dict, layer_type = initialize_layer_dict(layername)

layer_dict["paint"][color_attribute[layer_type]] = color_schema

style_dict["layers"].append(layer_dict)

# instantiating the legend:
style_legend = StandaloneLegend()

custom_line_args = {
"linewidth": 4,
}

# the invalid, if above:
if invalid_value_above:
style_legend.add_line(label="n.a.", color=invalid_color, **custom_line_args)

# adding regular values
style_legend.add_line(
# label=f"{default_value}{get_spaces(default_value)}- {sorted_keys[0]}",
label=get_formatted_interval_string(default_value, sorted_keys[0], n_digits),
color=default_color,
**custom_line_args,
)

last_position = len(sorted_keys) - 1
for i, key in enumerate(sorted_keys):

if i == last_position:
label_name = f"{key} +"

else: # avoiding invalid positions at the call
# label_name = f"{key} - {sorted_keys[i+1]}"
label_name = get_formatted_interval_string(
key, sorted_keys[i + 1], n_digits
)

style_legend.add_line(
label=label_name, color=color_dict[key], **custom_line_args
)

# the invalid, if below:
if not invalid_value_above:
style_legend.add_line(label="n.a.", color=invalid_color, **custom_line_args)

# exporting
style_legend.export(os.path.join(map_symbols_assets_path, f"{attribute_name}.png"))

return style_dict


# call just once:
Expand Down
Loading

0 comments on commit b99dec8

Please sign in to comment.