解析高程数据asc文件

本文主要针对通过python获取高程数据asc文件中的经纬度信息
实现思路,通过解析asc将其转换为tif,处理tif最终获取最大最小经纬度,用于获取地图边界。
asc文件
解析高程数据asc文件_第1张图片
python代码

# coding UTF-8
# author:ablue
import json
import sys
import os
# 支持中文
from pylab import *
from osgeo import osr, gdal

ascInfo = []


# ascdir asc文件目录
# parseFile 需要解析的文件
def ascii2tif(
        ascdir: str,
        parseFile: str
) -> None:
    # (1) 创建输出文件夹
    outpath = ascdir + '\TiffFile'

    # (2) 格式转换
    ascii_file_folder, ascii_file_name = os.path.split(parseFile)
    # 输出文件
    tif_file_path = outpath + '\\' + ascii_file_name.replace('.asc', '.tif')
    with open(parseFile, 'r') as f:
        lines = f.readlines()
        count = 0
        for line in lines:
            if count <= 4:
                ascInfo.append(re.findall("\d+\.?\d*", line))
            count += 1
    # 打开ascii文件
    ds_in = gdal.Open(parseFile)
    gtiff_driver = gdal.GetDriverByName('GTiff')  # 启动Gtiff驱动
    ds_out = gtiff_driver.CreateCopy(tif_file_path, ds_in)

    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)
    ds_out.SetProjection(srs.ExportToWkt())

    ds_in = None
    ds_out = None
    gtiff_driver = None  # 关闭驱动

    return tif_file_path


def saveAreaJson(tif_path):
    dataset = gdal.Open(tif_path)  # 打开tif

    # 获取行数列数和地理信息
    # geo_information(0) :左上像素左上角的x坐标。
    # geo_information(1):w - e像素分辨率 / 像素宽度。
    # geo_information(2):行旋转(通常为零)。
    # geo_information(3):左上像素左上角的y坐标。
    # geo_information(4):列旋转(通常为零)。
    # geo_information(5):n - s像素分辨率 / 像素高度(北半球上图像为负值)
    geo_information = dataset.GetGeoTransform()
    col = dataset.RasterXSize  # 438
    row = dataset.RasterYSize  # 671
    band = dataset.RasterCount
    dem = dataset.GetRasterBand(1).ReadAsArray()
    # 获取行列数,对应其经纬度,j对于x坐标
    cols = []
    maxLon = 0
    maxLat = 0
    for y in range(row):  # 行
        for x in range(col):  # 列
            # 有效高程
            if dem[y][x] > 0:
                # 输出经纬度
                lon = geo_information[0] + x * geo_information[1] + y * geo_information[2]
                lat = geo_information[3] + x * geo_information[4] + y * geo_information[5]
                if lon != None:
                    child = [lon, lat]
                    cols.append(child)

                    if lon > maxLon:
                        maxLon = lon
                    if lat > maxLat:
                        maxLat = lat

    minLon = cols[0][0]
    minLat = cols[0][1]

    for col in cols:
        lon = col[0]
        lat = col[1]

        if lon < minLon:
            minLon = lon
        if lat < minLat:
            minLat = lat

    areaJson = {
        "centerLon": ascInfo[2][0],
        "centerLat": ascInfo[3][0],
        "minLon": str(minLon),
        "minLat": str(minLat),
        "maxLon": str(maxLon),
        "maxLat": str(maxLat)
    }
    print("_end", areaJson)

    # 输出的json文件与tif同目录
    with open(tif_path + '.json', 'w', encoding='utf-8') as outfile:
        json.dump(areaJson, outfile, ensure_ascii=False)


if __name__ == "__main__":
    # 将asc转换成tif文件  1.目录 2.文件
    tif_path = ascii2tif(r"E:\project\ASC", r"E:\project\ASC\xx.asc")
    # 通过外部参数传递
    # tifPath = ascii2tif(sys.argv[1], sys.argv[2])
    # 解析tif文件并输出json
    saveAreaJson(tif_path)


你可能感兴趣的:(python,开发语言)