# -*- coding: utf-8 -*-
"""
Created on Fri Nov 30 11:45:03 2018 @author: Administrator
""" from osgeo import gdal
from osgeo import osr
import numpy as np
import math
import time lonMeter = 0.00001141
latMeter = 0.00000899 #MeterParam = 0.00001 * 42496 / (124.44282531738276-124.3288421630859)
MeterParam = 3.7282702222226876 def getSRSPair(dataset):
'''
获得给定数据的投影参考系和地理参考系
:param dataset: GDAL地理数据
:return: 投影参考系和地理参考系
'''
prosrs = osr.SpatialReference()
prosrs.ImportFromWkt(dataset.GetProjection())
geosrs = prosrs.CloneGeogCS()
return prosrs, geosrs def geo2lonlat(dataset, x, y):
'''
将投影坐标转为经纬度坐标(具体的投影坐标系由给定数据确定)
:param dataset: GDAL地理数据
:param x: 投影坐标x
:param y: 投影坐标y
:return: 投影坐标(x, y)对应的经纬度坐标(lon, lat)
'''
prosrs, geosrs = getSRSPair(dataset)
ct = osr.CoordinateTransformation(prosrs, geosrs)
coords = ct.TransformPoint(x, y)
return coords[:2] def lonlat2geo(dataset, lon, lat):
'''
将经纬度坐标转为投影坐标(具体的投影坐标系由给定数据确定)
:param dataset: GDAL地理数据
:param lon: 地理坐标lon经度
:param lat: 地理坐标lat纬度
:return: 经纬度坐标(lon, lat)对应的投影坐标
'''
prosrs, geosrs = getSRSPair(dataset)
ct = osr.CoordinateTransformation(geosrs, prosrs)
coords = ct.TransformPoint(lon, lat)
return coords[:2] def imagexy2geo(dataset, row, col):
'''
根据GDAL的六参数模型将影像图上坐标(行列号)转为投影坐标或地理坐标(根据具体数据的坐标系统转换)
:param dataset: GDAL地理数据
:param row: 像素的行号
:param col: 像素的列号
:return: 行列号(row, col)对应的投影坐标或地理坐标(x, y)
'''
trans = dataset.GetGeoTransform()
px = trans[0] + col * trans[1] + row * trans[2]
py = trans[3] + col * trans[4] + row * trans[5]
return px, py def geo2imagexy(dataset, x, y):
'''
根据GDAL的六 参数模型将给定的投影或地理坐标转为影像图上坐标(行列号)
:param dataset: GDAL地理数据
:param x: 投影或地理坐标x
:param y: 投影或地理坐标y
:return: 影坐标或地理坐标(x, y)对应的影像图上行列号(row, col)
'''
trans = dataset.GetGeoTransform()
a = np.array([[trans[1], trans[2]], [trans[4], trans[5]]])
b = np.array([x - trans[0], y - trans[3]])
return np.linalg.solve(a, b) # 使用numpy的linalg.solve进行二元一次方程的求解 def imagexy2lonlat(dataset,row, col):
'''
影像行列转经纬度:
:通过影像行列转平面坐标
:平面坐标转经纬度
'''
coords = imagexy2geo(dataset, row, col)
coords2 = geo2lonlat(dataset,coords[0], coords[1])
return (coords2[0], coords2[1]) def lonlat2imagexy(dataset,x, y):
'''
影像行列转经纬度:
:通过经纬度转平面坐标
:平面坐标转影像行列
'''
coords = lonlat2geo(dataset, x, y)
coords2 = geo2imagexy(dataset,coords[0], coords[1])
return (int(round(abs(coords2[0]))), int(round(abs(coords2[1])))) if __name__ == '__main__':
gdal.AllRegister()
dataset = gdal.Open(r"D:\RSData\DAQING_SHAERTU\萨尔图区_大图:拼接\L19.tif") print('数据投影:')
projection = dataset.GetProjection()
print(projection) print('数据的大小(行,列):')
print('(%s %s)' % (dataset.RasterYSize, dataset.RasterXSize)) geotransform = dataset.GetGeoTransform()
print('地理坐标:')
print(geotransform) x = 464201
y = 5818760
lon = 122.47242
lat = 52.51778
row = 0
col = 0 # print('投影坐标 -> 经纬度:')
# coords = geo2lonlat(dataset, x, y)
# print('(%s, %s)->(%s, %s)' % (x, y, coords[0], coords[1]))
#
# print('经纬度 -> 投影坐标:')
# coords = lonlat2geo(dataset, lon, lat)
# print('(%s, %s)->(%s, %s)' % (lon, lat, coords[0], coords[1]))
#
# print('图上坐标 -> 投影坐标:')
# coords = imagexy2geo(dataset, row, col)
# print('(%s, %s)->(%s, %s)' % (row, col, coords[0], coords[1]))
#
# print('投影坐标 -> 图上坐标:')
# coords = geo2imagexy(dataset, x, y)
# print('(%s, %s)->(%s, %s)' % (x, y, coords[0], coords[1])) # print('图上坐标 -> 投影坐标:')
# coords = imagexy2geo(dataset, row, col)
# print('(%s, %s)->(%s, %s)' % (row, col, coords[0], coords[1]))
# print('投影坐标 -> 经纬度:')
# coords2 = geo2lonlat(dataset,coords[0], coords[1])
# print('(%s, %s)->(%s, %s)' % (coords[0], coords[1], coords2[0], coords2[1])) # coords = imagexy2lonlat(dataset, row, col)
# print('影像像素 -> 经纬度:')
# print('(%s, %s)->(%s, %s)' % ( row, col, coords[0], coords[1]))
# coords = imagexy2lonlat(dataset, dataset.RasterXSize, dataset.RasterYSize)
# print('影像像素 -> 经纬度:')
# print('(%s, %s)->(%s, %s)' % ( dataset.RasterXSize, dataset.RasterYSize, coords[0], coords[1]))
#
# coords = lonlat2imagexy(dataset, 124.3288421630859, 46.391464001559044)
# print('经纬度 -> 影像像素 :')
# print('(%s, %s)->(%s, %s)' % ( 124.3288421630859, 46.391464001559044, coords[0], coords[1]))
# coords = lonlat2imagexy(dataset, 124.44282531738276, 46.32796494040744)
# print('经纬度 -> 影像像素 :')
# print('(%s, %s)->(%s, %s)' % ( 124.44282531738276, 46.32796494040744, coords[0], coords[1])) #经纬度转像素
xoffset=0
yoffset=0 x,y = 125.059,46.894 xoffset,yoffset = lonlat2imagexy(dataset, x,y)
print('坐标转换-对应行列像素位置')
print('(%s, %s)->(%s, %s)' % (x,y, xoffset,yoffset)) width=int(500 * MeterParam)
height=int(500 * MeterParam) if xoffset - width <= 0 and yoffset - height <= 0 :
print("左上角")
xoffset = 0
yoffset = 0
elif xoffset - width <= 0 and yoffset - height > 0 :
print("左边")
xoffset = 0
elif xoffset - width > 0 and yoffset - height <= 0 :
print("顶边")
yoffset = 0
else :
print("中间区域")
xoffset = xoffset - width
yoffset = yoffset - height width = width * 2
height = height * 2 print('切割范围')
print('宽高(%s, %s)->偏移起点(%s, %s)' % (width, height, xoffset,yoffset))
# xoffset,yoffset,width,height = 175360/2,123136/2,1000,1000 newData = np.zeros([width,height,3])
band = dataset.GetRasterBand(1)
r=band.ReadAsArray(xoffset,yoffset,width,height)
NoData = band.GetNoDataValue()
newData[:,:,0] = r band = dataset.GetRasterBand(2)
g=band.ReadAsArray(xoffset,yoffset,width,height) band = dataset.GetRasterBand(3)
b=band.ReadAsArray(xoffset,yoffset,width,height) ticks = time.time()
resultPath = "D:\\RS%s.jpg" % (int(ticks)) newData[:,:,0] = r
newData[:,:,1] = g
newData[:,:,2] = b format = "GTiff"
driver = gdal.GetDriverByName(format)
ds = driver.Create(resultPath, width, height, 3, gdal.GDT_Float32)
geotransform1 = geotransform
px = geotransform[0] + xoffset * geotransform[1] + yoffset * geotransform[2]
py = geotransform[3] + xoffset * geotransform[4] + yoffset * geotransform[5]
geotransform1 = (px, 0.29858214173896974, 0.0, py, 0.0, -0.29858214173896974)
# print(geotransform1[0])
ds.SetGeoTransform(geotransform1)
ds.SetProjection(projection)
lay01= ds.GetRasterBand(1)
lay02= ds.GetRasterBand(2)
lay03= ds.GetRasterBand(3)
# ds.GetRasterBand(1).SetNoDataValue(0)
# ds.GetRasterBand(2).SetNoDataValue(0)
# ds.GetRasterBand(3).SetNoDataValue(0)
lay01.WriteArray(b)
lay02.WriteArray(g)
lay03.WriteArray(r)
# ds.FlushCache()
# ds = None
del ds import cv2
import matplotlib.pyplot as plt img2=cv2.merge([r,g,b])
plt.imshow(img2)
plt.xticks([]),plt.yticks([]) # 不显示坐标轴
plt.show() ticks = time.time()
# cv2.imwrite("D:\\RS%s.jpg" % (int(ticks)) , img2) print("OK")

