graph_builder.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**
  2. * @file tests/plot/graph_builder.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef TESTS_PLOT_GRAPH_BUILDER_HPP
  26. #define TESTS_PLOT_GRAPH_BUILDER_HPP 1
  27. #include <tests/boost_graph/partitioning/gggp.hpp>
  28. #include <shapefil.h>
  29. #include <geos/geom/CoordinateArraySequenceFactory.h>
  30. #include <geos/geom/CoordinateSequenceFactory.h>
  31. #include <geos/geom/CoordinateSequence.h>
  32. #include <geos/geom/Coordinate.h>
  33. #include <geos/geom/GeometryFactory.h>
  34. #include <geos/geom/LinearRing.h>
  35. #include <geos/geom/Polygon.h>
  36. #include <geos/geom/Point.h>
  37. #include <geos/geom/PrecisionModel.h>
  38. #include <geos/simplify/DouglasPeuckerSimplifier.h>
  39. #include <geos/algorithm/CentroidArea.h>
  40. #include <geos/triangulate/DelaunayTriangulationBuilder.h>
  41. #include <geos/triangulate/quadedge/QuadEdgeSubdivision.h>
  42. #include <map>
  43. using namespace geos;
  44. using namespace geos::geom;
  45. using namespace geos::simplify;
  46. using namespace paradevs::tests::boost_graph;
  47. #define DISTANCE 1000.
  48. #define SCALE 4.
  49. #define DELTA 20 // 2.5
  50. namespace paradevs { namespace tests { namespace plot {
  51. class GraphBuilder
  52. {
  53. public:
  54. GraphBuilder(const std::string& file_name, int cluster_number) :
  55. _cluster_number(cluster_number), _file_name(file_name)
  56. { }
  57. void build(OrientedGraphs& graphs,
  58. InputEdgeList& input_edges,
  59. OutputEdgeList& output_edges,
  60. Connections& parent_connections)
  61. {
  62. std::vector < std::string > parameters = {
  63. "HEM", "rande", "diff", "ratio"
  64. };
  65. bool contraction_coef_flag = false;
  66. uint cluster_number = _cluster_number;
  67. OrientedGraph go;
  68. Edges edge_partie;
  69. Connections connections;
  70. generate(go);
  71. uint contraction_coef = 200;//num_vertices(go);
  72. output_edges = OutputEdgeList(cluster_number);
  73. if (contraction_coef_flag) {
  74. uint coars = num_vertices(go) / contraction_coef;
  75. std::vector < uint > numeric_parameters = { coars,
  76. cluster_number,
  77. 10 };
  78. graphs = Multiniveau(&go, numeric_parameters,
  79. parameters, edge_partie,
  80. output_edges, input_edges,
  81. parent_connections,
  82. false, 2);
  83. } else {
  84. std::vector < uint > numeric_parameters = { contraction_coef,
  85. cluster_number,
  86. 10 };
  87. graphs = Multiniveau(&go, numeric_parameters,
  88. parameters, edge_partie ,
  89. output_edges, input_edges,
  90. parent_connections, false, 2);
  91. }
  92. }
  93. private:
  94. void build_neighbourhood(
  95. OrientedGraph& g,
  96. std::map < int, OrientedGraph::vertex_descriptor >& vertices,
  97. const std::map < int, std::pair < Polygon*, Points > >& polygons,
  98. const std::map < int, bool >& select)
  99. {
  100. // build vertices
  101. std::map < int, std::pair < Polygon*, Points > >::const_iterator it =
  102. polygons.begin();
  103. while (it != polygons.end()) {
  104. vertices[it->first] = boost::add_vertex(g);
  105. if (select.find(it->first)->second) {
  106. g[vertices[it->first]] = VertexProperties(it->first, 1.,
  107. it->second.second, 0);
  108. } else {
  109. g[vertices[it->first]] = VertexProperties(it->first, 1.,
  110. Points(), 0);
  111. }
  112. ++it;
  113. }
  114. // build edges
  115. it = polygons.begin();
  116. while (it != polygons.end()) {
  117. if (select.find(it->first)->second) {
  118. std::map < int,
  119. std::pair < Polygon*,
  120. Points > >::const_iterator it2 =
  121. polygons.begin();
  122. while (it2 != polygons.end()) {
  123. if (select.find(it2->first)->second) {
  124. if (it->first != it2->first) {
  125. if (it->second.first->distance(
  126. it2->second.first) < DISTANCE) {
  127. boost::add_edge(vertices[it->first],
  128. vertices[it2->first], 1., g);
  129. g[vertices[it2->first]]._neighbour_number++;
  130. }
  131. }
  132. }
  133. ++it2;
  134. }
  135. }
  136. ++it;
  137. }
  138. // compute neighbour
  139. // int buffer = 10;
  140. // std::map < int, Polygon* >::const_iterator it = polygons.begin();
  141. // while (it != polygons.end()) {
  142. // Geometry* bufferedPolygon = it->second->buffer(buffer);
  143. // std::map < int, Polygon* >::const_iterator it2 = polygons.begin();
  144. // while (it2 != polygons.end()) {
  145. // if (it->first != it2->first) {
  146. // if (bufferedPolygon->intersects(it2->second)) {
  147. // boost::add_edge(vertices[it->first],
  148. // vertices[it2->first], 1., g);
  149. // g[vertices[it2->first]]._neighbour_centroids.push_back(
  150. // paradevs::tests::boost_graph::Point(
  151. // g[vertices[it->first]]._centroid));
  152. // }
  153. // }
  154. // ++it2;
  155. // }
  156. // delete bufferedPolygon;
  157. // ++it;
  158. // }
  159. }
  160. void generate(OrientedGraph& g)
  161. {
  162. SHPHandle hSHP;
  163. hSHP = SHPOpen(_file_name.c_str(), "rb" );
  164. int nShapeType, nEntities;
  165. double adfMinBound[4], adfMaxBound[4];
  166. SHPGetInfo(hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound);
  167. PrecisionModel precisionModel(100., 0, 0);
  168. GeometryFactory geomFactory(&precisionModel);
  169. const CoordinateSequenceFactory* seqFactory =
  170. CoordinateArraySequenceFactory::instance();
  171. std::vector < Geometry* > holes;
  172. std::map < int, std::pair < Polygon*, Points > > polygons;
  173. std::map < int, OrientedGraph::vertex_descriptor > vertices;
  174. // read shapefile file and build polygons
  175. for (int i = 0; i < nEntities; i++) {
  176. SHPObject *psShape;
  177. psShape = SHPReadObject(hSHP, i);
  178. int n = psShape->nVertices;
  179. if (psShape->padfX[0] != psShape->padfX[psShape->nVertices - 1] or
  180. psShape->padfY[0] != psShape->padfY[psShape->nVertices - 1])
  181. ++n;
  182. CoordinateSequence* seq = seqFactory->create(n, 2);
  183. Coordinate first;
  184. for (int j = 0; j < n; j++) {
  185. Coordinate pt(psShape->padfX[j],
  186. psShape->padfY[j]);
  187. if (j == 0) first = pt;
  188. seq->setAt(pt, j);
  189. }
  190. if (first != seq->back()) {
  191. seq->setAt(first, n-1);
  192. }
  193. LinearRing* ring(geomFactory.createLinearRing(seq));
  194. Polygon* polygon = geomFactory.createPolygon(*ring, holes);
  195. std::auto_ptr < Geometry > object =
  196. DouglasPeuckerSimplifier::simplify(polygon, 1);
  197. Points list;
  198. {
  199. GeometryFactory factory;
  200. for (double x = (*object->getEnvelope()
  201. ->getCoordinates())[0].x + DELTA / 2;
  202. x < (*object->getEnvelope()->getCoordinates())[1].x;
  203. x += DELTA) {
  204. for (double y = (*object->getEnvelope()
  205. ->getCoordinates())[0].y + DELTA / 2;
  206. y < (*object->getEnvelope()->getCoordinates())[2].y;
  207. y += DELTA) {
  208. Coordinate c;
  209. c.x = x;
  210. c.y = y;
  211. geos::geom::Point* pt = factory.createPoint(c);
  212. if (object->contains(pt->clone())) {
  213. list.push_back(
  214. paradevs::tests::boost_graph::Point(x, y));
  215. }
  216. }
  217. }
  218. }
  219. polygons[i] = std::make_pair(
  220. dynamic_cast < Polygon* >(object->clone()), list);
  221. delete ring;
  222. SHPDestroyObject(psShape);
  223. }
  224. std::map < int, bool > select;
  225. select_polygons(polygons, select);
  226. build_neighbourhood(g, vertices, polygons, select);
  227. std::map < int, std::pair < Polygon*, Points > >::const_iterator it =
  228. polygons.begin();
  229. while (it != polygons.end()) {
  230. delete it->second.first;
  231. ++it;
  232. }
  233. SHPClose(hSHP);
  234. // OrientedGraph::vertex_iterator it_og, end_og;
  235. // boost::tie(it_og, end_og) = boost::vertices(g);
  236. // for (; it_og != end_og; ++it_og) {
  237. // OrientedGraph::adjacency_iterator neighbour_it, neighbour_end;
  238. // std::cout << g[*it_og]._index << " -> { ";
  239. // tie(neighbour_it, neighbour_end) =
  240. // boost::adjacent_vertices(*it_og, g);
  241. // for (; neighbour_it != neighbour_end; ++neighbour_it) {
  242. // std::cout << g[*neighbour_it]._index << " ";
  243. // }
  244. // std::cout << "}" << std::endl;
  245. // }
  246. }
  247. void select_polygons(const std::map < int, std::pair < Polygon*, Points > >&
  248. polygons, std::map < int, bool >& select)
  249. {
  250. double sum = 0;
  251. unsigned int N = 0;
  252. std::map < int, std::pair < Polygon*, Points > >::const_iterator it =
  253. polygons.begin();
  254. while (it != polygons.end()) {
  255. sum += it->second.first->getArea() * SCALE * SCALE / 1e4;
  256. N += it->second.second.size();
  257. select[it->first] = false;
  258. ++it;
  259. }
  260. std::cout << "Total area = " << sum << " Ha" << std::endl;
  261. std::cout << polygons.size() << " plots - " << N << " cells"
  262. << std::endl;
  263. int n = 0;
  264. sum = 0;
  265. //while (sum < 500) {
  266. while (n < 400) {
  267. int index = rand() % polygons.size();
  268. if (not select[index] /*and
  269. polygons.find(index)->second.first->getArea() *
  270. SCALE * SCALE > 10 * 1e4*/) {
  271. select[index] = true;
  272. sum += polygons.find(index)->second.first->getArea() *
  273. SCALE * SCALE / 1e4;
  274. ++n;
  275. }
  276. }
  277. select[153] = true;
  278. std::cout << "Select number = " << n << " for " << sum << std::endl;
  279. }
  280. int _cluster_number;
  281. std::string _file_name;
  282. };
  283. } } } // namespace paradevs tests plot
  284. #endif