Osmium  0.1
include/osmium/geometry/from_way.hpp
Go to the documentation of this file.
00001 #ifndef OSMIUM_GEOMETRY_FROM_WAY_HPP
00002 #define OSMIUM_GEOMETRY_FROM_WAY_HPP
00003 
00004 /*
00005 
00006 Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README).
00007 
00008 This file is part of Osmium (https://github.com/joto/osmium).
00009 
00010 Osmium is free software: you can redistribute it and/or modify it under the
00011 terms of the GNU Lesser General Public License or (at your option) the GNU
00012 General Public License as published by the Free Software Foundation, either
00013 version 3 of the Licenses, or (at your option) any later version.
00014 
00015 Osmium is distributed in the hope that it will be useful, but WITHOUT ANY
00016 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00017 PARTICULAR PURPOSE. See the GNU Lesser General Public License and the GNU
00018 General Public License for more details.
00019 
00020 You should have received a copy of the Licenses along with Osmium. If not, see
00021 <http://www.gnu.org/licenses/>.
00022 
00023 */
00024 
00025 #include <osmium/geometry.hpp>
00026 #include <osmium/osm/way.hpp>
00027 
00028 namespace Osmium {
00029 
00030     namespace Geometry {
00031 
00032         class FromWay : public Geometry {
00033 
00034         protected:
00035 
00036             FromWay(const Osmium::OSM::WayNodeList& way_node_list,
00037                     bool reverse=false,
00038                     osm_object_id_t id=0)
00039                 : Geometry(id),
00040                   m_way_node_list(&way_node_list),
00041                   m_reverse(reverse) {
00042             }
00043 
00044 #ifdef OSMIUM_WITH_SHPLIB
00045 
00051             SHPObject* create_line_or_polygon(int shp_type) const {
00052                 if (!m_way_node_list->has_position()) {
00053                     throw std::runtime_error("node coordinates not available for building way geometry");
00054                 }
00055                 int size = m_way_node_list->size();
00056                 if (size == 0 || size == 1) {
00057                     if (Osmium::debug()) {
00058                         std::cerr << "error building way geometry for way " << id() << ": must at least contain two nodes" << std::endl;
00059                     }
00060                     throw Osmium::Exception::IllegalGeometry();
00061                 }
00062 
00063                 std::vector<double> lon_checked;
00064                 lon_checked.reserve(size);
00065                 lon_checked.push_back((*m_way_node_list)[0].position().lon());
00066 
00067                 std::vector<double> lat_checked;
00068                 lat_checked.reserve(size);
00069                 lat_checked.push_back((*m_way_node_list)[0].position().lat());
00070 
00071                 for (int i=1; i < size; i++) {
00072                     if ((*m_way_node_list)[i] == (*m_way_node_list)[i-1]) {
00073                         if (Osmium::debug()) {
00074                             std::cerr << "warning building way geometry for way " << id() << ": contains node " << (*m_way_node_list)[i].ref() << " twice" << std::endl;
00075                         }
00076                     } else if ((*m_way_node_list)[i].position() == (*m_way_node_list)[i-1].position()) {
00077                         if (Osmium::debug()) {
00078                             std::cerr << "warning building way geometry for way " << id() << ": contains position " << (*m_way_node_list)[i].position() << " twice" << std::endl;
00079                         }
00080                     } else {
00081                         lon_checked.push_back((*m_way_node_list)[i].position().lon());
00082                         lat_checked.push_back((*m_way_node_list)[i].position().lat());
00083                     }
00084                 }
00085                 if (lon_checked.size() == 1) {
00086                     if (Osmium::debug()) {
00087                         std::cerr << "error building way geometry for way " << id() << ": must at least contain two different points" << std::endl;
00088                     }
00089                     throw Osmium::Exception::IllegalGeometry();
00090                 }
00091                 if (m_reverse) {
00092                     reverse(lon_checked.begin(), lon_checked.end());
00093                     reverse(lat_checked.begin(), lat_checked.end());
00094                 }
00095                 return SHPCreateSimpleObject(shp_type, lon_checked.size(), &(lon_checked[0]), &(lat_checked[0]), NULL);
00096             }
00097 #endif // OSMIUM_WITH_SHPLIB
00098 
00099             const Osmium::OSM::WayNodeList* m_way_node_list;
00100             const bool m_reverse;
00101 
00102         }; // class FromWay
00103 
00104     } // namespace Geometry
00105 
00106 } // namespace Osmium
00107 
00108 #endif // OSMIUM_GEOMETRY_FROM_WAY_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines