Osmium  0.1
include/osmium/osm/way.hpp
Go to the documentation of this file.
00001 #ifndef OSMIUM_OSM_WAY_HPP
00002 #define OSMIUM_OSM_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 <stdexcept>
00026 #include <iostream>
00027 #include <boost/operators.hpp>
00028 
00029 #include <osmium/osm/object.hpp>
00030 #include <osmium/osm/way_node_list.hpp>
00031 
00036 #ifdef OSMIUM_WITH_GEOS
00037 # include <geos/geom/Coordinate.h>
00038 # include <geos/geom/CoordinateSequenceFactory.h>
00039 # include <geos/geom/Geometry.h>
00040 # include <geos/geom/Point.h>
00041 # include <geos/util/GEOSException.h>
00042 #endif // OSMIUM_WITH_GEOS
00043 
00044 #include <osmium/geometry.hpp>
00045 
00046 namespace Osmium {
00047 
00048     namespace OSM {
00049 
00050         class Way : public Object, boost::less_than_comparable<Way> {
00051 
00052             WayNodeList m_node_list;
00053 
00054         public:
00055 
00057             Way() : Object(), m_node_list() {
00058                 init();
00059             }
00060 
00061             Way(int size_of_node_list) : Object(), m_node_list(size_of_node_list) {
00062                 init();
00063             }
00064 
00066             Way(const Way& w) : Object(w) {
00067                 init();
00068                 m_node_list = w.m_node_list;
00069             }
00070 
00071             const WayNodeList& nodes() const {
00072                 return m_node_list;
00073             }
00074 
00075             WayNodeList& nodes() {
00076                 return m_node_list;
00077             }
00078 
00079         private:
00080 
00081             void init() {
00082 #ifdef OSMIUM_WITH_JAVASCRIPT
00083                 v8::HandleScope handle_scope;
00084                 js_object_instance = v8::Persistent<v8::Object>::New(JavascriptTemplate::get<JavascriptTemplate>().create_instance(this));
00085 #endif // OSMIUM_WITH_JAVASCRIPT
00086             }
00087 
00088         public:
00089 
00090             osm_object_type_t get_type() const {
00091                 return WAY;
00092             }
00093 
00094             osm_object_id_t get_node_id(osm_sequence_id_t n) const {
00095                 return m_node_list[n].ref();
00096             }
00097 
00098             double get_lon(osm_sequence_id_t n) const {
00099                 return m_node_list[n].position().lon();
00100             }
00101 
00102             double get_lat(osm_sequence_id_t n) const {
00103                 return m_node_list[n].position().lat();
00104             }
00105 
00111             void add_node(osm_object_id_t ref) {
00112                 m_node_list.add(ref);
00113             }
00114 
00118             osm_sequence_id_t node_count() const {
00119                 return m_node_list.size();
00120             }
00121 
00125             osm_object_id_t get_first_node_id() const {
00126                 return m_node_list.front().ref();
00127             }
00128 
00132             osm_object_id_t get_last_node_id() const {
00133                 return m_node_list.back().ref();
00134             }
00135 
00139             bool is_closed() const {
00140                 return m_node_list.is_closed();
00141             }
00142 
00143 #ifdef OSMIUM_WITH_GEOS
00144 
00148             geos::geom::Point* get_first_node_geometry() const {
00149                 if (!m_node_list.front().has_position()) {
00150                     throw std::range_error("geometry for nodes not available");
00151                 }
00152                 return Osmium::Geometry::geos_geometry_factory()->createPoint(m_node_list.front().position());
00153             }
00154 
00159             geos::geom::Point* get_last_node_geometry() const {
00160                 if (!m_node_list.back().has_position()) {
00161                     throw std::range_error("geometry for nodes not available");
00162                 }
00163                 return Osmium::Geometry::geos_geometry_factory()->createPoint(m_node_list.back().position());
00164             }
00165 
00170             geos::geom::Geometry* create_geos_geometry() const {
00171                 try {
00172                     std::vector<geos::geom::Coordinate>* c = new std::vector<geos::geom::Coordinate>;
00173                     for (osm_sequence_id_t i=0; i < m_node_list.size(); ++i) {
00174                         c->push_back(m_node_list[i].position());
00175                     }
00176                     geos::geom::CoordinateSequence* cs = Osmium::Geometry::geos_geometry_factory()->getCoordinateSequenceFactory()->create(c);
00177                     return (geos::geom::Geometry*) Osmium::Geometry::geos_geometry_factory()->createLineString(cs);
00178                 } catch (const geos::util::GEOSException& exc) {
00179                     std::cerr << "error building way geometry, leave it as NULL\n";
00180                     return NULL;
00181                 }
00182             }
00183 #endif // OSMIUM_WITH_GEOS
00184 
00185 #ifdef OSMIUM_WITH_JAVASCRIPT
00186             v8::Handle<v8::Value> js_nodes() const {
00187                 return m_node_list.js_instance();
00188             }
00189 
00190             v8::Handle<v8::Value> js_geom() const;
00191 
00192             v8::Handle<v8::Value> js_reverse_geom() const;
00193 
00194             v8::Handle<v8::Value> js_polygon_geom() const;
00195 
00196             struct JavascriptTemplate : public Osmium::OSM::Object::JavascriptTemplate {
00197 
00198                 JavascriptTemplate() : Osmium::OSM::Object::JavascriptTemplate() {
00199                     js_template->SetAccessor(v8::String::NewSymbol("nodes"),        accessor_getter<Way, &Way::js_nodes>);
00200                     js_template->SetAccessor(v8::String::NewSymbol("geom"),         accessor_getter<Way, &Way::js_geom>);
00201                     js_template->SetAccessor(v8::String::NewSymbol("reverse_geom"), accessor_getter<Way, &Way::js_reverse_geom>);
00202                     js_template->SetAccessor(v8::String::NewSymbol("polygon_geom"), accessor_getter<Way, &Way::js_polygon_geom>);
00203                 }
00204 
00205             };
00206 #endif // OSMIUM_WITH_JAVASCRIPT
00207 
00213             friend bool operator<(const Way& lhs, const Way& rhs) {
00214                 if (lhs.id() == rhs.id()) {
00215                     return lhs.version() < rhs.version();
00216                 } else {
00217                     return abs(lhs.id()) < abs(rhs.id());
00218                 }
00219             }
00220 
00224             friend bool operator<(const shared_ptr<Way const>& lhs, const shared_ptr<Way const>& rhs) {
00225                 return *lhs < *rhs;
00226             }
00227 
00228         }; // class Way
00229 
00230     } // namespace OSM
00231 
00232 } // namespace Osmium
00233 
00234 #endif // OSMIUM_OSM_WAY_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines