Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::CoordinateReferenceSystem Class Reference

Public Member Functions

 CoordinateReferenceSystem (const string &definition)
CoordinateReferenceSystemType GetType () const
 Get the identified type of the coordinate reference system.
const string & GetDefinition () const
 Get the full provided definition of the coordinate reference system.
const string & GetIdentifier () const
bool IsComplete () const
 Is this a fully-defined coordinate system?
bool Equals (const CoordinateReferenceSystem &other) const
void Serialize (Serializer &serializer) const
 Serialize this CRS to a binary format.

Static Public Member Functions

static unique_ptr< CoordinateReferenceSystem > TryIdentify (ClientContext &context, const string &source_crs)
static unique_ptr< CoordinateReferenceSystem > TryConvert (ClientContext &context, const CoordinateReferenceSystem &source_crs, CoordinateReferenceSystemType target_type)
static unique_ptr< CoordinateReferenceSystem > TryConvert (ClientContext &context, const string &source_crs, CoordinateReferenceSystemType target_type)
static CoordinateReferenceSystem Deserialize (Deserializer &deserializer)
 Deserialize a CRS from a binary format.

Static Private Member Functions

static void ParseDefinition (const string &text, CoordinateReferenceSystem &result)
static bool TryParseAuthCode (const string &text, CoordinateReferenceSystem &result)
static bool TryParseWKT2 (const string &text, CoordinateReferenceSystem &result)
static bool TryParsePROJJSON (const string &text, CoordinateReferenceSystem &result)

Private Attributes

CoordinateReferenceSystemType type = CoordinateReferenceSystemType::INVALID
 The type of the coordinate reference system.
string definition
string identifier

Friends

class CoordinateReferenceSystemProvider

Constructor & Destructor Documentation

◆ CoordinateReferenceSystem()

duckdb::CoordinateReferenceSystem::CoordinateReferenceSystem ( const string & definition)
inlineexplicit
20456 {
20457 ParseDefinition(definition, *this);
20458 }
string definition
Definition duckdb.cpp:20544

Member Function Documentation

◆ GetType()

CoordinateReferenceSystemType duckdb::CoordinateReferenceSystem::GetType ( ) const
inline

Get the identified type of the coordinate reference system.

20461 {
20462 return type;
20463 }
CoordinateReferenceSystemType type
The type of the coordinate reference system.
Definition duckdb.cpp:20540
Here is the caller graph for this function:

◆ GetDefinition()

const string & duckdb::CoordinateReferenceSystem::GetDefinition ( ) const
inline

Get the full provided definition of the coordinate reference system.

20466 {
20467 return definition;
20468 }

◆ GetIdentifier()

const string & duckdb::CoordinateReferenceSystem::GetIdentifier ( ) const
inline

Get the "identifier" of the coordinate reference system (e.g. "EPSG:4326") This is the same as the definition for AUTH:CODE and SRID types

20472 {
20473 return identifier.empty() ? definition : identifier;
20474 }
string identifier
Definition duckdb.cpp:20548
Here is the caller graph for this function:

◆ IsComplete()

bool duckdb::CoordinateReferenceSystem::IsComplete ( ) const
inline

Is this a fully-defined coordinate system?

20477 {
20478 switch (GetType()) {
20481 return true;
20482 default:
20483 return false;
20484 }
20485 }
CoordinateReferenceSystemType GetType() const
Get the identified type of the coordinate reference system.
Definition duckdb.cpp:20461
@ WKT2_2019
WKT2_2019 format.
Definition duckdb.cpp:20447
@ PROJJSON
PROJJSON format.
Definition duckdb.cpp:20445
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Equals()

bool duckdb::CoordinateReferenceSystem::Equals ( const CoordinateReferenceSystem & other) const
inline

Attempt to determine if this CRS is equivalent to another CRS This is currently not very precise, and may yield false negatives We consider two CRSs equal if one of the following is true:

  • Their identifiers match
  • Their type and full text definitions match, character-for-character
20492 {
20493 // Whatever the definitions are, if the identifiers match we consider them equal
20494 if (!identifier.empty() && identifier == other.identifier) {
20495 return true;
20496 }
20497
20498 // Fall back to comparing the full definitions
20499 // This is not ideal, because the same CRS (in the same format!) can often be expressed in multiple ways
20500 // E.g. field order, whitespace differences, casing, etc. But it's better than nothing for now.
20501
20502 // In the future we should:
20503 // 1. Implement proper normalization for each CRS format, and make _structured_ comparisons
20504 // 2. Allow extensions to inject a CRS handling library (e.g. PROJ) to perform proper _semantic_ comparisons
20505 return type == other.type && definition == other.definition;
20506 }

◆ TryIdentify()

unique_ptr< CoordinateReferenceSystem > duckdb::CoordinateReferenceSystem::TryIdentify ( ClientContext & context,
const string & source_crs )
static

Try to identify a CRS from a string If the string is unable to be identified as one of the registered coordinates systems, and

  • IS NOT a complete CRS definition, returns nullptr
  • IS a complete CRS definition (e.g. PROJJSON or WKT2), returns the CRS as is. Otherwise, returns the identified CRS in the most compact form possible (AUTH:CODE > SRID > PROJJSON > WKT2)
121551 {
121552 CoordinateReferenceSystem source(source_crs);
121553
121554 // We couldnt even parse the CRS
121555 if (source.GetType() == CoordinateReferenceSystemType::INVALID) {
121556 return nullptr;
121557 }
121558
121559 // We always want to identify the CRS as short as possible, so first check for AUTH:CODE
121560 auto auth_crs = TryConvert(context, source, CoordinateReferenceSystemType::AUTH_CODE);
121561 if (auth_crs) {
121562 return auth_crs;
121563 }
121564
121565 // Next, check for SRID
121566 auto srid_crs = TryConvert(context, source, CoordinateReferenceSystemType::SRID);
121567 if (srid_crs) {
121568 return srid_crs;
121569 }
121570
121571 // Otherwise, PROJJSON
121572 auto projjson_crs = TryConvert(context, source, CoordinateReferenceSystemType::PROJJSON);
121573 if (projjson_crs) {
121574 return projjson_crs;
121575 }
121576
121577 // Finally, WKT2:2019
121578 auto wkt2_crs = TryConvert(context, source, CoordinateReferenceSystemType::WKT2_2019);
121579 if (wkt2_crs) {
121580 return wkt2_crs;
121581 }
121582
121583 if (!source.IsComplete()) {
121584 return nullptr;
121585 }
121586
121587 return make_uniq<CoordinateReferenceSystem>(std::move(source));
121588}
static unique_ptr< CoordinateReferenceSystem > TryConvert(ClientContext &context, const CoordinateReferenceSystem &source_crs, CoordinateReferenceSystemType target_type)
Definition duckdb.cpp:121497
@ SRID
Opaque identifier.
Definition duckdb.cpp:20441
@ AUTH_CODE
AUTH:CODE format.
Definition duckdb.cpp:20443
@ INVALID
Not set / invalid.
Definition duckdb.cpp:20439
Here is the call graph for this function:

◆ TryConvert() [1/2]

unique_ptr< CoordinateReferenceSystem > duckdb::CoordinateReferenceSystem::TryConvert ( ClientContext & context,
const CoordinateReferenceSystem & source_crs,
CoordinateReferenceSystemType target_type )
static

Try to convert the CRS to another format Returns nullptr if no conversion could be performed

