# -*- coding: utf-8 -*-
__author__ = 'wanger'
__date__ = '2024-08-20'
__copyright__ = '(C) 2024 by siwei'
__revision__ = '1.0'
# inbuilt libraries
from typing import Dict, Iterable, List, Union
# third-party libraries
import seaborn as sns
from matplotlib.colors import rgb2hex
def coverage_style_colormapentry(
color_ramp: Union[List, Dict, Iterable],
min_value: float,
max_value: float,
number_of_classes: int = None,
):
style_append = ""
n = len(color_ramp)
if isinstance(color_ramp, list):
if n != number_of_classes:
number_of_classes = n
interval = (max_value - min_value) / (number_of_classes - 1)
for i, color in enumerate(color_ramp):
value = min_value + interval * i
value = round(value, 1)
style_append += (
''.format(
color, value, value
)
)
elif isinstance(color_ramp, dict):
if n != number_of_classes:
number_of_classes = n
interval = (max_value - min_value) / (number_of_classes - 1)
for name, color, i in zip(color_ramp.keys(), color_ramp.values(), range(n)):
value = min_value + interval * i
style_append += (
''.format(
color, name, value
)
)
else:
for i, color in enumerate(color_ramp):
interval = (max_value - min_value) / (number_of_classes - 1)
value = min_value + interval * i
style_append += (
''.format(
color, value, value
)
)
return style_append
def coverage_style_xml(
color_ramp, style_name, cmap_type, min_value, max_value, number_of_classes, opacity
):
min_max_difference = max_value - min_value
style_append = ""
interval = min_max_difference / (number_of_classes - 1) # noqa
# The main style of the coverage style
if isinstance(color_ramp, str):
palette = sns.color_palette(color_ramp, int(number_of_classes))
color_ramp = [rgb2hex(i) for i in palette]
style_append += coverage_style_colormapentry(
color_ramp, min_value, max_value, number_of_classes
)
style = """
{2}
{3}
1
{1}
""".format(
cmap_type, style_append, style_name, opacity
)
with open("style.sld", "w") as f:
f.write(style)
def outline_only_xml(color, width, geom_type="polygon"):
if geom_type == "point":
symbolizer = """
circle
{}
8
""".format(
color
)
elif geom_type == "line":
symbolizer = """
{}
""".format(
color, width
)
elif geom_type == "polygon":
symbolizer = """
{}
{}
""".format(
color, width
)
else:
print("Error: Invalid geometry type")
return
style = """
Layer name
Layer name
Single symbol
{}
""".format(
symbolizer
)
with open("style.sld", "w") as f:
f.write(style)
def catagorize_xml(
column_name: str,
values: List[float],
color_ramp: str = None,
geom_type: str = "polygon",
):
n = len(values)
palette = sns.color_palette(color_ramp, int(n))
palette_hex = [rgb2hex(i) for i in palette]
rule = ""
for value, color in zip(values, palette_hex):
if geom_type == "point":
rule += """
{0}
{1}
{0}
{1}
circle
{2}
5
""".format(
column_name, value, color
)
elif geom_type == "line":
rule += """
{1}
{0}
{1}
{2}
1
""".format(
column_name, value, color
)
elif geom_type == "polygon":
rule += """
{0}
{1}
{0}
{1}
{2}
{3}
0.5
""".format(
column_name, value, color, "#000000"
)
else:
print("Error: Invalid geometry type")
return
style = """
Layer name
Layer name
{}
""".format(
rule
)
with open("style.sld", "w") as f:
f.write(style)
def classified_xml(
style_name: str,
column_name: str,
values: List[float],
color_ramp: str = None,
geom_type: str = "polygon",
):
max_value = max(values)
min_value = min(values)
diff = max_value - min_value
n = 5
interval = diff / 5
palette = sns.color_palette(color_ramp, int(n))
palette_hex = [rgb2hex(i) for i in palette]
# interval = N/4
# color_values = [{value: color} for value, color in zip(values, palette_hex)]
# print(color_values)
rule = ""
for i, color in enumerate(palette_hex):
print(i)
rule += """
{1}
{4}
{0}
{5}
{0}
{4}
{2}
{3}
1
bevel
""".format(
column_name,
style_name,
color,
"#000000",
min_value + interval * i,
min_value + interval * (i + 1),
)
style = """
{0}
{0}
{1}
""".format(
style_name, rule
)
with open("style.sld", "w") as f:
f.write(style)