1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2019 Daniel Baston <dbaston@gmail.com>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#include <geos/profiler.h>
#include <geos_c.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/util/SineStarFactory.h>
#include <algorithm>
#include <random>
#include <vector>
#include <memory>
#include <fstream>
#include <iostream>
#include <sstream>
class GEOSPreparedContainsPerfTest {
public:
void test(const std::vector<GEOSGeometry*>& geoms, std::size_t num_points) {
using namespace geos::geom;
geos::util::Profile sw("GEOSPreparedContains");
geos::util::Profile sw_xy("GEOSPreparedContainsXY");
std::size_t hits = 0;
std::size_t hits_xy = 0;
for (const auto& g: geoms) {
double xmin, xmax, ymin, ymax;
GEOSGeom_getXMin(g, &xmin);
GEOSGeom_getXMax(g, &xmax);
GEOSGeom_getYMin(g, &ymin);
GEOSGeom_getYMax(g, &ymax);
std::default_random_engine e(12345);
std::uniform_real_distribution<> xdist(xmin, xmax);
std::uniform_real_distribution<> ydist(ymin, ymax);
std::vector<Coordinate> coords(num_points);
std::generate(coords.begin(), coords.end(), [&xdist, &ydist, &e]() {
return Coordinate(xdist(e), ydist(e));
});
sw.start();
auto prep = GEOSPrepare(g);
for (const auto& c : coords) {
auto pt = GEOSGeom_createPointFromXY(c.x, c.y);
if (GEOSPreparedContains(prep, pt)) {
hits++;
}
GEOSGeom_destroy(pt);
}
GEOSPreparedGeom_destroy(prep);
sw.stop();
sw_xy.start();
prep = GEOSPrepare(g);
for (const auto& c : coords) {
if (GEOSPreparedContainsXY(prep, c.x, c.y)) {
hits_xy++;
}
}
GEOSPreparedGeom_destroy(prep);
sw_xy.stop();
}
std::cout << sw.name << ": " << hits << " hits from " << num_points << " points in " << sw.getTotFormatted() << std::endl;
std::cout << sw_xy.name << ": " << hits_xy << " hits from " << num_points << " points in " << sw_xy.getTotFormatted() << std::endl;
}
};
int main(int argc, char** argv) {
if (argc != 3) {
std::cout << "perf_geospreparedcontains performs a specified number of point-in-polygon tests" << std::endl;
std::cout << "on randomly generated points from the bounding box of each geometry provided" << std::endl;
std::cout << "in a file as WKT." << std::endl;
std::cout << std::endl;
std::cout << "Usage: perf_geospreparedcontains [wktfile] [n]" << std::endl;
return 0;
}
initGEOS(nullptr, nullptr);
GEOSPreparedContainsPerfTest tester;
std::size_t n = static_cast<std::size_t>(std::atoi(argv[2]));
std::cout << "Performing " << n << " point-in-polygon tests." << std::endl;
std::string fname{argv[1]};
std::cout << "Reading shapes from " << fname << std::endl;
std::vector<GEOSGeometry*> geoms;
std::ifstream f(fname);
std::string line;
while(std::getline(f, line)) {
if (line != "") {
geoms.push_back(GEOSGeomFromWKT(line.c_str()));
}
}
f.close();
std::cout << "Read " << geoms.size() << " geometries." << std::endl;
tester.test(geoms, n);
for (auto& g : geoms) {
GEOSGeom_destroy(g);
}
}
|