/
qgsremoteeptpointcloudindex.cpp
206 lines (170 loc) · 6.57 KB
/
qgsremoteeptpointcloudindex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/***************************************************************************
qgsremoteeptpointcloudindex.cpp
--------------------
begin : March 2021
copyright : (C) 2021 by Belgacem Nedjima
email : belgacem dot nedjima at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsremoteeptpointcloudindex.h"
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTime>
#include <QtDebug>
#include <QQueue>
#include <QTimer>
#include "qgseptdecoder.h"
#include "qgscoordinatereferencesystem.h"
#include "qgspointcloudrequest.h"
#include "qgspointcloudattribute.h"
#include "qgslogger.h"
#include "qgsfeedback.h"
#include "qgsmessagelog.h"
#include "qgstiledownloadmanager.h"
#include "qgsblockingnetworkrequest.h"
#include "qgsfiledownloader.h"
#include "qgsfileutils.h"
#include "qgsapplication.h"
#include "qgspointcloudblockrequest.h"
///@cond PRIVATE
QgsRemoteEptPointCloudIndex::QgsRemoteEptPointCloudIndex() : QgsEptPointCloudIndex() { }
QgsRemoteEptPointCloudIndex::~QgsRemoteEptPointCloudIndex() = default;
QList<IndexedPointCloudNode> QgsRemoteEptPointCloudIndex::nodeChildren( const IndexedPointCloudNode &n ) const
{
// Potential bug: what if loadNodeHierarchy fails and returns false
if ( !loadNodeHierarchy( n ) )
{
return QList<IndexedPointCloudNode>();
}
return QgsPointCloudIndex::nodeChildren( n );
}
void QgsRemoteEptPointCloudIndex::load( const QString &url )
{
mUrl = QUrl( url );
QStringList splitUrl = url.split( '/' );
mUrlFileNamePart = splitUrl.back();
splitUrl.pop_back();
mUrlDirectoryPart = splitUrl.join( '/' );
QNetworkRequest nr( url );
QgsBlockingNetworkRequest req;
QgsBlockingNetworkRequest::ErrorCode errCode = req.get( nr );
if ( errCode != QgsBlockingNetworkRequest::NoError )
{
QgsDebugMsg( QStringLiteral( "Request failed: " ) + url );
mIsValid = false;
return;
}
QgsNetworkReplyContent reply = req.reply();
mIsValid = loadSchema( reply.content() );
}
QgsPointCloudBlock *QgsRemoteEptPointCloudIndex::nodeData( const IndexedPointCloudNode &n, const QgsPointCloudRequest &request )
{
QgsPointCloudBlockRequest *blockRequest = asyncNodeData( n, request );
if ( !blockRequest )
return nullptr;
QEventLoop loop;
connect( blockRequest, &QgsPointCloudBlockRequest::finished, &loop, &QEventLoop::quit );
loop.exec();
if ( !blockRequest->block() )
{
QgsDebugMsg( QStringLiteral( "Error downloading node %1 data, error : %2 " ).arg( n.toString(), blockRequest->errorStr() ) );
}
return blockRequest->block();
}
QgsPointCloudBlockRequest *QgsRemoteEptPointCloudIndex::asyncNodeData( const IndexedPointCloudNode &n, const QgsPointCloudRequest &request )
{
if ( !loadNodeHierarchy( n ) )
return nullptr;
QgsPointCloudBlockRequest *handle = nullptr;
QString fileUrl;
if ( mDataType == QLatin1String( "binary" ) )
{
fileUrl = QStringLiteral( "%1/ept-data/%2.bin" ).arg( mUrlDirectoryPart, n.toString() );
}
else if ( mDataType == QLatin1String( "zstandard" ) )
{
fileUrl = QStringLiteral( "%1/ept-data/%2.zst" ).arg( mUrlDirectoryPart, n.toString() );
}
else if ( mDataType == QLatin1String( "laszip" ) )
{
fileUrl = QStringLiteral( "%1/ept-data/%2.laz" ).arg( mUrlDirectoryPart, n.toString() );
}
QNetworkRequest nr( fileUrl );
QgsTileDownloadManagerReply *reply = QgsApplication::tileDownloadManager()->get( nr );
handle = new QgsPointCloudBlockRequest( mDataType, attributes(), request.attributes(), reply );
return handle;
}
int QgsRemoteEptPointCloudIndex::pointCount() const
{
return mPointCount;
}
bool QgsRemoteEptPointCloudIndex::loadNodeHierarchy( const IndexedPointCloudNode &nodeId ) const
{
if ( mHierarchy.contains( nodeId ) )
return true;
QVector<IndexedPointCloudNode> nodePathToRoot;
{
nodePathToRoot.push_back( nodeId );
IndexedPointCloudNode currentNode = nodeId;
while ( currentNode.d() != 0 )
{
currentNode = currentNode.parentNode();
nodePathToRoot.push_back( currentNode );
}
}
for ( int i = nodePathToRoot.size() - 1; i >= 0 && mHierarchy.find( nodeId ) == mHierarchy.end(); --i )
{
IndexedPointCloudNode node = nodePathToRoot[i];
//! The hierarchy of the node is found => No need to load its file
if ( mHierarchy.find( node ) != mHierarchy.end() )
continue;
const QString fileUrl = QStringLiteral( "%1/ept-hierarchy/%2.json" ).arg( mUrlDirectoryPart, node.toString() );
QNetworkRequest nr( fileUrl );
nr.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
nr.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
QgsBlockingNetworkRequest req;
QgsBlockingNetworkRequest::ErrorCode errCode = req.get( nr );
if ( errCode != QgsBlockingNetworkRequest::NoError )
{
QgsDebugMsgLevel( QStringLiteral( "unable to read hierarchy from file %1" ).arg( fileUrl ), 2 );
return false;
}
QgsNetworkReplyContent reply = req.reply();
QByteArray dataJsonH = reply.content();
QJsonParseError errH;
const QJsonDocument docH = QJsonDocument::fromJson( dataJsonH, &errH );
if ( errH.error != QJsonParseError::NoError )
{
QgsDebugMsgLevel( QStringLiteral( "QJsonParseError when reading hierarchy from file %1" ).arg( fileUrl ), 2 );
return false;
}
const QJsonObject rootHObj = docH.object();
for ( auto it = rootHObj.constBegin(); it != rootHObj.constEnd(); ++it )
{
QString nodeIdStr = it.key();
int nodePointCount = it.value().toInt();
if ( nodePointCount > 0 )
{
IndexedPointCloudNode nodeId = IndexedPointCloudNode::fromString( nodeIdStr );
mHierarchy[nodeId] = nodePointCount;
}
}
}
return mHierarchy.contains( nodeId );
}
bool QgsRemoteEptPointCloudIndex::isValid() const
{
return mIsValid;
}
///@endcond