Osmium  0.1
include/osmium/geometry/point.hpp
Go to the documentation of this file.
00001 #ifndef OSMIUM_GEOMETRY_POINT_HPP
00002 #define OSMIUM_GEOMETRY_POINT_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 <vector>
00026 #include <sstream>
00027 #include <iomanip>
00028 
00029 #ifdef OSMIUM_WITH_GEOS
00030 # include <geos/geom/Point.h>
00031 #endif // OSMIUM_WITH_GEOS
00032 
00033 #include <osmium/geometry.hpp>
00034 #include <osmium/osm/node.hpp>
00035 
00036 namespace Osmium {
00037 
00038     namespace Geometry {
00039 
00043         class Point : public Geometry {
00044 
00045         public:
00046 
00050             Point(const Osmium::OSM::Position& position, osm_object_id_t id=0) : Geometry(id), m_position(position) {
00051             }
00052 
00056             Point(const Osmium::OSM::Node& node)
00057                 : Geometry(node.id()),
00058                   m_position(Osmium::OSM::Position(node.get_lon(), node.get_lat())) {
00059             }
00060 
00061             double lon() const {
00062                 return m_position.lon();
00063             }
00064 
00065             double lat() const {
00066                 return m_position.lat();
00067             }
00068 
00069             std::ostream& write_to_stream(std::ostream& out, AsWKT, bool with_srid=false) const {
00070                 if (with_srid) {
00071                     out << "SRID=4326;";
00072                 }
00073                 return out << "POINT(" << std::setprecision(10) << lon() << " " << lat() << ")";
00074             }
00075 
00076             std::ostream& write_to_stream(std::ostream& out, AsWKB, bool with_srid=false) const {
00077                 write_binary_wkb_header(out, with_srid, wkbPoint);
00078                 write_binary<double>(out, lon());
00079                 write_binary<double>(out, lat());
00080                 return out;
00081             }
00082 
00083             std::ostream& write_to_stream(std::ostream& out, AsHexWKB, bool with_srid=false) const {
00084                 write_hex_wkb_header(out, with_srid, wkbPoint);
00085                 write_hex<double>(out, lon());
00086                 write_hex<double>(out, lat());
00087                 return out;
00088             }
00089 
00090 #ifdef OSMIUM_WITH_GEOS
00091 
00096             geos::geom::Point* create_geos_geometry() const {
00097                 return Osmium::Geometry::geos_geometry_factory()->createPoint(m_position);
00098             }
00099 #endif // OSMIUM_WITH_GEOS
00100 
00101 #ifdef OSMIUM_WITH_SHPLIB
00102 
00108             SHPObject* create_shp_object() const {
00109                 double x = lon();
00110                 double y = lat();
00111                 return SHPCreateSimpleObject(SHPT_POINT, 1, &x, &y, NULL);
00112             }
00113 #endif // OSMIUM_WITH_SHPLIB
00114 
00115 #ifdef OSMIUM_WITH_OGR
00116 
00121             OGRPoint* create_ogr_geometry() const {
00122                 OGRPoint* p = new OGRPoint(lon(), lat());
00123                 return p;
00124             }
00125 #endif // OSMIUM_WITH_OGR
00126 
00127 #ifdef OSMIUM_WITH_JAVASCRIPT
00128             v8::Local<v8::Object> js_instance() const {
00129                 return JavascriptTemplate::get<JavascriptTemplate>().create_instance((void*)this);
00130             }
00131 
00132             v8::Handle<v8::Value> js_lon() const {
00133                 return v8::Number::New(lon());
00134             }
00135 
00136             v8::Handle<v8::Value> js_lat() const {
00137                 return v8::Number::New(lat());
00138             }
00139 
00140             v8::Handle<v8::Value> js_to_array(const v8::Arguments& /*args*/) {
00141                 return m_position.js_to_array();
00142             }
00143 
00144             struct JavascriptTemplate : public Osmium::Geometry::Geometry::JavascriptTemplate {
00145 
00146                 JavascriptTemplate() : Osmium::Geometry::Geometry::JavascriptTemplate() {
00147                     js_template->SetAccessor(v8::String::NewSymbol("lon"), accessor_getter<Point, &Point::js_lon>);
00148                     js_template->SetAccessor(v8::String::NewSymbol("lat"), accessor_getter<Point, &Point::js_lat>);
00149                     js_template->Set("toArray", v8::FunctionTemplate::New(function_template<Point, &Point::js_to_array>));
00150                 }
00151 
00152             };
00153 #endif // OSMIUM_WITH_JAVASCRIPT
00154 
00155         private:
00156 
00157             const Osmium::OSM::Position m_position;
00158 
00159         }; // class Point
00160 
00161     } // namespace Geometry
00162 
00163 } // namespace Osmium
00164 
00165 
00166 #ifdef OSMIUM_WITH_JAVASCRIPT
00167 v8::Handle<v8::Value> Osmium::OSM::Node::js_get_geom() const {
00168     Osmium::Geometry::Point* geom = new Osmium::Geometry::Point(*this);
00169     return Osmium::Javascript::Template::get<Osmium::Geometry::Point::JavascriptTemplate>().create_persistent_instance<Osmium::Geometry::Point>(geom);
00170 }
00171 #endif // OSMIUM_WITH_JAVASCRIPT
00172 
00173 #endif // OSMIUM_GEOMETRY_POINT_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines