ALMaSS  1.2 (after EcoStack, March 2024)
The Animal, Landscape and Man Simulation System
RasterMap Class Reference

#include <Rastermap.h>

Public Member Functions

int MapWidth (void)
 
int MapHeight (void)
 
int Get (int a_x, int a_y)
 
char * GetID (void)
 
int * GetMagicP (int a_x, int a_y)
 
void Put (int a_x, int a_y, int a_elem)
 
 RasterMap (const char *a_mapfile, Landscape *m_landscape)
 
 ~RasterMap (void)
 
int CellReplacementNeighbour (int a_x, int a_y, int a_polyref)
 A method for helping remove tiny polygons. More...
 
bool MissingCellReplace (int a_x, int a_y, bool a_fieldsonly)
 A method for removing missing polygons. More...
 
bool MissingCellReplaceWrap (int a_x, int a_y, bool a_fieldsonly)
 A method for removing missing polygons - tests for edge conditions. More...
 

Protected Member Functions

void Init (const char *a_mapfile, Landscape *m_landscape)
 

Private Attributes

char m_id [12]
 A 12 character ID for the map. More...
 
int * m_map
 The raster of the landscape. More...
 
int m_width
 The width of the landscape. More...
 
int m_height
 The height of the landscape. More...
 
Landscapem_landscape
 A pointer to the parent landscape class. More...
 

Constructor & Destructor Documentation

◆ RasterMap()

RasterMap::RasterMap ( const char *  a_mapfile,
Landscape m_landscape 
)
43 {
44  m_landscape = a_landscape;
45  FILE * IFile;
46 
47  IFile = fopen(a_mapfile, "rb");
48  if (!IFile) {
49  g_msg->Warn(WARN_FILE, "RasterMap::RasterMap(): Unable to open file",
50  a_mapfile);
51  exit(1);
52  }
53  fread(m_id, 1, 12, IFile);
54  fclose(IFile);
55  Init(a_mapfile, a_landscape);
56 }

References g_msg, MapErrorMsg::Warn(), and WARN_FILE.

◆ ~RasterMap()

RasterMap::~RasterMap ( void  )
148 {
149  delete[] m_map;
150 }

Member Function Documentation

◆ CellReplacementNeighbour()

int RasterMap::CellReplacementNeighbour ( int  a_x,
int  a_y,
int  a_polyref 
)

A method for helping remove tiny polygons.

Replaces a cell value with the most common value from the surrounding 8 cells. If near the edge nothing is done. Return value is the new value of the cell or -1 if nothing is done. First tests to make sure that the polygon we are removing is at these coords.

154 {
161  if ((a_x < 1) || (a_x > m_width - 2) || (a_y < 1) || (a_y > m_height - 2)) return -1;
162  int toreplace = Get(a_x, a_y);
163  if (toreplace != a_polyref)
164  {
165  g_msg->Warn("RasterMap::CellReplacementNeighbour: x,y pair does not match polyref ", a_polyref);
166  exit(0);
167  }
168  int surroundingcells[8];
169  surroundingcells[0] = Get(a_x - 1, a_y - 1);
170  surroundingcells[1] = Get(a_x , a_y - 1);
171  surroundingcells[2] = Get(a_x + 1, a_y - 1);
172  surroundingcells[3] = Get(a_x - 1, a_y);
173  surroundingcells[4] = Get(a_x + 1, a_y);
174  surroundingcells[5] = Get(a_x - 1, a_y + 1);
175  surroundingcells[6] = Get(a_x , a_y + 1);
176  surroundingcells[7] = Get(a_x + 1, a_y + 1);
177  // Find out how many duplicates we have
178  int count[8];
179  for (int i = 0; i < 8; i++)
180  {
181  count[i] = 0;
182  for (int j = 0; j < 8; j++)
183  {
184  if (surroundingcells[j] == surroundingcells[i]) count[i]++;
185  }
186  }
187  int found = -1;
188  int index = 0;
189  for (int i = 0; i < 8; i++)
190  {
191  if (count[i]>found) index = i;
192  found = count[i];
193  }
194  // Here we just make sure that we have not chosen another illegal. If so quietly ignore this replacement
195  if (m_landscape->SupplyPolygonArea(surroundingcells[index]) < 2) return -1;
196  // All OK so replace
197  Put(a_x, a_y, surroundingcells[index]);
198  return 1; // Signals that replacement occured
199 }

