1 #include <algorithm>
2 
3 #include <ossim/imaging/ossimVpfAnnotationSource.h>
4 #include <ossim/imaging/ossimVpfAnnotationLibraryInfo.h>
5 #include <ossim/vec/ossimVpfExtent.h>
6 #include <ossim/projection/ossimEquDistCylProjection.h>
7 #include <ossim/imaging/ossimRgbImage.h>
8 #include <ossim/imaging/ossimU8ImageData.h>
9 #include <ossim/base/ossimTrace.h>
10 #include <ossim/base/ossimKeywordNames.h>
11 
12 using namespace std;
13 
14 static ossimTrace traceDebug("ossimVpfAnnotationSource:debug");
15 
ossimVpfAnnotationSource()16 ossimVpfAnnotationSource::ossimVpfAnnotationSource()
17    :ossimGeoAnnotationSource()
18 {
19   if(!m_geometry)
20   {
21      m_geometry = new ossimImageGeometry(0, new ossimEquDistCylProjection);
22   }
23    ossimMapProjection* mapProj = dynamic_cast<ossimMapProjection*>(m_geometry->getProjection());
24 
25    if(mapProj)
26    {
27       mapProj->setMetersPerPixel(ossimDpt(30, 30));
28    }
29 }
30 
~ossimVpfAnnotationSource()31 ossimVpfAnnotationSource::~ossimVpfAnnotationSource()
32 {
33    close();
34 }
35 
open(const ossimFilename & file)36 bool ossimVpfAnnotationSource::open(const ossimFilename& file)
37 {
38    if(traceDebug())
39    {
40       ossimNotify(ossimNotifyLevel_DEBUG) << "ossimVpfAnnotationSource::open DEBUG: entering..." << std::endl;
41    }
42    bool result = false;
43 
44    if(file.file().downcase() == "dht")
45    {
46       if(openDatabase(file))
47       {
48          vector<ossimString> libraryNames = theDatabase.getLibraryNames();
49          if(traceDebug())
50          {
51             ossimNotify(ossimNotifyLevel_DEBUG) << "Library names list size = " << libraryNames.size() << std::endl;
52          }
53 
54          for(int idx = 0; idx < (int)libraryNames.size();++idx)
55          {
56             ossimVpfAnnotationLibraryInfo* info = new ossimVpfAnnotationLibraryInfo;
57             info->setName(libraryNames[idx]);
58             theLibraryInfo.push_back(info);
59             info->setDatabase(&theDatabase);
60             if(traceDebug())
61             {
62                ossimNotify(ossimNotifyLevel_DEBUG) << "loading library " << libraryNames[idx] << std::endl;
63             }
64             info->buildLibrary();
65             info->getAllFeatures(theFeatureList);
66          }
67          transformObjects();
68          result = true;
69       }
70    }
71    if(traceDebug())
72    {
73       ossimNotify(ossimNotifyLevel_DEBUG) << "ossimVpfAnnotationSource::open DEBUG: leaving..." << std::endl;
74    }
75    return result;
76 }
77 
close()78 void ossimVpfAnnotationSource::close()
79 {
80    theDatabase.closeDatabase();
81    for(int idx = 0; idx < (int)theLibraryInfo.size(); ++idx)
82    {
83       delete theLibraryInfo[idx];
84    }
85 
86    theLibraryInfo.clear();
87 }
88 
getFilename() const89 ossimFilename ossimVpfAnnotationSource::getFilename()const
90 {
91    return theFilename;
92 }
93 
transformObjects(ossimImageGeometry * geom)94 void ossimVpfAnnotationSource::transformObjects(ossimImageGeometry* geom)
95 {
96    if(traceDebug())
97    {
98       ossimNotify(ossimNotifyLevel_DEBUG) << "ossimVpfAnnotationSource::transformObjects DEBUG: entered..." << std::endl;
99    }
100    ossimImageGeometry* tempGeom = geom;
101 
102    if(!tempGeom)
103    {
104       tempGeom = m_geometry.get();
105    }
106 
107    if(!tempGeom) return;
108    for(int idx = 0; idx < (int)theLibraryInfo.size(); ++idx)
109    {
110       theLibraryInfo[idx]->transform(tempGeom);
111    }
112    computeBoundingRect();
113    if(traceDebug())
114    {
115       ossimNotify(ossimNotifyLevel_DEBUG) << "ossimVpfAnnotationSource::transformObjects DEBUG: leaving..." << std::endl;
116    }
117 }
118 
computeBoundingRect()119 void ossimVpfAnnotationSource::computeBoundingRect()
120 
121 {
122   ossimIrect result;
123   result.makeNan();
124   for(int i = 0; i < (int)theLibraryInfo.size();++i)
125     {
126       ossimIrect tempRect = theLibraryInfo[i]->getBoundingProjectedRect();
127       if(!tempRect.hasNans())
128 	{
129 	  if(result.hasNans())
130 	    {
131 	      result = tempRect;
132 	    }
133 	  else
134 	    {
135 	      result = result.combine(tempRect);
136 	    }
137 	}
138     }
139 
140   theRectangle = result;
141 }
142 
143 
deleteAllLibraries()144 void ossimVpfAnnotationSource::deleteAllLibraries()
145 {
146   for(int idx = 0; idx < (int)theLibraryInfo.size();++idx)
147     {
148       delete theLibraryInfo[idx];
149     }
150 
151   theLibraryInfo.clear();
152 }
153 
getAllFeatures(std::vector<ossimVpfAnnotationFeatureInfo * > & features)154 void ossimVpfAnnotationSource::getAllFeatures(std::vector<ossimVpfAnnotationFeatureInfo*>& features)
155 {
156    features = theFeatureList;
157 }
158 
setAllFeatures(const std::vector<ossimVpfAnnotationFeatureInfo * > & features)159 void ossimVpfAnnotationSource::setAllFeatures(const std::vector<ossimVpfAnnotationFeatureInfo*>& features)
160 {
161   theFeatureList = features;
162 
163   computeBoundingRect();
164 
165 }
166 
167 
drawAnnotations(ossimRefPtr<ossimImageData> tile)168 void ossimVpfAnnotationSource::drawAnnotations(ossimRefPtr<ossimImageData> tile)
169 {
170    theImage->setCurrentImageData(tile);
171    if(theImage->getImageData().valid())
172    {
173       for(ossim_uint32 idx = 0; idx < theFeatureList.size();++idx)
174       {
175          if(theFeatureList[idx]->getEnabledFlag())
176          {
177             theFeatureList[idx]->drawAnnotations(theImage.get());
178          }
179       }
180    }
181 }
182 
openDatabase(const ossimFilename & file)183 bool ossimVpfAnnotationSource::openDatabase(const ossimFilename& file)
184 {
185   ossimAnnotationSource::deleteAll();
186   deleteAllLibraries();
187   theFeatureList.clear();
188   if(theDatabase.isOpened())
189     {
190       theDatabase.closeDatabase();
191     }
192 
193   theFilename = file;
194   return theDatabase.openDatabase(file);
195 }
196 
197 
saveState(ossimKeywordlist & kwl,const char * prefix) const198 bool ossimVpfAnnotationSource::saveState(ossimKeywordlist& kwl,
199                                          const char* prefix)const
200 {
201   kwl.add(prefix,
202 	  ossimKeywordNames::FILENAME_KW,
203 	  theFilename,
204 	  true);
205   for(ossim_uint32 idx = 0; idx < theLibraryInfo.size(); ++idx)
206     {
207        theLibraryInfo[idx]->saveState(kwl,
208  				     (ossimString(prefix) + "library" + ossimString::toString(idx) + ".").c_str());
209     }
210    return true;
211 }
212 
loadState(const ossimKeywordlist & kwl,const char * prefix)213 bool ossimVpfAnnotationSource::loadState(const ossimKeywordlist& kwl,
214                                          const char* prefix)
215 {
216   bool result = false;
217   deleteAllLibraries();
218   const char* filename    = kwl.find(prefix, ossimKeywordNames::FILENAME_KW);
219   if(filename)
220     {
221       if(openDatabase(filename))
222 	{
223 	  int idx = 0;
224 	  ossimString regExpression =  ossimString("^(") + ossimString(prefix) + "library[0-9]+.)";
225 	  vector<ossimString> keys =
226 	    kwl.getSubstringKeyList( regExpression );
227 	  std::vector<int> theNumberList(keys.size());
228 	  int offset = (int)(ossimString(prefix)+"library").size();
229 
230 	  for(idx = 0; idx < (int)theNumberList.size();++idx)
231 	    {
232 	      ossimString numberStr(keys[idx].begin() + offset,
233 				    keys[idx].end());
234 	      theNumberList[idx] = numberStr.toInt();
235 	    }
236 	  std::sort(theNumberList.begin(), theNumberList.end());
237 
238 	  for(idx=0;idx < (int)keys.size();++idx)
239 	    {
240 	      ossimString newPrefix = ossimString(prefix);
241 	      newPrefix += ossimString("library");
242 	      newPrefix += ossimString::toString(theNumberList[idx]);
243 	      newPrefix += ossimString(".");
244 
245 	      ossimVpfAnnotationLibraryInfo* info = new ossimVpfAnnotationLibraryInfo;
246 	      theLibraryInfo.push_back(info);
247 	      info->setDatabase(&theDatabase);
248 	      info->loadState(kwl,
249 			      newPrefix);
250 	      info->getAllFeatures(theFeatureList);
251 	    }
252 	  transformObjects();
253 	  result = true;
254 	}
255     }
256 
257   return result;
258 }
259 
open()260 bool ossimVpfAnnotationSource::open()
261 {
262    return open(theFilename);
263 }
264 
isOpen() const265 bool ossimVpfAnnotationSource::isOpen()const
266 {
267    return (theDatabase.isOpened());
268 }
269