Compare commits

...

3 commits

Author SHA1 Message Date
eriedaberrie 98604b0f91 Lowercased some consts 2024-01-08 22:53:24 -08:00
eriedaberrie 947d0c8a60 Just use c++17
Not worth
2024-01-08 22:14:28 -08:00
eriedaberrie 7c0d4e2a57 Only lock once per iteration 2024-01-08 21:56:16 -08:00
2 changed files with 43 additions and 48 deletions

View file

@ -1,6 +1,6 @@
project('shapefile-test', 'cpp', project('shapefile-test', 'cpp',
default_options : [ default_options : [
'cpp_std=c++20', 'cpp_std=c++17',
'warning_level=3', 'warning_level=3',
'buildtype=release', 'buildtype=release',
] ]

View file

@ -64,12 +64,12 @@ int main(int argc, char *argv[]) {
switch (argc - optind) { switch (argc - optind) {
case 0: { case 0: {
res_loc = NULL; res_loc = NULL;
for (const auto &FE : std::filesystem::directory_iterator(".")) { for (const auto& fe : std::filesystem::directory_iterator(".")) {
if (!FE.is_regular_file()) if (!fe.is_regular_file())
continue; continue;
const auto FP = FE.path(); const auto fp = fe.path();
if (FP.extension() != ".shp") if (fp.extension() != ".shp")
continue; continue;
if (res_loc != NULL) { if (res_loc != NULL) {
@ -78,10 +78,10 @@ int main(int argc, char *argv[]) {
break; break;
} }
const std::string FPS = FP; const std::string fps = fp;
res_loc = static_cast<char *>(malloc((FPS.size() + 1) * sizeof(*res_loc))); res_loc = static_cast<char *>(malloc((fps.size() + 1) * sizeof(*res_loc)));
FPS.copy(res_loc, FPS.size()); fps.copy(res_loc, fps.size());
res_loc[FPS.size()] = '\0'; res_loc[fps.size()] = '\0';
} }
if (res_loc == NULL) { if (res_loc == NULL) {
std::cerr << "Failed to automatically find residents shapefile!" << std::endl; std::cerr << "Failed to automatically find residents shapefile!" << std::endl;
@ -97,11 +97,11 @@ int main(int argc, char *argv[]) {
return 1; 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; std::cout << "Reading residents data..." << std::endl;
int res_n, res_type; 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) { if (res_type != SHPT_POINT) {
std::cerr << "Expected point data from " << res_loc << "!" << std::endl; std::cerr << "Expected point data from " << res_loc << "!" << std::endl;
return 1; return 1;
@ -109,62 +109,62 @@ int main(int argc, char *argv[]) {
std::vector<std::pair<double, double>> residentials; std::vector<std::pair<double, double>> residentials;
for (auto i = 0; i < res_n; i++) { for (auto i = 0; i < res_n; i++) {
const auto RES_P = SHPReadObject(RES_H, i); const auto res_p = SHPReadObject(res_h, i);
residentials.push_back(std::make_pair(RES_P->padfX[0], RES_P->padfY[0])); residentials.push_back(std::make_pair(res_p->padfX[0], res_p->padfY[0]));
SHPDestroyObject(RES_P); SHPDestroyObject(res_p);
} }
SHPClose(RES_H); SHPClose(res_h);
std::cout << "Reading fires data..." << std::endl; std::cout << "Reading fires data..." << std::endl;
std::vector<month_info> all_fires; std::vector<month_info> all_fires;
for (const auto& MONTH_ENTRY : std::filesystem::directory_iterator(fire_dir)) { for (const auto& month_entry : std::filesystem::directory_iterator(fire_dir)) {
if (!std::filesystem::is_directory(MONTH_ENTRY)) if (!std::filesystem::is_directory(month_entry))
continue; 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; std::map<std::string, std::vector<std::pair<double, double>>> all_days;
unsigned long long n_fires_month = 0; unsigned long long n_fires_month = 0;
for (const auto& DAY_ENTRY : std::filesystem::directory_iterator(MONTH_ENTRY)) { for (const auto& day_entry : std::filesystem::directory_iterator(month_entry)) {
const auto FP = DAY_ENTRY.path(); const auto fp = day_entry.path();
if (FP.extension() != ".shp") if (fp.extension() != ".shp")
continue; 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; 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) { 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; return 1;
} }
std::vector<std::pair<double, double>> fires; std::vector<std::pair<double, double>> fires;
for (auto i = 0; i < fire_n; i++) { 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; int zone;
bool northp; bool northp;
double x, y; 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)); fires.push_back(std::make_pair(x, y));
SHPDestroyObject(FIRE_P); SHPDestroyObject(fire_p);
} }
SHPClose(FIRE_H); SHPClose(fire_h);
if (do_sort) if (do_sort)
n_fires_month += fire_n; n_fires_month += fire_n;
all_days[DAY] = std::move(fires); all_days[day] = std::move(fires);
} }
all_fires.push_back(month_info{ all_fires.push_back(month_info{
.n_fires = n_fires_month, n_fires_month,
.name = MONTH, month,
.days = std::move(all_days), std::move(all_days),
}); });
} }
@ -183,18 +183,12 @@ int main(int argc, char *argv[]) {
for (unsigned int w = 1; w <= wc; w++) { for (unsigned int w = 1; w <= wc; w++) {
workers.push_back(std::thread([&it, &n_done, &it_lock, &all_fires, workers.push_back(std::thread([&it, &n_done, &it_lock, &all_fires,
&residentials, &out_dir, w, distance]() { &residentials, &out_dir, w, distance]() {
for (;;) {
it_lock.lock(); 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;
}
while (it != all_fires.end()) {
const auto& month_data = *(it++); const auto& month_data = *(it++);
auto& month = month_data.name; const auto& month = month_data.name;
auto& month_fires = month_data.days; const auto& month_fires = month_data.days;
std::cout << "Worker " << w << " processing data from " << month std::cout << "Worker " << w << " processing data from " << month
<< "... (finished " << n_done << "/" << "... (finished " << n_done << "/"
<< all_fires.size() << ")" << std::endl; << all_fires.size() << ")" << std::endl;
@ -225,12 +219,13 @@ int main(int argc, char *argv[]) {
outf << '\n'; outf << '\n';
} }
{ it_lock.lock();
std::lock_guard<std::mutex> lk(it_lock);
n_done++; n_done++;
} }
}
std::cout << "Worker " << w << " done! (finished " << n_done
<< "/" << all_fires.size() << ")" << std::endl;
it_lock.unlock();
})); }));
} }