References count, g_msg, and MapErrorMsg::Warn().

◆ Get()

int RasterMap::Get ( int  a_x,
int  a_y 
)
inline
82  {
83 #ifndef DNDEBUG
84  if (a_x<0 || a_x>=m_width ||
85  a_y<0 || a_y>=m_height ) {
86  g_msg->Warn( WARN_BUG, "RasterMap::Get(): Coordinates out of range!", "" );
87  g_msg->WarnAddInfo(WARN_BUG, "X Coord ", a_x);
88  g_msg->WarnAddInfo(WARN_BUG, "Y Coord ", a_y);
89  g_msg->WarnAddInfo(WARN_BUG, "X max ", m_width);
90  g_msg->WarnAddInfo(WARN_BUG, "Y max ", m_height);
91  exit(1);
92  }
93 #endif
94  return m_map[ a_y * m_width + a_x ];
95 }

References g_msg, MapErrorMsg::Warn(), WARN_BUG, and MapErrorMsg::WarnAddInfo().

Referenced by Landscape::GetActualGooseGrazingForage(), Landscape::GetOwner_tole(), PesticideMap::Spray(), Landscape::SupplyAttIsForest(), Landscape::SupplyAttIsHigh(), Landscape::SupplyAttIsUrbanNoVeg(), Landscape::SupplyAttIsVegGrass(), Landscape::SupplyAttIsVegPatchy(), Landscape::SupplyAttIsWater(), Landscape::SupplyAttIsWoody(), Landscape::SupplyAttUserDefinedBool(), Landscape::SupplyBirdMaizeForage(), Landscape::SupplyBirdSeedForage(), Landscape::SupplyCentroidX(), Landscape::SupplyCentroidY(), Landscape::SupplyCountryDesig(), Landscape::SupplyCropType(), Landscape::SupplyDeadBiomass(), Landscape::SupplyElementSubType(), Landscape::SupplyElementType(), Landscape::SupplyElementTypeCC(), Landscape::SupplyFarmOwner(), Landscape::SupplyFarmOwnerIndex(), Landscape::SupplyFarmRotFilename(), Landscape::SupplyFarmType(), Landscape::SupplyGrazingPressure(), Landscape::SupplyGreenBiomass(), Landscape::SupplyGreenBiomassProp(), Landscape::SupplyGreenBiomassTotal(), Landscape::SupplyHasTramlines(), Landscape::SupplyInsects(), Landscape::SupplyInterestedGreenBiomass(), Landscape::SupplyInterestedGreenBiomassTotal(), Landscape::SupplyIsVeg(), Landscape::SupplyJustSprayed(), Landscape::SupplyLAGreen(), Landscape::SupplyLastSownVeg(), Landscape::SupplyLastTreatment(), Landscape::SupplyLATotal(), Landscape::SupplyNectar(), Landscape::SupplyOpenness(), Landscape::SupplyOwner_tole(), Landscape::SupplyPollen(), Landscape::SupplyPolyLEptr(), Landscape::SupplyPolyRef(), Landscape::SupplyPolyRefCC(), Landscape::SupplyPolyRefIndex(), Landscape::SupplyRoadWidth(), Landscape::SupplySoilType(), Landscape::SupplySoilTypeR(), Landscape::SupplySugar(), Landscape::SupplyTotalNectar(), Landscape::SupplyTotalPollen(), Landscape::SupplyTrafficLoad(), Landscape::SupplyUMRef(), Landscape::SupplyVegAge(), Landscape::SupplyVegBiomass(), Landscape::SupplyVegCover(), Landscape::SupplyVegDensity(), Landscape::SupplyVegDigestibility(), Landscape::SupplyVegGrowthStage(), Landscape::SupplyVegHeight(), Landscape::SupplyVegType(), Landscape::SupplyWeedBiomass(), and Pesticide::TwinMapSpray().