121499 {
121500 if (source_crs.type == target_type && source_crs.IsComplete()) {
121501 // No conversion needed
121502 return make_uniq<CoordinateReferenceSystem>(source_crs);
121503 }
121504
121505 auto &catalog = Catalog::GetSystemCatalog(context);
121506 auto entry = catalog.GetEntry(context, CatalogType::COORDINATE_SYSTEM_ENTRY, DEFAULT_SCHEMA,
121507 source_crs.GetIdentifier(), OnEntryNotFound::RETURN_NULL);
121508 if (!entry) {
121509 return nullptr;
121510 }
121511
121512 auto &crs_entry = entry->Cast<CoordinateSystemCatalogEntry>();
121513
121514 switch (target_type) {
121516 if (crs_entry.authority.empty() || crs_entry.code.empty()) {
121517 return nullptr;
121518 }
121519 return make_uniq<CoordinateReferenceSystem>(crs_entry.authority + ":" + crs_entry.code);
121520 }
121522 return make_uniq<CoordinateReferenceSystem>(crs_entry.name);
121523 }
121525 if (crs_entry.projjson_definition.empty()) {
121526 return nullptr;
121527 }
121528 return make_uniq<CoordinateReferenceSystem>(crs_entry.projjson_definition);
121529 }
121531 if (crs_entry.wkt2_2019_definition.empty()) {
121532 return nullptr;
121533 }
121534 return make_uniq<CoordinateReferenceSystem>(crs_entry.wkt2_2019_definition);
121535 }
121536 default:
121537 break;
121538 }
121539
121540 return nullptr;
121541}
static DUCKDB_API Catalog & GetSystemCatalog(ClientContext &context)
Get the SystemCatalog from the ClientContext.
Definition duckdb.cpp:7173
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TryConvert() [2/2]

unique_ptr< CoordinateReferenceSystem > duckdb::CoordinateReferenceSystem::TryConvert ( ClientContext & context,
const string & source_crs,
CoordinateReferenceSystemType target_type )
static

Try to convert the CRS to another format Returns nullptr if no conversion could be performed

121545 {
121546 const CoordinateReferenceSystem source(source_crs);
121547 return TryConvert(context, source, target_type);
121548}
Here is the call graph for this function:

◆ Serialize()

void duckdb::CoordinateReferenceSystem::Serialize ( Serializer & serializer) const

Serialize this CRS to a binary format.

121474 {
121475 serializer.WritePropertyWithDefault<string>(100, "definition", definition, string());
121476}

◆ Deserialize()

CoordinateReferenceSystem duckdb::CoordinateReferenceSystem::Deserialize ( Deserializer & deserializer)
static

Deserialize a CRS from a binary format.

121478 {
121479 string definition;
121480 deserializer.ReadPropertyWithExplicitDefault<string>(100, "definition", definition, string());
121481
121482 // If this fails for whatever reason, just return an invalid CRS
121483 CoordinateReferenceSystem result;
121484 try {
121485 ParseDefinition(definition, result);
121486 } catch (...) {
121487 result.definition = "";
121489 }
121490 return result;
121491}

◆ ParseDefinition()

void duckdb::CoordinateReferenceSystem::ParseDefinition ( const string & text,
CoordinateReferenceSystem & result )
staticprivate
121436 {
121437 if (definition.empty()) {
121439 return;
121440 }
121441
121442 // Check if the text is all whitespace
121443 auto all_space = true;
121444 for (const auto c : definition) {
121445 if (!StringUtil::CharacterIsSpace(c)) {
121446 all_space = false;
121447 break;
121448 }
121449 }
121450
121451 if (all_space) {
121453 return;
121454 }
121455
121456 if (TryParsePROJJSON(definition, result)) {
121457 return;
121458 }
121459
121460 if (TryParseAuthCode(definition, result)) {
121461 return;
121462 }
121463
121464 // TODO: Also strip formatting
121465 if (TryParseWKT2(definition, result)) {
121466 return;
121467 }
121468
121469 // Otherwise, treat this as an opaque identifier, and don't set an explicit id
121471 result.definition = definition;
121472}

◆ TryParseAuthCode()

