Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for updating and deleting MapData features #2112

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions core/include/tangram/data/clientDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,29 @@ class ClientDataSource : public TileSource {
// Add geometry from a GeoJSON string
void addData(const std::string& _data);

void addPointFeature(Properties&& properties, LngLat coordinates);
uint64_t addPointFeature(Properties&& properties, LngLat coordinates);

void addPolylineFeature(Properties&& properties, PolylineBuilder&& polyline);
uint64_t addPolylineFeature(Properties&& properties, PolylineBuilder&& polyline);

void addPolygonFeature(Properties&& properties, PolygonBuilder
&& polygon);
uint64_t addPolygonFeature(Properties&& properties, PolygonBuilder&& polygon);

void updatePointFeature(uint64_t id, const LngLat coordinate);

void updatePolylineFeature(uint64_t id, const Coordinates& coordinates);

void updatePolygonFeature(uint64_t id, const Coordinates& coordinates);

void updatePointFeature(uint64_t id, const Properties&& properties);

void updatePolylineFeature(uint64_t id, const Properties&& properties);

void updatePolygonFeature(uint64_t id, const Properties&& properties);

void removePointFeature(uint64_t id);

void removePolylineFeature(uint64_t id);

void removePolygonFeature(uint64_t id);

// Transform added feature data into tiles.
void generateTiles();
Expand Down
204 changes: 195 additions & 9 deletions core/src/data/clientDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ struct ClientDataSource::Storage {
std::unique_ptr<geojsonvt::GeoJSONVT> tiles;
geometry::feature_collection<double> features;
std::vector<Properties> properties;
std::map<uint64_t, uint64_t> pointIds;
std::map<uint64_t, uint64_t> polylineIds;
std::map<uint64_t, uint64_t> polygonIds;
};

struct ClientDataSource::PolylineBuilderData : mapbox::geometry::line_string<double> {
Expand Down Expand Up @@ -249,35 +252,218 @@ void ClientDataSource::addData(const std::string& _data) {
std::make_move_iterator(features.end()));
}

void ClientDataSource::addPointFeature(Properties&& properties, LngLat coordinates) {
uint64_t ClientDataSource::addPointFeature(Properties&& properties, LngLat coordinates) {

std::lock_guard<std::mutex> lock(m_mutexStore);

geometry::point<double> geom {coordinates.longitude, coordinates.latitude};

uint64_t id = m_store->features.size();
m_store->features.emplace_back(geom, id);
uint64_t i = m_store->features.size();
uint64_t id = m_store->pointIds.size();

properties.set("id", std::to_string(id));

m_store->features.emplace_back(geom, i);
m_store->properties.emplace_back(properties);
m_store->pointIds.insert(std::pair<uint64_t, uint64_t>(id, i));

return id;
}

void ClientDataSource::addPolylineFeature(Properties&& properties, PolylineBuilder&& polyline) {
uint64_t ClientDataSource::addPolylineFeature(Properties&& properties, PolylineBuilder&& polyline) {

std::lock_guard<std::mutex> lock(m_mutexStore);

uint64_t id = m_store->features.size();
uint64_t i = m_store->features.size();
uint64_t id = m_store->polylineIds.size();

properties.set("id", std::to_string(id));

auto geom = std::move(polyline.data);
m_store->features.emplace_back(*geom, id);
m_store->features.emplace_back(*geom, i);
m_store->properties.emplace_back(properties);
m_store->polylineIds.insert(std::pair<uint64_t, uint64_t>(id, i));

return id;
}

void ClientDataSource::addPolygonFeature(Properties&& properties, PolygonBuilder&& polygon) {
uint64_t ClientDataSource::addPolygonFeature(Properties&& properties, PolygonBuilder&& polygon) {

std::lock_guard<std::mutex> lock(m_mutexStore);

uint64_t id = m_store->features.size();
uint64_t i = m_store->features.size();
uint64_t id = m_store->polygonIds.size();

properties.set("id", std::to_string(id));

auto geom = std::move(polygon.data);
m_store->features.emplace_back(*geom, id);
m_store->features.emplace_back(*geom, i);
m_store->properties.emplace_back(properties);
m_store->polygonIds.insert(std::pair<uint64_t, uint64_t>(id, i));

return id;
}

void ClientDataSource::updatePointFeature(uint64_t id, const LngLat coordinate) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->pointIds.find(id);

if (findIt != m_store->pointIds.end() && findIt->second < m_store->features.size()) {
uint64_t i = findIt->second;

geometry::point<double> geom {coordinate.longitude, coordinate.latitude};

m_store->features[i] = mapbox::geometry::feature<double>(geom, i);
}
}

void ClientDataSource::updatePolylineFeature(uint64_t id, const Coordinates& coordinates) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polylineIds.find(id);

if (findIt != m_store->polylineIds.end() && findIt->second < m_store->features.size()) {
uint64_t i = findIt->second;

geometry::line_string<double> geom;
for (auto &p : coordinates) {
geom.emplace_back(p.longitude, p.latitude);
}

m_store->features[i] = mapbox::geometry::feature<double>(geom, i);
}
}

void ClientDataSource::updatePolygonFeature(uint64_t id, const Coordinates& coordinates) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polygonIds.find(id);

if (findIt != m_store->polygonIds.end() && findIt->second < m_store->features.size()) {
uint64_t i = findIt->second;

geometry::line_string<double> geom;
for (auto &p : coordinates) {
geom.emplace_back(p.longitude, p.latitude);
}

m_store->features[i] = mapbox::geometry::feature<double>(geom, i);
}
}

void ClientDataSource::updatePointFeature(uint64_t id, const Properties&& properties) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->pointIds.find(id);

if (findIt != m_store->pointIds.end() && findIt->second < m_store->properties.size()) {
uint64_t i = findIt->second;

for (int j = 0; j < properties.items().size(); ++j) {
m_store->properties[i].set(properties.items()[j].key,
properties.getString(properties.items()[j].key));
}
}
}

