Skip to content

Commit

Permalink
修复GDAL转3dtiles坐标不对问题
Browse files Browse the repository at this point in the history
  • Loading branch information
scially committed Nov 18, 2022
1 parent 4b231d9 commit 8490a4a
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 261 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
cmake_minimum_required (VERSION 3.8)
project(Cesium3DTilesConverter VERSION 3.0.0)
project(Cesium3DTilesConverter VERSION 2.1.0)

configure_file (
"${PROJECT_SOURCE_DIR}/include/Config.h.in"
"${PROJECT_SOURCE_DIR}/include/Config.h"
)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_CXX_STANDARD 17)
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# About Project

基于C++17、Qt5的3DTiles 转换工具集。

# 简介
Expand All @@ -17,20 +18,20 @@
## 命令行格式

```sh
Converter.exe --format <FORMAT> [OPTIONS] <INPUT> <OUTPUT>
Converter -f <FORMAT> [OPTIONS] <INPUT> <OUTPUT>
```

## 示例命令

```sh
# from osgb dataset
Converter -f OSGB --yUpAxis <OSGB Path> <OUTPUT DIR>
Converter -f OSGB --yUpAxis <OSGB> <OUTPUT>

# from single shp file
Converter -f GDAL --field height <Shapefile Path> <OUTPUT DIR>
# from shp file
Converter -f GDAL --field height <Shapefile> <OUTPUT>

# from gdb file
Converter -f GDAL --field height --layer <Layer Name> <GDB Path> <OUTPUT DIR>
Converter -f GDAL --field height --layer <Layer> <GDB> <OUTPUT>
```

