1 //**************************************************************************************************
2 //
3 //     OSSIM Open Source Geospatial Data Processing Library
4 //     See top level LICENSE.txt file for license information
5 //
6 //**************************************************************************************************
7 
8 #include <ossim/reg/PhotoBlock.h>
9 #include <ossim/base/ossimException.h>
10 
11 using namespace std;
12 
13 namespace ossim
14 {
PhotoBlock()15 PhotoBlock::PhotoBlock()
16 {
17 }
18 
PhotoBlock(const Json::Value & pb_json_node)19 PhotoBlock::PhotoBlock(const Json::Value& pb_json_node)
20 {
21    loadJSON(pb_json_node);
22 }
23 
PhotoBlock(const PhotoBlock & copyThis)24 PhotoBlock::PhotoBlock(const PhotoBlock& copyThis)
25 : m_imageList (copyThis.m_imageList),
26   m_tiePointList (copyThis.m_tiePointList)
27 {
28    *this = copyThis;
29 }
30 
~PhotoBlock()31 PhotoBlock::~PhotoBlock()
32 {
33    m_imageList.clear();
34    m_tiePointList.clear();
35 }
36 
operator =(const PhotoBlock & copythis)37 PhotoBlock& PhotoBlock::operator=(const PhotoBlock& copythis)
38 {
39    m_imageList = copythis.m_imageList;
40    m_tiePointList = copythis.m_tiePointList;
41    m_gcpList = copythis.m_gcpList;
42    return *this;
43 }
44 
45 // TODO: Add of individual components not available until proper management of the JCM can be
46 //       provided
addImage(shared_ptr<Image> image)47 unsigned int  PhotoBlock::addImage(shared_ptr<Image> image)
48 {
49    unsigned int last_idx = m_imageList.size();
50    m_imageList.push_back(image);
51    return last_idx;
52 }
53 
addGroundPoint(shared_ptr<GroundControlPoint> gcp)54 unsigned int  PhotoBlock::addGroundPoint(shared_ptr<GroundControlPoint> gcp)
55 {
56    unsigned int last_idx = m_gcpList.size();
57    m_gcpList.push_back(gcp);
58    return last_idx;
59 }
60 
addTiePoint(shared_ptr<TiePoint> tiepoint)61 unsigned int PhotoBlock::addTiePoint(shared_ptr<TiePoint> tiepoint)
62 {
63    unsigned int index = m_tiePointList.size();
64    m_tiePointList.push_back(tiepoint);
65    return index;
66 }
67 
addTiePoints(TiePointList & tiepointList)68 void PhotoBlock::addTiePoints(TiePointList& tiepointList)
69 {
70    for (size_t i=0; i<tiepointList.size(); ++i)
71    {
72       addTiePoint(tiepointList[i]);
73    }
74 }
75 
getImage(const std::string & imageId)76 shared_ptr<Image> PhotoBlock::getImage(const std::string& imageId)
77 {
78    std::shared_ptr<Image> result;
79    for (size_t i=0; i<m_imageList.size(); ++i)
80    {
81       if (m_imageList[i]->getImageId() == imageId)
82       {
83          result = m_imageList[i];
84          break;
85       }
86    }
87    return result;
88 }
89 
getGroundPoint(const std::string & gcpId)90 shared_ptr<GroundControlPoint> PhotoBlock::getGroundPoint(const std::string& gcpId)
91 {
92    std::shared_ptr<GroundControlPoint> result;
93    for (size_t i=0; i<m_gcpList.size(); ++i)
94    {
95       if (m_gcpList[i]->getId() == gcpId)
96       {
97          result = m_gcpList[i];
98          break;
99       }
100    }
101    return result;
102 }
103 
getTiePoint(unsigned int tpId)104 shared_ptr<TiePoint> PhotoBlock::getTiePoint(unsigned int tpId)
105 {
106    std::shared_ptr<TiePoint> result;
107    for (size_t i=0; i<m_tiePointList.size(); ++i)
108    {
109       if (m_tiePointList[i]->getTiePointId() == tpId)
110       {
111          result = m_tiePointList[i];
112          break;
113       }
114    }
115    return result;
116 }
117 
loadJSON(const Json::Value & pb_json_node)118 void PhotoBlock::loadJSON(const Json::Value& pb_json_node)
119 {
120    // Always do images first, as tiepoints will be using the image list to correct image ID:
121    if (pb_json_node.isMember("images"))
122    {
123       const Json::Value& listJson = pb_json_node["images"];
124       unsigned int count = listJson.size();
125       for (unsigned int i=0; i<count; ++i)
126       {
127          const Json::Value& jsonItem = listJson[i];
128          shared_ptr<Image> item (new Image(jsonItem));
129          m_imageList.push_back(item);
130       }
131    }
132 
133    if (pb_json_node.isMember("groundPoints"))
134    {
135       const Json::Value& listJson = pb_json_node["groundPoints"];
136       unsigned int count = listJson.size();
137       for (unsigned int i=0; i<count; ++i)
138       {
139          const Json::Value& jsonItem = listJson[i];
140          shared_ptr<GroundControlPoint> item (new GroundControlPoint(jsonItem));
141          m_gcpList.push_back(item);
142       }
143    }
144 
145    if (pb_json_node.isMember("tiePoints"))
146    {
147       const Json::Value& listJson = pb_json_node["tiePoints"];
148       unsigned int count = listJson.size();
149       for (unsigned int i=0; i<count; ++i)
150       {
151          const Json::Value& jsonItem = listJson[i];
152          shared_ptr<TiePoint> item (new TiePoint(jsonItem));
153          m_tiePointList.push_back(item);
154       }
155    }
156 }
157 
saveJSON(Json::Value & pbJSON) const158 void PhotoBlock::saveJSON(Json::Value& pbJSON) const
159 {
160    Json::Value imageListJson (Json::arrayValue);
161    unsigned int count = m_imageList.size();
162    for (unsigned int i=0; i<count; ++i)
163    {
164       m_imageList[i]->saveJSON(imageListJson[i]);
165    }
166    pbJSON["images"] = imageListJson;
167 
168    Json::Value gcpListJson (Json::arrayValue);
169    count = m_gcpList.size();
170    for (unsigned int i=0; i<count; ++i)
171    {
172       m_gcpList[i]->saveJSON(gcpListJson[i]);
173    }
174    pbJSON["groundPoints"] = gcpListJson;
175 
176    Json::Value tpListJson (Json::arrayValue);
177    count = m_tiePointList.size();
178    for (unsigned int i=0; i<count; ++i)
179    {
180       m_tiePointList[i]->saveJSON(tpListJson[i]);
181    }
182    pbJSON["tiePoints"] = tpListJson;
183 }
184 
185 } // end namespace
186