dmpproject.cpp
3.7 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
/**************************************************************************
* 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/typeof/typeof.hpp>
#include "dmpproject.h"
#include "dmptilelayer.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
}
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;
}
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;
}
}