◆ GetID()

char* RasterMap::GetID ( void  )
inline
58 { return m_id; }

◆ GetMagicP()

int * RasterMap::GetMagicP ( int  a_x,
int  a_y 
)
inline
76 {
77  return &m_map[ a_y * m_width + a_x ];
78 }

Referenced by Landscape::hb_Add(), and Landscape::SupplyMagicMapP().

◆ Init()

void RasterMap::Init ( const char *  a_mapfile,
Landscape m_landscape 
)
protected
59 {
60  m_landscape = a_landscape;
61  char error_num[20];
62  int mx;
63  int my;
64 
65  ifstream IFile(a_mapfile, ios::binary);
66  if (!IFile.is_open()) {
67  g_msg->Warn(WARN_FILE, "RasterMap::RasterMap(): Unable to open file",
68  a_mapfile);
69  exit(1);
70  }
71 
72  IFile.read(m_id, 12); // Needs smth that is 12 characters
73 
74  IFile.read((char*)&m_width, sizeof(int));
75  IFile.read((char*)&m_height, sizeof(int));
76 
77  m_map = new int[m_width * m_height];
78  if (m_map == NULL) {
80  "RasterMap::RasterMap(): Out of memory.", "");
81  exit(1);
82  }
83 
84  IFile.read((char*)m_map, m_width * m_height * sizeof(int));
85 
86  mx = m_width;
87  my = m_height;
88 
89  int pref = -1;
90  int lastref = -1; // the first "last reference" is not good
91 
92  for (int x = 0; x < mx; x++)
93  {
94  for (int y = 0; y < my; y++)
95  {
96  pref = Get(x, y);
97 
98  if (pref < 0)
99  {
100  // This fills in holes caused by negative polyrefs, but if two consequetive holes are found, then it will exit
101  sprintf(error_num, "%d", pref);
102  g_msg->Warn(WARN_FILE, "RasterMap::RasterMap(): Unknown/Negative polygon map number (could be a hole):", error_num);
103  g_msg->WarnAddInfo(WARN_FILE, "X:", x);
104  g_msg->WarnAddInfo(WARN_FILE, "Y:", y);
105 
106  if (lastref >= 0)
107  {
108  sprintf(error_num, "%d", lastref);
109  g_msg->Warn(WARN_FILE, "Last polygon map number was good though, using it:", error_num);
110  Put(x, y, lastref);
111  }
112  else
113  {
114  g_msg->Warn(WARN_FILE, "Last polygon map number was bad also, exiting","");
115  exit(1);
116  }
117  }
118 
119  lastref = pref;
120 
121  if (m_landscape->SupplyPolymapping(Get(x, y)) == -1)
122  {
123  // Tripping out here means that the lsb file has a polygon that is not
124  // listed in the polygonrefs file
125  sprintf(error_num, "%d", Get(x, y));
126  g_msg->Warn(WARN_FILE, "RasterMap::RasterMap():" " Unknown polygon ref number:", error_num);
127  exit(1);
128  }
129 
130  if (m_landscape->SupplyPolymapping(Get(x, y)) == -1)
131  {
132  // Tripping out here means that the lsb file has a polygon that is not
133  // listed in the polygonrefs file
134  sprintf(error_num, "%d", Get(x, y));
135  g_msg->Warn(WARN_FILE, "RasterMap::RasterMap():" " Unknown polygon ref number:", error_num);
136  exit(1);
137  }
138 
139  }
140  }
141 
142  IFile.close();
143 
144 }

References g_msg, MapErrorMsg::Warn(), WARN_FATAL, WARN_FILE, and MapErrorMsg::WarnAddInfo().

◆ MapHeight()

int RasterMap::MapHeight ( void  )
inline

◆ MapWidth()

◆ MissingCellReplace()

bool RasterMap::MissingCellReplace ( int  a_x,
int  a_y,
bool  a_fieldsonly 
)

A method for removing missing polygons.

