openModeller  Version 1.5.0
OccurrencesReader.cpp
Go to the documentation of this file.
1 
31 #include <openmodeller/Occurrences.hh> // List of occurrences.
32 
33 #include <vector>
34 using std::vector;
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 /*********************/
41 /*** get Presences ***/
43 OccurrencesReader::getPresences( const char *groupId )
44 {
45  // If group was not specified, return empty set
46  if ( ! groupId ) {
47 
48  return new OccurrencesImpl( 1 );
49  }
50 
51  LstOccurrences::iterator ocs = _presences.begin();
52  LstOccurrences::iterator end = _presences.end();
53 
54  while ( ocs != end ) {
55 
56  OccurrencesPtr oc = *ocs;
57 
58  if ( ! strcasecmp( groupId, oc->label() ) ) {
59 
60  _presences.erase( ocs );
61 
62  return oc;
63  }
64 
65  ++ocs;
66  }
67 
68  return new OccurrencesImpl( 1 );
69 }
70 
71 
72 /********************/
73 /*** get Absences ***/
75 OccurrencesReader::getAbsences( const char *groupId )
76 {
77  // If group was not specified, return empty set
78  if ( ! groupId ) {
79 
80  return new OccurrencesImpl( 0 );
81  }
82 
83  LstOccurrences::iterator ocs = _absences.begin();
84  LstOccurrences::iterator end = _absences.end();
85 
86  while ( ocs != end ) {
87 
88  OccurrencesPtr oc = *ocs;
89 
90  if ( ! strcasecmp( groupId, oc->label() ) ) {
91 
92  _absences.erase( ocs );
93 
94  return oc;
95  }
96 
97  ++ocs;
98  }
99 
100  return new OccurrencesImpl( 0 );
101 }
102 
103 
104 /*************/
105 /*** print ***/
106 void
107 OccurrencesReader::printOccurrences( const std::string& msg )
108 {
109  printf( "%s", msg.c_str() );
110 
111  printf( "\nPresences" );
112 
113  LstOccurrences::const_iterator ocs = _presences.begin();
114  LstOccurrences::const_iterator end = _presences.end();
115 
116  while ( ocs != end ) {
117 
118  (*ocs)->dump( "\n****************" );
119  ++ocs;
120  }
121 
122  printf( "\nAbsences" );
123 
124  ocs = _absences.begin();
125  end = _absences.end();
126 
127  while ( ocs != end ) {
128 
129  (*ocs)->dump( "\n****************" );
130  ++ocs;
131  }
132 }
133 
134 /**********************/
135 /*** add Occurrence ***/
136 int
137 OccurrencesReader::_addOccurrence( const char *id, const char *groupId, Coord lg, Coord lt,
138  Scalar error, Scalar abundance,
139  int num_attributes, Scalar *attributes )
140 {
141  if ( abundance > 0 ) {
142 
143  _addPresence( id, groupId, lg, lt, error, abundance, num_attributes, attributes );
144  }
145  else {
146 
147  _addAbsence( id, groupId, lg, lt, error, num_attributes, attributes );
148  }
149 
150  return 1;
151 }
152 
153 /********************/
154 /*** add Presence ***/
155 int
156 OccurrencesReader::_addPresence( const char *id, const char *groupId, Coord lg, Coord lt,
157  Scalar error, Scalar abundance,
158  int num_attributes, Scalar *attributes )
159 {
160  // If a presence group with the same name already exists,
161  // just add another occurrence to it.
162 
163  // We search backwards because new Occurrences are pushed
164  // onto the back, so most likely, the Occurrences we are looking
165  // for is at the back.
166  LstOccurrences::const_reverse_iterator ocs = _presences.rbegin();
167  LstOccurrences::const_reverse_iterator end = _presences.rend();
168  while ( ocs != end ) {
169 
170  OccurrencesPtr const & group = *ocs;
171 
172  if ( ! strcasecmp( group->label(), groupId ) ) {
173 
174  group->createOccurrence( id, lg, lt, error, abundance, num_attributes, attributes );
175  return 0;
176  }
177 
178  ++ocs;
179  }
180 
181  // If the occurrences group does not exist, create it.
182  OccurrencesImpl *newgroup = new OccurrencesImpl( groupId, _coord_system );
183  newgroup->createOccurrence( id, lg, lt, error, abundance, num_attributes, attributes );
184  _presences.push_back( newgroup );
185 
186  return 1;
187 }
188 
189 /*******************/
190 /*** add Absence ***/
191 int
192 OccurrencesReader::_addAbsence( const char *id, const char *groupId, Coord lg, Coord lt,
193  Scalar error, int num_attributes, Scalar *attributes )
194 {
195  // If an absence group with the same name already exists,
196  // just add another occurrence to it.
197 
198  // We search backwards because new Occurrences are pushed
199  // onto the back, so most likely, the Occurrences we are looking
200  // for is at the back.
201  LstOccurrences::const_reverse_iterator ocs = _absences.rbegin();
202  LstOccurrences::const_reverse_iterator end = _absences.rend();
203  while ( ocs != end ) {
204 
205  OccurrencesPtr const & group = *ocs;
206 
207  if ( ! strcasecmp( group->label(), groupId ) ) {
208 
209  group->createOccurrence( id, lg, lt, error, 0.0, num_attributes, attributes );
210  return 0;
211  }
212 
213  ++ocs;
214  }
215 
216  // If the occurrences group does not exist, create it.
217  OccurrencesImpl *newgroup = new OccurrencesImpl( groupId, _coord_system );
218  newgroup->createOccurrence( id, lg, lt, error, 0.0, num_attributes, attributes );
219  _absences.push_back( newgroup );
220 
221  return 1;
222 }
223 
LstOccurrences _presences
void createOccurrence(const std::string &id, Coord longitude, Coord latitude, Scalar error, Scalar abundance, int num_attributes=0, Scalar *attributes=0, int num_env=0, Scalar *env=0)
double Scalar
Type of map values.
Definition: om_defs.hh:39
virtual OccurrencesPtr getAbsences(const char *groupId)
int _addOccurrence(const char *id, const char *groupId, Coord lg, Coord lt, Scalar error, Scalar abundance, int num_attributes, Scalar *attributes)
void printOccurrences(const std::string &msg="")
virtual OccurrencesPtr getPresences(const char *groupId)
int _addPresence(const char *id, const char *groupId, Coord lg, Coord lt, Scalar error, Scalar abundance, int num_attributes, Scalar *attributes)
LstOccurrences _absences
int _addAbsence(const char *id, const char *groupId, Coord lg, Coord lt, Scalar error, int num_attributes, Scalar *attributes)
double Coord
Type of map coordinates.
Definition: om_defs.hh:38
static char error[256]
Definition: FileParser.cpp:42