当前位置:首页>python>Python学习|RS Python入门02:列表、字典与流程控制:像元集合的编队【三】条件判断(if-elif-else)

Python学习|RS Python入门02:列表、字典与流程控制:像元集合的编队【三】条件判断(if-elif-else)

  • 2026-09-09 06:22:17
Python学习|RS Python入门02:列表、字典与流程控制:像元集合的编队【三】条件判断(if-elif-else)

点击上方蓝字 关注我们

2026

在上一课《Python学习 | RS Python 入门 02:列表、字典与流程控制:像元集合的编队【二】》中,我们已经认识了 Python 中最常用的两类容器:列表(list)与字典(dictionary)。

那么诸位现在一定知道,字典(dictionary)基本形式如下:

pixel = {    "B1": b1,    "B2": b2,    "B3": b3,    "B4": b4,    "B5": b5,    "B6": b6,    "B7": b7,    "sensor": "Landsat8",    "resolution": 30,    "acquisition_time": "2021-07-15"}

而此时,近红外波段的获取方式不再依赖位置记忆:

nir = pixel["B5"]

而遥感数据本质上就是一组强语义数据。当我们学会了用字典存数据之后,很快就会遇到几个现实问题:

  • 如果字典里没有某个波段,该怎么办?
  • 如果云量太高,是否应该跳过处理?
  • 如果是 Sentinel-2,用一套流程;如果是 Landsat,用另一套流程?
  • 如果分辨率不符合要求,是否需要重采样?
换句话说,当数据状态不同时程序就不能再无脑往下跑了。
此时,我们真正需要的,已经不只是存数据,而是让程序根据数据自己做判断、做选择、走不同的路。这时候我们就该使用条件判断了。它负责回答程序中的一些问题:要不要做?该做哪一步?用哪一种策略做?什么时候该停下来?
条件判断(if-elif-else)是程序实现逻辑分支的基础,它使程序能够根据不同的数据状态或用户输入做出不同的响应。在遥感数据处理中,条件判断用于质量控制、数据处理流程控制、异常检测等关键环节。

Part.01

基本语法结构

# 单条件判断if condition:    # 条件为True时执行的代码块    statement1    statement2
# 双分支判断if condition:    # 条件为True时执行    true_blockelse:    # 条件为False时执行    false_block
# 多分支判断if condition1:    block1elif condition2:    block2elif condition3:    block3else:    default_block

Part.02

缩进的重要性

# 正确示例if True:    print("条件成立")  # 缩进4个空格    print("继续执行")  # 相同缩进属于同一代码块
# 错误示例(会导致缩进错误)if True:print("没有缩进")  # IndentationError: expected an indented block
倘若运行错误示例,则会看到如下图的报错:

Part.03

遥感数据处理中的典型应用

场景一:检查云覆盖率

我们来模拟用一个云量检测流程,输入相关代码如下:

cloud_cover = 25.3  # 云覆盖率百分比if cloud_cover < 10:    quality = "Excellent"    process_level = "Full"elif 10 <= cloud_cover < 30:    quality = "Good"    process_level = "Standard"elif 30 <= cloud_cover < 50:    quality = "Moderate"    process_level = "Basic"    print("警告:云量较高,建议人工检查")else:    quality = "Poor"    process_level = "Skip"    print("错误:云量超过50%,跳过处理")print(f"影像质量: {quality}, 处理级别: {process_level}")
我们便可以得到结果:

场景二:传感器类型判断

