GDALDataset的创建和销毁
之前在GDALDestroyDriverManager 分析中没有看到对dGDALDatasert的回收。先看一个例子程序,这个例子打开了一个tif文件,读取了一些基本信息。
为了简单示范,没有写成C++的各种类分散到各个文件中。
#include "stdafx.h"
#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <boost/cstdint.hpp> using namespace std; void print_line() {
cout << "----------------------" << endl;
} void printDataType(GDALDataType type) {
if (type == GDALDataType::GDT_Byte) {
cout << "GDALDataType: GDT_Byte" << endl;
} else {
cout << "GDALDataType: GDT_Unknown" << endl;
}
} void printAccess(GDALAccess access) {
if (access == GDALAccess::GA_ReadOnly) {
cout << "GDALAccess: GA_ReadOnly" << endl;
} else {
cout << "GDALAccess: GA_Update" << endl;
}
} void printCategoryNames(char** names) {
if (names == NULL) {
cout << "non category names" << endl;
return;
}
size_t i = 0;
char* str = names[i];
while(str != "") {
cout << str << endl;
str = names[i];
}
} void printColorInterp(GDALColorInterp interp) {
switch(interp) {
case GDALColorInterp::GCI_AlphaBand:
cout << "GDALColorInterp: GCI_AlphaBand" << endl;
break;
case GDALColorInterp::GCI_BlackBand:
cout << "GDALColorInterp: GCI_BlackBand" << endl;
break;
case GDALColorInterp::GCI_RedBand:
cout << "GDALColorInterp: GCI_RedBand" << endl;
break;
case GDALColorInterp::GCI_GreenBand:
cout << "GDALColorInterp: GCI_GreenBand" << endl;
break;
case GDALColorInterp::GCI_BlueBand:
cout << "GDALColorInterp: GCI_BlueBand" << endl;
break;
default:
cout << "GDALColorInterp: GCI_Undefined: " << interp << endl;
}
} void printColorTable(GDALColorTable* table) {
if (table == NULL) {
cout << "color table is null" << endl;
return;
} cout << "ColorEntryCount: " << table->GetColorEntryCount() << endl;
} string getBytesAsHexString(uint8_t * data, size_t size) {
stringstream stream;
stream << std::hex << std::uppercase << std::setfill('0');
for (size_t i = 0; i < size; ++i) {
stream <<std::setw(2) << static_cast<uint32_t>(data[i] & 0xff) << " ";
}
return stream.str();
} void printBand(GDALRasterBand* band) {
cout << "XSize: " << band->GetXSize() << endl;
cout << "YSize: " << band->GetYSize() << endl;
cout << "Band number(index for itself): " << band->GetBand() << endl;
printDataType(band->GetRasterDataType());
int x_size = 0;
int y_size = 0;
band->GetBlockSize(&x_size, &y_size);
cout << "Band Block size, x size: " << x_size << " pixels , y size: " << y_size << " pixels" << endl;
printAccess(band->GetAccess());
printCategoryNames(band->GetCategoryNames());
cout << "no data value: " << band->GetNoDataValue() << endl;
cout << "minimum value: " << band->GetMinimum() << endl;
cout << "maximum value: " << band->GetMaximum() << endl;
cout << "offset value: " << band->GetOffset() << endl;
cout << "scale value: " << band->GetScale() << endl;
cout << "unit type: " << band->GetUnitType() << endl;
printColorInterp(band->GetColorInterpretation());
printColorTable(band->GetColorTable()); GByte * data = static_cast<GByte*>(CPLMalloc(x_size * y_size));
band->ReadBlock(5000, 3000, data);
cout << "one block data: " << getBytesAsHexString(data, x_size * y_size) << endl;
} void printTiffInfo(GDALDataset* set) {
print_line();
cout << "Tiff info " << endl;
cout << "RasterXSize: " << set->GetRasterXSize() << endl;
cout << "RasterYSize: " << set->GetRasterYSize() << endl;
size_t band_count = set->GetRasterCount();
cout << "Raster band count: " << band_count << endl;
for (size_t i = 1; i <= band_count; ++i) {
GDALRasterBand* band = set->GetRasterBand(i);
print_line();
cout << "Band " << i << " : " << endl;
printBand(band);
}
} int _tmain(int argc, _TCHAR* argv[])
{ GDALAllRegister();
string data_dir = "../../../study/data/";
string tiffPath = data_dir + "v4_dem_gtopo30/v4_dem.tif";
GDALDataset* dataset = (GDALDataset *) GDALOpen(tiffPath.c_str(), GA_ReadOnly);
if (dataset == NULL) {
cout << "open ttf file in TiffCopy app failed" << endl;
GDALDestroyDriverManager();
exit(1);
} cout << "loading tiff file successfully" << endl;
printTiffInfo(dataset); GDALClose(dataset);
GDALDestroyDriverManager();
return 0;
}
在main函数中,用GDALOpen打开了一个tif文件,并且获得了GDALDataset对象的指针,然后用GDALClose销毁了这个对象。下面是GDALClose代码:
/**
* \brief Close GDAL dataset.
*
* For non-shared datasets (opened with GDALOpen()) the dataset is closed
* using the C++ "delete" operator, recovering all dataset related resources.
* For shared datasets (opened with GDALOpenShared()) the dataset is
* dereferenced, and closed only if the referenced count has dropped below 1.
*
* @param hDS The dataset to close. May be cast from a "GDALDataset *".
*/ void CPL_STDCALL GDALClose( GDALDatasetH hDS ) {
VALIDATE_POINTER0( hDS, "GDALClose" ); GDALDataset *poDS = (GDALDataset *) hDS;
CPLMutexHolderD( &hDLMutex );
CPLLocaleC oLocaleForcer; if (poDS->GetShared())
{
/* -------------------------------------------------------------------- */
/* If this file is in the shared dataset list then dereference */
/* it, and only delete/remote it if the reference count has */
/* dropped to zero. */
/* -------------------------------------------------------------------- */
if( poDS->Dereference() > 0 )
return; delete poDS;
return;
} /* -------------------------------------------------------------------- */
/* This is not shared dataset, so directly delete it. */
/* -------------------------------------------------------------------- */
delete poDS;
}
注释表明GDALDataset支持普通和引用计数管理两种生命周期管理方式。代码中表明在销毁的时候使用了互斥量,因此是线程安全的。
GDALDataset的创建和销毁的更多相关文章
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- 《Effective Java》—— 创建与销毁对象
本篇主要总结的是<Effecticve Java>中关于创建和销毁对象的内容. 比如: 何时以及如何创建对象 何时以及如何避免创建对象 如何确保及时销毁 如何管理对象销毁前的清理动作 考虑 ...
- ch2 创建和销毁对象
ch2 创建和销毁对象
- [Effective Java]第二章 创建和销毁对象
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 《Effect Java》学习笔记1———创建和销毁对象
第二章 创建和销毁对象 1.考虑用静态工厂方法代替构造器 四大优势: i. 有名称 ii. 不必在每次调用它们的时候都创建一个新的对象: iii. 可以返回原返回类型的任何子类型的对象: JDBC ...
- effective java读书小记(一)创建和销毁对象
序言 <effective java>可谓是java学习者心中的一本绝对不能不拜读的好书,她对于目标读者(有一点编程基础和开发经验)的人来说,由浅入深,言简意赅.每一章节都分为若干的条目, ...
- [ Java学习基础 ] Java对象的创建和销毁
类实例化可生成对象,实例方法就是对象方法,实例变量就是对象属性.一个对象的生命周期包括三个阶段:创建.使用和销毁. 创建对象 创建对象包括两个步骤:声明和实例化. 声明 声明对象与声明普通变量没有区别 ...
- JAVA创建和销毁对象
类静态方法取代构造方法创建对象 类静态方法有名称,可以通过名称说明返回的是什么类型的实例 可以控制是否需要新开辟内存空间 返回值是可以控制的 实体类属性非常多的时候使用build模式创建对象 单例实体 ...
- 和我一起学Effective Java之创建和销毁对象
前言 主要学习创建和销毁对象: 1.何时以及如何创建对象 2.何时以及如何避免创建对象 3.如何确保它们能够适时地销毁 4.如何管理对象销毁之前必须进行的清理动作 正文 一.用静态工厂方法代替构造器 ...
随机推荐
- [BZOJ4456] [Zjoi2016]旅行者 分治+最短路
4456: [Zjoi2016]旅行者 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 777 Solved: 439[Submit][Status] ...
- Python BeautifulSoup 简单笔记
Beautiful Soup 是用 Python 写的一个 HTML/XML 的解析器,它可以很好的处理不规范标记并生成剖析树.通常用来分析爬虫抓取的web文档.对于 不规则的 Html文档,也有很多 ...
- 如何直接运行python文件
1. 在Windows上是不能直接运行python文件的,但是,在Mac和Linux上是可以的,方法是在.py文件的第一行加上一个特殊的注释: #!/usr/bin/env python3 print ...
- 【我要学python】愣头青之小数点精度控制
写在最前面:今天遇到了棘手的问题,看了两遍才看懂,本文属于转载+修改,原出处是Herbert's Blog 基础 浮点数是用机器上浮点数的本机双精度(64 bit)表示的.提供大约17位的精度和范围从 ...
- python 写文件write(string), writelines(list) ,读文件
read()方法用于直接读取字节到字符串中,可以接参数给定最多读取的字节数,如果没有给定,则文件读取到末尾. readline()方法读取打开文件的一行(读取下个行结束符之前的所有字节),然后整行,包 ...
- 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序
1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...
- AGC 022 C - Remainder Game
题面在这里! 显然权值是 2^i 这种的话就是要你贪心,高位能不选就不选. 并且如果 % x 之后再去 % 一个>=x的数是没有用的,所以我们可以把操作的k看成单调递减序列. 这样的话就是一个有 ...
- 【BFS】POJ3669-Meteor Shower
[思路] 预处理时先将陨石落到各点的最短时间纪录到数组中,然后在时间允许的范围内进行广搜.一旦到某点永远不会砸到,退出广搜. #include<iostream> #include< ...
- 将ip对应城市数据导入redis并查询
1.GeoLite免费数据库 先去地址http://dev.maxmind.com/zh-hans/geoip/legacy/geolite/#i-5下载GeoLiteCity-latest .zip ...
- [转]spring tx:advice 和 aop:config 配置事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www. ...