转自:https://blog.csdn.net/theonegis/article/details/54427906

【GIS】GDAL Python 影像裁剪的更多相关文章

  1. GDAL python教程(1)——用OGR读写矢量数据

    本教程的讲义和源码都是取自Utah State University的openGIS课程 相关资料,包括讲义.源码.数据样例,请从此处下载http://www.gis.usu.edu/~chrisg/ ...

  2. ArcGIS + Python 批量裁剪、添加X/Y坐标脚本

    前言 前一段时间,同事拿来的数据范围太大,用不了那么多(只需要一个乡镇的,结果拿来区县的),太多了加载也是问题.所以就让我给处理下. 由于文件较多,手动裁剪的话,我一个一个用ArcGIS工具箱中的工具 ...

  3. ARCGIS多种影像裁剪

    在互联网上下载的遥感影像都进行过分幅处理,下载下来的影像多是规则的四方形,而在进行遥感影像研究时,多是针对特定区域来进行,比如研究北京市的遥感影像,不在北京市范围内的影像对于研究者就没有利用意义,如果 ...

  4. AE + GDAL实现影像按标准图幅分割(上)

    最近有个项目,其中有个功能是要将遥感影像按标准图幅分割,一开始用AE的接口,慢的让人抓狂,就改用GDAL,速度提升很大.我主要通过http://blog.csdn.net/liminlu0314/学习 ...

  5. C#+GDAL读取影像(1)

    环境:VS2010,C#,GDAL1.7 读取影像: using System; using System.Collections.Generic; using System.ComponentMod ...

  6. GDAL读取影像并插值

    影像读取 并缩放 读取大影像某一部分,并缩放到指定大小,我们有时会用如下代码: #include "gdal.h" #include "gdal_priv.h" ...

  7. 利用GDAL实现影像的几何校正

    一.概述 遥感影像和地理坐标进行关联的方式一般有好几种,一种是直接给出了仿射变换系数,即6个參数,左上角地理坐标,纵横方向上的分辨率,以及旋转系数.在这样的情况下,求出某一像素点的地理坐标非常easy ...

  8. Ubuntu中安装gdal python版本

    安装过程: python包是从C++包中编译出来的,所以需要将源码下载进行编译安装 1.GDAL中的矢量数据处理OGR依赖于Geos,在安装GDAL之前要安装Geos Geos的下载地址:http:/ ...

  9. AE + GDAL实现影像按标准图幅分割(下)

    在上篇实现了遥感影像的切割,本篇讲切割前的准备.主要分为以下几步: (1)将影像的投影坐标转换为地理坐标,以便于之后的图幅划分.AE坐标转换函数如下 private bool Proj2Geo(ISp ...

随机推荐

  1. 开始一段新的敏捷学习之旅 —— IT帮读书会第4期《Scrum实战》

    刚看了一下,距离上一次写博客过去快1年半了.之前的知识管理都放到笔记软件中了,但是现在看来,收藏了很多东西,输入很多,但是输出有限. 学习任何领域的知识,如果只有输入没有输出,效果都是很有限的,有时需 ...

  2. 17个CSS知识点整理

    1.对WEB标准以及W3C的理解与认识 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率:使用外链css和js脚本.结构行为表现的分离.文件下载与页面速度更快:内容能被更多的用户所访问.内容能被更 ...

  3. 安卓程序代写 网上程序代写[原]BluetoothAdapter解析

    这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每个常量含义. 一 BluetoothAdapter简介 1.继承关系 该类仅继承了Object类; 2.该类作用 ...

  4. "add-apt-repository" On Linux

    在 Ubuntu 9.10以后 我们可以使用 "add-apt-repository" 脚本添加 ppa 到当前的库中并且自动导入公钥. 再终端下使用下面的语法: add-apt- ...

  5. Java 之 File

    11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...

  6. 第三百六十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索功能

    第三百六十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索功能 Django实现搜索功能 1.在Django配置搜索结果页的路由映 ...

  7. elasticsearch系列四:搜索详解(搜索API、Query DSL)

    一.搜索API 1. 搜索API 端点地址 从索引tweet里面搜索字段user为kimchy的记录 GET /twitter/_search?q=user:kimchy 从索引tweet,user里 ...

  8. Java如何格式化24小时格式的时间?

    在Java中,如何格式化24小时格式的时间?? 此示例使用SimpleDateFormat类的sdf.format(date)方法将时间格式化为24小时格式(00:00-24:00). package ...

  9. Spring JDBC RowMapper接口示例

    JdbcTemplate类使用org.springframework.jdbc.core.RowMapper <T>接口在每行的基础上映射ResultSet的行.该接口的实现执行将每行映射 ...

  10. Spring JDBC对象批量操作

    以下示例将演示如何使用spring jdbc中的对象进行批量更新.我们将在单次批次操作中更新student表中的记录. student表的结果如下 - CREATE TABLE student( id ...