def get_sensor_parameters(sensor_name):    """根据传感器类型返回处理参数"""    sensor_name = sensor_name.upper()    if "LANDSAT8" in sensor_name or "LANDSAT9" in sensor_name:        params = {            "bands": 11,            "resolution": 30,            "radiometric_correction": "LEDAPS",            "thermal_bands": True        }    elif "SENTINEL2" in sensor_name:        params = {            "bands": 13,            "resolution": [10, 20, 60],            "radiometric_correction": "Sen2Cor",            "atmospheric_correction": True        }    elif "MODIS" in sensor_name:        params = {            "bands": 36,            "resolution": [250, 500, 1000],            "revisit_period": 1,  # 天            "swath_width": 2330   # 公里        }    else:        params = {            "bands": 1,            "resolution": None,            "message": "未知传感器,使用默认参数"        }        print(f"警告:未识别的传感器类型: {sensor_name}")    return params
而后我们来测试一下来加深理解:
sensor = "Landsat8_OLI"parameters = get_sensor_parameters(sensor)print(f"传感器: {sensor}")print(f"波段数: {parameters['bands']}")
再或者:
sensor = "Modis"parameters = get_sensor_parameters(sensor)print(f"传感器: {sensor}")print(f"波段数: {parameters['bands']}")
我们便可以得到以下结果:

场景三:处理流程控制

或者我们可以模拟一些对影像处理的流程或函数:

def process_remote_sensing_data(image_path, options=None):    options = options or {}    print(f"开始处理: {image_path}")    if not os.path.exists(image_path):        print(f"错误:文件不存在 - {image_path}")        return None    metadata = extract_metadata(image_path)    if metadata.get('cloud_cover', 100) > 70:        print("云量过高,终止处理")        return None    if options.get('cloud_mask', True):        print("执行云检测和掩膜...")        image_data = apply_cloud_mask(image_data, metadata)    if options.get('atmospheric_correction', True):        print("执行大气校正...")        image_data = atmospheric_correction(image_data, metadata)    if metadata.get('projection') != "WGS84":        print("执行几何校正...")        image_data = geometric_correction(image_data)    print("处理完成")    return image_data

Part.04

条件表达式详解

1. 比较运算符

我们可以借助其对影像分辨率进行数值比较:
resolution = 30if resolution == 10:    category = "高分辨率"elif resolution == 30:    category = "中分辨率"elif resolution > 100:    category = "低分辨率"else:    category = "自定义分辨率"
又或者对文件格式进行字符串比较:
file_format = "GEOTIFF"if file_format.lower() == "geotiff":    driver = "GTiff"elif file_format.lower() == "hdf":    driver = "HDF4"elif file_format.lower() in ["jpg", "jpeg", "png"]:    driver = "JPEG"else:    driver = file_format
以及对一些植被数据范围进行检查:
ndvi_value = 0.65if 0.6 <= ndvi_value <= 0.8:    vegetation_health = "健康"elif ndvi_value > 0.8:    vegetation_health = "非常健康"elif 0.3 <= ndvi_value < 0.6:    vegetation_health = "中等"else:    vegetation_health = "稀疏/非植被"

2. 逻辑运算符

# 组合条件判断is_sentinel = Truehas_cloud_mask = Falsecloud_cover = 15
# and 运算符(所有条件都为True)if is_sentinel and cloud_cover < 20:    print("适用于高精度土地覆盖分类")    processing_level = "L2A"
# or 运算符(至少一个条件为True)if cloud_cover > 50 or not has_cloud_mask:    print("警告:数据质量可能影响分析结果")    recommend_check = True
# not 运算符(逻辑取反)if not os.path.exists(output_dir):    print("创建输出目录")    os.makedirs(output_dir)
# 复杂逻辑组合is_optical = Trueis_thermal = Falseresolution = 30if (is_optical and resolution <= 30) or (is_thermal and resolution <= 100):    suitable_for_urban = Trueelse:    suitable_for_urban = False

3. 成员运算符与身份运算符

# in 运算符(检查成员)available_sensors = ["Landsat8", "Landsat9", "Sentinel2", "MODIS"]user_sensor = "Landsat8"if user_sensor in available_sensors:    print(f"{user_sensor} 在支持列表中")else:    print(f"{user_sensor} 不在支持列表中,使用默认设置")
# not in 运算符if "VIIRS" not in available_sensors:    print("警告:VIIRS传感器暂不支持")
# is 运算符(身份比较,检查是否为同一对象)default_params = {"method": "standard"}user_params = default_paramscustom_params = {"method": "custom"}if user_params is default_params:    print("使用默认参数配置")  # 会执行if custom_params is not default_params:    print("使用自定义参数配置")  # 会执行
我们运行上述代码便会得到如下的截图:

Part.05

嵌套条件与复杂逻辑

1. 多层嵌套判断

def evaluate_image_quality(metadata):    quality_score = 0    issues = []    # 第一层:基础检查    if metadata.get('cloud_cover', 100) < 20:        quality_score += 40    else:        issues.append("高云量")    # 第二层:几何质量    if metadata.get('geometric_rmse', 10) < 2:        quality_score += 30    else:        issues.append("几何精度不足")    # 第三层:辐射质量(嵌套判断)    radiometric = metadata.get('radiometric_quality', 'unknown')    if radiometric == 'high':        quality_score += 30    elif radiometric == 'medium':        quality_score += 15        issues.append("辐射质量中等")    else:        issues.append("辐射质量未知或较差")    # 第四层:综合评级    if quality_score >= 80:        rating = "A"        recommendation = "适用于精密分析"    elif quality_score >= 60:        rating = "B"        recommendation = "适用于常规分析"    elif quality_score >= 40:        rating = "C"        recommendation = "仅适用于粗略分析"    else:        rating = "D"        recommendation = "建议不使用"    return {        "score": quality_score,        "rating": rating,        "issues": issues,        "recommendation": recommendation    }
而后我们来使用上述函数:
metadata = {    'cloud_cover': 15,    'geometric_rmse': 1.5,    'radiometric_quality': 'high'}result = evaluate_image_quality(metadata)print(f"质量评级: {result['rating']}, 得分: {result['score']}")
便会得到下图结果:

2. 条件表达式(三元运算符)

基本语法尾:value_if_true if condition else value_if_false
cloud_cover = 25processing_method = "精细处理" if cloud_cover < 10 else "标准处理"resolution_ratio = 3 resample_method = "cubic" if resolution_ratio < 2 else "nearest"ndvi = 0.7land_cover = (    "茂密植被" if ndvi > 0.6 else    "中等植被" if ndvi > 0.3 else    "稀疏植被" if ndvi > 0.1 else    "非植被")band_count = len(image_bands)data_type = "multispectral" if band_count >= 3 else "single_band"

Part.06

最佳实践与注意事项

1. 避免过度嵌套

# 不佳的写法(嵌套过深)def process_data_ugly(data, options):    if data is not None:        if len(data) > 0:            if options.get('preprocess'):                if options.get('method') == 'standard':                    # 处理逻辑...                    pass                elif options.get('method') == 'advanced':                    # 处理逻辑...                    pass                else:                    print("未知方法")            else:                print("跳过预处理")        else:            print("空数据")    else:        print("无数据")
而后我们可以使用改进的写法,也就是使用早期返回:
def process_data_better(data, options):    # 早期返回,减少嵌套    if data is None:        print("无数据")        return None    if len(data) == 0:        print("空数据")        return None    if not options.get('preprocess', False):        print("跳过预处理")        return data    method = options.get('method', 'standard')    if method == 'standard':        # 处理逻辑...        pass    elif method == 'advanced':        # 处理逻辑...        pass    else:        print("未知方法")        return data    return processed_data

2. 使用布尔变量增强可读性

def should_process_image(metadata, requirements):    """判断是否应该处理影像"""    has_low_cloud = metadata.get('cloud_cover', 100) < requirements.get('max_cloud', 30)    has_georeference = metadata.get('is_georeferenced', False)    has_required_bands = set(requirements.get('required_bands', [])).issubset(        set(metadata.get('available_bands', []))    )    is_recent = metadata.get('acquisition_year', 1900) >= requirements.get('min_year', 2000)    if has_low_cloud and has_georeference and has_required_bands and is_recent:        return True, "满足所有要求"    else:        reasons = []        if not has_low_cloud:            reasons.append("云量过高")        if not has_georeference:            reasons.append("缺少地理参考")        if not has_required_bands:            reasons.append("缺少必需波段")        if not is_recent:            reasons.append("数据过时")        return False, "; ".join(reasons)
requirements = {    'max_cloud': 20,    'required_bands': ['B4', 'B8'],    'min_year': 2015}metadata = {    'cloud_cover': 15,    'is_georeferenced': True,    'available_bands': ['B2', 'B3', 'B4', 'B8'],    'acquisition_year': 2020}should_process, message = should_process_image(metadata, requirements)print(f"处理决策: {should_process}, 原因: {message}")
好的,现在我们便得到了如下的结果:

3. 处理边界条件

def classify_pixel_value(value, classification_rules):    # 处理None或NaN    if value is None:        return "NoData"    try:        value = float(value)    except (ValueError, TypeError):        return "Invalid"    # 处理无穷大    if value == float('inf'):        return "Positive_Infinity"    elif value == float('-inf'):        return "Negative_Infinity"    # NaN检查    import math    if math.isnan(value):        return "NaN"    # 正常分类逻辑    for class_name, (lower, upper) in classification_rules.items():        # 处理开闭区间        if classification_rules.get(f"{class_name}_inclusive", True):            if lower <= value <= upper:                return class_name        else:            if lower < value < upper:                return class_name    # 默认:超出范围    return "Out_of_Range"
rules = {    'Water': (0.0, 0.2),    'Vegetation': (0.2, 0.6),    'Built_up': (0.6, 1.0)}test_values = [0.1, 0.3, 0.8, None, float('nan'), float('inf')]for val in test_values:    category = classify_pixel_value(val, rules)    print(f"值: {val} -> 类别: {category}")
运行结果后不难看出我们对任意值均可返回一定的分类:

Part.07

总结要点

基础结构:if-elif-else实现多分支逻辑判断

比较运算:使用==, !=, <, >, <=, >=进行数值和字符串比较

逻辑组合:and, or, not运算符实现复杂条件

成员检查:in, not in运算符检查元素存在性

三元表达式:简洁的单行条件赋值

嵌套控制:合理组织嵌套层次,避免过深

早期返回:减少嵌套,提高代码可读性

边界处理:充分考虑None、NaN、边界值等特殊情况

在遥感数据处理中,条件判断是实现质量控制、流程控制、异常处理、自适应算法选择等功能的核心机制,合理运用条件判断可以显著提高程序的稳定适应性。

好了,诸位,我们的条件就讲到这里了,至于策略模式的条件分发、状态机的条件迁移、事件驱动的条件响应……这些进一步的玩法,也就先留一盏灯,改日再同诸位逐层登顶。

如果你觉得“原来Python也能讲得这么遥感”,点个关注,后续系列推文将第一时间推送:

结构、控制流、函数、面向对象……

每一步都用卫星影像、波段、像元做例子,拒绝枯燥抽象。

附赠可运行的 Notebook 源码,自行上传即可上手。本公众号后台回复【RS Python入门02-3】即可获取本节 Notebook 源码。

最新文章

随机文章