add feature
This commit is contained in:
parent
89ebc990ed
commit
6df0ea83bb
BIN
__pycache__/color.cpython-310.pyc
Normal file
BIN
__pycache__/color.cpython-310.pyc
Normal file
Binary file not shown.
97
color.py
Normal file
97
color.py
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
import numpy as np
|
||||||
|
from typing import Dict, Tuple
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def create_color_gradient_mapping(val_sequence: np.ndarray) -> Dict[int, tuple]:
|
||||||
|
"""
|
||||||
|
创建颜色渐变映射,颜色值范围为0-255,使用Python list类型,分界值四舍五入为整数
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
val_sequence: numpy数组,包含分界值序列
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict[int, tuple]: 键为四舍五入后的分界值(整数),值为对应的RGBA颜色元组(Python list)
|
||||||
|
"""
|
||||||
|
# 确保输入是numpy数组
|
||||||
|
val_sequence = np.array(val_sequence)
|
||||||
|
|
||||||
|
# 定义基础颜色 (蓝色到红色),使用0-255格式和Python list类型
|
||||||
|
blue = [0, 0, 255, 255] # 起始蓝色 (R,G,B,A)
|
||||||
|
red = [255, 0, 0, 255] # 结束红色 (R,G,B,A)
|
||||||
|
|
||||||
|
# 创建结果字典
|
||||||
|
result = {}
|
||||||
|
|
||||||
|
# 添加第一个元素(纯蓝色)
|
||||||
|
result[round(float(val_sequence[0]))] = tuple(blue)
|
||||||
|
|
||||||
|
# 为中间值生成渐变色
|
||||||
|
for i in range(1, len(val_sequence) - 1):
|
||||||
|
# 计算当前位置的比例
|
||||||
|
ratio = i / (len(val_sequence) - 1)
|
||||||
|
# 线性插值计算当前颜色
|
||||||
|
current_color = [int(b * (1 - ratio) + r * ratio) for b, r in zip(blue, red)]
|
||||||
|
|
||||||
|
result[round(float(val_sequence[i]))] = tuple(current_color)
|
||||||
|
|
||||||
|
# 添加最后一个元素(纯红色)
|
||||||
|
result[round(float(val_sequence[-1]))] = tuple(red)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def get_color_from_value(
|
||||||
|
value: float, color_mapping: Dict[int, Tuple[int, int, int, int]]
|
||||||
|
) -> Tuple[int, int, int, int]:
|
||||||
|
"""
|
||||||
|
根据输入的值,从颜色映射字典中查找对应的颜色,使用线性插值
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
value: 输入值
|
||||||
|
color_mapping: 颜色映射字典
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple[int, int, int, int]: RGBA颜色元组
|
||||||
|
"""
|
||||||
|
if value <= 10:
|
||||||
|
return (0, 0, 255, 255)
|
||||||
|
if value >= 3500:
|
||||||
|
return (255, 0, 0, 255)
|
||||||
|
# 找到小于等于输入值的最大分界值
|
||||||
|
max_key = max([k for k in color_mapping if k <= value])
|
||||||
|
|
||||||
|
# 找到大于输入值的最小分界值
|
||||||
|
min_key = min([k for k in color_mapping if k > value])
|
||||||
|
|
||||||
|
# 计算当前值在两个分界值之间的比例
|
||||||
|
max_val = color_mapping[max_key]
|
||||||
|
min_val = color_mapping[min_key]
|
||||||
|
ratio = (value - max_key) / (min_key - max_key)
|
||||||
|
|
||||||
|
# 线性插值计算颜色
|
||||||
|
current_color = [
|
||||||
|
int(max_c * (1 - ratio) + min_c * ratio)
|
||||||
|
for max_c, min_c in zip(max_val, min_val)
|
||||||
|
]
|
||||||
|
return tuple(current_color)
|
||||||
|
|
||||||
|
|
||||||
|
# 使用示例:
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 生成原始序列
|
||||||
|
x_min = 10
|
||||||
|
x_max = 3500
|
||||||
|
a = 0.25
|
||||||
|
n = 19
|
||||||
|
val = (np.linspace((x_min**a), (x_max**a), n)) ** (1 / a)
|
||||||
|
|
||||||
|
# 生成颜色映射
|
||||||
|
color_mapping = create_color_gradient_mapping(val)
|
||||||
|
|
||||||
|
print(json.dumps(color_mapping))
|
||||||
|
|
||||||
|
# 使用get_color_from_value()方法查找颜色
|
||||||
|
print(get_color_from_value(89.47, color_mapping)) # (15, 0, 240, 255)
|
||||||
|
print(get_color_from_value(3500, color_mapping)) # (255, 0, 0, 255)
|
||||||
|
print(get_color_from_value(50, color_mapping)) # (64, 0, 191, 255)
|
16
main.py
16
main.py
@ -6,6 +6,17 @@ import json
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from shapely.geometry import mapping
|
from shapely.geometry import mapping
|
||||||
|
from color import create_color_gradient_mapping, get_color_from_value
|
||||||
|
|
||||||
|
|
||||||
|
x_min = 10
|
||||||
|
x_max = 3500
|
||||||
|
a = 0.25
|
||||||
|
n = 19
|
||||||
|
val = (np.linspace((x_min**a), (x_max**a), n)) ** (1 / a)
|
||||||
|
|
||||||
|
# 生成颜色映射
|
||||||
|
color_mapping = create_color_gradient_mapping(val)
|
||||||
|
|
||||||
|
|
||||||
def convert_coordinates(coord_list):
|
def convert_coordinates(coord_list):
|
||||||
@ -133,8 +144,9 @@ def generate_river_czml(river_df, runoff_df, level=4, start_time=None):
|
|||||||
|
|
||||||
for hour, runoff in enumerate(river_runoff):
|
for hour, runoff in enumerate(river_runoff):
|
||||||
time_offset = hour * 3600 # 转换为秒
|
time_offset = hour * 3600 # 转换为秒
|
||||||
color = get_color(runoff, 0, max_runoff)
|
print("runoff", runoff)
|
||||||
color_property["rgba"].extend([time_offset] + color)
|
color = get_color_from_value(abs(runoff), color_mapping)
|
||||||
|
color_property["rgba"].extend([time_offset] + list(color))
|
||||||
|
|
||||||
# 创建河段实体
|
# 创建河段实体
|
||||||
entity = {
|
entity = {
|
||||||
|
25
prompt.txt
25
prompt.txt
@ -72,3 +72,28 @@
|
|||||||
|
|
||||||
[object]
|
[object]
|
||||||
请给出生成czml的python代码
|
请给出生成czml的python代码
|
||||||
|
|
||||||
|
|
||||||
|
=======================================================================
|
||||||
|
|
||||||
|
[context]
|
||||||
|
我有一个幂次序列,是有如下代码生成的:
|
||||||
|
|
||||||
|
CODE```
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
min=10
|
||||||
|
max=3500
|
||||||
|
a = 0.25
|
||||||
|
n = 19
|
||||||
|
val = (np.linspace((xmin ** a), (3500 ** a), n)) ** (1 / a)
|
||||||
|
```
|
||||||
|
|
||||||
|
[object]
|
||||||
|
请根据这个序列,生成颜色区间
|
||||||
|
|
||||||
|
颜色区间由蓝色渐变为红色
|
||||||
|
|
||||||
|
小于val中第一元素值的为蓝色,大于val中最后一个元素值的为红色。
|
||||||
|
|
||||||
|
请用python实现,该方法返回一组列表,列表中的元素包含了两个属性,value和rgba
|
Loading…
x
Reference in New Issue
Block a user