|
| 1 | +// Copyright 2020-2024 CesiumGS, Inc. and Contributors |
| 2 | + |
| 3 | +#include "CesiumGeoJsonDocumentRasterOverlay.h" |
| 4 | + |
| 5 | +#include "CesiumCustomVersion.h" |
| 6 | +#include "CesiumGeometry/QuadtreeTilingScheme.h" |
| 7 | +#include "CesiumGeospatial/GlobeRectangle.h" |
| 8 | +#include "CesiumGeospatial/Projection.h" |
| 9 | +#include "CesiumRasterOverlays/GeoJsonDocumentRasterOverlay.h" |
| 10 | +#include "CesiumVectorData/VectorStyle.h" |
| 11 | + |
| 12 | +#include "CesiumRuntime.h" |
| 13 | + |
| 14 | +namespace { |
| 15 | +CesiumAsync::Future<std::shared_ptr<CesiumVectorData::GeoJsonDocument>> |
| 16 | +wrapLoaderFuture( |
| 17 | + UCesiumGeoJsonDocumentRasterOverlay* pThis, |
| 18 | + CesiumAsync::Future< |
| 19 | + CesiumUtility::Result<CesiumVectorData::GeoJsonDocument>>&& future) { |
| 20 | + return std::move(future).thenInMainThread( |
| 21 | + [pThis](CesiumUtility::Result<CesiumVectorData::GeoJsonDocument>&& |
| 22 | + documentResult) |
| 23 | + -> std::shared_ptr<CesiumVectorData::GeoJsonDocument> { |
| 24 | + if (documentResult.errors) { |
| 25 | + documentResult.errors.logError( |
| 26 | + spdlog::default_logger(), |
| 27 | + "Errors loading GeoJSON document: "); |
| 28 | + return nullptr; |
| 29 | + } |
| 30 | + |
| 31 | + std::shared_ptr<CesiumVectorData::GeoJsonDocument> pGeoJsonDocument = |
| 32 | + std::make_shared<CesiumVectorData::GeoJsonDocument>( |
| 33 | + std::move(*documentResult.value)); |
| 34 | + |
| 35 | + if (pThis->OnDocumentLoaded.IsBound()) { |
| 36 | + pThis->OnDocumentLoaded.Execute(FCesiumGeoJsonDocument( |
| 37 | + std::shared_ptr<CesiumVectorData::GeoJsonDocument>( |
| 38 | + pGeoJsonDocument))); |
| 39 | + } |
| 40 | + |
| 41 | + return pGeoJsonDocument; |
| 42 | + }); |
| 43 | +} |
| 44 | +} // namespace |
| 45 | + |
| 46 | +std::unique_ptr<CesiumRasterOverlays::RasterOverlay> |
| 47 | +UCesiumGeoJsonDocumentRasterOverlay::CreateOverlay( |
| 48 | + const CesiumRasterOverlays::RasterOverlayOptions& options) { |
| 49 | + if (this->Source == ECesiumGeoJsonDocumentRasterOverlaySource::FromDocument && |
| 50 | + !this->GeoJsonDocument.IsValid()) { |
| 51 | + // Don't create an overlay with an invalid document. |
| 52 | + return nullptr; |
| 53 | + } |
| 54 | + |
| 55 | + CesiumRasterOverlays::GeoJsonDocumentRasterOverlayOptions vectorOptions{ |
| 56 | + this->DefaultStyle.toNative(), |
| 57 | + options.ellipsoid, |
| 58 | + this->MipLevels}; |
| 59 | + |
| 60 | + if (this->Source == |
| 61 | + ECesiumGeoJsonDocumentRasterOverlaySource::FromCesiumIon) { |
| 62 | + if (!IsValid(this->CesiumIonServer)) { |
| 63 | + this->CesiumIonServer = UCesiumIonServer::GetServerForNewObjects(); |
| 64 | + } |
| 65 | + |
| 66 | + return std::make_unique<CesiumRasterOverlays::GeoJsonDocumentRasterOverlay>( |
| 67 | + TCHAR_TO_UTF8(*this->MaterialLayerKey), |
| 68 | + wrapLoaderFuture( |
| 69 | + this, |
| 70 | + CesiumVectorData::GeoJsonDocument::fromCesiumIonAsset( |
| 71 | + getAsyncSystem(), |
| 72 | + getAssetAccessor(), |
| 73 | + this->IonAssetID, |
| 74 | + TCHAR_TO_UTF8(*this->CesiumIonServer->DefaultIonAccessToken), |
| 75 | + std::string(TCHAR_TO_UTF8(*this->CesiumIonServer->ApiUrl)) + |
| 76 | + "/")), |
| 77 | + vectorOptions, |
| 78 | + options); |
| 79 | + } else if ( |
| 80 | + this->Source == ECesiumGeoJsonDocumentRasterOverlaySource::FromUrl) { |
| 81 | + std::vector<CesiumAsync::IAssetAccessor::THeader> headers; |
| 82 | + headers.reserve(this->RequestHeaders.Num()); |
| 83 | + |
| 84 | + for (auto& [k, v] : this->RequestHeaders) { |
| 85 | + headers.push_back({TCHAR_TO_UTF8(*k), TCHAR_TO_UTF8(*v)}); |
| 86 | + } |
| 87 | + |
| 88 | + return std::make_unique<CesiumRasterOverlays::GeoJsonDocumentRasterOverlay>( |
| 89 | + TCHAR_TO_UTF8(*this->MaterialLayerKey), |
| 90 | + wrapLoaderFuture( |
| 91 | + this, |
| 92 | + CesiumVectorData::GeoJsonDocument::fromUrl( |
| 93 | + getAsyncSystem(), |
| 94 | + getAssetAccessor(), |
| 95 | + TCHAR_TO_UTF8(*this->Url), |
| 96 | + std::move(headers))), |
| 97 | + vectorOptions, |
| 98 | + options); |
| 99 | + } |
| 100 | + |
| 101 | + if (this->OnDocumentLoaded.IsBound()) { |
| 102 | + this->OnDocumentLoaded.Execute(this->GeoJsonDocument); |
| 103 | + } |
| 104 | + |
| 105 | + return std::make_unique<CesiumRasterOverlays::GeoJsonDocumentRasterOverlay>( |
| 106 | + getAsyncSystem(), |
| 107 | + TCHAR_TO_UTF8(*this->MaterialLayerKey), |
| 108 | + this->GeoJsonDocument.GetDocument(), |
| 109 | + vectorOptions, |
| 110 | + options); |
| 111 | +} |
0 commit comments