feat: add production-like particle field feed
This commit is contained in:
@@ -42,7 +42,8 @@ bool ParticleFieldSnapshot::isTimeInterpolationCompatibleWith(const ParticleFiel
|
||||
ParticleFieldAdapterResult ParticleFieldDataAdapter::decode(const ParticleFieldDescriptor& descriptor,
|
||||
const std::vector<char>& bytes,
|
||||
const uint64_t generation) {
|
||||
if (descriptor.fieldID.empty() || descriptor.fieldType.empty() || descriptor.displayDomain.empty() || generation == 0) {
|
||||
if (descriptor.fieldID.empty() || descriptor.fieldType.empty() || descriptor.displayDomain.empty() ||
|
||||
descriptor.generation == 0 || generation == 0 || descriptor.generation != generation) {
|
||||
return reject(ParticleFieldAdapterError::InvalidTimestampOrIdentity, "field identity or generation is invalid");
|
||||
}
|
||||
if (descriptor.width == 0 || descriptor.height == 0 || !(descriptor.west < descriptor.east) ||
|
||||
@@ -73,7 +74,7 @@ ParticleFieldAdapterResult ParticleFieldDataAdapter::decode(const ParticleFieldD
|
||||
field.north = descriptor.north;
|
||||
field.noData = descriptor.noData;
|
||||
field.dataVersion = descriptor.dataVersion;
|
||||
field.generation = generation;
|
||||
field.generation = descriptor.generation;
|
||||
field.fieldSha256 = descriptor.fieldSha256;
|
||||
field.velocity.resize(static_cast<size_t>(cells) * 2);
|
||||
std::memcpy(field.velocity.data(), bytes.data(), bytes.size());
|
||||
|
||||
74
src/mbgl/particle/field/local_particle_field_feed.cpp
Normal file
74
src/mbgl/particle/field/local_particle_field_feed.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <mbgl/particle/field/local_particle_field_feed.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace mbgl::particle {
|
||||
namespace {
|
||||
|
||||
ParticleFieldAdapterResult reject(const ParticleFieldAdapterError error, std::string message) {
|
||||
return {.snapshot = std::nullopt, .error = error, .message = std::move(message)};
|
||||
}
|
||||
|
||||
bool hasString(const rapidjson::Value& object, const char* name) {
|
||||
return object.HasMember(name) && object[name].IsString();
|
||||
}
|
||||
bool hasNumber(const rapidjson::Value& object, const char* name) {
|
||||
return object.HasMember(name) && object[name].IsNumber();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ParticleFieldAdapterResult LocalParticleFieldFeed::load(const std::string& metadataText,
|
||||
const std::vector<char>& payload) {
|
||||
rapidjson::Document metadata;
|
||||
metadata.Parse(metadataText.c_str());
|
||||
if (metadata.HasParseError() || !metadata.IsObject() || !hasString(metadata, "schema") ||
|
||||
std::string(metadata["schema"].GetString()) != "maplibre-particle-field/v1" ||
|
||||
!hasString(metadata, "fieldId") || !hasString(metadata, "fieldType") ||
|
||||
!hasString(metadata, "displayDomain") || !metadata.HasMember("generation") ||
|
||||
!metadata["generation"].IsUint64() || !metadata.HasMember("validTime") ||
|
||||
!metadata["validTime"].IsUint64()) {
|
||||
return reject(ParticleFieldAdapterError::InvalidDescriptor, "invalid local field metadata");
|
||||
}
|
||||
const auto& grid = metadata["grid"];
|
||||
const auto& bounds = metadata["bounds"];
|
||||
const auto& vector = metadata["vector"];
|
||||
const auto& payloadInfo = metadata["payload"];
|
||||
if (!grid.IsObject() || !bounds.IsObject() || !vector.IsObject() || !payloadInfo.IsObject() ||
|
||||
!grid.HasMember("width") || !grid["width"].IsUint() || !grid.HasMember("height") ||
|
||||
!grid["height"].IsUint() || !hasNumber(bounds, "west") || !hasNumber(bounds, "south") ||
|
||||
!hasNumber(bounds, "east") || !hasNumber(bounds, "north") || !hasString(vector, "layout") ||
|
||||
!hasString(vector, "encoding") || !hasNumber(vector, "noData") || !payloadInfo.HasMember("byteLength") ||
|
||||
!payloadInfo["byteLength"].IsUint64()) {
|
||||
return reject(ParticleFieldAdapterError::InvalidDescriptor, "invalid local field grid or payload metadata");
|
||||
}
|
||||
ParticleFieldDescriptor descriptor;
|
||||
descriptor.fieldID = metadata["fieldId"].GetString();
|
||||
descriptor.fieldType = metadata["fieldType"].GetString();
|
||||
descriptor.displayDomain = metadata["displayDomain"].GetString();
|
||||
descriptor.generation = metadata["generation"].GetUint64();
|
||||
descriptor.validTime = metadata["validTime"].GetUint64();
|
||||
if (descriptor.generation == 0 || descriptor.validTime == 0) {
|
||||
return reject(ParticleFieldAdapterError::InvalidTimestampOrIdentity,
|
||||
"local field generation and validTime must be non-zero");
|
||||
}
|
||||
descriptor.width = grid["width"].GetUint();
|
||||
descriptor.height = grid["height"].GetUint();
|
||||
descriptor.west = bounds["west"].GetDouble();
|
||||
descriptor.south = bounds["south"].GetDouble();
|
||||
descriptor.east = bounds["east"].GetDouble();
|
||||
descriptor.north = bounds["north"].GetDouble();
|
||||
descriptor.layout = vector["layout"].GetString();
|
||||
descriptor.encoding = vector["encoding"].GetString();
|
||||
descriptor.noData = static_cast<float>(vector["noData"].GetDouble());
|
||||
descriptor.byteLength = payloadInfo["byteLength"].GetUint64();
|
||||
if (metadata.HasMember("dataVersion") && metadata["dataVersion"].IsUint64()) {
|
||||
descriptor.dataVersion = metadata["dataVersion"].GetUint64();
|
||||
}
|
||||
if (metadata.HasMember("stride") && metadata["stride"].IsUint()) descriptor.stride = metadata["stride"].GetUint();
|
||||
return ParticleFieldDataAdapter::decode(descriptor, payload, descriptor.generation);
|
||||
}
|
||||
|
||||
} // namespace mbgl::particle
|
||||
@@ -157,6 +157,7 @@ ParticleFieldSnapshot OfflineFixtureParticleAdapter::load(const std::string& fix
|
||||
descriptor.fieldID = fixtureID;
|
||||
descriptor.fieldType = member(metadata, "fieldType").GetString();
|
||||
descriptor.displayDomain = member(metadata, "displayDomain").GetString();
|
||||
descriptor.generation = generation;
|
||||
descriptor.width = grid["width"].GetUint();
|
||||
descriptor.height = grid["height"].GetUint();
|
||||
descriptor.west = bounds["west"].GetDouble();
|
||||
|
||||
@@ -207,6 +207,30 @@ std::optional<std::unique_ptr<Source>> convertImageSource(const std::string& id,
|
||||
std::optional<std::unique_ptr<Source>> convertRasterArraySource(const std::string& id,
|
||||
const Convertible& value,
|
||||
Error& error) {
|
||||
if (const auto localFieldsValue = objectMember(value, "production-fields")) {
|
||||
if (!isArray(*localFieldsValue) || arrayLength(*localFieldsValue) < 2) {
|
||||
error.message = "raster-array production-fields must contain at least two entries";
|
||||
return std::nullopt;
|
||||
}
|
||||
std::vector<RasterArrayLocalFieldDescriptor> localFields;
|
||||
for (size_t index = 0; index < arrayLength(*localFieldsValue); ++index) {
|
||||
const auto entry = arrayMember(*localFieldsValue, index);
|
||||
const auto metadata = objectMember(entry, "metadata");
|
||||
const auto field = objectMember(entry, "field");
|
||||
if (!metadata || !field) {
|
||||
error.message = "raster-array production field requires metadata and field";
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto metadataURL = toString(*metadata);
|
||||
const auto fieldURL = toString(*field);
|
||||
if (!metadataURL || !fieldURL) {
|
||||
error.message = "raster-array production field inputs must be strings";
|
||||
return std::nullopt;
|
||||
}
|
||||
localFields.push_back({*metadataURL, *fieldURL});
|
||||
}
|
||||
return {std::make_unique<RasterArraySource>(id, std::move(localFields))};
|
||||
}
|
||||
const auto fixture = objectMember(value, "fixture");
|
||||
const auto metadata = objectMember(value, "metadata");
|
||||
const auto field = objectMember(value, "field");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <mbgl/style/sources/raster_array_source.hpp>
|
||||
|
||||
#include <mbgl/particle/field/offline_fixture_particle_adapter.hpp>
|
||||
#include <mbgl/particle/field/local_particle_field_feed.hpp>
|
||||
#include <mbgl/storage/file_source.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/style/source_observer.hpp>
|
||||
@@ -203,6 +204,9 @@ RasterArraySource::RasterArraySource(std::string id,
|
||||
std::move(nextFieldURL),
|
||||
std::move(runtimeFields))) {}
|
||||
|
||||
RasterArraySource::RasterArraySource(std::string id, std::vector<RasterArrayLocalFieldDescriptor> localFields)
|
||||
: Source(makeMutable<Impl>(std::move(id), std::move(localFields))) {}
|
||||
|
||||
RasterArraySource::~RasterArraySource() {
|
||||
weakFactory.invalidateWeakPtrs();
|
||||
}
|
||||
@@ -213,6 +217,39 @@ const RasterArraySource::Impl& RasterArraySource::impl() const {
|
||||
|
||||
void RasterArraySource::loadDescription(FileSource& fileSource) {
|
||||
if (metadataRequest || fieldRequest) return;
|
||||
if (!impl().localFields.empty()) {
|
||||
localFields.reserve(impl().localFields.size());
|
||||
for (const auto& descriptor : impl().localFields) {
|
||||
LocalFieldCandidate candidate;
|
||||
candidate.descriptor = descriptor;
|
||||
localFields.push_back(std::move(candidate));
|
||||
const auto index = localFields.size() - 1;
|
||||
auto& local = localFields.back();
|
||||
local.metadataRequest = fileSource.request(Resource::source(local.descriptor.metadataURL),
|
||||
[this, self = makeWeakPtr(), index](const Response& response) {
|
||||
if (auto guard = self.lock(); self) {
|
||||
if (response.error || response.noContent || !response.data) {
|
||||
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("particle local metadata request failed")));
|
||||
return;
|
||||
}
|
||||
localFields[index].metadata = *response.data;
|
||||
tryLoadLocalField(index);
|
||||
}
|
||||
});
|
||||
local.fieldRequest = fileSource.request(Resource::source(local.descriptor.fieldURL),
|
||||
[this, self = makeWeakPtr(), index](const Response& response) {
|
||||
if (auto guard = self.lock(); self) {
|
||||
if (response.error || response.noContent || !response.data) {
|
||||
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("particle local field request failed")));
|
||||
return;
|
||||
}
|
||||
localFields[index].fieldBytes = std::vector<char>(response.data->begin(), response.data->end());
|
||||
tryLoadLocalField(index);
|
||||
}
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
metadataRequest = fileSource.request(
|
||||
Resource::source(impl().metadataURL), [this, self = makeWeakPtr()](const Response& response) {
|
||||
if (auto guard = self.lock(); self) {
|
||||
@@ -371,17 +408,68 @@ void RasterArraySource::tryLoadRuntimeField(const size_t index) {
|
||||
}
|
||||
}
|
||||
|
||||
void RasterArraySource::tryLoadLocalField(const size_t index) {
|
||||
auto& candidate = localFields.at(index);
|
||||
if (candidate.snapshot || !candidate.metadata || !candidate.fieldBytes) return;
|
||||
const auto result = particle::LocalParticleFieldFeed::load(*candidate.metadata, *candidate.fieldBytes);
|
||||
if (!result) {
|
||||
const auto fieldID = candidate.metadata->find("fieldId") == std::string::npos ? "" : "metadata";
|
||||
Log::Warning(Event::Render, "ParticleField: event=ADAPTER_REJECTED sourceId=" + getID() +
|
||||
" errorCode=" + particle::particleFieldAdapterErrorName(result.error) +
|
||||
" fieldId=" + fieldID + " payloadBytes=" + std::to_string(candidate.fieldBytes->size()) +
|
||||
" error=" + result.message);
|
||||
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(result.message)));
|
||||
return;
|
||||
}
|
||||
candidate.snapshot = std::move(*result.snapshot);
|
||||
Log::Info(Event::Render, "ParticleField: event=PRODUCTION_FEED_LOAD fieldId=" + candidate.snapshot->fieldID +
|
||||
" generation=" + std::to_string(candidate.snapshot->generation) +
|
||||
" validTime=" + std::to_string(candidate.snapshot->validTime) +
|
||||
" payloadBytes=" + std::to_string(candidate.fieldBytes->size()));
|
||||
tryInitializeTimeline();
|
||||
}
|
||||
|
||||
void RasterArraySource::tryInitializeTimeline() {
|
||||
if (timeline || !loaded || !impl().field || !impl().nextField || runtimeFields.empty()) return;
|
||||
if (timeline) return;
|
||||
if (!impl().localFields.empty()) {
|
||||
if (localFields.size() != impl().localFields.size() || localFields.size() < 2) return;
|
||||
for (const auto& candidate : localFields) if (!candidate.snapshot) return;
|
||||
std::vector<RasterArrayTimelineRuntime::Entry> entries;
|
||||
entries.reserve(localFields.size());
|
||||
for (const auto& candidate : localFields) {
|
||||
entries.push_back({std::make_shared<particle::ParticleFieldSnapshot>(*candidate.snapshot), candidate.snapshot->validTime});
|
||||
}
|
||||
try {
|
||||
if (submitInitialRuntimeFieldWindow(*localFields[0].snapshot, *localFields[1].snapshot) !=
|
||||
RasterArrayRuntimeFieldUpdateResult::Applied) {
|
||||
return;
|
||||
}
|
||||
timeline = std::make_shared<RasterArrayTimelineRuntime>(std::move(entries));
|
||||
auto updated = makeMutable<Impl>(impl());
|
||||
updated->timeline = timeline;
|
||||
baseImpl = std::move(updated);
|
||||
loaded = true;
|
||||
Log::Info(Event::Render, "ParticleTimeline: event=READY snapshots=" + std::to_string(localFields.size()));
|
||||
observer->onSourceLoaded(*this);
|
||||
observer->onSourceChanged(*this);
|
||||
} catch (const std::exception& error) {
|
||||
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(error.what())));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!loaded || !impl().field || !impl().nextField || runtimeFields.empty()) return;
|
||||
std::vector<RasterArrayTimelineRuntime::Entry> entries;
|
||||
entries.reserve(runtimeFields.size() + 2);
|
||||
entries.push_back({std::make_shared<particle::ParticleFieldSnapshot>(*impl().field), 0});
|
||||
entries.push_back({std::make_shared<particle::ParticleFieldSnapshot>(*impl().nextField), 40000});
|
||||
entries.push_back({std::make_shared<particle::ParticleFieldSnapshot>(*impl().field),
|
||||
impl().field->validTime ? impl().field->validTime : 0});
|
||||
entries.push_back({std::make_shared<particle::ParticleFieldSnapshot>(*impl().nextField),
|
||||
impl().nextField->validTime ? impl().nextField->validTime : 40000});
|
||||
for (size_t index = 0; index < runtimeFields.size(); ++index) {
|
||||
if (!runtimeFields[index].snapshot) return;
|
||||
auto field = *runtimeFields[index].snapshot;
|
||||
entries.push_back({std::make_shared<particle::ParticleFieldSnapshot>(std::move(field)),
|
||||
80000 + static_cast<uint64_t>(index) * 40000});
|
||||
runtimeFields[index].descriptor.timestamp ? runtimeFields[index].descriptor.timestamp
|
||||
: 80000 + static_cast<uint64_t>(index) * 40000});
|
||||
}
|
||||
try {
|
||||
timeline = std::make_shared<RasterArrayTimelineRuntime>(std::move(entries));
|
||||
@@ -403,6 +491,19 @@ void RasterArraySource::tryInitializeTimeline() {
|
||||
}
|
||||
}
|
||||
|
||||
RasterArrayRuntimeFieldUpdateResult RasterArraySource::submitInitialRuntimeFieldWindow(
|
||||
particle::ParticleFieldSnapshot t0, particle::ParticleFieldSnapshot t1) {
|
||||
if (loaded || !t0.isValid() || !t1.isValid() || t0.generation == 0 || t1.generation == 0 ||
|
||||
t0.validTime >= t1.validTime || !t0.isTimeInterpolationCompatibleWith(t1)) {
|
||||
return rejectRuntimeFieldUpdate(RasterArrayRuntimeFieldUpdateResult::RejectedInvalidSnapshot,
|
||||
"INVALID_INITIAL_WINDOW");
|
||||
}
|
||||
baseImpl = makeMutable<Impl>(impl(), std::move(t0), std::move(t1));
|
||||
loaded = true;
|
||||
Log::Info(Event::Render, "ParticleField: event=SNAPSHOT_SUBMIT kind=INITIAL_T0_T1");
|
||||
return RasterArrayRuntimeFieldUpdateResult::Applied;
|
||||
}
|
||||
|
||||
RasterArrayRuntimeFieldUpdateResult RasterArraySource::rejectRuntimeFieldUpdate(
|
||||
const RasterArrayRuntimeFieldUpdateResult result, const std::string& reason) const {
|
||||
Log::Warning(Event::Render,
|
||||
|
||||
@@ -24,6 +24,8 @@ public:
|
||||
nextMetadataURL(std::move(nextMetadataURL_)),
|
||||
nextFieldURL(std::move(nextFieldURL_)),
|
||||
runtimeFields(std::move(runtimeFields_)) {}
|
||||
Impl(std::string id, std::vector<RasterArrayLocalFieldDescriptor> localFields_)
|
||||
: Source::Impl(SourceType::RasterArray, std::move(id)), localFields(std::move(localFields_)) {}
|
||||
Impl(const Impl&) = default;
|
||||
Impl(const Impl& other, particle::ParticleFieldSnapshot field_)
|
||||
: Source::Impl(other),
|
||||
@@ -34,6 +36,7 @@ public:
|
||||
nextMetadataURL(other.nextMetadataURL),
|
||||
nextFieldURL(other.nextFieldURL),
|
||||
runtimeFields(other.runtimeFields),
|
||||
localFields(other.localFields),
|
||||
timeline(other.timeline),
|
||||
field(std::move(field_)) {}
|
||||
Impl(const Impl& other, particle::ParticleFieldSnapshot field_, particle::ParticleFieldSnapshot nextField_)
|
||||
@@ -49,6 +52,7 @@ public:
|
||||
std::string nextMetadataURL;
|
||||
std::string nextFieldURL;
|
||||
std::vector<RasterArrayRuntimeFieldDescriptor> runtimeFields;
|
||||
std::vector<RasterArrayLocalFieldDescriptor> localFields;
|
||||
std::shared_ptr<RasterArrayTimelineRuntime> timeline;
|
||||
std::optional<particle::ParticleFieldSnapshot> field;
|
||||
std::optional<particle::ParticleFieldSnapshot> nextField;
|
||||
|
||||
Reference in New Issue
Block a user