Skip to content

Commit

Permalink
Add conversion methods from mbgl::GeoJSONFeature to QMapLibreGL::Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
haluk-pointr committed Jul 25, 2022
1 parent 5a384e7 commit 35de3b6
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
200 changes: 200 additions & 0 deletions platform/qt/src/utils/geojson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,57 @@ mbgl::Value asPropertyValue(const QVariant &value) {
}
}

QVariant toQVariant(const mbgl::Value &value){

auto valueList = [](const mapbox::util::recursive_wrapper<std::vector<mbgl::Value>> &list) {
QVariantList varList;
varList.reserve(list.get().size());
for (const auto& listValue : list.get()) {
varList.emplace_back(toQVariant(listValue));
}
return varList;
};

auto valueMap = [](const mapbox::util::recursive_wrapper<std::unordered_map<std::string, mbgl::Value>> &map) {
QVariantMap varMap;
for (auto it = map.get().begin(); it != map.get().end(); ++it) {
varMap.insert(QString::fromStdString(it->first), toQVariant(it->second));
}
return varMap;
};


switch (value.which()){
// Null
case 0:
return {};
// Bool
case 1:
return{value.get<bool>()};
// uint64_t
case 2:
return{value.get<uint64_t>()};
// int64_t
case 3:
return{value.get<int64_t>()};
// double
case 4:
return{value.get<double>()};
// std::string
case 5:
return QString::fromStdString(value.get<std::string>());
//mapbox::util::recursive_wrapper<std::vector<value>>
case 6:
return valueList(value.get<mapbox::util::recursive_wrapper<std::vector<mbgl::Value>>>());
//mapbox::util::recursive_wrapper<std::unordered_map<std::string, value>>
case 7:
return valueMap(value.get<mapbox::util::recursive_wrapper<std::unordered_map<std::string, mbgl::Value>>>());
default:
qWarning();
return {};
}
}

mbgl::FeatureIdentifier asFeatureIdentifier(const QVariant &id) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
switch (id.typeId()) {
Expand All @@ -126,6 +177,126 @@ mbgl::FeatureIdentifier asFeatureIdentifier(const QVariant &id) {
}
}

QVariant toQVariant(const mbgl::FeatureIdentifier &id){
switch (id.which()){
// Null
case 0:
return {};
// uint64_t
case 1:
return{id.get<uint64_t>()};
// int64_t
case 2:
return{id.get<int64_t>()};
// double
case 3:
return{id.get<double>()};
// std::string
case 4:
return QString::fromStdString(id.get<std::string>());
default:
qWarning();
return {};
}
}

// CoordinatesCollections toCoordinatesCollections(const mbgl::Feature::geometry_type & geometry){
// CoordinatesCollections coordinates;

// return coordinates;
// }


Coordinate toCoordinate (const mbgl::Point<double> &point){
return {point.y, point.x};
}

Coordinates toCoordinates (const mbgl::LineString<double> &lineString){
Coordinates coordinates;
for(auto const & point: lineString){
coordinates.push_back(toCoordinate(point));
}
return coordinates;
}

Coordinates toCoordinates (const mbgl::MultiPoint<double> &points){
Coordinates coordinates;
for(auto const & point: points){
coordinates.push_back(toCoordinate(point));
}
return coordinates;
}

CoordinatesCollection toCoordinatesCollection (const mbgl::Polygon<double> &polygon){
CoordinatesCollection coordinatesCollection;
for (auto const & linearRing: polygon){
Coordinates coordinates;
for(auto const &point: linearRing){
coordinates.push_back(toCoordinate(point));
}
coordinatesCollection.push_back(coordinates);
}
return coordinatesCollection;
}

CoordinatesCollection toCoordinatesCollection (const mbgl::MultiLineString<double> &lineStrings){
CoordinatesCollection coordinatesCollection;
for (auto const & lineString: lineStrings){
Coordinates coordinates;
for(auto const &point: lineString){
coordinates.push_back(toCoordinate(point));
}
coordinatesCollection.push_back(coordinates);
}
return coordinatesCollection;
}

