sudo apt-get install libboost-dev

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/index/rtree.hpp>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;

// Define the point type
typedef bg::model::point<double, 2, bg::cs::cartesian> point;
typedef std::pair<point, unsigned> value;

// Define the GeoJSON feature type
struct GeoJSONFeature {
    std::string type;
    std::map<std::string, std::string> properties;
    point geometry;
};

int main() {
    // Read CSV file (File A)
    std::ifstream csvFile("file_a.csv");
    std::vector<point> pointsA;

    while (csvFile) {
        double lon, lat;
        char comma;
        csvFile >> lon >> comma >> lat;

        if (csvFile)
            pointsA.emplace_back(lon, lat);
    }

    // Read GeoJSON file (File B) and build spatial index
    std::ifstream geojsonFile("file_b.geojson");
    std::vector<GeoJSONFeature> featuresB;
    bgi::rtree<value, bgi::rstar<16>> rtreeB;

    while (geojsonFile) {
        GeoJSONFeature feature;
        geojsonFile >> feature.type;

        if (feature.type == "Feature") {
            geojsonFile >> std::ws; // Skip whitespace

            char c;
            while (geojsonFile.get(c) && c != '}') {
                if (c == '"') {
                    std::string key, value;
                    std::getline(geojsonFile, key, '"');
                    geojsonFile >> c >> value;
                    feature.properties[key] = value;
                }
            }

            geojsonFile >> std::ws; // Skip whitespace
            geojsonFile.ignore(2); // Ignore "type" and ","
            geojsonFile >> feature.geometry;

            featuresB.push_back(feature);
            rtreeB.insert({ feature.geometry, featuresB.size() - 1 });
        }
    }

    // Define query distance (adjust as needed)
    double queryDistance = 0.1;

    // Find nearby points and output to result GeoJSON file
    std::ofstream resultGeoJSON("result.geojson");

    for (const auto& pointA : pointsA) {
        // Create a bounding box for the query
        bg::model::box<point> queryBox;
        bg::envelope(pointA, queryBox);
        bg::buffer(queryBox, queryBox, queryDistance);

        // Query the spatial index for nearby features
        std::vector<value> result;
        rtreeB.query(bgi::intersects(queryBox), std::back_inserter(result));

        // Output nearby features to result GeoJSON file
        for (const auto& match : result) {
            const GeoJSONFeature& nearbyFeature = featuresB[match.second];

            resultGeoJSON << "{\n";
            resultGeoJSON << "  \"type\": \"Feature\",\n";
            resultGeoJSON << "  \"properties\": {\n";

            for (const auto& property : nearbyFeature.properties) {
                resultGeoJSON << "    \"" << property.first << "\": \"" << property.second << "\",\n";
            }

            resultGeoJSON << "  },\n";
            resultGeoJSON << "  \"geometry\": {\n";
            resultGeoJSON << "    \"type\": \"Point\",\n";
            resultGeoJSON << "    \"coordinates\": [" << bg::get<0>(nearbyFeature.geometry) << ", " << bg::get<1>(nearbyFeature.geometry) << "]\n";
            resultGeoJSON << "  }\n";
            resultGeoJSON << "}\n";
        }
    }

    std::cout << "Processing completed. Results saved in result.geojson." << std::endl;

    return 0;
}
 

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值