Osmium
0.1
|
00001 #ifndef OSMIUM_GEOMETRY_LINESTRING_HPP 00002 #define OSMIUM_GEOMETRY_LINESTRING_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 <algorithm> 00026 00027 #include <osmium/geometry/from_way.hpp> 00028 00029 namespace Osmium { 00030 00031 namespace Geometry { 00032 00036 class LineString : public FromWay { 00037 00038 public: 00039 00043 LineString(const Osmium::OSM::WayNodeList& way_node_list, 00044 bool reverse=false, 00045 osm_object_id_t id=0) 00046 : FromWay(way_node_list, reverse, id) { 00047 } 00048 00052 LineString(const Osmium::OSM::Way& way, 00053 bool reverse=false) 00054 : FromWay(way.nodes(), reverse, way.id()) { 00055 } 00056 00057 std::ostream& write_to_stream(std::ostream& out, AsWKT, bool with_srid=false) const { 00058 if (with_srid) { 00059 out << "SRID=4326;"; 00060 } 00061 LonLatListWriter<Osmium::OSM::WayNode> writer(out); 00062 out << "LINESTRING(" << std::setprecision(10); 00063 if (m_reverse) { 00064 for_each(m_way_node_list->rbegin(), m_way_node_list->rend(), writer); 00065 } else { 00066 for_each(m_way_node_list->begin(), m_way_node_list->end(), writer); 00067 } 00068 return out << ")"; 00069 } 00070 00071 std::ostream& write_to_stream(std::ostream& out, AsWKB, bool with_srid=false) const { 00072 write_binary_wkb_header(out, with_srid, wkbLineString); 00073 write_binary<uint32_t>(out, m_way_node_list->size()); 00074 if (m_reverse) { 00075 for (Osmium::OSM::WayNodeList::const_reverse_iterator it = m_way_node_list->rbegin(); it != m_way_node_list->rend(); ++it) { 00076 write_binary<double>(out, it->lon()); 00077 write_binary<double>(out, it->lat()); 00078 } 00079 } else { 00080 for (Osmium::OSM::WayNodeList::const_iterator it = m_way_node_list->begin(); it != m_way_node_list->end(); ++it) { 00081 write_binary<double>(out, it->lon()); 00082 write_binary<double>(out, it->lat()); 00083 } 00084 } 00085 return out; 00086 } 00087 00088 std::ostream& write_to_stream(std::ostream& out, AsHexWKB, bool with_srid=false) const { 00089 write_hex_wkb_header(out, with_srid, wkbLineString); 00090 write_hex<uint32_t>(out, m_way_node_list->size()); 00091 if (m_reverse) { 00092 for (Osmium::OSM::WayNodeList::const_reverse_iterator it = m_way_node_list->rbegin(); it != m_way_node_list->rend(); ++it) { 00093 write_hex<double>(out, it->lon()); 00094 write_hex<double>(out, it->lat()); 00095 } 00096 } else { 00097 for (Osmium::OSM::WayNodeList::const_iterator it = m_way_node_list->begin(); it != m_way_node_list->end(); ++it) { 00098 write_hex<double>(out, it->lon()); 00099 write_hex<double>(out, it->lat()); 00100 } 00101 } 00102 return out; 00103 } 00104 00105 #ifdef OSMIUM_WITH_GEOS 00106 00111 geos::geom::Geometry* create_geos_geometry() const { 00112 try { 00113 std::vector<geos::geom::Coordinate>* c = new std::vector<geos::geom::Coordinate>; 00114 if (m_reverse) { 00115 for (Osmium::OSM::WayNodeList::const_reverse_iterator it = m_way_node_list->rbegin(); it != m_way_node_list->rend(); ++it) { 00116 c->push_back(it->position()); 00117 } 00118 } else { 00119 for (Osmium::OSM::WayNodeList::const_iterator it = m_way_node_list->begin(); it != m_way_node_list->end(); ++it) { 00120 c->push_back(it->position()); 00121 } 00122 } 00123 geos::geom::CoordinateSequence* cs = Osmium::Geometry::geos_geometry_factory()->getCoordinateSequenceFactory()->create(c); 00124 return static_cast<geos::geom::Geometry*>(Osmium::Geometry::geos_geometry_factory()->createLineString(cs)); 00125 } catch (const geos::util::GEOSException& exc) { 00126 if (Osmium::debug()) { 00127 std::cerr << "error building geometry for way #" << id() << 00128 " (returning NULL): " << exc.what(); 00129 } 00130 return NULL; 00131 } 00132 } 00133 #endif // OSMIUM_WITH_GEOS 00134 00135 #ifdef OSMIUM_WITH_SHPLIB 00136 00142 SHPObject* create_shp_object() const { 00143 return create_line_or_polygon(SHPT_ARC); 00144 } 00145 #endif // OSMIUM_WITH_SHPLIB 00146 00147 #ifdef OSMIUM_WITH_OGR 00148 00153 OGRLineString* create_ogr_geometry() const { 00154 OGRLineString* p = new OGRLineString(); 00155 if (m_reverse) { 00156 for (Osmium::OSM::WayNodeList::const_reverse_iterator it = m_way_node_list->rbegin(); it != m_way_node_list->rend(); ++it) { 00157 p->addPoint(it->lon(), it->lat()); 00158 } 00159 } else { 00160 for (Osmium::OSM::WayNodeList::const_iterator it = m_way_node_list->begin(); it != m_way_node_list->end(); ++it) { 00161 p->addPoint(it->lon(), it->lat()); 00162 } 00163 } 00164 return p; 00165 } 00166 #endif // OSMIUM_WITH_OGR 00167 00168 #ifdef OSMIUM_WITH_JAVASCRIPT 00169 v8::Local<v8::Object> js_instance() const { 00170 return JavascriptTemplate::get<JavascriptTemplate>().create_instance((void *)this); 00171 } 00172 00173 v8::Handle<v8::Value> js_to_array(const v8::Arguments& /*args*/) { 00174 v8::HandleScope scope; 00175 v8::Local<v8::Array> linestring = v8::Array::New(m_way_node_list->size()); 00176 unsigned int max = m_way_node_list->size() - 1; 00177 if (m_reverse) { 00178 for (unsigned int i=0; i <= max; ++i) { 00179 linestring->Set(max - i, (*m_way_node_list)[i].position().js_to_array()); 00180 } 00181 } else { 00182 for (unsigned int i=0; i <= max; ++i) { 00183 linestring->Set(i, (*m_way_node_list)[i].position().js_to_array()); 00184 } 00185 } 00186 return scope.Close(linestring); 00187 } 00188 00189 struct JavascriptTemplate : public Osmium::Geometry::Geometry::JavascriptTemplate { 00190 00191 JavascriptTemplate() : Osmium::Geometry::Geometry::JavascriptTemplate() { 00192 js_template->Set("toArray", v8::FunctionTemplate::New(function_template<LineString, &LineString::js_to_array>)); 00193 } 00194 00195 }; 00196 #endif // OSMIUM_WITH_JAVASCRIPT 00197 00198 }; // class LineString 00199 00200 } // namespace Geometry 00201 00202 } // namespace Osmium 00203 00204 #ifdef OSMIUM_WITH_JAVASCRIPT 00205 v8::Handle<v8::Value> Osmium::OSM::Way::js_geom() const { 00206 if (m_node_list.has_position()) { 00207 Osmium::Geometry::LineString* geom = new Osmium::Geometry::LineString(*this); 00208 return Osmium::Javascript::Template::get<Osmium::Geometry::LineString::JavascriptTemplate>().create_persistent_instance<Osmium::Geometry::LineString>(geom); 00209 } else { 00210 Osmium::Geometry::Null* geom = new Osmium::Geometry::Null(); 00211 return Osmium::Javascript::Template::get<Osmium::Geometry::Null::JavascriptTemplate>().create_persistent_instance<Osmium::Geometry::Null>(geom); 00212 } 00213 } 00214 00215 v8::Handle<v8::Value> Osmium::OSM::Way::js_reverse_geom() const { 00216 if (m_node_list.has_position()) { 00217 Osmium::Geometry::LineString* geom = new Osmium::Geometry::LineString(*this, true); 00218 return Osmium::Javascript::Template::get<Osmium::Geometry::LineString::JavascriptTemplate>().create_persistent_instance<Osmium::Geometry::LineString>(geom); 00219 } else { 00220 Osmium::Geometry::Null* geom = new Osmium::Geometry::Null(); 00221 return Osmium::Javascript::Template::get<Osmium::Geometry::Null::JavascriptTemplate>().create_persistent_instance<Osmium::Geometry::Null>(geom); 00222 } 00223 } 00224 #endif // OSMIUM_WITH_JAVASCRIPT 00225 00226 #endif // OSMIUM_GEOMETRY_LINESTRING_HPP