## 参数说明
Expand All @@ -52,8 +53,9 @@ Options:
-H, --height <height> height offset(default value 0), OSGB高度偏移字段
<INPUT PATH> 输入数据的目录,OSGB数据截止到 `<DIR>/Data` 目录的上一级,GDAL参考GDAL数据格式。
<OUTPUT DIR> 输出目录。OSGB转换的3DTiles输出的数据文件位于 <DIR>/Data`目录, GDAL转换的3DTiles输出的数据文件位于<DIR>/Tile目录,tileset.json位于<DIR>根目录。
<INPUT> 输入数据的目录,OSGB数据截止到 `<DIR>/Data` 目录的上一级,GDAL参考GDAL数据格式。
<OUTPUT> 输出目录。OSGB转换的3DTiles输出的数据文件位于 <DIR>/Data`目录, GDAL转换的3DTiles输出的数据文件位于<DIR>/Tile目录,tileset.json位于<DIR>根目录。
```

# 数据要求及说明
Expand Down
8 changes: 8 additions & 0 deletions include/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#define PROJECT_VERSION_MAJOR 2
#define PROJECT_VERSION_MINOR 1
#define PROJECT_VERSION_PATCH 0

#define PROJECT_VERSION "2.1.0"
#define PROJECT_NAME "Cesium3DTilesConverter"
8 changes: 8 additions & 0 deletions include/Config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#define PROJECT_VERSION_MAJOR @Cesium3DTilesConverter_VERSION_MAJOR@
#define PROJECT_VERSION_MINOR @Cesium3DTilesConverter_VERSION_MINOR@
#define PROJECT_VERSION_PATCH @Cesium3DTilesConverter_VERSION_PATCH@

#cmakedefine PROJECT_VERSION "${Cesium3DTilesConverter_VERSION}"
#cmakedefine PROJECT_NAME "${PROJECT_NAME}"
2 changes: 1 addition & 1 deletion include/CoordinateConvert.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <GDALWrapper.h>
#include <OGRException.h>

#include <osg/Math>
namespace scially {

Expand Down
133 changes: 89 additions & 44 deletions include/GDALWrapper.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once

#include <OGRException.h>

#include <ogrsf_frmts.h>

#include <QDebug>
#include <QCoreApplication>
#include <QtDebug>
#include <QException>
#include <QSharedPointer>

namespace scially {
Expand All @@ -29,64 +28,111 @@ namespace scially {
std::string projData;
};

class OGRException : QException {
public:
OGRException(const QString& err) : err_(err) {}
OGRException(OGRErr err) {
switch (err) {
case OGRERR_NOT_ENOUGH_DATA:
err_ = "[gdal/ogr] not enough data";
break;
case OGRERR_NOT_ENOUGH_MEMORY:
err_ = "[gdal/ogr] not enough memory";
break;
case OGRERR_UNSUPPORTED_GEOMETRY_TYPE:
err_ = "[gdal/ogr] unsupported geometry type";
break;
case OGRERR_UNSUPPORTED_OPERATION:
err_ = "[gdal/ogr] unsupported operation";
break;
case OGRERR_CORRUPT_DATA:
err_ = "[gdal/ogr] corrupt data";
break;
case OGRERR_FAILURE:
err_ = "[gdal/ogr] failure";
break;
case OGRERR_UNSUPPORTED_SRS:
err_ = "[gdal/ogr] unsupported srs";
break;
case OGRERR_INVALID_HANDLE:
err_ = "[gdal/ogr] invalid handle";
break;
case OGRERR_NON_EXISTING_FEATURE:
err_ = "[gdal/ogr] non existing feataure";
break;
default:
err_ = "[gdal/ogr] unkonwn error";
}
}
virtual void raise() const override {
throw* this;
}
virtual OGRException* clone() const override {
return new OGRException(*this);
}

QString error() const noexcept {
return err_;
}

private:
QString err_;
};

class OGRFeatureWrapper;
class OGRLayerWrapper;
class GDALDatasetWrapper;


class OGRFeatureWrapper {
public:
friend class OGRLayerWrapper;

OGRFeatureWrapper() = default;
OGRGeometry* GetGeometryRef() {
return feature->GetGeometryRef();
return feature_->GetGeometryRef();
}

double GetFieldAsDouble(GIntBig fid) {
return feature->GetFieldAsDouble(fid);
return feature_->GetFieldAsDouble(fid);
}

bool isValid() const {
return feature != nullptr;
return feature_ != nullptr;
}

GIntBig GetFID() {
return feature->GetFID();
return feature_->GetFID();
}

private:
explicit OGRFeatureWrapper(OGRFeature* f) {
feature = QSharedPointer<OGRFeature>(f, &OGRFeature::DestroyFeature);
feature_.reset(f);
}

QSharedPointer<OGRFeature> feature;
QSharedPointer<OGRFeature> feature_;
};


class OGRLayerWrapper {
public:
friend class GDALDatasetWrapper;


OGRLayerWrapper() = default;
OGRFeatureWrapper GetNextFeature() {
OGRFeature* feature = layer->GetNextFeature();
OGRFeature* feature = layer_->GetNextFeature();
return OGRFeatureWrapper(feature);
}

OGREnvelope GetExtent(int bForce = 1) {
OGREnvelope envelope;
OGRErr err = layer->GetExtent(&envelope, bForce);
OGRErr err = layer_->GetExtent(&envelope, bForce);
if (err != OGRERR_NONE)
throw OGRException(err);

return envelope;
}

OGRFeatureWrapper GetFeature(GIntBig fid) {
OGRFeature* feature = layer->GetFeature(fid);
if (feature == nullptr) {
throw OGRException(QString("No feature ") + fid + "in Layer");
}
OGRFeature* feature = layer_->GetFeature(fid);
return OGRFeatureWrapper(feature);
}

Expand All @@ -95,48 +141,45 @@ namespace scially {
}

void ResetReading() {
layer->ResetReading();
layer_->ResetReading();
}

OGRSpatialReference* GetSpatialRef() {
return layer->GetSpatialRef();
return layer_->GetSpatialRef();
}

OGRFeatureDefn* GetLayerDefn() {
return layer->GetLayerDefn();
return layer_->GetLayerDefn();
}

private:
explicit OGRLayerWrapper(OGRLayer* layer) : layer(layer) {}
OGRLayer* layer; // will auto dispose when ds close.
explicit OGRLayerWrapper(OGRLayer* layer) : layer_(layer) {}
OGRLayer* layer_; // will auto dispose when ds close.
};

class GDALDatasetWrapper {
public:
friend class OGRLayerWrapper;
static inline GDALDatasetWrapper open(const char* pszFilename,
unsigned int nOpenFlags,
const char* const* papszAllowedDrivers = nullptr,
const char* const* papszOpenOptions = nullptr,
const char* const* papszSiblingFiles = nullptr) {
GDALDataset* dataset = (GDALDataset*)GDALOpenEx(pszFilename,
GDALDatasetWrapper() = default;

bool open(const QString &fileName, unsigned int nOpenFlags) {
GDALDataset* dataset = (GDALDataset*)GDALOpenEx(fileName.toStdString().c_str(),
nOpenFlags,
papszAllowedDrivers,
papszOpenOptions,
papszSiblingFiles);
if (dataset == nullptr)
throw OGRException(QString("Can't open dataset %1").arg(pszFilename));

return GDALDatasetWrapper(dataset);
nullptr,
nullptr,
nullptr);
ds_.reset(dataset);
return ds_ != nullptr;
}

OGRLayerWrapper GetLayer(int iLayer) {
OGRLayer* layer = ds->GetLayer(iLayer);
OGRLayer* layer = ds_->GetLayer(iLayer);
return OGRLayerWrapper(layer);
}

OGRLayerWrapper GetLayerByName(const char* nLayer) {
OGRLayer* layer = ds->GetLayerByName(nLayer);
OGRLayerWrapper GetLayerByName(const QString& nLayer) {
OGRLayer* layer = ds_->GetLayerByName(nLayer.toStdString().c_str());

return OGRLayerWrapper(layer);
}
bool isValid() const {
Expand All @@ -145,15 +188,17 @@ namespace scially {

private:
struct GDALDatasetDeleteWrapper {
void operator() (GDALDataset* dataset) {
void operator()(GDALDataset* dataset) {
if (dataset != nullptr)
GDALClose(dataset);
}
};
// dataset 将被 GDALDatasetWrapper 接管
explicit GDALDatasetWrapper(GDALDataset* dataset) {
ds = QSharedPointer<GDALDataset>(dataset, GDALDatasetDeleteWrapper());

GDALDatasetWrapper(GDALDataset* dataset) {
Q_ASSERT(dataset != nullptr);
ds_.reset(dataset, GDALDatasetDeleteWrapper());
}
QSharedPointer<GDALDataset> ds;

QSharedPointer<GDALDataset> ds_;
};
}
53 changes: 0 additions & 53 deletions include/OGRException.h

This file was deleted.

Loading

0 comments on commit 8490a4a

Please sign in to comment.