随机提取POI点

项目场景:gdal抽样点

提示:这里简述项目相关背景:


问题描述

提示:这里描述项目中遇到的问题:

根据属性抽样点,输入为一个shp文件,要抽样的属性字符串,抽样比例
输出为抽样后点的属性和坐标

#include <ogrsf_frmts.h>
#include <iostream>
#include <gdal.h>
#include <gdal_priv.h>
#include <bits/stdc++.h>
using namespace std;
int fieldCount;
vector<string> defns;
map<int,vector<OGRFeature*>> mp;
vector<OGRFeature*> select;
int tot=-1;
vector<int> GenerateDiffNumber(int min,int max,int num)
{
    int rnd;
    vector<int> diff;
    vector<int> tmp;//存储剩余的数
    //初始化
    for(int i = min;i < max+1 ; i++ )
    {
        tmp.push_back(i);
    }
    srand((unsigned)time(0)); //初始化随机数种子
    for(int i = 0 ; i < num ; i++)
    {
        do{
            rnd = min+rand()%(max-min+1);

        }while(tmp.at(rnd-min)==-1);
        diff.push_back(rnd);
        tmp.at(rnd-min) = -1;
    }
    return diff;
}

void select_POI(vector<OGRFeature*> &v,double p,vector<OGRFeature*> &output){
    int sum=v.size();
    int need=sum*p;
    vector<int> t=GenerateDiffNumber(0,sum-1,need); //从0到sum-1随机生成need个数
    for (int i=0;i<t.size();i++){
        output.push_back((v[t[i]]));
    }
}
/*
void print_POI(vector<OGRFeature*> &output){
    OGRFeature * feature;
    for (int i=0;i<output.size();i++) {
        feature = output[i];
        if (feature == NULL) {
            cout << "数据读取结束" << endl;
            break;
        }
        for (int i = 0; i < fieldCount; i++) {
            OGRFieldDefn *fieldDefn = feature->GetFieldDefnRef(i);
            const char *fieldName = fieldDefn->GetNameRef();
            OGRFieldType fieldType = fieldDefn->GetType();
            switch (fieldType) {
                case OFTString:
                    printf("%s:%s\t", fieldName, feature->GetFieldAsString(i));
                    break;
                case OFTReal:
                    printf("%s:%0.5f\t", fieldName, feature->GetFieldAsDouble(i));
                    break;
                default:
                    break;
            }
        }
        //输出坐标
        OGRGeometry *geom = feature->GetGeometryRef();
        OGRPoint *point = (OGRPoint *) geom;
        cout << point->getX() << '\t';
        cout << point->getY() << '\t';
        cout << point->getZ() << '\t';
        cout << endl;
    }
}
*/
int main()
{
    cout<<"test"<<endl;
    //支持中文路径
    //CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
    //支持属相表中文字符
    //CPLGetConfigOption("SHAPE_ENCODING", "UTF-8");
    const char * shpFile = "C:/Users/syz_c/Desktop/lele/shp/test1.shp";
    GDALAllRegister();
    GDALDataset * ds;
    ds = (GDALDataset *)GDALOpenEx(shpFile, GDAL_OF_VECTOR and GDAL_OF_READONLY,NULL,NULL,NULL);
    if (ds == NULL)
    {
        cout << "打开数据失败!" << endl;
        return 0;
    }
    OGRLayer * lyr = ds->GetLayer(0);
    lyr->ResetReading();
    OGRFeatureDefn * lyrDefn = lyr->GetLayerDefn();
    fieldCount = lyrDefn->GetFieldCount();
    while (true) {
        OGRFeature *feature;
        feature = lyr->GetNextFeature();
        if (feature == NULL) {
            cout << "数据读取结束" << endl;
            break;
        }
        for (int i = 0; i < fieldCount; i++) {
            OGRFieldDefn *fieldDefn = feature->GetFieldDefnRef(i);
            const char *fieldName = fieldDefn->GetNameRef();
            OGRFieldType fieldType = fieldDefn->GetType();
            if (fieldType==OFTString) {
                const char *kind=feature->GetFieldAsString(i);
                string s = kind;
                int num = -1; //属于第几种类型
                for (int j = 0; j < defns.size(); j++) {
                    if (s == defns[j]) {
                        num = j;
                        break;
                    }
                }
                if (num == -1) {
                    tot += 1;
                    num = tot;
                    defns.push_back(s);
                }
                mp[num].push_back(feature); //把该feature放入对应的vector中
            }
        }
    }
    string ss; double a;
    cin>>ss;
    cin>>a;
    bool flag=false;
    for (int i=0;i<defns.size();i++){
        if (defns[i]==ss){
            flag=true;
            select_POI(mp[i],a,select);
        }
    }
    if (!flag) cout<<"没找到这种属性"<<endl;
    //print_POI(select);
    return 0;
}





代码逻辑:

首先读入shp文件,使用gdal库,然后对于某个feature遍历所有属性,将属性记录下来,把该feature放到属性所对应的vector中,defns是属性的vector,下标对应的feature集合是mp
GenerateDiffNumber是生成不重复的随机数
select_POI是随机取feature,保存进output中
print_POI是打印输出,由于环境问题还在调试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值