#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, gdal, os
from gdalconst import GA_ReadOnly
from os.path import realpath
from shapely.geometry import LineString #根据坐标点计算该点所在栅格上的值
def get_elevation(x_coord, y_coord, raster, bands, geo_trans):
"""
get the elevation value of each pixel under
location x, y
:param x_coord: x coordinate
:param y_coord: y coordinate
:param raster: gdal raster open object
:param bands: number of bands in image
:param gt: raster limits
:return: elevation value of raster at point x,y
"""
elev_list = []
x_origin = geo_trans[0]
y_origin = geo_trans[3]
pix_width = geo_trans[1]
pix_height = geo_trans[5]
#计算栅格点的位置
x_pt = int((x_coord - x_origin) / pix_width)
y_pt = int((y_coord - y_origin) / pix_height)
for band_num in range(bands):
ras_band = raster.GetRasterBand(band_num + 1)
#计算栅格点的值
ras_data = ras_band.ReadAsArray(x_pt, y_pt, 1, 1)
elev_list.append(ras_data[0][0])
return elev_list def write_to_csv(csv_out, profil_x_z):
# check if output file exists on disk if yes delete it
if os.path.isfile(csv_out):
os.remove(csv_out) # create new CSV file containing X (distance) and Z value pairs
with open(csv_out, 'a') as outfile:
# write first row column names into CSV
outfile.write("distance,elevation" + "\n")
# loop through each pair and write to CSV
for x, z in profil_x_z:
outfile.write(str(round(x, 2)) + ',' + str(round(z, 2)) + '\n') if __name__ == '__main__':
# set directory
in_dem = realpath("../geodata/dem_3857.dem") # open the image
ds = gdal.Open(in_dem, GA_ReadOnly) if ds is None:
print('Could not open image')
sys.exit(1) # get raster bands
bands = ds.RasterCount # get georeference info
transform = ds.GetGeoTransform() # line defining the the profile
line = LineString([(-13659328.8483806, 6450545.73152317), (-13651422.7820022, 6466228.25663444)])
# length in meters of profile line
length_m = line.length # lists of coords and elevations
x = []
y = []
z = []
# distance of the topographic profile
distance = []
#每隔20取一个点
for curent_dist in range(0, int(length_m), 20):
#利用线性分段计算点的坐标
# creation of the point on the line
point = line.interpolate(curent_dist)
xp, yp = point.x, point.y
x.append(xp)
y.append(yp)
#获取该点的高程值
# extraction of the elevation value from the MNT
z.append(get_elevation(xp, yp, ds, bands, transform)[0])
distance.append(curent_dist) print (x)
print (y)
print (z)
print (distance) # combine distance and elevation vales as pairs
profile_x_z = zip(distance, z) csv_file = os.path.realpath('../geodata/output_profile.csv')
# output final csv data
write_to_csv(csv_file, profile_x_z)

计算DEM上的Profile图的更多相关文章

  1. 相机拍的图,电脑上画的图,word里的文字,电脑屏幕,手机屏幕,相机屏幕显示大小一切的一切都搞明白了!

    相机拍的图,电脑上画的图,word里的文字,电脑屏幕,手机屏幕,相机屏幕显示大小一切的一切都搞明白了! 先说图片X×dpi=点数dotX是图片实际尺寸,简单点,我们只算图片的高吧,比如说拍了张图片14 ...

  2. java 计算地球上两点间距离

    /** * 计算地球上任意两点(经纬度)距离 * * @param long1 * 第一点经度 * @param lat1 * 第一点纬度 * @param long2 * 第二点经度 * @para ...

  3. JAVA 计算地球上任意两点(经纬度)距离

    /** * 计算地球上任意两点(经纬度)距离 * * @param long1 * 第一点经度 * @param lat1 * 第一点纬度 * @param long2 * 第二点经度 * @para ...

  4. OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 6.在计算节点上安装并配置计算服务Nova

    安装和配置计算节点这个章节描述如何在计算节点上安装和配置计算服务. 计算服务支持几种不同的 hypervisors.为了简单起见,这个配置在计算节点上使用 :KVM <kernel-based ...

  5. vue 弹性布局 实现长图垂直居上,短图垂直居中

    vue 弹性布局 实现长图垂直居上,短图垂直居中 大致效果如下图,只考虑垂直方向.长图可以通过滚动条看,短图居中效果,布局合理 html代码(vue作用域内): <div class=" ...

  6. 帝国CMS7.2新增多图同时上传插件,上传多图效率更高

    原来上传多图文件,需要挨个选择文件,然后再点批量上传,比较麻烦.所以帝国CMS7.2新增了多图上传插件:为采用FLASH方式实现同时选择多个图片一起上传,提高多图上传效率. 帝国CMS多图上传插件特性 ...

  7. 计算地球上两个坐标点(经度,纬度)之间距离sql函数

    go --计算地球上两个坐标点(经度,纬度)之间距离sql函数 --作者:lordbaby --整理:www.aspbc.com CREATE FUNCTION [dbo].[fnGetDistanc ...

  8. 计算地图上两点间的距离PHP类

    计算地图上两点间的距离,使用的是谷歌地图 <?php class GeoHelper { /** * @param int $lat1 * @param int $lon1 * @param i ...

  9. 在IOS中根据圆心坐标、半径和角度计算圆弧上的点坐标

    /** 日期:2015-10-15 版本: 1.0.0 -------------------------------------------------------------- 功能说明 ---- ...

随机推荐

  1. TortoiseSVN安装以及淘宝 TAE SDK 开发环境的搭建

    一.TortoiseSVN 的下载和安装 1.进入TortoiseSVN 官网下载地址http://tortoisesvn.net/downloads.html,根据自己的操作系统位数下载相应最新版本 ...

  2. Testing - 测试基础 - 自动

    自动化测试模型 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块. 自动化测试框架在发展的过程中,不断有新的模型(概念)被提出,目 ...

  3. 使用 SVG 实现一个漂亮的页面预加载效果

    今天我们要为您展示如何使用 CSS 动画, SVG 和 JavaScript 创建一个简单的页面预加载效果.对于网站来说,这些预载入得画面提供了一种创造性的方法,使用户在等待内容加载的时候不会那么无聊 ...

  4. 微信开发之.Net

    撸主是一个新手,最近几天在研究微信服务号交互,上网搜了搜C#的代码,再结合自己的习惯,下面把代码解析一下,其中有些代码非本人原创. 首先,你要有个公众服务号,只有服务号才可以自定义菜单,没有条件的可以 ...

  5. 在.NET Core程序中设置全局异常处理

    以前我们想设置全局异常处理只需要这样的代码: AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledExc ...

  6. rtf格式的一些说明,转载的

    RTF是Rich TextFormat的缩写,意即多文本格式.这是一种类似DOC格式(Word文档)的文件,有很好的兼容性,使用Windows"附件"中的"写字板&quo ...

  7. 解决 DotNetCore.1.0.1-VS2015Tools.Preview2.0.3.exe 在VS2015 Update3 安装失败的问题

    今天抽空升级VS2015 Update3. 在安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.3.exe 时报错了,看了错误日志 显示: 看到我标红的两个地方,那么 ...

  8. makefile变量赋值

    在定义变量的值时,我们可以使用其它变量来构造变量的值,在Makefile中有两种方式来在用变量定义变量的值. 先看第一种方式,也就是简单的使用“=”号,在“=”左侧是变量,右侧是变量的值,右侧变量的值 ...

  9. Base64 字符串转图片 问题整理汇总

    前言 最近碰到了一些base64字符串转图片的开发任务,开始觉得没啥难度,但随着开发的进展还是发现有些东西需要记录下. Base64 转二进制 这个在net有现有方法调用: Convert.FromB ...

  10. jquery内容选择器(匹配内容为空的元素)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...