制作一个 class 来访问和指定矢量类型并构建一个 class 来获取位置并为其分配区域
Making a class to access and appoint vector types and building a class which gets positions and assigns a region to it
我正在分析如下所示的数据集
#Latitude Longitude Depth [m] Bathy depth [m] CaCO3 [%] ...
-78 -177 0 693 1
-78 -173 0 573 2
.
.
计划是能够将数据读入一个向量,然后将其分解成不同的 "Bathy depth"(测深)组。它还需要同时归入不同的海洋盆地。例如,北大西洋内的所有数据点,也就是介于 500-1500m、1000-2000m、1500-2500m 的 BathyDepth 之间的所有数据点……都应该在其自己的组中(可能是矢量或其他对象)。这个想法是能够将这些输出到不同的文本文件中。
我尝试过这个有点结结巴巴。你可以在下面看到这个
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <GeographicLib/Geodesic.hpp> //Library which allows for distance calculations
using namespace std;
using namespace GeographicLib;
//Define each basin spatially
//North Atlantic
double NAtlat1 = 1, NAtlong1 = 1, NAtlat2 = 2, NAtlong2=2; //Incorrect values, to be set later
//South Atlantic
double SAtlat1 = 1, SAtlong1 = 1, SAtlat2 = 2, SAtlong2=2;
//North Pacific and the all others...
struct Point
{
//structure Sample code/label--Lat--Long--SedimentDepth[m]--BathymetricDepth[m]--CaCO3[%]...
string dummy;
double latitude, longitude, rockDepth, bathyDepth, CaCO3, fCaCO3, bSilica, Quartz, CO3i, CO3c, DCO3;
string dummy2;
//Use Overload>> operator
friend istream& operator>>(istream& inputFile, Point& p);
};
//Overload operator
istream& operator>>(istream& inputFile, Point& p)
{
string row_text;
//Read line from input, store in row_text
getline(inputFile, row_text);
//Extract line, store in ss row_stream
istringstream row_stream(row_text);
//Read-in data into each variable
row_stream >> p.dummy >> p.latitude >> p.longitude >> p.rockDepth >> p.bathyDepth >> p.CaCO3 >> p.fCaCO3 >> p.bSilica >> p.Quartz >> p.CO3i >> p.CO3c >> p.DCO3 >> p.dummy2;
return inputFile;
}
int main ()
{
//Geodesic class.
const Geodesic& geod = Geodesic::WGS84();
//Input file
ifstream inputFile("Data.csv");
//Point-type vector to store all data
vector<Point> database;
//bathDepth515 = depths between 500m and 1500m
vector<Point> bathyDepth515;
vector<Point> bathyDepth1020; //Create the rest too
Point p;
if (inputFile)
{
while(inputFile >> p)
{
database.push_back(p);
}
inputFile.close();
}
else
{
cout <<"Unable to open file";
}
for(int i = 0; i < database.size(); ++i)
{
//Group data in database in sets of bathyDepth
if(database[i].bathyDepth >= 500 && database[i].bathyDepth < 1500)
{
//Find and fill particular bathDepths
bathyDepth515.push_back(database[i]);
}
if(database[i].bathyDepth >= 1000 && database[i].bathyDepth < 2000)
{
bathyDepth1020.push_back(database[i]);
}
//...Further conditional statements based on Bathymetric depth, could easily include a spatial condition too...
//Calculate distance between point i and all other points within 500-1500m depth window. Do the same with all other windows.
for(int i = 0; i < bathyDepth515.size(); ++i)
{
for(int j = 0; j < bathyDepth515.size(); ++j)
{
double s12;
if(i != j)
{
geod.Inverse(bathyDepth515[i].latitude, bathyDepth515[i].longitude, bathyDepth515[j].latitude, bathyDepth515[j].longitude, s12);
}
}
}
return 0;
}
问题 1:
我认为很明显,有些方法论不是面向对象的。例如,可能有更好的方法将每个数据 Point
分配给特定的海洋盆地,而不是像我在按深度对数据分组时所做的那样,在我的程序开始时手动将它们放入。我开始创建一个盆地 class,其中包含检索位置的方法以及每个盆地的定义 lat/long,但在寻找一种直观的方法方面并没有走得太远。我希望有人能告诉我如何更好地构建它。我尝试构建一个(非常脆弱的)class 如下
class Basin
{
public:
Basin();
Basin(double latit1, double longit1, double latit2, double longit2);
double getLatitude();
...
private:
double NAt, SAt, NPac, SPac; //All basins
double latitude1, longitude1, latitude2, longitude2; // Boundaries defined by two latitude markers, and two longitude markers.
};
class NAt: public Basin{...}
//...Build class definitions...
问题二:
我的第二个问题是我为不同深度 windows 创建向量的方法。如果我必须改变分割深度的方式或添加更多深度,这可能会变得非常麻烦。我不想仅仅为了适应我决定如何滑动我的深度 windows 就必须更改程序的几乎所有部分。我很感激有人给我一些关于如何去做的想法。
对于问题 2,我能想到的唯一解决方案如下
//Vector of vectors to store each rows' entries separately. Not sure if this is needed as my `Point` `Struct` already allows for each of the entries in a row to be accessed.
vector<vector<Point> > database;
//Create database vectors for different bathDepth windows, eventhough their names will be the same, once push_back, it doesn't matter
for(int i = 1*500; i<=8*500; i+=500)
{
vector<Point> bathDepthi;
//Possibly push these back into database. These can then be accessed and populated using if statements.
}
//Create vectors individually, creating vector of vectors would be more elegant as I won't have to create more if one were to decide to change the range, I'd just have to change the for loop.
我不知道我在这方面的尝试有多大帮助,但我想我可能会更好地了解我想要达到的目的。抱歉这么长 post.
编辑设计从 std::vector
切换到 std::map
根据 heke 的 posted 回答的输入,我尝试了以下方法,但我不确定这是否是上述用户的意思。我选择使用条件语句来确定一个点是否在特定盆地内。如果我的尝试是正确的,如果这是正确的,我仍然不确定如何继续。更具体地说,我不确定如何存储以访问分区向量,例如,将它们写入单独的文本文件(即不同测深深度的 .txt 文件)。如果是这样,我应该将分区迭代器存储到什么类型的向量中?迭代器是用 auto
声明的,这让我很困惑如何声明向量的类型来保存这个迭代器。
我的尝试:
std::map<std::string, std::vector<Point> > seamap;
seamap.insert( std::pair<std::string, std::vector<Point> > ("Nat", vector<Point>{}) );
seamap.insert( std::pair<std::string, std::vector<Point> > ("Sat", vector<Point>{}) ); //Repeat for all ocean basins
Point p;
while (inputFile >> p && !inputFile.eof() )
{
//Check if Southern Ocean
if (p.latitude > Slat2)
{
//Check if Atlantic, Pacific, Indian...
if (p.longitude >= NAtlong1 && p.longitude < SAtlong2 && p.latitude > SPLIT)
{
seamap["Nat"].push_back(p);
} // Repeat for different basins
}
else
{
seamap["South"].push_back(p);
}
}
//Partition basins by depth
for ( std::map<std::string, std::vector<Point> >::iterator it2 = seamap.begin(); it2 != seamap.end(); it2++ )
{
for ( int i = 500; i<=4500; i+=500 )
{
auto itp = std::partition( it2->second.begin(), it2->second.end(), [&i](const auto &a) {return a.bathyDepth < i;} );
}
}
问题 1
看看这个:
Determining if a set of points are inside or outside a square
您可以在读取数据文件时按盆地排列点。你只需要一个数据结构来处理它们。 std::map 可能是一个不错的候选人。 (以海洋名称为键,点向量为值)
问题2
您的深度 window 限制似乎以 500 米的步长增长,并且范围似乎重叠。也许对该地图内的那些向量使用 std::partition 会起作用?
有些伪:
- 以 <500 为谓词对向量进行分区,使用整体
范围。
- 存储步骤 1 返回的迭代器并将其用作下迭代器,同时仍将结束迭代器用作上迭代器。
- 现在以 <1000 作为谓词进行分区。
- 同2-3。但 < 1500 作为谓词和 <1000 作为较低返回的迭代器。
- 重复直到覆盖所有深度。
- 对存储在该地图中的所有海洋矢量执行相同的操作。
您存储的迭代器可用于从该特定向量获取特定范围。
即使用 <500 分区返回的迭代器作为较低的迭代器和 <1500 分区返回的迭代器,您将获得该特定向量中深度介于 500 和 1500 之间的范围。
现在你应该有一张充满矢量的地图,你可以通过海洋名称和深度范围访问这些矢量,间隔为 500 米。
编辑:一些说明
std::map<std::string, std::vector<point>> seamap;
seamap.insert("Nat", std::vector<point>{}); // do this for every sea.
//read point data from file
while(FILESTREAM_NOT_EMPTY)
{
//construct a point object here.
// add points to correct vector
if (THIS_PARTICULAR_POINT_IS_IN_NAT)
seamap["Nat"].push_back(point);
else if (THIS_PARTICULAR_POINT_IS_IN_SAT)
seamap["Sat"].push_back(point);
//and so on...
}
在此之后对该海图中的每个矢量进行分区。 (帮手:How to use range-based for() loop with std::map?)
for (int i =500; i<4500;i+500)
{
auto iter_for_next = std::partition(LOWER_ITERATOR,
UPPER_ITERATOR,
[&i]( const auto &a){return a < i;});
//here save the iterator for later use in some kind of structure.
}
希望对您有所帮助!
我正在分析如下所示的数据集
#Latitude Longitude Depth [m] Bathy depth [m] CaCO3 [%] ...
-78 -177 0 693 1
-78 -173 0 573 2
.
.
计划是能够将数据读入一个向量,然后将其分解成不同的 "Bathy depth"(测深)组。它还需要同时归入不同的海洋盆地。例如,北大西洋内的所有数据点,也就是介于 500-1500m、1000-2000m、1500-2500m 的 BathyDepth 之间的所有数据点……都应该在其自己的组中(可能是矢量或其他对象)。这个想法是能够将这些输出到不同的文本文件中。
我尝试过这个有点结结巴巴。你可以在下面看到这个
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <GeographicLib/Geodesic.hpp> //Library which allows for distance calculations
using namespace std;
using namespace GeographicLib;
//Define each basin spatially
//North Atlantic
double NAtlat1 = 1, NAtlong1 = 1, NAtlat2 = 2, NAtlong2=2; //Incorrect values, to be set later
//South Atlantic
double SAtlat1 = 1, SAtlong1 = 1, SAtlat2 = 2, SAtlong2=2;
//North Pacific and the all others...
struct Point
{
//structure Sample code/label--Lat--Long--SedimentDepth[m]--BathymetricDepth[m]--CaCO3[%]...
string dummy;
double latitude, longitude, rockDepth, bathyDepth, CaCO3, fCaCO3, bSilica, Quartz, CO3i, CO3c, DCO3;
string dummy2;
//Use Overload>> operator
friend istream& operator>>(istream& inputFile, Point& p);
};
//Overload operator
istream& operator>>(istream& inputFile, Point& p)
{
string row_text;
//Read line from input, store in row_text
getline(inputFile, row_text);
//Extract line, store in ss row_stream
istringstream row_stream(row_text);
//Read-in data into each variable
row_stream >> p.dummy >> p.latitude >> p.longitude >> p.rockDepth >> p.bathyDepth >> p.CaCO3 >> p.fCaCO3 >> p.bSilica >> p.Quartz >> p.CO3i >> p.CO3c >> p.DCO3 >> p.dummy2;
return inputFile;
}
int main ()
{
//Geodesic class.
const Geodesic& geod = Geodesic::WGS84();
//Input file
ifstream inputFile("Data.csv");
//Point-type vector to store all data
vector<Point> database;
//bathDepth515 = depths between 500m and 1500m
vector<Point> bathyDepth515;
vector<Point> bathyDepth1020; //Create the rest too
Point p;
if (inputFile)
{
while(inputFile >> p)
{
database.push_back(p);
}
inputFile.close();
}
else
{
cout <<"Unable to open file";
}
for(int i = 0; i < database.size(); ++i)
{
//Group data in database in sets of bathyDepth
if(database[i].bathyDepth >= 500 && database[i].bathyDepth < 1500)
{
//Find and fill particular bathDepths
bathyDepth515.push_back(database[i]);
}
if(database[i].bathyDepth >= 1000 && database[i].bathyDepth < 2000)
{
bathyDepth1020.push_back(database[i]);
}
//...Further conditional statements based on Bathymetric depth, could easily include a spatial condition too...
//Calculate distance between point i and all other points within 500-1500m depth window. Do the same with all other windows.
for(int i = 0; i < bathyDepth515.size(); ++i)
{
for(int j = 0; j < bathyDepth515.size(); ++j)
{
double s12;
if(i != j)
{
geod.Inverse(bathyDepth515[i].latitude, bathyDepth515[i].longitude, bathyDepth515[j].latitude, bathyDepth515[j].longitude, s12);
}
}
}
return 0;
}
问题 1:
我认为很明显,有些方法论不是面向对象的。例如,可能有更好的方法将每个数据 Point
分配给特定的海洋盆地,而不是像我在按深度对数据分组时所做的那样,在我的程序开始时手动将它们放入。我开始创建一个盆地 class,其中包含检索位置的方法以及每个盆地的定义 lat/long,但在寻找一种直观的方法方面并没有走得太远。我希望有人能告诉我如何更好地构建它。我尝试构建一个(非常脆弱的)class 如下
class Basin
{
public:
Basin();
Basin(double latit1, double longit1, double latit2, double longit2);
double getLatitude();
...
private:
double NAt, SAt, NPac, SPac; //All basins
double latitude1, longitude1, latitude2, longitude2; // Boundaries defined by two latitude markers, and two longitude markers.
};
class NAt: public Basin{...}
//...Build class definitions...
问题二: 我的第二个问题是我为不同深度 windows 创建向量的方法。如果我必须改变分割深度的方式或添加更多深度,这可能会变得非常麻烦。我不想仅仅为了适应我决定如何滑动我的深度 windows 就必须更改程序的几乎所有部分。我很感激有人给我一些关于如何去做的想法。 对于问题 2,我能想到的唯一解决方案如下
//Vector of vectors to store each rows' entries separately. Not sure if this is needed as my `Point` `Struct` already allows for each of the entries in a row to be accessed.
vector<vector<Point> > database;
//Create database vectors for different bathDepth windows, eventhough their names will be the same, once push_back, it doesn't matter
for(int i = 1*500; i<=8*500; i+=500)
{
vector<Point> bathDepthi;
//Possibly push these back into database. These can then be accessed and populated using if statements.
}
//Create vectors individually, creating vector of vectors would be more elegant as I won't have to create more if one were to decide to change the range, I'd just have to change the for loop.
我不知道我在这方面的尝试有多大帮助,但我想我可能会更好地了解我想要达到的目的。抱歉这么长 post.
编辑设计从 std::vector
切换到 std::map
根据 heke 的 posted 回答的输入,我尝试了以下方法,但我不确定这是否是上述用户的意思。我选择使用条件语句来确定一个点是否在特定盆地内。如果我的尝试是正确的,如果这是正确的,我仍然不确定如何继续。更具体地说,我不确定如何存储以访问分区向量,例如,将它们写入单独的文本文件(即不同测深深度的 .txt 文件)。如果是这样,我应该将分区迭代器存储到什么类型的向量中?迭代器是用 auto
声明的,这让我很困惑如何声明向量的类型来保存这个迭代器。
我的尝试:
std::map<std::string, std::vector<Point> > seamap;
seamap.insert( std::pair<std::string, std::vector<Point> > ("Nat", vector<Point>{}) );
seamap.insert( std::pair<std::string, std::vector<Point> > ("Sat", vector<Point>{}) ); //Repeat for all ocean basins
Point p;
while (inputFile >> p && !inputFile.eof() )
{
//Check if Southern Ocean
if (p.latitude > Slat2)
{
//Check if Atlantic, Pacific, Indian...
if (p.longitude >= NAtlong1 && p.longitude < SAtlong2 && p.latitude > SPLIT)
{
seamap["Nat"].push_back(p);
} // Repeat for different basins
}
else
{
seamap["South"].push_back(p);
}
}
//Partition basins by depth
for ( std::map<std::string, std::vector<Point> >::iterator it2 = seamap.begin(); it2 != seamap.end(); it2++ )
{
for ( int i = 500; i<=4500; i+=500 )
{
auto itp = std::partition( it2->second.begin(), it2->second.end(), [&i](const auto &a) {return a.bathyDepth < i;} );
}
}
问题 1
看看这个: Determining if a set of points are inside or outside a square
您可以在读取数据文件时按盆地排列点。你只需要一个数据结构来处理它们。 std::map 可能是一个不错的候选人。 (以海洋名称为键,点向量为值)
问题2
您的深度 window 限制似乎以 500 米的步长增长,并且范围似乎重叠。也许对该地图内的那些向量使用 std::partition 会起作用?
有些伪:
- 以 <500 为谓词对向量进行分区,使用整体 范围。
- 存储步骤 1 返回的迭代器并将其用作下迭代器,同时仍将结束迭代器用作上迭代器。
- 现在以 <1000 作为谓词进行分区。
- 同2-3。但 < 1500 作为谓词和 <1000 作为较低返回的迭代器。
- 重复直到覆盖所有深度。
- 对存储在该地图中的所有海洋矢量执行相同的操作。
您存储的迭代器可用于从该特定向量获取特定范围。
即使用 <500 分区返回的迭代器作为较低的迭代器和 <1500 分区返回的迭代器,您将获得该特定向量中深度介于 500 和 1500 之间的范围。
现在你应该有一张充满矢量的地图,你可以通过海洋名称和深度范围访问这些矢量,间隔为 500 米。
编辑:一些说明
std::map<std::string, std::vector<point>> seamap;
seamap.insert("Nat", std::vector<point>{}); // do this for every sea.
//read point data from file
while(FILESTREAM_NOT_EMPTY)
{
//construct a point object here.
// add points to correct vector
if (THIS_PARTICULAR_POINT_IS_IN_NAT)
seamap["Nat"].push_back(point);
else if (THIS_PARTICULAR_POINT_IS_IN_SAT)
seamap["Sat"].push_back(point);
//and so on...
}
在此之后对该海图中的每个矢量进行分区。 (帮手:How to use range-based for() loop with std::map?)
for (int i =500; i<4500;i+500)
{
auto iter_for_next = std::partition(LOWER_ITERATOR,
UPPER_ITERATOR,
[&i]( const auto &a){return a < i;});
//here save the iterator for later use in some kind of structure.
}
希望对您有所帮助!