|
31 | 31 | #include "create_datasource.hpp" |
32 | 32 | // stl |
33 | 33 | #include <vector> |
34 | | -//pybind11 |
| 34 | +// pybind11 |
35 | 35 | #include <pybind11/pybind11.h> |
36 | 36 | #include <pybind11/operators.h> |
37 | 37 | #include <pybind11/stl.h> |
38 | 38 | #include <pybind11/native_enum.h> |
39 | 39 |
|
| 40 | +using mapnik::attribute_descriptor; |
40 | 41 | using mapnik::datasource; |
41 | | -using mapnik::memory_datasource; |
42 | 42 | using mapnik::layer_descriptor; |
43 | | -using mapnik::attribute_descriptor; |
| 43 | +using mapnik::memory_datasource; |
44 | 44 | using mapnik::parameters; |
45 | 45 |
|
46 | 46 | namespace py = pybind11; |
47 | 47 |
|
48 | 48 | namespace |
49 | 49 | { |
50 | 50 |
|
51 | | -py::dict describe(std::shared_ptr<mapnik::datasource> const& ds) |
52 | | -{ |
53 | | - py::dict description; |
54 | | - mapnik::layer_descriptor ld = ds->get_descriptor(); |
55 | | - description["type"] = ds->type(); |
56 | | - description["name"] = ld.get_name(); |
57 | | - description["geometry_type"] = ds->get_geometry_type(); |
58 | | - description["encoding"] = ld.get_encoding(); |
59 | | - for (auto const& param : ld.get_extra_parameters()) |
| 51 | + py::dict describe(std::shared_ptr<mapnik::datasource> const &ds) |
60 | 52 | { |
61 | | - description[py::str(param.first)] = mapnik_param_to_python::convert(param.second); |
| 53 | + py::dict description; |
| 54 | + mapnik::layer_descriptor ld = ds->get_descriptor(); |
| 55 | + description["type"] = ds->type(); |
| 56 | + description["name"] = ld.get_name(); |
| 57 | + description["geometry_type"] = ds->get_geometry_type(); |
| 58 | + description["encoding"] = ld.get_encoding(); |
| 59 | + for (auto const ¶m : ld.get_extra_parameters()) |
| 60 | + { |
| 61 | + description[py::str(param.first)] = mapnik_param_to_python::convert(param.second); |
| 62 | + } |
| 63 | + return description; |
62 | 64 | } |
63 | | - return description; |
64 | | -} |
65 | 65 |
|
66 | | -py::list fields(std::shared_ptr<mapnik::datasource> const& ds) |
67 | | -{ |
68 | | - py::list flds; |
69 | | - if (ds) |
| 66 | + py::list fields(std::shared_ptr<mapnik::datasource> const &ds) |
70 | 67 | { |
71 | | - layer_descriptor ld = ds->get_descriptor(); |
72 | | - std::vector<attribute_descriptor> const& desc_ar = ld.get_descriptors(); |
73 | | - std::vector<attribute_descriptor>::const_iterator it = desc_ar.begin(); |
74 | | - std::vector<attribute_descriptor>::const_iterator end = desc_ar.end(); |
75 | | - for (; it != end; ++it) |
| 68 | + py::list flds; |
| 69 | + if (ds) |
76 | 70 | { |
77 | | - flds.append(it->get_name()); |
| 71 | + layer_descriptor ld = ds->get_descriptor(); |
| 72 | + std::vector<attribute_descriptor> const &desc_ar = ld.get_descriptors(); |
| 73 | + std::vector<attribute_descriptor>::const_iterator it = desc_ar.begin(); |
| 74 | + std::vector<attribute_descriptor>::const_iterator end = desc_ar.end(); |
| 75 | + for (; it != end; ++it) |
| 76 | + { |
| 77 | + flds.append(it->get_name()); |
| 78 | + } |
78 | 79 | } |
| 80 | + return flds; |
79 | 81 | } |
80 | | - return flds; |
81 | | -} |
82 | | -py::list field_types(std::shared_ptr<mapnik::datasource> const& ds) |
83 | | -{ |
84 | | - py::list fld_types; |
85 | | - if (ds) |
| 82 | + py::list field_types(std::shared_ptr<mapnik::datasource> const &ds) |
86 | 83 | { |
87 | | - layer_descriptor ld = ds->get_descriptor(); |
88 | | - std::vector<attribute_descriptor> const& desc_ar = ld.get_descriptors(); |
89 | | - std::vector<attribute_descriptor>::const_iterator it = desc_ar.begin(); |
90 | | - std::vector<attribute_descriptor>::const_iterator end = desc_ar.end(); |
91 | | - for (; it != end; ++it) |
| 84 | + py::list fld_types; |
| 85 | + if (ds) |
92 | 86 | { |
93 | | - unsigned type = it->get_type(); |
94 | | - if (type == mapnik::Integer) |
95 | | - fld_types.append(py::str("int")); |
96 | | - else if (type == mapnik::Float) |
97 | | - fld_types.append(py::str("float")); |
98 | | - else if (type == mapnik::Double) |
99 | | - fld_types.append(py::str("float")); |
100 | | - else if (type == mapnik::String) |
101 | | - fld_types.append(py::str("str")); |
102 | | - else if (type == mapnik::Boolean) |
103 | | - fld_types.append(py::str("bool")); |
104 | | - else if (type == mapnik::Geometry) |
105 | | - fld_types.append(py::str("geometry")); |
106 | | - else if (type == mapnik::Object) |
107 | | - fld_types.append(py::str("object")); |
108 | | - else |
109 | | - fld_types.append(py::str("unknown")); |
| 87 | + layer_descriptor ld = ds->get_descriptor(); |
| 88 | + std::vector<attribute_descriptor> const &desc_ar = ld.get_descriptors(); |
| 89 | + std::vector<attribute_descriptor>::const_iterator it = desc_ar.begin(); |
| 90 | + std::vector<attribute_descriptor>::const_iterator end = desc_ar.end(); |
| 91 | + for (; it != end; ++it) |
| 92 | + { |
| 93 | + unsigned type = it->get_type(); |
| 94 | + if (type == mapnik::Integer) |
| 95 | + fld_types.append(py::str("int")); |
| 96 | + else if (type == mapnik::Float) |
| 97 | + fld_types.append(py::str("float")); |
| 98 | + else if (type == mapnik::Double) |
| 99 | + fld_types.append(py::str("float")); |
| 100 | + else if (type == mapnik::String) |
| 101 | + fld_types.append(py::str("str")); |
| 102 | + else if (type == mapnik::Boolean) |
| 103 | + fld_types.append(py::str("bool")); |
| 104 | + else if (type == mapnik::Geometry) |
| 105 | + fld_types.append(py::str("geometry")); |
| 106 | + else if (type == mapnik::Object) |
| 107 | + fld_types.append(py::str("object")); |
| 108 | + else |
| 109 | + fld_types.append(py::str("unknown")); |
| 110 | + } |
110 | 111 | } |
| 112 | + return fld_types; |
111 | 113 | } |
112 | | - return fld_types; |
113 | | -} |
114 | 114 |
|
115 | | -py::dict parameters_impl(std::shared_ptr<mapnik::datasource> const& ds) |
116 | | -{ |
117 | | - auto const params = ds->params(); |
118 | | - py::dict d; |
119 | | - for (auto kv : params) |
| 115 | + py::dict parameters_impl(std::shared_ptr<mapnik::datasource> const &ds) |
120 | 116 | { |
121 | | - d[py::str(kv.first)] = mapnik_param_to_python::convert(kv.second); |
| 117 | + auto const params = ds->params(); |
| 118 | + py::dict d; |
| 119 | + for (auto const &kv : params) |
| 120 | + { |
| 121 | + d[py::str(kv.first)] = mapnik_param_to_python::convert(kv.second); |
| 122 | + } |
| 123 | + return d; |
122 | 124 | } |
123 | | - return d; |
124 | | -} |
125 | 125 |
|
126 | 126 | } // namespace |
127 | 127 |
|
128 | | - |
129 | | -void export_datasource(py::module& m) |
| 128 | +void export_datasource(py::module &m) |
130 | 129 | { |
131 | 130 | py::native_enum<mapnik::datasource::datasource_t>(m, "DataType", "enum.Enum") |
132 | | - .value("Vector",mapnik::datasource::Vector) |
133 | | - .value("Raster",mapnik::datasource::Raster) |
134 | | - .finalize() |
135 | | - ; |
| 131 | + .value("Vector", mapnik::datasource::Vector) |
| 132 | + .value("Raster", mapnik::datasource::Raster) |
| 133 | + .finalize(); |
136 | 134 |
|
137 | 135 | py::native_enum<mapnik::datasource_geometry_t>(m, "DataGeometryType", "enum.Enum") |
138 | | - .value("Point",mapnik::datasource_geometry_t::Point) |
139 | | - .value("LineString",mapnik::datasource_geometry_t::LineString) |
140 | | - .value("Polygon",mapnik::datasource_geometry_t::Polygon) |
141 | | - .value("Collection",mapnik::datasource_geometry_t::Collection) |
142 | | - .finalize() |
143 | | - ; |
| 136 | + .value("Point", mapnik::datasource_geometry_t::Point) |
| 137 | + .value("LineString", mapnik::datasource_geometry_t::LineString) |
| 138 | + .value("Polygon", mapnik::datasource_geometry_t::Polygon) |
| 139 | + .value("Collection", mapnik::datasource_geometry_t::Collection) |
| 140 | + .finalize(); |
144 | 141 |
|
145 | | - py::class_<datasource,std::shared_ptr<datasource>> (m, "Datasource") |
146 | | - .def(py::init([] (py::kwargs const& kwargs) { return create_datasource(kwargs);})) |
| 142 | + py::class_<datasource, std::shared_ptr<datasource>>(m, "Datasource") |
| 143 | + .def(py::init([](py::kwargs const &kwargs) |
| 144 | + { return create_datasource(kwargs); })) |
147 | 145 | .def("type", &datasource::type) |
148 | 146 | .def("geometry_type", &datasource::get_geometry_type) |
149 | 147 | .def("describe", &describe) |
150 | 148 | .def("envelope", &datasource::envelope) |
151 | 149 | .def("features", &datasource::features) |
152 | | - .def("fields" ,&fields) |
| 150 | + .def("fields", &fields) |
153 | 151 | .def("field_types", &field_types) |
154 | 152 | .def("features_at_point", &datasource::features_at_point, py::arg("coord"), py::arg("tolerance") = 0) |
155 | 153 | .def("parameters", ¶meters_impl, |
156 | 154 | "The configuration parameters of the data source. " |
157 | 155 | "These vary depending on the type of data source.") |
158 | 156 | .def(py::self == py::self) |
159 | | - .def("__iter__", |
160 | | - [](datasource const& ds) { |
| 157 | + .def("__iter__", [](datasource const &ds) |
| 158 | + { |
161 | 159 | mapnik::query q(ds.envelope()); |
162 | 160 | layer_descriptor ld = ds.get_descriptor(); |
163 | 161 | std::vector<attribute_descriptor> const& desc_ar = ld.get_descriptors(); |
164 | 162 | for (auto const& desc : desc_ar) |
165 | 163 | { |
166 | 164 | q.add_property_name(desc.get_name()); |
167 | 165 | } |
168 | | - return ds.features(q); |
169 | | - }, |
170 | | - py::keep_alive<0, 1>()) |
171 | | - ; |
| 166 | + return ds.features(q); }, py::keep_alive<0, 1>()); |
172 | 167 |
|
173 | | - m.def("CreateDatasource",&create_datasource); |
| 168 | + m.def("CreateDatasource", &create_datasource); |
174 | 169 |
|
175 | | - py::class_<memory_datasource, datasource, std::shared_ptr<memory_datasource>> |
176 | | - (m, "MemoryDatasource") |
177 | | - .def(py::init([]() { |
| 170 | + py::class_<memory_datasource, datasource, std::shared_ptr<memory_datasource>>(m, "MemoryDatasource") |
| 171 | + .def(py::init([]() |
| 172 | + { |
178 | 173 | mapnik::parameters p; |
179 | 174 | p.insert(std::make_pair("type","memory")); |
180 | | - return std::make_shared<memory_datasource>(p);})) |
| 175 | + return std::make_shared<memory_datasource>(p); })) |
181 | 176 | .def("add_feature", &memory_datasource::push, |
182 | 177 | "Adds a Feature:\n" |
183 | 178 | ">>> ms = MemoryDatasource()\n" |
184 | 179 | ">>> feature = Feature(Context(),1)\n" |
185 | 180 | ">>> ms.add_feature(f)\n") |
186 | | - .def("num_features", &memory_datasource::size) |
187 | | - ; |
| 181 | + .def("num_features", &memory_datasource::size); |
188 | 182 |
|
189 | 183 | py::implicitly_convertible<memory_datasource, datasource>(); |
190 | 184 | } |
0 commit comments