CoordinatesCollections toCoordinatesCollections (const mbgl::Point<double> &point){
CoordinatesCollections coordinatesCollections;
CoordinatesCollection coordinatesCollection;
Coordinates coordinates;
coordinates.push_back(toCoordinate(point));
coordinatesCollection.push_back(coordinates);
coordinatesCollections.push_back(coordinatesCollection);
return coordinatesCollections;
}

CoordinatesCollections toCoordinatesCollections (const mbgl::LineString<double> &lineString){
CoordinatesCollections coordinatesCollections;
CoordinatesCollection coordinatesCollection;
coordinatesCollection.push_back(toCoordinates(lineString));
coordinatesCollections.push_back(coordinatesCollection);
return coordinatesCollections;
}

CoordinatesCollections toCoordinatesCollections (const mbgl::Polygon<double> &polygon){
CoordinatesCollections coordinatesCollections;
coordinatesCollections.push_back(toCoordinatesCollection(polygon));
return coordinatesCollections;
}

CoordinatesCollections toCoordinatesCollections (const mbgl::MultiPoint<double> &points){
CoordinatesCollections coordinatesCollections;
CoordinatesCollection coordinatesCollection;
coordinatesCollection.push_back(toCoordinates(points));
coordinatesCollections.push_back(coordinatesCollection);
return coordinatesCollections;
}

CoordinatesCollections toCoordinatesCollections (const mbgl::MultiLineString<double> &lineStrings){
CoordinatesCollections coordinatesCollections;
coordinatesCollections.push_back(toCoordinatesCollection(lineStrings));
return coordinatesCollections;
}

CoordinatesCollections toCoordinatesCollections (const mbgl::MultiPolygon<double> &polygons){
CoordinatesCollections coordinatesCollections;
for(auto const & polygon: polygons){
coordinatesCollections.push_back(toCoordinatesCollection(polygon));
}
return coordinatesCollections;
}

mbgl::GeoJSONFeature asFeature(const Feature &feature) {
mbgl::PropertyMap properties;
properties.reserve(feature.properties.size());
Expand Down Expand Up @@ -159,4 +330,33 @@ mbgl::GeoJSONFeature asFeature(const Feature &feature) {
}
};

Feature toFeature(const mbgl::GeoJSONFeature &feature){
QVariantMap properties;
for( auto it= feature.properties.begin(); it!=feature.properties.end(); it++) {
properties.insert(QString::fromStdString(it->first), toQVariant(it->second));
}

auto id = toQVariant(feature.id);

// auto geometry = toCoordinatesCollections(feature.geometry);
switch (feature.geometry.which()){
case 0:
return {Feature::PointType, CoordinatesCollections(), properties, id};
case 1:
return {Feature::PointType, toCoordinatesCollections(feature.geometry.get<mbgl::Point<double>>()), std::move(properties), std::move(id)};
case 2:
return {Feature::LineStringType, toCoordinatesCollections(feature.geometry.get<mbgl::LineString<double>>()), std::move(properties), std::move(id)};
case 3:
return {Feature::PolygonType, toCoordinatesCollections(feature.geometry.get<mbgl::Polygon<double>>()), std::move(properties), std::move(id)};
case 4:
return {Feature::PointType, toCoordinatesCollections(feature.geometry.get<mbgl::MultiPoint<double>>()), std::move(properties), std::move(id)};
case 5:
return {Feature::LineStringType, toCoordinatesCollections(feature.geometry.get<mbgl::MultiLineString<double>>()), std::move(properties), std::move(id)};
case 6:
return {Feature::PolygonType, toCoordinatesCollections(feature.geometry.get<mbgl::MultiPolygon<double>>()), std::move(properties), std::move(id)};
default:
return {};
}
}

} // namespace QMapLibreGL::GeoJSON
2 changes: 2 additions & 0 deletions platform/qt/src/utils/geojson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ mbgl::Value asPropertyValue(const QVariant &value);
mbgl::FeatureIdentifier asFeatureIdentifier(const QVariant &id);
mbgl::GeoJSONFeature asFeature(const Feature &feature);

Feature toFeature(const mbgl::GeoJSONFeature &feature);

} // namespace QMapLibreGL::GeoJSON

0 comments on commit 35de3b6

Please sign in to comment.