x04_wxwidgets_world_mapper.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. //
  3. // Copyright (c) 2010-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // wxWidgets World Mapper example
  9. // #define EXAMPLE_WX_USE_GRAPHICS_CONTEXT 1
  10. #include <fstream>
  11. #include <sstream>
  12. #include <boost/foreach.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <boost/scoped_array.hpp>
  15. #include <boost/geometry/geometry.hpp>
  16. #include <boost/geometry/geometries/geometries.hpp>
  17. #include <boost/geometry/geometries/point_xy.hpp>
  18. #include <boost/geometry/geometries/multi_geometries.hpp>
  19. #include <boost/geometry/geometries/register/point.hpp>
  20. #include <boost/geometry/geometries/register/ring.hpp>
  21. #include <boost/geometry/extensions/algorithms/selected.hpp>
  22. // wxWidgets, if these headers are NOT found, adapt include path (and lib path)
  23. #include "wx/wx.h"
  24. #include "wx/math.h"
  25. #include "wx/stockitem.h"
  26. #ifdef EXAMPLE_WX_USE_GRAPHICS_CONTEXT
  27. #include "wx/graphics.h"
  28. #include "wx/dcgraph.h"
  29. #endif
  30. typedef boost::geometry::model::d2::point_xy<double> point_2d;
  31. typedef boost::geometry::model::multi_polygon
  32. <
  33. boost::geometry::model::polygon<point_2d>
  34. > country_type;
  35. // Adapt wxWidgets points to Boost.Geometry points such that they can be used
  36. // in e.g. transformations (see below)
  37. BOOST_GEOMETRY_REGISTER_POINT_2D(wxPoint, int, cs::cartesian, x, y)
  38. BOOST_GEOMETRY_REGISTER_POINT_2D(wxRealPoint, double, cs::cartesian, x, y)
  39. // wxWidgets draws using wxPoint*, so we HAVE to use that.
  40. // Therefore have to make a wxPoint* array
  41. // 1) compatible with Boost.Geometry
  42. // 2) compatible with Boost.Range (required by Boost.Geometry)
  43. // 3) compatible with std::back_inserter (required by Boost.Geometry)
  44. // For compatible 2):
  45. typedef std::pair<wxPoint*,wxPoint*> wxPointPointerPair;
  46. // For compatible 1):
  47. BOOST_GEOMETRY_REGISTER_RING(wxPointPointerPair);
  48. // For compatible 3):
  49. // Specialize back_insert_iterator for the wxPointPointerPair
  50. // (has to be done within "namespace std")
  51. namespace std
  52. {
  53. template <>
  54. class back_insert_iterator<wxPointPointerPair>
  55. {
  56. public:
  57. typedef std::output_iterator_tag iterator_category;
  58. typedef void value_type;
  59. typedef void difference_type;
  60. typedef void pointer;
  61. typedef void reference;
  62. typedef wxPointPointerPair container_type;
  63. explicit back_insert_iterator(wxPointPointerPair& x)
  64. : current(boost::begin(x))
  65. , end(boost::end(x))
  66. {}
  67. inline back_insert_iterator<wxPointPointerPair>&
  68. operator=(wxPoint const& value)
  69. {
  70. // Check if not passed beyond
  71. if (current != end)
  72. {
  73. *current++ = value;
  74. }
  75. return *this;
  76. }
  77. // Boiler-plate
  78. inline back_insert_iterator<wxPointPointerPair>& operator*() { return *this; }
  79. inline back_insert_iterator<wxPointPointerPair>& operator++() { return *this; }
  80. inline back_insert_iterator<wxPointPointerPair>& operator++(int) { return *this; }
  81. private:
  82. boost::range_iterator<wxPointPointerPair>::type current, end;
  83. };
  84. } // namespace std
  85. // ----------------------------------------------------------------------------
  86. // Read an ASCII file containing WKT's
  87. // ----------------------------------------------------------------------------
  88. template <typename Geometry, typename Box>
  89. inline void read_wkt(std::string const& filename, std::vector<Geometry>& geometries, Box& box)
  90. {
  91. std::ifstream cpp_file(filename.c_str());
  92. if (cpp_file.is_open())
  93. {
  94. while (! cpp_file.eof() )
  95. {
  96. std::string line;
  97. std::getline(cpp_file, line);
  98. if (! line.empty())
  99. {
  100. Geometry geometry;
  101. boost::geometry::read_wkt(line, geometry);
  102. geometries.push_back(geometry);
  103. boost::geometry::expand(box, boost::geometry::return_envelope<Box>(geometry));
  104. }
  105. }
  106. }
  107. }
  108. // ----------------------------------------------------------------------------
  109. class HelloWorldFrame: public wxFrame
  110. {
  111. public:
  112. HelloWorldFrame(wxFrame *frame, wxString const& title, wxPoint const& pos, wxSize const& size);
  113. void OnCloseWindow(wxCloseEvent& );
  114. void OnExit(wxCommandEvent& );
  115. DECLARE_EVENT_TABLE()
  116. };
  117. // ----------------------------------------------------------------------------
  118. class HelloWorldCanvas: public wxWindow
  119. {
  120. public:
  121. HelloWorldCanvas(wxFrame *frame);
  122. private:
  123. void DrawCountries(wxDC& dc);
  124. void DrawCountry(wxDC& dc, country_type const& country);
  125. void OnPaint(wxPaintEvent& );
  126. void OnMouseMove(wxMouseEvent&);
  127. typedef boost::geometry::strategy::transform::map_transformer
  128. <
  129. double, 2, 2,
  130. true, true
  131. > map_transformer_type;
  132. typedef boost::geometry::strategy::transform::inverse_transformer
  133. <
  134. double, 2, 2
  135. > inverse_transformer_type;
  136. boost::shared_ptr<map_transformer_type> m_map_transformer;
  137. boost::shared_ptr<inverse_transformer_type> m_inverse_transformer;
  138. boost::geometry::model::box<point_2d> m_box;
  139. std::vector<country_type> m_countries;
  140. int m_focus;
  141. wxBrush m_orange;
  142. wxFrame* m_owner;
  143. DECLARE_EVENT_TABLE()
  144. };
  145. // ----------------------------------------------------------------------------
  146. class HelloWorldApp: public wxApp
  147. {
  148. public:
  149. bool OnInit()
  150. {
  151. // Create the main frame window
  152. HelloWorldFrame *frame = new HelloWorldFrame(NULL, _T("Boost.Geometry for wxWidgets - Hello World!"), wxDefaultPosition, wxSize(640, 480));
  153. wxMenu *file_menu = new wxMenu;
  154. file_menu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT));
  155. wxMenuBar* menuBar = new wxMenuBar;
  156. menuBar->Append(file_menu, _T("&File"));
  157. frame->SetMenuBar(menuBar);
  158. int width, height;
  159. frame->GetClientSize(&width, &height);
  160. (void) new HelloWorldCanvas(frame);
  161. // Show the frame
  162. frame->Show(true);
  163. return true;
  164. }
  165. };
  166. // ----------------------------------------------------------------------------
  167. HelloWorldFrame::HelloWorldFrame(wxFrame *frame, wxString const& title, wxPoint const& pos, wxSize const& size)
  168. : wxFrame(frame, wxID_ANY, title, pos, size, wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE )
  169. {
  170. CreateStatusBar(2);
  171. }
  172. void HelloWorldFrame::OnExit(wxCommandEvent& )
  173. {
  174. this->Destroy();
  175. }
  176. void HelloWorldFrame::OnCloseWindow(wxCloseEvent& )
  177. {
  178. static bool destroyed = false;
  179. if (! destroyed)
  180. {
  181. this->Destroy();
  182. destroyed = true;
  183. }
  184. }
  185. // ----------------------------------------------------------------------------
  186. HelloWorldCanvas::HelloWorldCanvas(wxFrame *frame)
  187. : wxWindow(frame, wxID_ANY)
  188. , m_owner(frame)
  189. , m_focus(-1)
  190. {
  191. boost::geometry::assign_inverse(m_box);
  192. read_wkt("../data/world.wkt", m_countries, m_box);
  193. m_orange = wxBrush(wxColour(255, 128, 0), wxSOLID);
  194. }
  195. void HelloWorldCanvas::OnMouseMove(wxMouseEvent &event)
  196. {
  197. namespace bg = boost::geometry;
  198. if (m_inverse_transformer)
  199. {
  200. // Boiler-plate wxWidgets code
  201. wxClientDC dc(this);
  202. PrepareDC(dc);
  203. m_owner->PrepareDC(dc);
  204. // Transform the point to Lon/Lat
  205. point_2d point;
  206. bg::transform(event.GetPosition(), point, *m_inverse_transformer);
  207. // Determine selected object
  208. int i = 0;
  209. int previous_focus = m_focus;
  210. m_focus = -1;
  211. BOOST_FOREACH(country_type const& country, m_countries)
  212. {
  213. if (bg::selected(country, point, 0))
  214. {
  215. m_focus = i;
  216. }
  217. i++;
  218. }
  219. // On change:
  220. if (m_focus != previous_focus)
  221. {
  222. // Undraw old focus
  223. if (previous_focus >= 0)
  224. {
  225. dc.SetBrush(*wxWHITE_BRUSH);
  226. DrawCountry(dc, m_countries[previous_focus]);
  227. }
  228. // Draw new focus
  229. if (m_focus >= 0)
  230. {
  231. dc.SetBrush(m_orange);
  232. DrawCountry(dc, m_countries[m_focus]);
  233. }
  234. }
  235. // Create a string and set it in the status text
  236. std::ostringstream out;
  237. out << "Position: " << point.x() << ", " << point.y();
  238. m_owner->SetStatusText(wxString(out.str().c_str(), wxConvUTF8));
  239. }
  240. }
  241. void HelloWorldCanvas::OnPaint(wxPaintEvent& )
  242. {
  243. #if defined(EXAMPLE_WX_USE_GRAPHICS_CONTEXT)
  244. wxPaintDC pdc(this);
  245. wxGCDC gdc(pdc);
  246. wxDC& dc = (wxDC&) gdc;
  247. #else
  248. wxPaintDC dc(this);
  249. #endif
  250. PrepareDC(dc);
  251. static bool running = false;
  252. if (! running)
  253. {
  254. running = true;
  255. // Update the transformers
  256. wxSize sz = dc.GetSize();
  257. m_map_transformer.reset(new map_transformer_type(m_box, sz.x, sz.y));
  258. m_inverse_transformer.reset(new inverse_transformer_type(*m_map_transformer));
  259. DrawCountries(dc);
  260. running = false;
  261. }
  262. }
  263. void HelloWorldCanvas::DrawCountries(wxDC& dc)
  264. {
  265. namespace bg = boost::geometry;
  266. dc.SetBackground(*wxLIGHT_GREY_BRUSH);
  267. dc.Clear();
  268. BOOST_FOREACH(country_type const& country, m_countries)
  269. {
  270. DrawCountry(dc, country);
  271. }
  272. if (m_focus != -1)
  273. {
  274. dc.SetBrush(m_orange);
  275. DrawCountry(dc, m_countries[m_focus]);
  276. }
  277. }
  278. void HelloWorldCanvas::DrawCountry(wxDC& dc, country_type const& country)
  279. {
  280. namespace bg = boost::geometry;
  281. BOOST_FOREACH(bg::model::polygon<point_2d> const& poly, country)
  282. {
  283. // Use only exterior ring, holes are (for the moment) ignored. This would need
  284. // a holey-polygon compatible wx object
  285. std::size_t n = boost::size(bg::exterior_ring(poly));
  286. boost::scoped_array<wxPoint> points(new wxPoint[n]);
  287. wxPointPointerPair pair = std::make_pair(points.get(), points.get() + n);
  288. bg::transform(bg::exterior_ring(poly), pair, *m_map_transformer);
  289. dc.DrawPolygon(n, points.get());
  290. }
  291. }
  292. // ----------------------------------------------------------------------------
  293. BEGIN_EVENT_TABLE(HelloWorldFrame, wxFrame)
  294. EVT_CLOSE(HelloWorldFrame::OnCloseWindow)
  295. EVT_MENU(wxID_EXIT, HelloWorldFrame::OnExit)
  296. END_EVENT_TABLE()
  297. BEGIN_EVENT_TABLE(HelloWorldCanvas, wxWindow)
  298. EVT_PAINT(HelloWorldCanvas::OnPaint)
  299. EVT_MOTION(HelloWorldCanvas::OnMouseMove)
  300. END_EVENT_TABLE()
  301. IMPLEMENT_APP(HelloWorldApp)