mirror of
https://github.com/boostorg/geometry.git
synced 2025-05-09 15:14:02 +00:00
ext: Modify and remove old examples.
This commit is contained in:
parent
ebeac9def7
commit
a666d638ee
@ -16,4 +16,3 @@ project boost-geometry-extensions-gis-examples
|
||||
|
||||
|
||||
build-project latlong ;
|
||||
build-project projections ;
|
||||
|
@ -13,5 +13,5 @@ project boost-geometry-example-extensions-gis-projections
|
||||
: # requirements
|
||||
;
|
||||
|
||||
exe distance_example : distance_example.cpp ;
|
||||
exe point_ll_example : point_ll_example.cpp ;
|
||||
#exe distance_example : distance_example.cpp ;
|
||||
#exe point_ll_example : point_ll_example.cpp ;
|
||||
|
@ -18,10 +18,10 @@
|
||||
#include <boost/geometry/geometries/point_xy.hpp>
|
||||
#include <boost/geometry/strategies/spherical/distance_cross_track.hpp>
|
||||
#include <boost/geometry/extensions/gis/latlong/latlong.hpp>
|
||||
#include <boost/geometry/extensions/gis/geographic/strategies/andoyer.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/proj/sterea.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/proj/laea.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/parameters.hpp>
|
||||
//#include <boost/geometry/extensions/gis/geographic/strategies/andoyer.hpp>
|
||||
//#include <boost/geometry/extensions/gis/projections/proj/sterea.hpp>
|
||||
//#include <boost/geometry/extensions/gis/projections/proj/laea.hpp>
|
||||
//#include <boost/geometry/extensions/gis/projections/parameters.hpp>
|
||||
|
||||
// BSG 28-10-2010
|
||||
// TODO: clear up this test
|
||||
|
@ -60,7 +60,7 @@ int main()
|
||||
using namespace boost::geometry;
|
||||
|
||||
typedef model::ll::point<degree> latlon_point;
|
||||
|
||||
|
||||
latlon_point paris;
|
||||
|
||||
// Assign coordinates to the latlong point, using the methods lat and lon
|
||||
@ -115,7 +115,7 @@ int main()
|
||||
|
||||
// Other way round: have Amsterdam and go 430 km to the south (i.e. first calculate direction)
|
||||
double tc = get_course(amsterdam, paris);
|
||||
std::cout << "Course: " << (tc * boost::geometry::math::r2d) << std::endl;
|
||||
std::cout << "Course: " << (tc * boost::geometry::math::r2d<double>()) << std::endl;
|
||||
|
||||
latlon_point paris_calculated;
|
||||
point_at_distance(amsterdam, 430 * 1000.0, tc, average_earth_radius, paris_calculated);
|
||||
|
@ -1,21 +0,0 @@
|
||||
# Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
#
|
||||
# Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
# Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
|
||||
# Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
|
||||
|
||||
# Use, modification and distribution is subject to the Boost Software License,
|
||||
# Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
# http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
|
||||
project boost-geometry-example-extensions-gis-latlong
|
||||
: requirements
|
||||
<library>/boost/foreach//boost_foreach
|
||||
;
|
||||
|
||||
exe p01_projection_example : p01_projection_example.cpp ;
|
||||
exe p02_projfactory_example : p02_projfactory_example.cpp ;
|
||||
exe p03_projmap_example : p03_projmap_example.cpp ;
|
||||
exe p04_example : p04_example.cpp ;
|
||||
exe p05_example : p05_example.cpp ;
|
@ -1,62 +0,0 @@
|
||||
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
//
|
||||
// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
|
||||
// Use, modification and distribution is subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Projection example 1, direct
|
||||
|
||||
#include <boost/geometry.hpp>
|
||||
#include <boost/geometry/geometries/point_xy.hpp>
|
||||
#include <boost/geometry/extensions/algorithms/parse.hpp>
|
||||
|
||||
#include <boost/geometry/extensions/gis/latlong/latlong.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/parameters.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/proj/robin.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost::geometry;
|
||||
|
||||
// Initialize projection parameters
|
||||
projections::parameters par = projections::init("+ellps=WGS84 +units=m");
|
||||
|
||||
// Construct a Robinson projection, using specified point types
|
||||
// (This delivers a projection without virtual methods. Note that in p02 example
|
||||
// the projection is created using a factory, which delivers a projection with virtual methods)
|
||||
typedef model::ll::point<degree> point_ll_deg;
|
||||
typedef model::d2::point_xy<double> point_xy;
|
||||
projections::robin_spheroid<point_ll_deg, point_xy> prj(par);
|
||||
|
||||
// Define Amsterdam / Barcelona in decimal degrees / degrees/minutes
|
||||
point_ll_deg amsterdam = parse<point_ll_deg>("52.4N", "5.9E");
|
||||
point_ll_deg barcelona = parse<point_ll_deg>("41 23'N", "2 11'E");
|
||||
|
||||
point_xy pa, pb;
|
||||
|
||||
// Now do the projection. "Forward" means from latlong to meters.
|
||||
// (Note that a map projection might fail. This is not 'exceptional'.
|
||||
// Therefore the forward function does not throw but returns false)
|
||||
if (prj.forward(amsterdam, pa) && prj.forward(barcelona, pb))
|
||||
{
|
||||
std::cout << "Amsterdam: " << wkt(pa) << std::endl << "Barcelona: " << wkt(pb) << std::endl;
|
||||
|
||||
std::cout << "Distance (unprojected):" << distance(amsterdam, barcelona) / 1000.0 << " km" << std::endl;
|
||||
std::cout << "Distance ( projected):" << distance(pa, pb) / 1000.0 << " km" << std::endl;
|
||||
|
||||
// Do the inverse projection. "Inverse" means from meters to latlong
|
||||
// It also might fail or might not exist, not all projections
|
||||
// have their inverse implemented
|
||||
point_ll_deg a1;
|
||||
if (prj.inverse(pa, a1))
|
||||
{
|
||||
std::cout << "Amsterdam (original): " << wkt(amsterdam) << std::endl
|
||||
<< "Amsterdam (projected, and back):" << wkt(a1) << std::endl;
|
||||
std::cout << "Distance a-a': " << distance(amsterdam, a1) << " meter" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
//
|
||||
// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
|
||||
// Use, modification and distribution is subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Projection example 2, using factory
|
||||
|
||||
#include <boost/geometry.hpp>
|
||||
#include <boost/geometry/geometries/point_xy.hpp>
|
||||
|
||||
#include <boost/geometry/extensions/gis/latlong/latlong.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/parameters.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/factory.hpp>
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost::geometry;
|
||||
|
||||
// Initialize projection parameters. For construction using a factory the projection name is required.
|
||||
projections::parameters par = projections::init("+proj=robin +ellps=WGS84 +units=m");
|
||||
|
||||
// Construct the specified projection, using specified point types
|
||||
// Note that this is the only difference from p01_projection_example. It constructs a projection
|
||||
// with virtual methods, which can be used polymorphically. Therefore it is a pointer. For
|
||||
// convenience we use a boost shared pointer here.
|
||||
typedef model::ll::point<degree> point_ll_deg;
|
||||
typedef model::d2::point_xy<double> point_xy;
|
||||
projections::factory<point_ll_deg, point_xy> fac;
|
||||
boost::shared_ptr<projections::projection<point_ll_deg, point_xy> > prj(fac.create_new(par));
|
||||
|
||||
// Define Amsterdam / Barcelona in decimal degrees / degrees/minutes
|
||||
point_ll_deg amsterdam(longitude<>(5.9), latitude<>(52.4));
|
||||
point_ll_deg barcelona(
|
||||
latitude<>(dms<north>(41, 23)),
|
||||
longitude<>(dms<east>(2, 11))
|
||||
);
|
||||
|
||||
point_xy pa, pb;
|
||||
|
||||
// Do the forward projection
|
||||
if (prj->forward(amsterdam, pa) && prj->forward(barcelona, pb))
|
||||
{
|
||||
std::cout << "Amsterdam: " << wkt(pa) << std::endl << "Barcelona: " << wkt(pb) << std::endl;
|
||||
|
||||
std::cout << "Distance (unprojected):" << distance(amsterdam, barcelona) / 1000.0 << " km" << std::endl;
|
||||
std::cout << "Distance ( projected):" << distance(pa, pb) / 1000.0 << " km" << std::endl;
|
||||
|
||||
// Get the inverse
|
||||
point_ll_deg a1;
|
||||
if (prj->inverse(pa, a1))
|
||||
{
|
||||
std::cout << "Amsterdam (original): " << wkt(amsterdam) << std::endl
|
||||
<< "Amsterdam (projected, and back):" << wkt(a1) << std::endl;
|
||||
std::cout << "Distance a-a': " << distance(amsterdam, a1) << " meter" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,149 +0,0 @@
|
||||
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
//
|
||||
// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
|
||||
// Use, modification and distribution is subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Projection example 3, combined with shapelib and SVG
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <boost/geometry.hpp>
|
||||
#include <boost/geometry/geometries/point_xy.hpp>
|
||||
#include <boost/geometry/geometries/register/point.hpp>
|
||||
#include <boost/geometry/geometries/multi_polygon.hpp>
|
||||
|
||||
#include <boost/geometry/io/svg/write.hpp>
|
||||
#include <boost/geometry/extensions/gis/latlong/latlong.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/project_transformer.hpp>
|
||||
|
||||
void read_wkt_and_project_and_write_svg(std::string const& wkt_filename,
|
||||
std::string const& projection_parameters,
|
||||
std::string const& svg_filename)
|
||||
{
|
||||
using namespace boost::geometry;
|
||||
|
||||
// Declare a vector containing the world countries
|
||||
typedef model::ll::point<degree> point_ll_deg;
|
||||
typedef model::polygon<point_ll_deg> polygon_ll_deg;
|
||||
std::vector<polygon_ll_deg> ll_polygons;
|
||||
|
||||
typedef model::d2::point_xy<double> point_xy;
|
||||
|
||||
// Read polygons from a Well-Known Text file using the ggl parser
|
||||
std::ifstream cpp_file(wkt_filename.c_str());
|
||||
if (! cpp_file.is_open())
|
||||
{
|
||||
throw std::string("File not found: ") + wkt_filename;
|
||||
}
|
||||
|
||||
while (! cpp_file.eof() )
|
||||
{
|
||||
std::string line;
|
||||
std::getline(cpp_file, line);
|
||||
if (boost::starts_with(line, "MULTIPOLYGON"))
|
||||
{
|
||||
typedef boost::geometry::model::multi_polygon<boost::geometry::model::polygon<point_ll_deg> > mp_type;
|
||||
mp_type mp;
|
||||
boost::geometry::read_wkt(line, mp);
|
||||
for (mp_type::const_iterator it = boost::begin(mp);
|
||||
it != boost::end(mp); ++it)
|
||||
{
|
||||
ll_polygons.push_back(*it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Our latlong polygon collection will be projected into this vector
|
||||
// (Of course it is also possible to do this while reading and have one vector)
|
||||
std::vector<model::polygon<point_xy> > xy_polygons;
|
||||
|
||||
// Declare transformation strategy which contains a projection
|
||||
projections::project_transformer
|
||||
<
|
||||
point_ll_deg,
|
||||
point_xy
|
||||
> projection(projection_parameters);
|
||||
|
||||
// Project the polygons, and at the same time get the bounding box (in xy)
|
||||
model::box<point_xy> bbox;
|
||||
assign_inverse(bbox);
|
||||
for (std::vector<polygon_ll_deg>::const_iterator it = ll_polygons.begin();
|
||||
it != ll_polygons.end();
|
||||
++it)
|
||||
{
|
||||
model::polygon<point_xy> xy_polygon;
|
||||
|
||||
if (transform(*it, xy_polygon, projection))
|
||||
{
|
||||
// Update bbox with box of this projected polygon
|
||||
expand(bbox, return_envelope<model::box<point_xy> >(xy_polygon));
|
||||
|
||||
// Add projected polygon
|
||||
xy_polygons.push_back(xy_polygon);
|
||||
}
|
||||
}
|
||||
|
||||
// Create an SVG image
|
||||
std::ofstream out(svg_filename.c_str());
|
||||
out
|
||||
<< "<?xml version=\"1.0\" standalone=\"no\"?>" << std::endl
|
||||
<< "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"" << std::endl
|
||||
<< "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" << std::endl
|
||||
<< "<svg width=\"100%\" height=\"100%\" version=\"1.1\"" << std::endl
|
||||
<< "xmlns=\"http://www.w3.org/2000/svg\">" << std::endl;
|
||||
|
||||
// Setup the transformation to SVG
|
||||
// (alternatively this could be skipped because SVG can transform itself,
|
||||
// but this example shows it like this)
|
||||
typedef boost::geometry::model::d2::point_xy<int> svg_point;
|
||||
boost::geometry::strategy::transform::map_transformer
|
||||
<
|
||||
double, 2, 2,
|
||||
true, true
|
||||
> svg_transformer(bbox, 800, 600);
|
||||
|
||||
// Create the background
|
||||
boost::geometry::model::box<svg_point> box;
|
||||
boost::geometry::assign_values(box, 0, 0, 800, 600);
|
||||
out << boost::geometry::svg(box, "fill:rgb(0,0,255)") << std::endl;
|
||||
|
||||
for (std::vector<model::polygon<point_xy> >::const_iterator it = xy_polygons.begin();
|
||||
it != xy_polygons.end();
|
||||
++it)
|
||||
{
|
||||
boost::geometry::model::polygon<svg_point> svg_polygon;
|
||||
boost::geometry::transform(*it, svg_polygon, svg_transformer);
|
||||
out << boost::geometry::svg(svg_polygon,
|
||||
"fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0.2")
|
||||
<< std::endl;
|
||||
}
|
||||
out << "</svg>" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Note, file location: trunk/libs/geometry/example/data
|
||||
// update path below if necessary
|
||||
read_wkt_and_project_and_write_svg(
|
||||
"../../../../example/data/world.wkt",
|
||||
"+proj=moll +ellps=clrk66",
|
||||
"world.svg");
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
catch(std::string const& s)
|
||||
{
|
||||
std::cout << "Exception: " << s << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
//
|
||||
// Copyright (c) 2012 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
//
|
||||
// Use, modification and distribution is subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Projection example 4, reworked version of example 3
|
||||
// Now using svg mapper, multi polygons and specific transform strategy
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/geometry.hpp>
|
||||
#include <boost/geometry/geometries/point_xy.hpp>
|
||||
#include <boost/geometry/geometries/register/point.hpp>
|
||||
#include <boost/geometry/geometries/multi_polygon.hpp>
|
||||
|
||||
#include <boost/geometry/io/svg/svg_mapper.hpp>
|
||||
#include <boost/geometry/extensions/gis/latlong/latlong.hpp>
|
||||
|
||||
#include <boost/geometry/extensions/gis/projections/parameters.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/proj/robin.hpp>
|
||||
|
||||
// Define a specific projection transformer
|
||||
// (NOTE: this might become part of the library)
|
||||
template <typename Projection>
|
||||
struct projection_transformer
|
||||
{
|
||||
Projection const& m_prj;
|
||||
|
||||
inline projection_transformer(Projection const& prj)
|
||||
: m_prj(prj)
|
||||
{}
|
||||
|
||||
inline bool apply(typename Projection::geographic_point_type const& p1,
|
||||
typename Projection::cartesian_point_type& p2) const
|
||||
{
|
||||
return m_prj.forward(p1, p2);
|
||||
}
|
||||
};
|
||||
|
||||
void read_wkt_and_project_and_map_svg(std::string const& wkt_filename,
|
||||
std::string const& svg_filename)
|
||||
{
|
||||
using namespace boost::geometry;
|
||||
|
||||
typedef model::ll::point<degree> point_ll_deg;
|
||||
typedef model::d2::point_xy<double> point_xy;
|
||||
|
||||
typedef model::multi_polygon<model::polygon<point_ll_deg> > mp_ll;
|
||||
typedef model::multi_polygon<model::polygon<point_xy> > mp_xy;
|
||||
|
||||
typedef projections::robin_spheroid<point_ll_deg, point_xy> robin;
|
||||
|
||||
std::vector<mp_ll> countries_in_ll;
|
||||
|
||||
// Read polygons from WKT
|
||||
std::ifstream cpp_file(wkt_filename.c_str());
|
||||
if (! cpp_file.is_open())
|
||||
{
|
||||
throw std::string("File not found: ") + wkt_filename;
|
||||
}
|
||||
|
||||
while (! cpp_file.eof() )
|
||||
{
|
||||
std::string line;
|
||||
std::getline(cpp_file, line);
|
||||
if (boost::starts_with(line, "MULTIPOLYGON"))
|
||||
{
|
||||
countries_in_ll.resize(countries_in_ll.size() + 1);
|
||||
boost::geometry::read_wkt(line, countries_in_ll.back());
|
||||
}
|
||||
}
|
||||
|
||||
robin prj(projections::init("+ellps=WGS84 +units=m"));
|
||||
projection_transformer<robin> projection(prj);
|
||||
|
||||
// Project the polygons, and at the same time get the bounding box (in xy)
|
||||
std::vector<mp_xy> countries_in_xy;
|
||||
model::box<point_xy> bbox;
|
||||
assign_inverse(bbox);
|
||||
BOOST_FOREACH(mp_ll const& country_ll, countries_in_ll)
|
||||
{
|
||||
mp_xy country_xy;
|
||||
if (transform(country_ll, country_xy, projection))
|
||||
{
|
||||
expand(bbox, return_envelope<model::box<point_xy> >(country_xy));
|
||||
countries_in_xy.push_back(country_xy);
|
||||
}
|
||||
}
|
||||
|
||||
// Create an SVG image
|
||||
std::ofstream svg(svg_filename.c_str());
|
||||
boost::geometry::svg_mapper<point_xy> mapper(svg, 1000, 800);
|
||||
mapper.add(bbox);
|
||||
|
||||
BOOST_FOREACH(mp_xy const& country, countries_in_xy)
|
||||
{
|
||||
mapper.map(country, "fill-opacity:0.6;fill:rgb(153,204,0);stroke:rgb(0,128,0);stroke-width:0.2");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Note, file location: trunk/libs/geometry/example/data
|
||||
// update path below if necessary
|
||||
read_wkt_and_project_and_map_svg(
|
||||
"../../../../example/data/world.wkt",
|
||||
"world4.svg");
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
catch(std::string const& s)
|
||||
{
|
||||
std::cout << "Exception: " << s << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,155 +0,0 @@
|
||||
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
//
|
||||
// Copyright (c) 2012 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
//
|
||||
// Use, modification and distribution is subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Projection example 5 (reworked from 4), using small factory
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/geometry.hpp>
|
||||
#include <boost/geometry/geometries/point_xy.hpp>
|
||||
#include <boost/geometry/geometries/register/point.hpp>
|
||||
#include <boost/geometry/geometries/multi_polygon.hpp>
|
||||
|
||||
#include <boost/geometry/io/svg/svg_mapper.hpp>
|
||||
#include <boost/geometry/extensions/gis/latlong/latlong.hpp>
|
||||
|
||||
#include <boost/geometry/extensions/gis/projections/parameters.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/proj/goode.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/proj/moll.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/proj/natearth.hpp>
|
||||
#include <boost/geometry/extensions/gis/projections/proj/robin.hpp>
|
||||
|
||||
#include <boost/geometry/extensions/gis/projections/new_projection.hpp>
|
||||
|
||||
// Define a specific projection transformer
|
||||
// (NOTE: this might become part of the library - copied from p04)
|
||||
template <typename Projection>
|
||||
struct projection_transformer
|
||||
{
|
||||
Projection const& m_prj;
|
||||
|
||||
inline projection_transformer(Projection const& prj)
|
||||
: m_prj(prj)
|
||||
{}
|
||||
|
||||
inline bool apply(typename Projection::geographic_point_type const& p1,
|
||||
typename Projection::cartesian_point_type& p2) const
|
||||
{
|
||||
return m_prj.forward(p1, p2);
|
||||
}
|
||||
};
|
||||
|
||||
void p05_example(int projection_id,
|
||||
std::string const& wkt_filename,
|
||||
std::string const& svg_filename)
|
||||
{
|
||||
using namespace boost::geometry;
|
||||
using namespace boost::geometry::projections;
|
||||
|
||||
typedef model::ll::point<degree> pll;
|
||||
typedef model::d2::point_xy<double> pxy;
|
||||
|
||||
// Idea and headerfile "new_projection" submitted by Krzysztof Czainski:
|
||||
// They are useful, when:
|
||||
// - you have a small set of types of projections you'll use,
|
||||
// - you know the type of projection during it's creation, and later
|
||||
// you want to use it through an abstract base pointer,
|
||||
// - you want to avoid the overhead of factory: generating code for
|
||||
// creating a projection of every type you don't use, and selecting
|
||||
// the type from a string.
|
||||
|
||||
projection<pll, pxy>* prj = NULL;
|
||||
|
||||
parameters pars = projections::init("+ellps=WGS84");
|
||||
switch(projection_id)
|
||||
{
|
||||
case 1 : prj = new_projection<robin_spheroid<pll, pxy> >(pars); break;
|
||||
case 2 : prj = new_projection<moll_spheroid<pll, pxy> >(pars); break;
|
||||
case 3 : prj = new_projection<goode_spheroid<pll, pxy> >(pars); break;
|
||||
case 4 : prj = new_projection<natearth_spheroid<pll, pxy> >(pars); break;
|
||||
default : return;
|
||||
}
|
||||
|
||||
typedef model::multi_polygon<model::polygon<pll> > mp_ll;
|
||||
typedef model::multi_polygon<model::polygon<pxy> > mp_xy;
|
||||
|
||||
std::vector<mp_ll> countries_in_ll;
|
||||
|
||||
// Read polygons from WKT
|
||||
std::ifstream cpp_file(wkt_filename.c_str());
|
||||
if (! cpp_file.is_open())
|
||||
{
|
||||
throw std::string("File not found: ") + wkt_filename;
|
||||
}
|
||||
|
||||
while (! cpp_file.eof() )
|
||||
{
|
||||
std::string line;
|
||||
std::getline(cpp_file, line);
|
||||
if (boost::starts_with(line, "MULTIPOLYGON"))
|
||||
{
|
||||
countries_in_ll.resize(countries_in_ll.size() + 1);
|
||||
boost::geometry::read_wkt(line, countries_in_ll.back());
|
||||
}
|
||||
}
|
||||
|
||||
projection_transformer<projection<pll, pxy> > strategy(*prj);
|
||||
|
||||
// Project the polygons, and at the same time get the bounding box (in xy)
|
||||
std::vector<mp_xy> countries_in_xy;
|
||||
model::box<pxy> bbox;
|
||||
assign_inverse(bbox);
|
||||
BOOST_FOREACH(mp_ll const& country_ll, countries_in_ll)
|
||||
{
|
||||
mp_xy country_xy;
|
||||
if (transform(country_ll, country_xy, strategy))
|
||||
{
|
||||
expand(bbox, return_envelope<model::box<pxy> >(country_xy));
|
||||
countries_in_xy.push_back(country_xy);
|
||||
}
|
||||
}
|
||||
|
||||
// Create an SVG image
|
||||
std::ofstream svg(svg_filename.c_str());
|
||||
boost::geometry::svg_mapper<pxy> mapper(svg, 1000, 800);
|
||||
mapper.add(bbox);
|
||||
|
||||
BOOST_FOREACH(mp_xy const& country, countries_in_xy)
|
||||
{
|
||||
mapper.map(country, "fill-opacity:0.6;fill:rgb(153,204,0);stroke:rgb(0,128,0);stroke-width:0.2");
|
||||
}
|
||||
|
||||
delete prj;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Note, file location: trunk/libs/geometry/example/data
|
||||
// update path below if necessary
|
||||
std::string const data = "../../../../example/data/world.wkt";
|
||||
try
|
||||
{
|
||||
p05_example(1, data, "p05_world_1.svg");
|
||||
p05_example(2, data, "p05_world_2.svg");
|
||||
p05_example(3, data, "p05_world_3.svg");
|
||||
p05_example(4, data, "p05_world_4.svg");
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
catch(std::string const& s)
|
||||
{
|
||||
std::cerr << "Exception: " << s << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user