202 {
203  int surroundingcells[8];
204  surroundingcells[0] = Get(a_x - 1, a_y - 1);
205  surroundingcells[1] = Get(a_x, a_y - 1);
206  surroundingcells[2] = Get(a_x + 1, a_y - 1);
207  surroundingcells[3] = Get(a_x - 1, a_y);
208  surroundingcells[4] = Get(a_x + 1, a_y);
209  surroundingcells[5] = Get(a_x - 1, a_y + 1);
210  surroundingcells[6] = Get(a_x, a_y + 1);
211  surroundingcells[7] = Get(a_x + 1, a_y + 1);
212  // If we prefer fields then check if we have any
213  if (a_fieldsonly)
214  {
215  unsigned offset = unsigned(g_random_fnc(8) + 8); // [8,9,..,15)
216  for (unsigned i = 0; i < 8; i++)
217  {
218  if (m_landscape->IsFieldType(m_landscape->SupplyElementType(surroundingcells[(i + offset) % 8])))
219  {
220  // We have a next door field, so use this
221  Put(a_x, a_y, surroundingcells[(i + offset) % 8]);
222  return true;
223  }
224  }
225 
226  }
227  return false;
228 }

References g_random_fnc().

◆ MissingCellReplaceWrap()

bool RasterMap::MissingCellReplaceWrap ( int  a_x,
int  a_y,
bool  a_fieldsonly 
)

A method for removing missing polygons - tests for edge conditions.

231 {
232  int surroundingcells[8];
233  if (a_x >= 1 && a_y >= 1) surroundingcells[0] = Get(a_x - 1, a_y - 1); else surroundingcells[0] = Get(a_x, a_y);
234  if (a_y >= 1) surroundingcells[1] = Get(a_x, a_y - 1); else surroundingcells[1] = Get(a_x, a_y);
235  if (a_x < m_width-1 && a_y >= 1) surroundingcells[2] = Get(a_x + 1, a_y - 1); else surroundingcells[2] = Get(a_x, a_y);
236  if (a_x >= 1) surroundingcells[3] = Get(a_x - 1, a_y); else surroundingcells[3] = Get(a_x, a_y);
237  if (a_x < m_width-1) surroundingcells[4] = Get(a_x + 1, a_y); else surroundingcells[4] = Get(a_x, a_y);
238  if (a_x >= 1 && a_y < m_height-1) surroundingcells[5] = Get(a_x - 1, a_y + 1); else surroundingcells[5] = Get(a_x, a_y);
239  if (a_y < m_height-1) surroundingcells[6] = Get(a_x, a_y + 1); else surroundingcells[6] = Get(a_x, a_y);
240  if (a_x < m_width-1 && a_y < m_height-1) surroundingcells[7] = Get(a_x + 1, a_y + 1); else surroundingcells[7] = Get(a_x, a_y);
241  // If we prefer fields then check if we have any
242  if (a_fieldsonly)
243  {
244  unsigned offset = unsigned(g_random_fnc(8) + 8); // [8,9,..,15)
245  for (unsigned i = 0; i < 8; i++)
246  {
247  TTypesOfLandscapeElement tole = m_landscape->SupplyElementType(surroundingcells[(i + offset) % 8]);
248  if ((tole == tole_Field) || (tole == tole_Orchard) || (tole == tole_PermanentSetaside) || (tole == tole_PermPasture) || (tole == tole_PermPastureLowYield)
249  || (tole == tole_PermPastureTussocky) || (tole == tole_PermPastureTussockyWet) || (tole == tole_Vildtager))
250  {
251  // We have a next door field, so use this
252  Put(a_x, a_y, surroundingcells[(i + offset) % 8]);
253  return true;
254  }
255  }
256 
257  }
258 /* else
259  {
260  unsigned offset = unsigned(random(8) + 8); // [8,9,..,15)
261  for (unsigned i = 0; i < 8; i++)
262  {
263  TTypesOfLandscapeElement tole = m_landscape->SupplyElementType(surroundingcells[(i + offset) % 8]);
264  if ((tole != tole_Missing))
265  {
266  // We have a next door useful polygon, so use this
267  Put(a_x, a_y, surroundingcells[(i + offset) % 8]);
268  return true;
269  }
270  }
271  }
272 */
273  return false;
274 }

