dmpproject.cpp
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**************************************************************************
* file: dmpproject.cpp
* Author: wanzhongping
* Date: 2021-07-07 16:46:54
* Email: zhongpingw@chinadci.com
* copyright: 广州城市信息研究所有限公司
***************************************************************************/
#include <iostream>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/typeof/typeof.hpp>
#include "dmpproject.h"
#include "dmptilelayer.h"
#include "dmpvectorlayer.h"
using namespace boost::property_tree;
DmpProject *DmpProject::project_ = nullptr;
DmpProject::DmpProject()
{
}
DmpProject *DmpProject::Instance()
{
if (!project_)
{
project_ = new DmpProject;
}
return project_;
}
DmpProject::~DmpProject()
{
if (this == project_)
{
project_ = nullptr;
}
std::map<std::string, DmpMapLayer *>::iterator iter = mapLayers_.begin();
for (; iter != mapLayers_.end(); ++iter)
{
DmpMapLayer *mapLayer = iter->second;
if (mapLayer)
{
delete mapLayer;
mapLayer = nullptr;
}
}
mapLayers_.clear();
}
bool DmpProject::Read(const std::string &data)
{
try
{
std::istringstream stream(data);
ptree pt;
read_xml(stream, pt);
projectName_ = pt.get<std::string>("dmap.<xmlattr>.projectname");
version_ = pt.get<std::string>("dmap.<xmlattr>.version");
//坐标系
ptree pSpatialRef = pt.get_child("dmap.projectCrs.spatialrefsys");
if (!crs_.readXml(pSpatialRef))
{
return false;
}
//图层
ptree pLayers = pt.get_child("dmap.projectlayers");
for (BOOST_AUTO(pos, pLayers.begin()); pos != pLayers.end(); ++pos)
{
DmpMapLayer *mapLayer = nullptr;
ptree pLayer = pos->second;
int layerType = pLayer.get<int>("<xmlattr>.type");
if (layerType == 0)
{
//tile
mapLayer = new DmpTileLayer();
}
else if (layerType == 1)
{
//vector
mapLayer = new DmpVectorLayer();
}
else if (layerType == 2)
{
//image
}
else
{
return false;
}
if (mapLayer && mapLayer->readXml(pLayer))
{
std::string name = mapLayer->name();
boost::to_upper(name);
mapLayers_[name] = mapLayer;
}
else
{
return false;
}
}
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
return false;
}
return true;
}
bool DmpProject::Write(const std::string &filename, const std::string &data)
{
std::istringstream stream(data);
boost::property_tree::ptree pt;
boost::property_tree::read_xml(stream, pt);
auto setting = boost::property_tree::xml_writer_make_settings<std::string>('\t', 1);
boost::property_tree::write_xml(filename, pt, std::locale(), setting);
return true;
}
bool DmpProject::WritePtree(boost::property_tree::ptree& ptDoc)
{
//目前还未完善 只支持 矢量图层
boost::property_tree::ptree ptDmap;
ptDmap.add("<xmlattr>.projectname",projectName_);
ptDmap.add("<xmlattr>.version",version_);
boost::property_tree::ptree ptProjectCrs;
crs_.write(ptProjectCrs);
ptDmap.add_child("projectCrs",ptProjectCrs);
boost::property_tree::ptree ptProjectlayers;
std::map<std::string, DmpMapLayer *>::iterator iter = mapLayers_.begin();
for (; iter != mapLayers_.end(); ++iter)
{
DmpMapLayer *mapLayer = iter->second;
if (mapLayer && mapLayer->type() == DmpMapLayerType::VectorLayer)
{
boost::property_tree::ptree ptlayer;
ptlayer.add("<xmlattr>.type", "1");
DmpVectorLayer *vectorMapLayer =(DmpVectorLayer *) mapLayer;
vectorMapLayer->writeXml(ptlayer);
ptProjectlayers.add_child("maplayer",ptlayer);
}
}
ptDmap.add_child("projectlayers", ptProjectlayers);
ptDoc.add_child("dmap",ptDmap);
return true;
}
std::string DmpProject::WriteJson()
{
boost::property_tree::ptree ptDoc;
WritePtree(ptDoc);
std::stringstream stream;
write_json(stream, ptDoc);
std::string data = stream.str();
return data;
}
std::string DmpProject::WriteXml()
{
boost::property_tree::ptree ptDoc;
WritePtree(ptDoc);
std::stringstream stream;
write_xml(stream, ptDoc);
std::string data = stream.str();
return data;
}
DmpCoordinateReferenceSystem DmpProject::crs() const
{
return crs_;
}
std::map<std::string, DmpMapLayer*> DmpProject::mapLayers() const
{
return mapLayers_;
}
DmpMapLayer *DmpProject::getLayer(const std::string &layerName) const
{
if(layerName.empty()){
return getLayer();
}
std::string name = boost::to_upper_copy(layerName);
std::map<std::string, DmpMapLayer*>::const_iterator iter = mapLayers_.find(name);
if (iter != mapLayers_.end())
{
return iter->second;
}
else
{
return nullptr;
}
}
DmpMapLayer *DmpProject::getLayer() const
{
std::map<std::string, DmpMapLayer*>::const_iterator iter = mapLayers_.begin();
if (iter != mapLayers_.end())
{
return iter->second;
}
else
{
return nullptr;
}
}