[try Beta version]
Not logged in

 
working with matrix and header

Jan 19, 2011 at 4:13pm
Hi guys!

I'm a c++ beginner working on ArcGis derived .asc data, with the follow form:

ncols 572
nrows 375
xllcorner 614271.24923309
yllcorner 108824.59085717
cellsize 2
NODATA_value -9999
-9999 -9999 -9999 123.34 121.23 ...
...

I need to make calculation with that kind of matrix, reporting the header in a new file, and considering ncols and nrows automatically as maxi and maxj, and using celssize and NODATA_value as constant. Than make calculation on the matrix (example calue ij * cellsize) and copy the result under the header of the new file. I can use the matrix alone, but I havent idea on how working with the header (note the problem of the variable number of tabs...).
Someone can help me?

Thank's!

Gabriele
Jan 19, 2011 at 9:54pm
Search this site for "xllcorner" (it is suitably unique to ArcGIS) and you will find examples of code for working ArcGIS data.
Jan 20, 2011 at 8:25am
Thank's for your suggest and sorry for the disturb! I find some interesting solution and I'm triing!
Jan 20, 2011 at 6:00pm
I just try to follow indication, but I have already some problem...
I can work on the header of the matrix file, but I have many problems with the matrix. I try to do 2 codes using indication find in the forum. I need to copy the header of the matrix in the result .txt file, and after that read each i-j double varaible, and if it isn't noData add to it 10 (it is a try), and than copy on the right place in the same result.txt file, under the header.
First one will be the quicly one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <deque>
#include <vector>

using namespace std;

typedef vector <float>     container;
typedef deque  <container> container2;
int ncols, nrows, nodataValue;
double xllcorner, yllcorner, size;
typedef double matrice [1437][1681];
void scrivi ()
{
	string s;
	cout << "dati georiferiti" << "\n";
	ifstream ifs ("D:\\projects\\tesaf\\programmazione\\prova.txt");
	ofstream ofs ("D:\\projects\\tesaf\\programmazione\\demout.txt", ios_base::trunc);
	for (int i=0; i<6; i++)
	{
		getline (ifs, s);
		ofs << s << endl;
	}
	ifs.close ();
	ofs.close ();
}
void ricon()
{
	string s1;
	ifstream ifs1 ("D:\\projects\\tesaf\\programmazione\\prova.txt");
	ifs1 >> s1 >> ncols;
	ifs1 >> s1 >> nrows;
	ifs1 >> s1 >> xllcorner;
	ifs1 >> s1 >> yllcorner;
	ifs1 >> s1 >> size;
	ifs1 >> s1 >> nodataValue;
	cout << "ncols" << ncols << "\n";
	cout << "nrows" << nrows << "\n";
	cout << "xllcorner" << xllcorner << "\n";
	cout << "yllcorner" << yllcorner << "\n";
	cout << "size" << size << "\n";
	cout << "nodataValue" << nodataValue << "\n";
	ifs1.close ();
	cout << "fine elaborazione";
	cout << endl << endl;
}

void load_at_once( const char* filename, string& result )
  {
  ifstream f( "D:\\projects\\tesaf\\programmazione\\prova.txt", ios::binary );
  result.assign( istream_iterator <char> ( f ), istream_iterator <char> () );
  }

struct data_t
  {
  unsigned nrows;
  unsigned ncols;
  double   xllcorner;
  double   yllcorner;
  int      cellsize;
  double   nodatavalue;

  container2 data;
  };

void load_in_pieces( istream& f, data_t& data )
  {
ofstream ofs2 ("D:\\projects\\tesaf\\programmazione\\demout.txt", ios_base::trunc);
double r;
string s;
  f >> s >> data.ncols
    >> s >> data.nrows
    >> s >> data.xllcorner
    >> s >> data.yllcorner
    >> s >> data.cellsize
    >> s >> data.nodatavalue;
  data.data.resize( data.nrows );
  for (container2::iterator row = data.data.begin(); row != data.data.end(); ++row)
    {
    row->resize( data.ncols );
    for (container::iterator col = row->begin(); col != row->end(); ++col)
      {
      f >> *col;
	  if (*col == data.nodatavalue)
	  {
		  ofs2 << *col;
	  }
	  else
	  {
	r= *col+10;
	ofs2 << *col;
	  }
      }
	ofs2.close();
    }
  }

int main ()
{
	string raw_data;
	data_t data;
	cout << "\aparti";
	scrivi();
	ricon();
	load_at_once ("D:\\projects\\tesaf\\programmazione\\prova.txt", raw_data);
	istringstream iss (raw_data);
	load_in_pieces (iss, data);
	cout << "fine elaborazione";
	cout << endl << endl;
	system ("pause");
	return 0;
}

the second should be more easy to realize, but a little slower:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <vector>
#include <deque>
using namespace std;
int ncols, nrows, nodataValue;
double xllcorner, yllcorner, size;
string line;
void scrivi ()
{
	string s;
	cout << "dati georiferiti" << "\n";
	ifstream ifs ("D:\\projects\\tesaf\\programmazione\\prova.txt");
	ofstream ofs ("D:\\projects\\tesaf\\programmazione\\demout.txt", ios_base::trunc);
	for (int i=0; i<6; i++)
	{
		getline (ifs, s);
		ofs << s << endl;
	}
	ifs.close ();
	ofs.close ();
}
void ricon()
{
	string s1;
	ifstream ifs1 ("D:\\projects\\tesaf\\programmazione\\prova.txt");
	ifs1 >> s1 >> ncols;
	ifs1 >> s1 >> nrows;
	ifs1 >> s1 >> xllcorner;
	ifs1 >> s1 >> yllcorner;
	ifs1 >> s1 >> size;
	ifs1 >> s1 >> nodataValue;
	cout << "ncols" << ncols << "\n";
	cout << "nrows" << nrows << "\n";
	cout << "xllcorner" << xllcorner << "\n";
	cout << "yllcorner" << yllcorner << "\n";
	cout << "size" << size << "\n";
	cout << "nodataValue" << nodataValue << "\n";
	ifs1.close ();
	cout << "fine elaborazione";
	cout << endl << endl;
}
void elaborazione ()
{
	vector<vector<double>> data;
	ifstream ifs2 ("D:\\projects\\tesaf\\programmazione\\prova.txt");
	ofstream ofs2 ("D:\\projects\\tesaf\\programmazione\\demout.txt", ios_base::ate);
if (ncols * nrows > 0)
		{		
			// Set up sizes. (rows x cols)
			data.resize(nrows);
			for (unsigned row = 0; row < nrows; ++row)
			{
				data[row].resize(ncols);
			}

			// Load values in	
			double quota;
			unsigned row = 0;
			while (row < nrows)
			{							
				getline(ifs2, line);
				istringstream ss (line);
				for (unsigned col =0; col < ncols; col++)
				{
					ss >> data[row][col];
					ss>>quota;
				}
				if (quota == nodataValue)
			{
			  ofs2 << quota;
			}
		else
		{
		quota = quota+10;
		ofs2 << quota;
		 }
				row ++;
			}
			ifs2.close();	
			}
		ifs2.close ();
		ofs2.close ();
}
int main ()
{
	cout << "\aparti";
	scrivi();
	ricon();
	elaborazione ();
	cout << "fine elaborazione";
	cout << endl << endl;
	system ("pause");
	return 0;

}



The problem is that noone work... someone can help me?
Topic archived. No new replies allowed.