void ClientDataSource::updatePolylineFeature(uint64_t id, const Properties&& properties) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polylineIds.find(id);

if (findIt != m_store->polylineIds.end() && findIt->second < m_store->properties.size()) {
uint64_t i = findIt->second;

for (int j = 0; j < properties.items().size(); ++j) {
m_store->properties[i].set(properties.items()[j].key,
properties.getString(properties.items()[j].key));
}
}
}

void ClientDataSource::updatePolygonFeature(uint64_t id, const Properties&& properties) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polygonIds.find(id);

if (findIt != m_store->polygonIds.end() && findIt->second < m_store->properties.size()) {
uint64_t i = findIt->second;

for (int j = 0; j < properties.items().size(); ++j) {
m_store->properties[i].set(properties.items()[j].key,
properties.getString(properties.items()[j].key));
}
}
}

void ClientDataSource::removePointFeature(uint64_t id) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->pointIds.find(id);

if (findIt != m_store->pointIds.end() && findIt->second < m_store->features.size()) {
uint64_t i = findIt->second;

m_store->features.erase(std::next(m_store->features.begin(), i));
m_store->properties.erase(std::next(m_store->properties.begin(), i));

for (std::map<uint64_t, uint64_t>::iterator it = m_store->pointIds.begin(); it != m_store->pointIds.end(); ++it) {
if (it->second > i) {
it->second = it->second - 1;
m_store->features[it->second].id = it->second;
}
}
}
}

void ClientDataSource::removePolylineFeature(uint64_t id) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polylineIds.find(id);

if (findIt != m_store->polylineIds.end() && findIt->second < m_store->features.size()) {
uint64_t i = findIt->second;

m_store->features.erase(std::next(m_store->features.begin(), i));
m_store->properties.erase(std::next(m_store->properties.begin(), i));

for (std::map<uint64_t, uint64_t>::iterator it = m_store->polylineIds.begin(); it != m_store->polylineIds.end(); ++it) {
if (it->second > i) {
it->second = it->second - 1;
m_store->features[it->second].id = it->second;
}
}
}
}

void ClientDataSource::removePolygonFeature(uint64_t id) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polygonIds.find(id);

if (findIt != m_store->polygonIds.end() && findIt->second < m_store->features.size()) {
uint64_t i = findIt->second;

m_store->features.erase(std::next(m_store->features.begin(), i));
m_store->properties.erase(std::next(m_store->properties.begin(), i));

for (std::map<uint64_t, uint64_t>::iterator it = m_store->polygonIds.begin(); it != m_store->polygonIds.end(); ++it) {
if (it->second > i) {
it->second = it->second - 1;
m_store->features[it->second].id = it->second;
}
}
}
}

struct add_geometry {
Expand Down
Loading