Skip to content

Commit

Permalink
Merge pull request #349 from KNMI/optimize-bbox
Browse files Browse the repository at this point in the history
Refactored optimized bbox
  • Loading branch information
maartenplieger authored Mar 6, 2024
2 parents 57dfc18 + cd23162 commit 3f16824
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 185 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"compare": "cpp",
"concepts": "cpp",
"semaphore": "cpp",
"stop_token": "cpp"
"stop_token": "cpp",
"ranges": "cpp"
},
"cmake.buildDirectory": "${workspaceFolder}/bin",
"cmake.configureOnOpen": true,
Expand Down
2 changes: 1 addition & 1 deletion CCDFDataModel/CTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ CTime *CTime::GetCTimeInstance(CDF::Variable *timeVariable) {
return NULL;
}
CTimeInstances.insert(std::pair<CT::string, CTime *>(key, ctime));
CDBDebug("Inserting new CTime with key %s and pointer %d", key.c_str(), ctime);
// CDBDebug("Inserting new CTime with key %s and pointer %d", key.c_str(), ctime);
}
return ctime;
}
Expand Down
29 changes: 17 additions & 12 deletions adagucserverEC/CImageWarper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
*
******************************************************************************/

#include "./Types/ProjectionKey.h"
#include "./Types/ProjectionStore.h"
#include "Types/ProjectionStore.h"
#include "CImageWarper.h"
#include "ProjCache.h"
#include <iostream>
Expand Down Expand Up @@ -357,12 +356,16 @@ int CImageWarper::findExtentUnSynchronized(CDataSource *dataSource, double *dfBB
}

// CDBDebug("findExtent for %s and %f %f %f %f", destinationCRS.c_str(), dfBBOX[0], dfBBOX[1], dfBBOX[2], dfBBOX[3]);

ProjectionKey pKey = MakeProjectionKey(dfBBOX, dfMaxExtent, sourceCRSString, destinationCRS);
auto projectionStore = ProjectionStore::getProjectionStore();

BBOX bbox;
if (projectionStore->findExtentForKey(pKey, &bbox) == 0) {
ProjectionMapKey key = {sourceCRSString, destinationCRS, makeBBOX(dfBBOX)};
bool found;
BBOX bbox{};
std::tie(found, bbox) = getBBOXProjection(key);

if (found) {
#ifdef CIMAGEWARPER_DEBUG
CDBDebug("FOUND AND REUSING!!! %s %s (%0.3f, %0.3f, %0.3f, %0.3f) to (%0.3f, %0.3f, %0.3f, %0.3f)", key.sourceCRS.c_str(), key.destCRS.c_str(), key.extent.bbox[0], key.extent.bbox[1],
key.extent.bbox[2], key.extent.bbox[3], bbox.bbox[0], bbox.bbox[1], bbox.bbox[2], bbox.bbox[3]);
#endif
for (size_t j = 0; j < 4; j++) {
dfBBOX[j] = bbox.bbox[j];
}
Expand Down Expand Up @@ -394,6 +397,7 @@ int CImageWarper::findExtentUnSynchronized(CDataSource *dataSource, double *dfBB
double inY = testPosY;
double inX = testPosX;

// Make sure testPosX and testPosY are not NaN values
if (testPosX == testPosX && testPosY == testPosY) {

try {
Expand Down Expand Up @@ -474,11 +478,12 @@ int CImageWarper::findExtentUnSynchronized(CDataSource *dataSource, double *dfBB
dfBBOX[1] -= 1;
dfBBOX[3] += 1;
}
#ifdef CIMAGEWARPER_DEBUG

pKey.setFoundExtent(dfBBOX);
projectionStore->keys.push_back(pKey);

// CDBDebug("out: %f %f %f %f",dfBBOX[0],dfBBOX[1],dfBBOX[2],dfBBOX[3]);
CDBDebug("INSERTING!!! %s %s (%0.3f, %0.3f, %0.3f, %0.3f) to (%0.3f, %0.3f, %0.3f, %0.3f)", key.sourceCRS.c_str(), key.destCRS.c_str(), key.extent.bbox[0], key.extent.bbox[1], key.extent.bbox[2],
key.extent.bbox[3], dfBBOX[0], dfBBOX[1], dfBBOX[2], dfBBOX[3]);
#endif
addBBOXProjection(key, makeBBOX(dfBBOX));
return 0;
};

Expand Down
2 changes: 1 addition & 1 deletion adagucserverEC/CImageWarper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class CImageWarper {
bool initialized;

int _initreprojSynchronized(const char *projString, CGeoParams *GeoDest, std::vector<CServerConfig::XMLE_Projection *> *_prj);
int findExtentUnSynchronized(CDataSource *dataSource, double *dfBBOX);

public:
int findExtentUnSynchronized(CDataSource *dataSource, double *dfBBOX);
bool requireReprojection;
CImageWarper() {
prj = NULL;
Expand Down
3 changes: 1 addition & 2 deletions adagucserverEC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ add_library(
LayerTypeLiveUpdate/LayerTypeLiveUpdate.cpp
LayerTypeLiveUpdate/LayerTypeLiveUpdate.h
Types/BBOX.h
Types/ProjectionKey.h
Types/ProjectionKey.cpp
Types/BBOX.cpp
Types/ProjectionStore.h
Types/ProjectionStore.cpp
testadagucserver.cpp
Expand Down
1 change: 0 additions & 1 deletion adagucserverEC/CRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ int CRequest::runRequest() {
CDFObjectStore::getCDFObjectStore()->clear();
CConvertGeoJSON::clearFeatureStore();
CDFStore::clear();
ProjectionStore::getProjectionStore()->clear();
CDBFactory::clear();
return status;
}
Expand Down
5 changes: 2 additions & 3 deletions adagucserverEC/CXMLGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ int CXMLGen::getFileNameForLayer(WMSLayer *myWMSLayer) {

bool databaseError = false;

CDBDebug("%s tableName", tableName.c_str());
CDBStore::Store *values = CDBFactory::getDBAdapter(srvParam->cfg)->getUniqueValuesOrderedByValue("path", 1, false, tableName.c_str());

if (values == NULL) {
Expand Down Expand Up @@ -957,7 +956,7 @@ int CXMLGen::getWMS_1_1_1_Capabilities(CT::string *XMLDoc, std::vector<WMSLayer

for (size_t lnr = 0; lnr < myWMSLayerList->size(); lnr++) {
WMSLayer *layer = (*myWMSLayerList)[lnr];
if (layer->group.equals(&groupKeys[groupIndex])) {
if (layer->group.equals(groupKeys[groupIndex])) {
// CDBError("layer %d %s",groupDepth,layer->name.c_str());
if (layer->hasError == 0) {
XMLDoc->printconcat("<Layer queryable=\"%d\" opaque=\"1\" cascaded=\"%d\">\n", layer->isQuerable, layer->dataSource->dLayerType == CConfigReaderLayerTypeCascaded ? 1 : 0);
Expand Down Expand Up @@ -1330,7 +1329,7 @@ int CXMLGen::getWMS_1_3_0_Capabilities(CT::string *XMLDoc, std::vector<WMSLayer
CDBDebug("Comparing %s == %s", layer->group.c_str(), groupKeys[groupIndex].c_str());
#endif

if (layer->group.equals(&groupKeys[groupIndex])) {
if (layer->group.equals(groupKeys[groupIndex])) {
#ifdef CXMLGEN_DEBUG
CDBDebug("layer %d %s", groupDepth, layer->name.c_str());
#endif
Expand Down
13 changes: 13 additions & 0 deletions adagucserverEC/Types/BBOX.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <tuple>
#include "BBOX.h"

bool operator<(BBOX a, BBOX b) { return std::make_tuple(a.bbox[0], a.bbox[1], a.bbox[2], a.bbox[3]) < std::make_tuple(b.bbox[0], b.bbox[1], b.bbox[2], b.bbox[3]); }

BBOX makeBBOX(double *bbox) {
BBOX box;
box.bbox[0] = bbox[0];
box.bbox[1] = bbox[1];
box.bbox[2] = bbox[2];
box.bbox[3] = bbox[3];
return box;
}
15 changes: 11 additions & 4 deletions adagucserverEC/Types/BBOX.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@

#ifndef BBOX_H

#define BBOX_H

class BBOX {
public:
struct BBOX {
double bbox[4];
};

/**
* Custom comparison operator for the BBOX
*/
bool operator<(BBOX a, BBOX b);

/**
* Makes a BBOX from a array of doubles
*/
BBOX makeBBOX(double *bbox);

#endif // ! BBOX_H
47 changes: 0 additions & 47 deletions adagucserverEC/Types/ProjectionKey.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions adagucserverEC/Types/ProjectionKey.h

This file was deleted.

59 changes: 16 additions & 43 deletions adagucserverEC/Types/ProjectionStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,24 @@
*
******************************************************************************/

#include <map>
#include "ProjectionStore.h"
#include <bits/pthreadtypes.h>
#include <pthread.h>

const char *ProjectionStore::className = "ProjectionStore";
bool operator< (const ProjectionMapKey& a, const ProjectionMapKey& b) { return std::make_tuple(a.sourceCRS, a.destCRS, a.extent) < std::make_tuple(b.sourceCRS, b.destCRS, b.extent); }
static std::map<ProjectionMapKey, BBOX> projectionMap;

ProjectionStore projectionStore;
extern ProjectionStore projectionStore;

ProjectionStore *ProjectionStore::getProjectionStore() { return &projectionStore; }

ProjectionStore::ProjectionStore() {}
ProjectionStore::~ProjectionStore() { clear(); }
std::tuple<bool, BBOX> getBBOXProjection(ProjectionMapKey key) {
auto it = projectionMap.find(key);
if (it == projectionMap.end()) {
return std::make_tuple(false, BBOX{});
}
return std::make_tuple(true, it->second);
}

pthread_mutex_t ProjectionKey_clear;
void ProjectionStore::clear() { keys.clear(); }
void addBBOXProjection(ProjectionMapKey key, BBOX bbox) {
projectionMap[key] = bbox;
}

int ProjectionStore::findExtentForKey(ProjectionKey pKey, BBOX *bbox) {
for (size_t j = 0; j < this->keys.size(); j++) {
if (this->keys[j].isSet == true) {
bool match = true;
// First find matching destination CRS
if (!this->keys[j].destinationCRS.equals(pKey.destinationCRS)) match = false;
// Next match on bbox
if (match) {
for (int i = 0; i < 4; i++) {
if (this->keys[j].bbox[i] != pKey.bbox[i]) {
// CDBDebug("%f != %f", this->keys[j].bbox[i], pKey.bbox[i]);
match = false;
break;
}
}
// Destination CRS and BBOX do match
if (match) {
if (!this->keys[j].sourceCRS.equals(pKey.sourceCRS)) match = false;
if (match) {
// Source CRS, Destination CRS and BBOX do Match
for (int i = 0; i < 4; i++) {
bbox->bbox[i] = this->keys[j].foundExtent[i];
}
return 0;
}
}
}
}
}
return -1;
}
void BBOXProjectionClearCache() {
projectionMap.clear();
}
32 changes: 17 additions & 15 deletions adagucserverEC/Types/ProjectionStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@
*
******************************************************************************/

#include "ProjectionKey.h"
#include "CDebugger.h"
#include <iostream>
#include <vector>
#ifndef PROJECTIONSTORE_H
#define PROJECTIONSTORE_H
#include <tuple>
#include "CTString.h"
#include "BBOX.h"

class ProjectionStore {
private:
DEF_ERRORFUNCTION();

public:
std::vector<ProjectionKey> keys;
ProjectionStore();
~ProjectionStore();
static ProjectionStore *getProjectionStore();
void clear();
int findExtentForKey(ProjectionKey key, BBOX *bbox);
struct ProjectionMapKey {
CT::string sourceCRS;
CT::string destCRS;
BBOX extent;
};

std::tuple<bool, BBOX> getBBOXProjection(ProjectionMapKey key);

void addBBOXProjection(ProjectionMapKey key, BBOX bbox);

void BBOXProjectionClearCache();

#endif // !PROJECTIONSTORE_H
2 changes: 2 additions & 0 deletions adagucserverEC/adagucserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void serverWarningFunction(const char *msg) {
void serverLogFunctionCMDLine(const char *msg) { printf("%s", msg); }

#include "adagucserver.h"
#include "Types/ProjectionStore.h"

void serverLogFunctionNothing(const char *) {}

Expand Down Expand Up @@ -450,6 +451,7 @@ int main(int argc, char **argv, char **envp) {
CDFObjectStore::getCDFObjectStore()->clear();

proj_clear_cache();
BBOXProjectionClearCache();

if (pLogDebugFile != NULL) {
fclose(pLogDebugFile);
Expand Down
Loading

0 comments on commit 3f16824

Please sign in to comment.