bool duckdb::CoordinateReferenceSystem::TryParseAuthCode ( const string & text,
CoordinateReferenceSystem & result )
staticprivate
121385 {
121386 auto beg = text.c_str();
121387 auto end = beg + text.size();
121388
121389 // Remove whitespace
121390 while (beg != end) {
121391 if (!StringUtil::CharacterIsSpace(*beg)) {
121392 break;
121393 }
121394 beg++;
121395 }
121396 while (end != beg) {
121397 if (!StringUtil::CharacterIsSpace(*(end - 1))) {
121398 break;
121399 }
121400 end--;
121401 }
121402
121403 for (auto colon_pos = beg; colon_pos != end; colon_pos++) {
121404 if (*colon_pos == ':') {
121405 bool auth_valid = true;
121406 for (auto ptr = beg; ptr != colon_pos; ptr++) {
121407 if (!StringUtil::CharacterIsAlpha(*ptr)) {
121408 auth_valid = false;
121409 break;
121410 }
121411 }
121412
121413 bool code_valid = true;
121414 for (auto ptr = colon_pos + 1; ptr != end; ptr++) {
121415 if (!StringUtil::CharacterIsAlphaNumeric(*ptr)) {
121416 code_valid = false;
121417 break;
121418 }
121419 }
121420
121421 if (auth_valid && code_valid) {
121422 // Valid AUTH:CODE
121424 result.definition = string(beg, UnsafeNumericCast<size_t>(end - beg));
121425 return true;
121426 }
121427 break;
121428 }
121429 }
121430 return false;
121431}

◆ TryParseWKT2()

bool duckdb::CoordinateReferenceSystem::TryParseWKT2 ( const string & text,
CoordinateReferenceSystem & result )
staticprivate
121155 {
121156 const auto node = WKTParser::Parse(text);
121157
121158 if (!node) {
121159 // Not a valid WKT2 string
121160 return false;
121161 }
121162
121163 if (!node->IsKeyword()) {
121164 return false;
121165 }
121166
121167 const auto &keyword = node->As<WKTKeyword>();
121168
121169 // Check that this is a valid CRS keyword
121170 auto found = false;
121171
121172 const auto crs_keywords = {
121173 // WKT2019 keywords
121174 "BOUNDCRS", "COMPOUNDCRS", "ENGCRS", "ENGINEERINGCRS", "GEODCRS",
121175 "GEODETICCRS", "GEOGCRS", "GEOGRAPHICCRS", "PARAMETRICCRS", "PROJCRS",
121176 "PROJECTEDCRS", "TIMECRS", "VERTCRS", "VERTICALCRS"
121177 // Older WKT1 keywords (we dont support these for now)
121178 // "COMPDCS",
121179 // "FITTED_CS",
121180 // "GEOCCS",
121181 // "GEOGCS",
121182 // "LOCAL_CS",
121183 // "PROJCS",
121184 // "VERT_CS",
121185 };
121186
121187 // Check if we match any of the CRS keywords
121188 for (auto &kw : crs_keywords) {
121189 if (keyword.Match(kw)) {
121190 found = true;
121191 break;
121192 }
121193 }
121194
121195 if (!found) {
121196 // Not a valid CRS keyword
121197 return false;
121198 }
121199
121200 auto &children = keyword.GetChildren();
121201 if (children.empty()) {
121202 // Needs a name
121203 return false;
121204 }
121205
121206 // Special case for BOUNDCRS, use the name of the transform instead
121207 if (keyword.Match("BOUNDCRS")) {
121208 if (children.size() < 3) {
121209 // Needs at least source CRS, target CRS and transform
121210 return false;
121211 }
121212
121213 const auto &transform_node = children[2];
121214 if (!transform_node->IsKeyword()) {
121215 // Transform needs to be a keyword
121216 return false;
121217 }
121218
121219 const auto &transform_keyword = transform_node->As<WKTKeyword>();
121220 auto &transform_children = transform_keyword.GetChildren();
121221 if (transform_children.empty()) {
121222 // Transform needs a name
121223 return false;
121224 }
121225
121226 const auto &first_transform_child = transform_children[0];
121227 if (!first_transform_child->IsString()) {
121228 // First child of transform needs to be a string (the name)
121229 return false;
121230 }
121231
121232 // TODO: Parse "ID" subnode to get the code
121233 auto name = TryExtractIDFromWKTNode(transform_keyword);
121234 if (name.empty()) {
121235 // Pick name as fallback instead of code
121236 name = first_transform_child->As<WKTString>().GetValue();
121237 }
121238
121240 result.identifier = name;
121241 result.definition = text;
121242
121243 // Also trim text
121244 // TODO: Normalize WKT Input
121245 StringUtil::Trim(result.definition);
121246
121247 return true;
121248 }
121249
121250 // Otherwise, the name is the first child
121251 const auto &first = children[0];
121252 if (!first->IsString()) {
121253 // Pick name as fallback instead of code
121254 return false;
121255 }
121256
121257 auto name = TryExtractIDFromWKTNode(keyword);
121258 if (name.empty()) {
121259 // Pick name as fallback
121260 name = first->As<WKTString>().GetValue();
121261 }
121262
121264 result.identifier = name;
121265 result.definition = text;
121266
121267 // Also trim text
121268 // TODO: Normalize WKT Input
121269 StringUtil::Trim(result.definition);
121270
121271 return true;
121272}
static DUCKDB_API void Trim(string &str)
Remove the whitespace char in the left and right end of the string.
Definition duckdb.cpp:109372

