73 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
| 
 | |
|   EXAMPLE osmium_tiles
 | |
| 
 | |
|   Convert WGS84 longitude and latitude to Mercator coordinates and tile
 | |
|   coordinates.
 | |
| 
 | |
|   DEMONSTRATES USE OF:
 | |
|   * the Location and Coordinates classes
 | |
|   * the Mercator projection function
 | |
|   * the Tile class
 | |
| 
 | |
|   LICENSE
 | |
|   The code in this example file is released into the Public Domain.
 | |
| 
 | |
| */
 | |
| 
 | |
| #include <cstdlib>  // for std::exit, std::atoi, std::atof
 | |
| #include <iostream> // for std::cout, std::cerr
 | |
| 
 | |
| // The Location contains a longitude and latitude and is usually used inside
 | |
| // a node to store its location in the world.
 | |
| #include <osmium/osm/location.hpp>
 | |
| 
 | |
| // Needed for the Mercator projection function. Osmium supports the Mercator
 | |
| // projection out of the box, or pretty much any projection using the Proj.4
 | |
| // library (with the osmium::geom::Projection class).
 | |
| #include <osmium/geom/mercator_projection.hpp>
 | |
| 
 | |
| // The Tile class handles tile coordinates and zoom levels.
 | |
| #include <osmium/geom/tile.hpp>
 | |
| 
 | |
| int main(int argc, char* argv[]) {
 | |
|     if (argc != 4) {
 | |
|         std::cerr << "Usage: " << argv[0] << " ZOOM LON LAT\n";
 | |
|         std::exit(1);
 | |
|     }
 | |
| 
 | |
|     const int zoom = std::atoi(argv[1]);
 | |
| 
 | |
|     if (zoom < 0 || zoom > 30) {
 | |
|         std::cerr << "ERROR: Zoom must be between 0 and 30\n";
 | |
|         std::exit(1);
 | |
|     }
 | |
| 
 | |
|     const double lon = std::atof(argv[2]);
 | |
|     const double lat = std::atof(argv[3]);
 | |
| 
 | |
|     // Create location from WGS84 coordinates. In Osmium the order of
 | |
|     // coordinate values is always x/longitude first, then y/latitude.
 | |
|     const osmium::Location location{lon, lat};
 | |
| 
 | |
|     std::cout << "WGS84:    lon=" << lon << " lat=" << lat << "\n";
 | |
| 
 | |
|     // A location can store some invalid locations, ie locations outside the
 | |
|     // -180 to 180 and -90 to 90 degree range. This function checks for that.
 | |
|     if (!location.valid()) {
 | |
|         std::cerr << "ERROR: Location is invalid\n";
 | |
|         std::exit(1);
 | |
|     }
 | |
| 
 | |
|     // Project the coordinates using a helper function. You can also use the
 | |
|     // osmium::geom::MercatorProjection class.
 | |
|     const osmium::geom::Coordinates c = osmium::geom::lonlat_to_mercator(location);
 | |
|     std::cout << "Mercator: x=" << c.x << " y=" << c.y << "\n";
 | |
| 
 | |
|     // Create a tile at this location. This will also internally use the
 | |
|     // Mercator projection and then calculate the tile coordinates.
 | |
|     const osmium::geom::Tile tile{uint32_t(zoom), location};
 | |
|     std::cout << "Tile:     zoom=" << tile.z << " x=" << tile.x << " y=" << tile.y << "\n";
 | |
| }
 | |
| 
 |