References g_random_fnc(), tole_Field, tole_Orchard, tole_PermanentSetaside, tole_PermPasture, tole_PermPastureLowYield, tole_PermPastureTussocky, tole_PermPastureTussockyWet, and tole_Vildtager.

◆ Put()

void RasterMap::Put ( int  a_x,
int  a_y,
int  a_elem 
)
inline
60 { m_map[ a_y * m_width + a_x ] = a_elem; }

Member Data Documentation

◆ m_height

int RasterMap::m_height
private

The height of the landscape.

◆ m_id

char RasterMap::m_id[12]
private

A 12 character ID for the map.

◆ m_landscape

Landscape* RasterMap::m_landscape
private

A pointer to the parent landscape class.

◆ m_map

int* RasterMap::m_map
private

The raster of the landscape.

◆ m_width

int RasterMap::m_width
private

The width of the landscape.


The documentation for this class was generated from the following files:
WARN_FILE
Definition: MapErrorMsg.h:37
Landscape::SupplyElementType
TTypesOfLandscapeElement SupplyElementType(int a_polyref)
Returns the landscape type of the polygon using the polygon reference number a_polyref or coordinates...
Definition: Landscape.h:1732
RasterMap::m_landscape
Landscape * m_landscape
A pointer to the parent landscape class.
Definition: Rastermap.h:51
MapErrorMsg::WarnAddInfo
void WarnAddInfo(MapErrorState a_level, std::string a_add1, std::string a_add2)
Definition: MapErrorMsg.cpp:160
RasterMap::Get
int Get(int a_x, int a_y)
Definition: Rastermap.h:82
tole_PermPastureTussockyWet
Definition: LandscapeFarmingEnums.h:122
WARN_FATAL
Definition: MapErrorMsg.h:35
RasterMap::Put
void Put(int a_x, int a_y, int a_elem)
Definition: Rastermap.h:60
tole_PermanentSetaside
Definition: LandscapeFarmingEnums.h:68
TTypesOfLandscapeElement
TTypesOfLandscapeElement
Values that represent the types of landscape polygon that are represented in ALMaSS.
Definition: LandscapeFarmingEnums.h:57
RasterMap::m_id
char m_id[12]
A 12 character ID for the map.
Definition: Rastermap.h:43
RasterMap::m_height
int m_height
The height of the landscape.
Definition: Rastermap.h:49
Landscape::IsFieldType
bool IsFieldType(TTypesOfLandscapeElement a_tole)
Function for backwards code compatibility when TOV attributes would now be used.
Definition: Landscape.h:998
tole_PermPastureLowYield
Definition: LandscapeFarmingEnums.h:66
RasterMap::m_map
int * m_map
The raster of the landscape.
Definition: Rastermap.h:45
MapErrorMsg::Warn
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: MapErrorMsg.cpp:69
Landscape::SupplyPolygonArea
double SupplyPolygonArea(int a_polyref)
Returns the polygon area of a particular polygon referenced by polygon.
Definition: Landscape.h:1058
tole_PermPastureTussocky
Definition: LandscapeFarmingEnums.h:67
RasterMap::m_width
int m_width
The width of the landscape.
Definition: Rastermap.h:47
RasterMap::Init
void Init(const char *a_mapfile, Landscape *m_landscape)
Definition: Rastermap.cpp:58
Landscape::SupplyPolymapping
int SupplyPolymapping(int a_index)
Returns the value of the m_polymapping array indexed by a_index.
Definition: Landscape.h:329
g_msg
MapErrorMsg * g_msg
Definition: MapErrorMsg.cpp:41
tole_Orchard
Definition: LandscapeFarmingEnums.h:93
tole_Field
Definition: LandscapeFarmingEnums.h:65
tole_Vildtager
Definition: LandscapeFarmingEnums.h:121
g_random_fnc
int g_random_fnc(const int a_range)
Definition: ALMaSS_Random.cpp:74
count
Definition: SubPopulation.h:48
WARN_BUG
Definition: MapErrorMsg.h:34
tole_PermPasture
Definition: LandscapeFarmingEnums.h:69