◆ TryParsePROJJSON()

bool duckdb::CoordinateReferenceSystem::TryParsePROJJSON ( const string & text,
CoordinateReferenceSystem & result )
staticprivate
121277 {
121278 using namespace duckdb_yyjson; // NOLINT
121279
121280 unique_ptr<yyjson_doc, void (*)(yyjson_doc *)> doc(yyjson_read(text.c_str(), text.size(), YYJSON_READ_NOFLAG),
121281 yyjson_doc_free);
121282
121283 if (!doc) {
121284 // Not a valid JSON
121285 return false;
121286 }
121287
121288 yyjson_val *root = yyjson_doc_get_root(doc.get());
121289 if (!root || !yyjson_is_obj(root)) {
121290 // The root is not an object
121291 return false;
121292 }
121293
121294 // Get the "type" field from the root object
121295 yyjson_val *type_val = yyjson_obj_get(root, "type");
121296 if (!type_val || !yyjson_is_str(type_val)) {
121297 return false;
121298 }
121299
121300 // Check that the type is one of the PROJJSON CRS types
121301 // There are other (derived CRS) types, but they can not be used as root CRS definitions
121302 const string type_str = yyjson_get_str(type_val);
121303 const auto projjson_crs_types = {"GeographicCRS", "GeodeticCRS", "ProjectedCRS", "CompoundCRS", "BoundCRS",
121304 "VerticalCRS", "EngineeringCRS", "TemporalCRS", "ParametricCRS"};
121305
121306 auto found = false;
121307 for (auto &kw : projjson_crs_types) {
121308 if (StringUtil::CIEquals(type_str, kw)) {
121309 found = true;
121310 break;
121311 }
121312 }
121313
121314 if (!found) {
121315 return false;
121316 }
121317
121318 // Start out with the root object
121319 yyjson_val *target_val = root;
121320
121321 // Special case for BoundCRS, use the name of the transformation instead
121322 if (StringUtil::CIEquals(type_str, "BoundCRS")) {
121323 const auto trans_val = yyjson_obj_get(root, "transformation");
121324 if (!trans_val || !yyjson_is_obj(trans_val)) {
121325 return false;
121326 }
121327
121328 // Switch to the transformation object
121329 target_val = trans_val;
121330 }
121331
121332 // Try to get the "name" field from the target object
121333 yyjson_val *name_val = yyjson_obj_get(target_val, "name");
121334 if (name_val && yyjson_is_str(name_val)) {
121335 const char *name_str = yyjson_get_str(name_val);
121336 if (name_str) {
121337 result.identifier = string(name_str);
121338 }
121339 }
121340
121341 // Try to get the "id" field from the target object
121342 yyjson_val *id_val = yyjson_obj_get(target_val, "id");
121343 if (id_val && yyjson_is_obj(id_val)) {
121344 const auto auth_val = yyjson_obj_get(id_val, "authority");
121345 if (auth_val && yyjson_is_str(auth_val)) {
121346 const auto auth_str = yyjson_get_str(auth_val);
121347
121348 if (auth_str) {
121349 result.identifier = string(auth_str);
121350
121351 const auto code_val = yyjson_obj_get(id_val, "code");
121352 if (code_val && yyjson_is_int(code_val)) {
121353 const auto code_int = yyjson_get_int(code_val);
121354 result.identifier += ":" + StringUtil::Format("%d", code_int);
121355 }
121356 if (code_val && yyjson_is_str(code_val)) {
121357 const auto code_str = yyjson_get_str(code_val);
121358 if (code_str) {
121359 result.identifier += ":" + string(code_str);
121360 }
121361 }
121362 }
121363 }
121364 }
121365
121367
121368 // Print the PROJJSON back to a string to normalize it
121369 // TODO: We should actually normalize the PROJJSON here (e.g. sort fields) to ensure consistent equality checks
121370 size_t json_size = 0;
121371 const auto json_text = yyjson_write(doc.get(), YYJSON_WRITE_NOFLAG, &json_size);
121372 if (!json_text) {
121373 return false;
121374 }
121375
121376 result.definition = string(json_text, json_size);
121377 free(json_text);
121378
121379 return true;
121380}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002
static DUCKDB_API bool CIEquals(const string &l1, const string &l2)
Case insensitive equals.
Definition duckdb.cpp:109696
yyjson_api_inline bool yyjson_is_str(yyjson_val *val)
Definition duckdb.cpp:41935
yyjson_api_inline yyjson_val * yyjson_obj_get(yyjson_val *obj, const char *key)
Definition duckdb.cpp:42203
yyjson_api_inline const char * yyjson_get_str(yyjson_val *val)
Definition duckdb.cpp:42014
yyjson_api_inline bool yyjson_is_obj(yyjson_val *val)
Definition duckdb.cpp:41943
struct yyjson_doc yyjson_doc
Definition duckdb.cpp:37748
yyjson_api_inline char * yyjson_write(const yyjson_doc *doc, yyjson_write_flag flg, size_t *len)
Definition duckdb.cpp:38286
struct yyjson_val yyjson_val
Definition duckdb.cpp:37755
yyjson_api_inline int yyjson_get_int(yyjson_val *val)
Definition duckdb.cpp:42002
yyjson_api_inline bool yyjson_is_int(yyjson_val *val)
Definition duckdb.cpp:41923
yyjson_api_inline yyjson_doc * yyjson_read(const char *dat, size_t len, yyjson_read_flag flg)
Definition duckdb.cpp:37983
yyjson_api_inline yyjson_val * yyjson_doc_get_root(yyjson_doc *doc)
Definition duckdb.cpp:41868

Member Data Documentation

◆ definition

string duckdb::CoordinateReferenceSystem::definition
private

The text definition of the coordinate reference system E.g. "AUTH:CODE", or a PROJJSON or WKT2 string

◆ identifier

string duckdb::CoordinateReferenceSystem::identifier
private

The identifier code of the coordinate reference system This can usually be derived from the text definition, but is cached here for convenience


The documentation for this class was generated from the following file: