1 #ifndef OSMIUM_GEOM_GEOJSON_HPP 2 #define OSMIUM_GEOM_GEOJSON_HPP 3 4 /* 5 6 This file is part of Osmium (https://osmcode.org/libosmium). 7 8 Copyright 2013-2021 Jochen Topf <jochen@topf.org> and others (see README). 9 10 Boost Software License - Version 1.0 - August 17th, 2003 11 12 Permission is hereby granted, free of charge, to any person or organization 13 obtaining a copy of the software and accompanying documentation covered by 14 this license (the "Software") to use, reproduce, display, distribute, 15 execute, and transmit the Software, and to prepare derivative works of the 16 Software, and to permit third-parties to whom the Software is furnished to 17 do so, all subject to the following: 18 19 The copyright notices in the Software and this entire statement, including 20 the above license grant, this restriction and the following disclaimer, 21 must be included in all copies of the Software, in whole or in part, and 22 all derivative works of the Software, unless such copies or derivative 23 works are solely in the form of machine-executable object code generated by 24 a source language processor. 25 26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 32 DEALINGS IN THE SOFTWARE. 33 34 */ 35 36 #include <osmium/geom/coordinates.hpp> 37 #include <osmium/geom/factory.hpp> 38 39 #include <cassert> 40 #include <cstddef> 41 #include <string> 42 #include <utility> 43 44 namespace osmium { 45 46 namespace geom { 47 48 namespace detail { 49 50 class GeoJSONFactoryImpl { 51 52 std::string m_str; 53 int m_precision; 54 55 public: 56 57 using point_type = std::string; 58 using linestring_type = std::string; 59 using polygon_type = std::string; 60 using multipolygon_type = std::string; 61 using ring_type = std::string; 62 GeoJSONFactoryImpl(int,int precision=7)63 explicit GeoJSONFactoryImpl(int /*srid*/, int precision = 7) : 64 m_precision(precision) { 65 } 66 67 /* Point */ 68 69 // { "type": "Point", "coordinates": [100.0, 0.0] } make_point(const osmium::geom::Coordinates & xy) const70 point_type make_point(const osmium::geom::Coordinates& xy) const { 71 std::string str{"{\"type\":\"Point\",\"coordinates\":"}; 72 xy.append_to_string(str, '[', ',', ']', m_precision); 73 str += "}"; 74 return str; 75 } 76 77 /* LineString */ 78 79 // { "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] } linestring_start()80 void linestring_start() { 81 m_str = "{\"type\":\"LineString\",\"coordinates\":["; 82 } 83 linestring_add_location(const osmium::geom::Coordinates & xy)84 void linestring_add_location(const osmium::geom::Coordinates& xy) { 85 xy.append_to_string(m_str, '[', ',', ']', m_precision); 86 m_str += ','; 87 } 88 linestring_finish(size_t)89 linestring_type linestring_finish(size_t /*num_points*/) { 90 assert(!m_str.empty()); 91 std::string str; 92 93 using std::swap; 94 swap(str, m_str); 95 96 str.back() = ']'; 97 str += "}"; 98 return str; 99 } 100 101 /* Polygon */ polygon_start()102 void polygon_start() { 103 m_str = "{\"type\":\"Polygon\",\"coordinates\":[["; 104 } 105 polygon_add_location(const osmium::geom::Coordinates & xy)106 void polygon_add_location(const osmium::geom::Coordinates& xy) { 107 xy.append_to_string(m_str, '[', ',', ']', m_precision); 108 m_str += ','; 109 } 110 polygon_finish(size_t)111 polygon_type polygon_finish(size_t /*num_points*/) { 112 assert(!m_str.empty()); 113 std::string str; 114 115 using std::swap; 116 swap(str, m_str); 117 118 str.back() = ']'; 119 str += "]}"; 120 return str; 121 } 122 123 /* MultiPolygon */ 124 multipolygon_start()125 void multipolygon_start() { 126 m_str = "{\"type\":\"MultiPolygon\",\"coordinates\":["; 127 } 128 multipolygon_polygon_start()129 void multipolygon_polygon_start() { 130 m_str += '['; 131 } 132 multipolygon_polygon_finish()133 void multipolygon_polygon_finish() { 134 m_str += "],"; 135 } 136 multipolygon_outer_ring_start()137 void multipolygon_outer_ring_start() { 138 m_str += '['; 139 } 140 multipolygon_outer_ring_finish()141 void multipolygon_outer_ring_finish() { 142 assert(!m_str.empty()); 143 m_str.back() = ']'; 144 } 145 multipolygon_inner_ring_start()146 void multipolygon_inner_ring_start() { 147 m_str += ",["; 148 } 149 multipolygon_inner_ring_finish()150 void multipolygon_inner_ring_finish() { 151 assert(!m_str.empty()); 152 m_str.back() = ']'; 153 } 154 multipolygon_add_location(const osmium::geom::Coordinates & xy)155 void multipolygon_add_location(const osmium::geom::Coordinates& xy) { 156 xy.append_to_string(m_str, '[', ',', ']', m_precision); 157 m_str += ','; 158 } 159 multipolygon_finish()160 multipolygon_type multipolygon_finish() { 161 assert(!m_str.empty()); 162 std::string str; 163 164 using std::swap; 165 swap(str, m_str); 166 167 str.back() = ']'; 168 str += "}"; 169 return str; 170 } 171 172 }; // class GeoJSONFactoryImpl 173 174 } // namespace detail 175 176 template <typename TProjection = IdentityProjection> 177 using GeoJSONFactory = GeometryFactory<osmium::geom::detail::GeoJSONFactoryImpl, TProjection>; 178 179 } // namespace geom 180 181 } // namespace osmium 182 183 #endif // OSMIUM_GEOM_GEOJSON_HPP 184