#!/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. [转载]基于TFS实践敏捷-项目管理

    最近,一直想让团队加入敏捷开发,但TFS2010用下来,并不是令人满意,还好,TFS2012横空出世了.相比TFS2010,TFS2012改进太多了,主要体现在以下方面: Team Web Acces ...

  2. EnjoyCSS – 在线的,先进的 CSS3 代码生成器

    EnjoyCSS 是一款先进的 CSS3 代码生成工具,可以让你摆脱日常的编码.它方便和易于使用的用户界面允许您快速,无需编码就可以调节出丰富的图形样式.您将能够玩转所有的 EnjoyCSS 参数,就 ...

  3. springmvc学习笔记--json--返回json的日期格式问题

    (一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...

  4. JS魔法堂:ASI(自动分号插入机制)和前置分号

    一.前言 今晚在知乎看到百姓网前端技术专家——贺师俊对<JavaScript 语句后应该加分号么?>的回答,让我又一次看到大牛的风采,实在佩服万分.但单纯的敬佩是不足以回报他如此优秀的文字 ...

  5. nodejs学习篇 (1)webstorm创建nodejs + express + jade 的web 项目

    之前简单了解过nodejs,觉得用nodejs来做个网站也太麻烦了,要自己拼html的字符串返回,这能做网站嘛? 最近看到使用jade模板来开发,觉得挺新奇的,于是试了一把,也了解了一些特性,算是个新 ...

  6. ASP.NET Core 1.0到底是什么?

    ASP.NET Core1.0 是微软的下一代ASP.NET框架.我们将ASP.NET升级到最新版本我们会发现最高级的版本是ASP.NET 4.6对应的FrameWork版本是4.6.1. 那么很多人 ...

  7. log4net的配置详解

    log4net是一款优秀的第三方日志框架,可以很容易的加载到开发项目中(引用log4net的dll,再配置些基本参数即可),帮助程序员把日志信息输出到各种不同的目标,常见的有文本.数据库.window ...

  8. C#去掉HTML标记

    该方法亲测可行,下面直接粘贴代码. public string RemoveHTMLTags(string htmlStream) { if (htmlStream == null) { throw ...

  9. 【iOS】Object-C注释

    与其他语言一样,Object-C的注释也分为两种,一种是普通注释,一种是文档注释,普通注释通常给阅读代码的人看,而文档注释可以被appledoc识别,在使用的时候xcode能给出智能提示 简单注释 1 ...

  10. exception throw in progress runner thread_VS2015中SVN源代码无说明提交异常

    1-问题描述:通过SVN将代码变更提交服务器时,你可能会遇到以下异常:  exception throw in progress runner thread 2-解决办法: SVN代码源代码管理和TF ...