GeometryFile.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/geometry/GeometryFile.cpp
22 */
23 #include "GeometryFile.h"
24 
25 // TerraLib
26 #include "../../../geometry/Geometry.h"
27 #include "../../../geometry/WKBReader.h"
28 
30 m_i(0)
31 {
32  m_file.reset(nullptr);
33  m_posFile.reset(nullptr);
34 }
35 
37 {
38  m_file->close();
39  m_posFile->close();
40 }
41 
42 void GeometryFile::openFile(const char* fileName) throw (te::common::Exception)
43 {
44  m_file.reset(new std::ifstream(fileName, std::ios::in | std::ios::binary));
45  m_posFile.reset(new std::ifstream((fileName+std::string(".pos")).c_str(), std::ios::in | std::ios::binary));
46 
47  if(!m_file->is_open() || !m_posFile->is_open())
48  throw te::common::Exception("Fail to open file.");
49 }
50 
52 {
53  if((m_i > 0) && (m_i >= m_gids.size()) && !(m_gids.empty()))
54  return nullptr;
55 
56  unsigned int gSize;
57  unsigned int id = m_i;
58  unsigned int gPos;
59 
60  if(!m_gids.empty())
61  id = m_gids[m_i];
62 
63  m_i++;
64 
65  m_posFile->seekg(id*sizeof(unsigned int), std::ios::beg);
66  m_posFile->read((char*)&gPos, sizeof(unsigned int));
67 
68  if(m_posFile->gcount() == 0)
69  return nullptr;
70 
71  m_file->seekg(gPos, std::ios::beg);
72 
73  m_file->read((char*)&gSize, sizeof(unsigned int));
74 
75  if(m_file->gcount() == 0)
76  return nullptr;
77 
78  char* wkb = new char[gSize];
79 
80  m_file->read(wkb, gSize);
81 
82  te::gm::Geometry* gm = nullptr;
83 
84  if(m_file->gcount() != 0)
85  gm = te::gm::WKBReader::read(wkb);
86 
87  delete[] wkb;
88 
89  return gm;
90 }
91 
92 void GeometryFile::writeGeometries(const char* fileName, const std::map<int, te::gm::Geometry*>& geoms)
93 {
94  std::string gFileName = std::string(fileName) + ".geom";
95  std::string posFileName = gFileName + ".pos";
96 
97  std::ofstream gFile(gFileName.c_str(), std::ios::out | std::ios::binary);
98  std::ofstream gposFile(posFileName.c_str(), std::ios::out | std::ios::binary);
99 
100  if(!gFile.is_open() || !gposFile.is_open())
101  return;
102 
103  unsigned int pos = 0;
104 
105  for(std::map<int, te::gm::Geometry*>::const_iterator it = geoms.begin(); it != geoms.end(); ++it)
106  {
107  // Getting the position on file
108  unsigned int gBS;// = (unsigned int)gm->getWkbSize();
109 
110  size_t gBSi;
111 
112  char * wkb = it->second->asBinary(gBSi);
113 
114  gBS = (unsigned int)gBSi;
115 
116  // Index of the geometry in the file
117  gposFile.write((char*)&pos, sizeof(unsigned int));
118 
119  // Write wkb size and wkb
120  gFile.write((char*)&gBS, sizeof(unsigned int));
121  gFile.write(wkb, gBS);
122 
123  delete[] wkb;
124 
125  // Updating position
126  pos += sizeof(unsigned int)+gBS;
127  }
128 
129  gFile.close();
130  gposFile.close();
131 }
132 
133 void GeometryFile::writeGeometries(const char* fileName, const std::vector<te::gm::Geometry*>& geoms)
134 {
135  std::string gFileName = std::string(fileName) + ".geom";
136  std::string posFileName = gFileName + ".pos";
137  std::string idsFileName = gFileName + ".ids";
138 
139  std::ofstream gFile(gFileName.c_str(), std::ios::out | std::ios::binary);
140  std::ofstream gposFile(posFileName.c_str(), std::ios::out | std::ios::binary);
141 
142  if(!gFile.is_open() || !gposFile.is_open())
143  return;
144 
145  unsigned int pos = 0;
146 
147  for(std::vector<te::gm::Geometry*>::const_iterator it = geoms.begin(); it != geoms.end(); ++it)
148  {
149  // Getting the position on file
150  unsigned int gBS;
151 
152  size_t gBSi;
153 
154  char * wkb = (*it)->asBinary(gBSi);
155 
156  gBS = (unsigned int)gBSi;
157 
158  // Index of the geometry in the file
159  gposFile.write((char*)&pos, sizeof(unsigned int));
160 
161  // Write wkb size and wkb
162  gFile.write((char*)&gBS, sizeof(unsigned int));
163  gFile.write(wkb, gBS);
164 
165  delete[] wkb;
166 
167  // Updating position
168  pos += sizeof(unsigned int)+gBS;
169  }
170 
171  gFile.close();
172  gposFile.close();
173 }
174 
175 void GeometryFile::setGeometries(const std::vector<unsigned int>& gids)
176 {
177  m_gids = gids;
178  m_i = 0;
179 }
void setGeometries(const std::vector< unsigned int > &gids)
Sets the desired geometries to get from file.
void openFile(const char *fileName)
Opens the file fileName.
~GeometryFile()
Destructor.
static void writeGeometries(const char *fileName, const std::map< int, te::gm::Geometry * > &geoms)
GeometryFile()
Constructor.
std::unique_ptr< std::ifstream > m_posFile
Pointer to the geometry pos file.
Definition: GeometryFile.h:116
unsigned int m_i
Current position on traversing.
Definition: GeometryFile.h:118
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
std::vector< unsigned int > m_gids
Selected ids to use on traverse operation.
Definition: GeometryFile.h:117
std::unique_ptr< std::ifstream > m_file
Pointer to the geometry file.
Definition: GeometryFile.h:115
te::gm::Geometry * next()
Returns the next geometry.
static Geometry * read(const char *wkb)
It returns a valid geometry from a given WKB.
Definition: WKBReader.cpp:48