【无标题】萨芬

Free

两个列表分别为A和B,列表中的元素为点的经纬度,将A列表中每个点附近的B列表中的点打印出来

您好!您的需求可以通过循环嵌套来实现。以下是Python语言的代码示例:

 

pythonCopy

for a_point in A:
    nearby_points = []
    for b_point in B:
        # 计算两个点之间的距离
        distance = calculate_distance(a_point, b_point)
        if distance < threshold:  # 如果距离小于阈值,则认为B中的点在A点附近
            nearby_points.append(b_point)
    print("点", a_point, "附近的点有:", nearby_points)
 

其中,calculate_distance函数用于计算两个点之间的距离,threshold是一个阈值,用于判断两个点是否在附近。您需要根据实际情况来确定这些参数的值。

希望这可以帮助到您!

使用c++代码实现

好的,以下是C++语言的代码示例,与之前的Python代码逻辑相同:

 

cppCopy

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

struct Point {
    double longitude;
    double latitude;
};

double calculate_distance(Point p1, Point p2) {
    double dlon = p2.longitude - p1.longitude;
    double dlat = p2.latitude - p1.latitude;
    double a = pow(sin(dlat/2), 2) + cos(p1.latitude) * cos(p2.latitude) * pow(sin(dlon/2), 2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    double distance = 6371 * c;  // 6371是地球半径,单位为千米
    return distance;
}

int main() {
    vector<Point> A = {{30.0, 40.0}, {31.0, 41.0}, {32.0, 42.0}};  // A列表中的点
    vector<Point> B = {{30.5, 40.5}, {31.5, 41.5}, {32.5, 42.5}};  // B列表中的点
    double threshold = 1.0;  // 阈值

    for (auto a_point : A) {
        vector<Point> nearby_points;
        for (auto b_point : B) {
            double distance = calculate_distance(a_point, b_point);
            if (distance < threshold) {
                nearby_points.push_back(b_point);
            }
        }
        cout << "点(" << a_point.longitude << ", " << a_point.latitude << ")附近的点有:";
        for (auto nearby_point : nearby_points) {
            cout << "(" << nearby_point.longitude << ", " << nearby_point.latitude << ") ";
        }
        cout << endl;
    }

    return 0;
}

 

这里使用了C++11的新特性auto和初始化列表来初始化向量。calculate_distance函数计算两个点之间的距离,threshold是阈值。

数据量很大,计算效率最高的方法

在数据量很大的情况下,可以考虑使用空间索引来加速点之间的距离计算和查找。常用的空间索引包括kd-tree和R树等。

这里以kd-tree为例,给出C++语言的代码示例:

 

cppCopy

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

struct Point {
    double longitude;
    double latitude;
};

struct Node {
    Point point;
    int axis;  // 划分轴,0表示经度轴,1表示纬度轴
    Node* left;
    Node* right;
};

struct CompareNode {
    Point query_point;
    bool operator()(Node* n1, Node* n2) {
        double d1 = pow(query_point.longitude - n1->point.longitude, 2) + pow(query_point.latitude - n1->point.latitude, 2);
        double d2 = pow(query_point.longitude - n2->point.longitude, 2) + pow(query_point.latitude - n2->point.latitude, 2);
        return d1 > d2;
    }
};

double calculate_distance(Point p1, Point p2) {
    double dlon = p2.longitude - p1.longitude;
    double dlat = p2.latitude - p1.latitude;
    double a = pow(sin(dlat/2), 2) + cos(p1.latitude) * cos(p2.latitude) * pow(sin(dlon/2), 2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    double distance = 6371 * c;  // 6371是地球半径,单位为千米
    return distance;
}

Node* build_kdtree(vector<Point>& points, int depth) {
    if (points.empty()) return nullptr;
    int axis = depth % 2;  // 交替划分经度和纬度
    auto cmp = [](Point& p1, Point& p2) { return axis == 0 ? p1.longitude < p2.longitude : p1.latitude < p2.latitude; };
    auto median_it = points.begin() + points.size() / 2;
    nth_element(points.begin(), median_it, points.end(), cmp);
    Node* node = new Node{ *median_it, axis };
    node->left = build_kdtree(vector<Point>(points.begin(), median_it), depth + 1);
    node->right = build_kdtree(vector<Point>(median_it + 1, points.end()), depth + 1);
    return node;
}

void search_kdtree(Node* node, Point query_point, double threshold, priority_queue<Node*, vector<Node*>, CompareNode>& pq) {
    if (!node) return;
    double distance = calculate_distance(node->point, query_point);
    if (distance < threshold) {
        pq.push(node);
        if (pq.size() > 10) pq.pop();
        threshold = calculate_distance(query_point, pq.top()->point);
    }
    double delta = node->axis == 0 ? query_point.longitude - node->point.longitude : query_point.latitude - node->point.latitude;
    if (delta < 0) {
        search_kdtree(node->left, query_point, threshold, pq);
        if (delta * delta < threshold) search_kdtree(node->right, query_point, threshold, pq);
    }
    else {
        search_kdtree(node->right, query_point, threshold, pq);
        if (delta * delta < threshold) search_kdtree(node->left, query_point, threshold, pq);
    }
}

int main() {
    vector<Point> A = {{30.0, 40.0}, {31.0, 41.0}, {32.0, 42.0}};  // A列表中的点
    vector<Point> B = {{30.5, 40.5}, {31.5, 41.5}, {32.5, 42.5}};  // B列表中的点
    double threshold = 1.0;  // 阈值

    Node* root = build_kdtree(B, 0);

    for (auto a_point : A) {
        priority_queue<Node*, vector<Node*>, CompareNode> pq{{a_point}};
        search_kdtree(root, a_point, threshold, pq);
        cout << "点(" << a_point.longitude << ", " << a_point.latitude << ")附近的点有:";
        while (!pq.empty()) {
            cout << "(" << pq.top()->point.longitude << ", " << pq.top()->point.latitude << ") ";
            pq.pop();
        }
        cout << endl;
    }

    return 0;
}

 

这里使用了kd-tree来构建B列表中的点的空间索引。build_kdtree函数用于构建kd-tree,search_kdtree函数用于在kd-tree中查找附近的点。这里使用了一个最大堆来保存距离最近的10个点,以避免遍历整个kd-tree。

使用python实现,A列表来自csv文件,b列表来自geojson

当A列表来自CSV文件,B列表来自GeoJSON时,可以使用Python中的第三方库来处理和计算。以下是使用pandasgeopandas库的示例代码:

 

pythonCopy

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point

# 从CSV文件读取A列表数据
df_a = pd.read_csv('a.csv')

# 从GeoJSON文件读取B列表数据
gdf_b = gpd.read_file('b.geojson')

# 将A列表数据转换为Point对象
geometry_a = [Point(x, y) for x, y in zip(df_a['longitude'], df_a['latitude'])]
gdf_a = gpd.GeoDataFrame(df_a, geometry=geometry_a)

# 创建空间索引
gdf_b_sindex = gdf_b.sindex

# 遍历A列表中的点,查找附近的B列表中的点
for a_point in gdf_a['geometry']:
    nearby_points = []
    # 计算A点的边界框
    bbox = a_point.buffer(distance_threshold)
    # 在B列表中查找与A点边界框相交的点
    possible_matches = list(gdf_b_sindex.intersection(bbox.bounds))
    for b_index in possible_matches:
        b_point = gdf_b.loc[b_index, 'geometry']
        # 判断B点是否在A点附近
        if a_point.distance(b_point) < distance_threshold:
            nearby_points.append(b_point)
    print("点", a_point, "附近的点有:", nearby_points)

 

在上述代码中,需要将文件路径a.csvb.geojson替换为实际的文件路径。其中,distance_threshold是距离阈值,用于判断两个点是否在附近。

希望这可以帮助到您!

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <sstream>
#include "csv_parser.hpp"
#include "rapidjson/document.h"
#include "rapidjson/istreamwrapper.h"

using namespace std;
using namespace rapidjson;

struct Point {
    double longitude;
    double latitude;
};

double calculate_distance(Point p1, Point p2) {
    double dlon = p2.longitude - p1.longitude;
    double dlat = p2.latitude - p1.latitude;
    double a = pow(sin(dlat/2), 2) + cos(p1.latitude) * cos(p2.latitude) * pow(sin(dlon/2), 2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    double distance = 6371 * c;  // 6371是地球半径,单位为千米
    return distance;
}

vector<Point> read_csv(string filename) {
    vector<Point> points;
    ifstream file(filename);
    if (file.is_open()) {
        string line;
        while (getline(file, line)) {
            stringstream ss(line);
            string longitude_str, latitude_str;
            getline(ss, longitude_str, ',');
            getline(ss, latitude_str, ',');
            double longitude = stod(longitude_str);
            double latitude = stod(latitude_str);
            points.push_back({longitude, latitude});
        }
        file.close();
    }
    return points;
}

vector<Point> read_geojson(string filename) {
    vector<Point> points;
    ifstream file(filename);
    if (file.is_open()) {
        IStreamWrapper isw(file);
        Document doc;
        doc.ParseStream(isw);
        const Value& features = doc["features"];
        for (SizeType i = 0; i < features.Size(); i++) {
            const Value& geometry = features[i]["geometry"];
            const Value& coordinates = geometry["coordinates"];
            double longitude = coordinates[0].GetDouble();
            double latitude = coordinates[1].GetDouble();
            points.push_back({longitude, latitude});
        }
        file.close();
    }
    return points;
}

int main() {
    vector<Point> A = read_csv("a.csv");  // A列表中的点
    vector<Point> B = read_geojson("b.geojson");  // B列表中的点
    double distance_threshold = 1.0;  // 距离阈值

    for (auto a_point : A) {
        vector<Point> nearby_points;
        for (auto b_point : B) {
            double distance = calculate_distance(a_point, b_point);
            if (distance < distance_threshold) {
                nearby_points.push_back(b_point);
            }
        }
        cout << "点(" << a_point.longitude << ", " << a_point.latitude << ")附近的点有:";
        for (auto nearby_point : nearby_points) {
            cout << "(" << nearby_point.longitude << ", " << nearby_point.latitude << ") ";
        }
        cout << endl;
    }

    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
报错信息"Cannot determine value type from string '萨芬'"表示无法从字符串'萨芬'中确定值的数据类型。这个错误通常发生在使用MyBatis-Plus时尝试从结果集中获取列'user_name'的值时。有几种可能的原因导致这个错误。 首先,可能是实体类'xxx.java'中的数据类型与数据库中对应列的类型不匹配。请确保实体类中的属性与数据库中的列类型相匹配。 其次,可能是由于使用了有参构造而没有提供无参构造函数。在使用MyBatis-Plus时,通常会使用无参构造函数来创建实体对象。请确保实体类中有一个无参构造函数。 最后,可能是输出参数类型设置错误。比如,数据库查询的结果是对象集合,但你将输出参数设置为int或String。请检查你的代码,确保输出参数的类型与查询结果的类型相匹配。 综上所述,解决这个问题的方法包括确认实体类与数据库中的数据类型匹配、提供无参构造函数以及检查输出参数的类型是否正确。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [获取数据无法查到 Cause: Cannot determine value type from string](https://blog.csdn.net/m0_60824353/article/details/128999900)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Cannot determine value type from string ‘xxx](https://blog.csdn.net/qq_51326491/article/details/127930035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值