Compare commits
3 commits
98604b0f91
...
ffd9247a21
Author | SHA1 | Date | |
---|---|---|---|
ffd9247a21 | |||
11ed6e0e7d | |||
f51e37e8c6 |
|
@ -1,6 +1,6 @@
|
|||
project('shapefile-test', 'cpp',
|
||||
default_options : [
|
||||
'cpp_std=c++20',
|
||||
'cpp_std=c++17',
|
||||
'warning_level=3',
|
||||
'buildtype=release',
|
||||
]
|
||||
|
|
91
src/main.cpp
91
src/main.cpp
|
@ -64,12 +64,12 @@ int main(int argc, char *argv[]) {
|
|||
switch (argc - optind) {
|
||||
case 0: {
|
||||
res_loc = NULL;
|
||||
for (const auto &FE : std::filesystem::directory_iterator(".")) {
|
||||
if (!FE.is_regular_file())
|
||||
for (const auto& fe : std::filesystem::directory_iterator(".")) {
|
||||
if (!fe.is_regular_file())
|
||||
continue;
|
||||
|
||||
const auto FP = FE.path();
|
||||
if (FP.extension() != ".shp")
|
||||
const auto fp = fe.path();
|
||||
if (fp.extension() != ".shp")
|
||||
continue;
|
||||
|
||||
if (res_loc != NULL) {
|
||||
|
@ -78,10 +78,10 @@ int main(int argc, char *argv[]) {
|
|||
break;
|
||||
}
|
||||
|
||||
const std::string FPS = FP;
|
||||
res_loc = static_cast<char *>(malloc((FPS.size() + 1) * sizeof(*res_loc)));
|
||||
FPS.copy(res_loc, FPS.size());
|
||||
res_loc[FPS.size()] = '\0';
|
||||
const std::string fps = fp;
|
||||
res_loc = static_cast<char *>(malloc((fps.size() + 1) * sizeof(*res_loc)));
|
||||
fps.copy(res_loc, fps.size());
|
||||
res_loc[fps.size()] = '\0';
|
||||
}
|
||||
if (res_loc == NULL) {
|
||||
std::cerr << "Failed to automatically find residents shapefile!" << std::endl;
|
||||
|
@ -97,11 +97,11 @@ int main(int argc, char *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
const auto RES_H = SHPOpen(res_loc, "rb");
|
||||
const auto res_h = SHPOpen(res_loc, "rb");
|
||||
|
||||
std::cout << "Reading residents data..." << std::endl;
|
||||
int res_n, res_type;
|
||||
SHPGetInfo(RES_H, &res_n, &res_type, NULL, NULL);
|
||||
SHPGetInfo(res_h, &res_n, &res_type, NULL, NULL);
|
||||
if (res_type != SHPT_POINT) {
|
||||
std::cerr << "Expected point data from " << res_loc << "!" << std::endl;
|
||||
return 1;
|
||||
|
@ -109,62 +109,62 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
std::vector<std::pair<double, double>> residentials;
|
||||
for (auto i = 0; i < res_n; i++) {
|
||||
const auto RES_P = SHPReadObject(RES_H, i);
|
||||
residentials.push_back(std::make_pair(RES_P->padfX[0], RES_P->padfY[0]));
|
||||
SHPDestroyObject(RES_P);
|
||||
const auto res_p = SHPReadObject(res_h, i);
|
||||
residentials.push_back(std::make_pair(res_p->padfX[0], res_p->padfY[0]));
|
||||
SHPDestroyObject(res_p);
|
||||
}
|
||||
|
||||
SHPClose(RES_H);
|
||||
SHPClose(res_h);
|
||||
|
||||
|
||||
std::cout << "Reading fires data..." << std::endl;
|
||||
std::vector<month_info> all_fires;
|
||||
for (const auto& MONTH_ENTRY : std::filesystem::directory_iterator(fire_dir)) {
|
||||
if (!std::filesystem::is_directory(MONTH_ENTRY))
|
||||
for (const auto& month_entry : std::filesystem::directory_iterator(fire_dir)) {
|
||||
if (!std::filesystem::is_directory(month_entry))
|
||||
continue;
|
||||
|
||||
const std::string MONTH = MONTH_ENTRY.path().filename();
|
||||
const std::string month = month_entry.path().filename();
|
||||
std::map<std::string, std::vector<std::pair<double, double>>> all_days;
|
||||
unsigned long long n_fires_month = 0;
|
||||
|
||||
for (const auto& DAY_ENTRY : std::filesystem::directory_iterator(MONTH_ENTRY)) {
|
||||
const auto FP = DAY_ENTRY.path();
|
||||
if (FP.extension() != ".shp")
|
||||
for (const auto& day_entry : std::filesystem::directory_iterator(month_entry)) {
|
||||
const auto fp = day_entry.path();
|
||||
if (fp.extension() != ".shp")
|
||||
continue;
|
||||
|
||||
const auto DAY = FP.stem().string().substr(8);
|
||||
const auto day = fp.stem().string().substr(8);
|
||||
|
||||
const auto FIRE_H = SHPOpen(FP.c_str(), "rb");
|
||||
const auto fire_h = SHPOpen(fp.c_str(), "rb");
|
||||
int fire_n, fire_type;
|
||||
SHPGetInfo(FIRE_H, &fire_n, &fire_type, NULL, NULL);
|
||||
SHPGetInfo(fire_h, &fire_n, &fire_type, NULL, NULL);
|
||||
if (res_type != SHPT_POINT) {
|
||||
std::cerr << "Expected point data from " << FP << "!" << std::endl;
|
||||
std::cerr << "Expected point data from " << fp << "!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<std::pair<double, double>> fires;
|
||||
for (auto i = 0; i < fire_n; i++) {
|
||||
const auto FIRE_P = SHPReadObject(FIRE_H, i);
|
||||
const auto fire_p = SHPReadObject(fire_h, i);
|
||||
int zone;
|
||||
bool northp;
|
||||
double x, y;
|
||||
GeographicLib::UTMUPS::Forward(FIRE_P->padfY[0], FIRE_P->padfX[0], zone, northp, x, y);
|
||||
GeographicLib::UTMUPS::Forward(fire_p->padfY[0], fire_p->padfX[0], zone, northp, x, y);
|
||||
fires.push_back(std::make_pair(x, y));
|
||||
SHPDestroyObject(FIRE_P);
|
||||
SHPDestroyObject(fire_p);
|
||||
}
|
||||
|
||||
SHPClose(FIRE_H);
|
||||
SHPClose(fire_h);
|
||||
|
||||
if (do_sort)
|
||||
n_fires_month += fire_n;
|
||||
|
||||
all_days[DAY] = std::move(fires);
|
||||
all_days[day] = std::move(fires);
|
||||
}
|
||||
|
||||
all_fires.push_back(month_info{
|
||||
.n_fires = n_fires_month,
|
||||
.name = MONTH,
|
||||
.days = std::move(all_days),
|
||||
n_fires_month,
|
||||
month,
|
||||
std::move(all_days),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -182,19 +182,13 @@ int main(int argc, char *argv[]) {
|
|||
std::cout << "Starting " << wc << " worker threads..." << std::endl;
|
||||
for (unsigned int w = 1; w <= wc; w++) {
|
||||
workers.push_back(std::thread([&it, &n_done, &it_lock, &all_fires,
|
||||
&residentials, &out_dir, w, distance]() {
|
||||
for (;;) {
|
||||
it_lock.lock();
|
||||
if (it == all_fires.end()) {
|
||||
std::cout << "Worker " << w << " done! (finished " << n_done
|
||||
<< "/" << all_fires.size() << ")" << std::endl;
|
||||
it_lock.unlock();
|
||||
return;
|
||||
}
|
||||
&residentials, &out_dir, w, distance]() {
|
||||
it_lock.lock();
|
||||
|
||||
while (it != all_fires.end()) {
|
||||
const auto& month_data = *(it++);
|
||||
auto& month = month_data.name;
|
||||
auto& month_fires = month_data.days;
|
||||
const auto& month = month_data.name;
|
||||
const auto& month_fires = month_data.days;
|
||||
std::cout << "Worker " << w << " processing data from " << month
|
||||
<< "... (finished " << n_done << "/"
|
||||
<< all_fires.size() << ")" << std::endl;
|
||||
|
@ -225,12 +219,13 @@ int main(int argc, char *argv[]) {
|
|||
outf << '\n';
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(it_lock);
|
||||
|
||||
n_done++;
|
||||
}
|
||||
it_lock.lock();
|
||||
n_done++;
|
||||
}
|
||||
|
||||
std::cout << "Worker " << w << " done! (finished " << n_done
|
||||
<< "/" << all_fires.size() << ")" << std::endl;
|
||||
it_lock.unlock();
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue