Pre-Lab Assignment 3 ENGO 333
Pre-Lab Assignment #3
Mapping using ortho-images – File streams
Marks: There are 4 marks available for this pre-lab assignment.
Note that this represents the absolute marks of this pre-lab assignment and is not the representative of its
total share in the final grade. The weight (final percentage) of each pre-lab assignment in the final grade
will be announced at the end of the term.
Due date: Submit the required files (see the end of this document) to Dropbox on D2L before 09:15 AM
on Tuesday, September 25, 2018.
Objective: To learn file I/O streams; to use file I/O streams and functions to perform basic aerial
mapping calculations; to organize programs in several header and cpp files.
Completion: Please read the lab hand out completely and carefully. This lab is to be completed
individually. Questions regarding this lab should be directed to the instructor or the TA either via Email
or in-person during their office hours. If you need to meet the instructor at other times, please ask her for
an appointment via Email.
1. Introduction
1.1. Introduction to ortho-images
An ortho-image1
(also called ortho-photo) is an aerial image which has the same characteristics as a map.
That is, it is an orthographic projection of the ground to the horizontal surface of an image (Figure 1). The
main characteristic of an ortho-image is that it has a uniform scale all over it. For example, if the scale of
an ortho-image is 1:100, then it means that every 1 millimeter on the image is equivalent to 100
millimeters (10 centimeters) on the ground. For example, if we measure the distance between two corners
of a building on the image and we observe that it is 3 mm, this means that the real distance between these
two corners is 30 cm.
1 Geomatics engineers use the techniques of photogrammetry and image-processing to produce ortho-images.
Pre-Lab Assignment 3 ENGO 333
2
Figure 1. Orthographic projection and creation of an ortho-photo
1.2. Measurements from ortho-photos
In this assignment, our objective is to measure the approximate area of some of the buildings in the orthophoto
of Figure 2.
Scale of the ortho-photo: The scale of this ortho-photo is 1:900. For example, if we measure the
distance between two points on the photo with a ruler and see that it is 4 mm, then the real distance
between those points (on the ground) is 3600 mm or 360 cm.
Measuring distance on the ortho-photo: I have identified four buildings (Buildings A, B, C and D) as in
Figure 3. Then, I measured the sides of the roofs (starting from the top-left corner of the roof and
continuing clock-wise) and listed them in Table 1. For instance, the value at column “d1” and row
“Building A” means that the distance between the top-left and the top-right corners of Building A is 9.7
mm. Similarly, value of d2 is the distance between the top-right and the bottom-right corners. Value of d3
is the distance between the bottom-right and the bottom-left corners. Value of d4 is the distance between
the bottom-left and the top-left corners.
Pre-Lab Assignment 3 ENGO 333
3
Note that although the roof outlines are supposed to be perfectly rectangular, due to my measurement
errors, the parallel sides are not exactly of the same length. For instance, at Building D, sides d2 and d4
have a difference of 0.1 mm.
Table 1. Roof measurements made from the ortho-photo
d1(mm) d2(mm) d3(mm) d4(mm)
Building A 9.6 11.6 9.8 11.5
Building B 9.5 11.6 9.7 11.7
Building C 9.8 11.6 9.8 11.6
Building D 6.4 8.6 6.6 8.7
Scaling the photo measurements: The measurement made on the ortho-photo can be transformed to
real-world measurement through scaling by ratio of 1:900. Each element of Table 1 should be
multiplied to 900 (as 1 mm of photo is equivalent to 900 mm on the ground) and divided by 1000 (as 1
meter is equal to 1000 mm) to yield real distances in meters (see Table 2).
Table 2. Roof measurements transformed to the ground
D1(m) D2(m) D3(m) D4(m)
Building A 8.64 10.44 8.82 10.35
Building B 8.55 10.44 8.73 10.53
Building C 8.82 10.44 8.82 10.44
Building D 5.76 7.74 5.94 7.83
Measuring building area: These measurements can be used to calculate the approximate area of each
roof. For instance, area of the roof at Building A is 90.75 m2
:
?????????? =
(??1 + ??3)
2
×
(??2 + ??4)
2
Pre-Lab Assignment 3 ENGO 333
4
Figure 2. Ortho-photo of an area in Ontario, Canada
Pre-Lab Assignment 3 ENGO 333
5
Figure 3. The subject buildings
1.3. Introduction to reading from and writing to files in C++ (fstream)
As you remember, input/output (I/O) is done with streams, which are nothing more than source of data
(for input) and a destination for data (for output). We saw that the most basic input source is the keyboard
and a basic output destination is the screen. Also, we learnt how to use the functions of library
<iostream> to represent the keyboard with cin and the screen with cout, and how to control these
streams to receive or send data.
The other source and destination of data can be files, e.g. text files. Steps for file I/O are as follows:
1- Include the <fstream> library; this is pretty much similar to the <iostream> library:
#include <fstream>
2- Make sure you are using namespace std:
using namespace std;
3- Create a stream either for input or output. In contrast with the keyboard and screen which are fixed
streams (always cin and cout), the file streams should be defined each time we want to use them. The
reason is that we don’t read from and write to the same exact file all the time!
To create an “input” file stream: ifstream file_in;
To create an “output” file stream: ofsteam file_out;
In the above examples, file_in and file_out are the identifiers (names) we gave to the streams.
4- Open the stream. Opening the stream means getting access to a file for either reading from or writing
to.
Pre-Lab Assignment 3 ENGO 333
6
To open an ifstream for reading a file named “filename.txt” from its beginning:
file_in.open (“filename.txt”, ifstream::in);
To open an ofstream for writing to a file named “filename.txt” from its beginning
(overwriting the existing content in the file):
file_out.open (“filename.txt”, ifstream::out);
To open an ofstream for writing to a file named “filename.txt” by appending to its
existing content:
file_out.open (“filename.txt”, ifstream::app);
Note that in the above examples, the assumption is that the file “filename.txt” is physically located in the
project folder. If this is not the case, then the full path to the file should be provided. For example, if the
file is located on Drive “C:” in a folder named “Test”, then the syntax for reading this file will be:
file_in.open (“C:/Test/filename.txt”, ifstream::in);
Also, note that we could combine steps 3 and 4 together. For instance, we could create an ifstream and
open it as follows:
ifstream file_in(“C:/Test/filename.txt”, ifstream::in);
4- Check for failure. Opening a file stream might not be successful. For instance, if the path of the file is
wrong, then the opening will face failure.
If opening file_in fails, the member function fail() returns true. For example, we can check the
failure with an if statement as follows:
if ( file_out.fail() )
{
cout << "Opening the file failed!" << endl;
}
5- Use the stream with insertion (<<) and extraction (>>) operators. You can read from the file or write to
it using these operators.
For example, let’s suppose Figure 4 shows the content of a file “C:/Test/filename.txt”.
100 200 300
400 20.5 10.1
1.1 3.4 200
Figure 4. The sample content of “C:/Test/filename.txt”
To read all the data in this file, until the end of file (eof), and show them on the screen:
double a,b,c;
while (!file_in.eof()){
file_in >> a >> b>> c;
Pre-Lab Assignment 3 ENGO 333
7
cout<<a <<b <<c << endl;
}
Note that the member function eof() returns true if the end of the file is reached by the input stream.
Note that when we use the extraction (>>) operator, the delimiter between the numbers should be blankspace,
tab or new-line.
As another example, let’s append the content of the file “C:/Test/filename.txt” with some strings.
ofstream file_out("C:/Test/filename.txt", ifstream::app);
string str1="My name is Mozhdeh!";
string str2="I instruct ENGO333";
file_out<<endl<<str1<<endl<<str2;
The above-code opens an output file stream and appends two lines to “filename.txt”. As a result, the file
of Figure 4 will look like Figure 5.
100 200 300
400 20.5 10.1
1.1 3.4 200
My name is Mozhdeh!
I instruct ENGO333
Figure 5. The content of “C:/Test/filename.txt” after appending two strings to it.
6- Once we don’t need the file stream any more, we should close the stream. If we leave the stream open,
other applications cannot access that file.
To close the input file stream file_in: file_in.close();
To close the output file stream file_out: file_out.close();
Summary:
The following source code shows how to create and open input and output file streams, how to read
formatted data from a text file, how to write formatted data to a text file by overwriting it, and how to
close the files.
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
int main()
{
/*creat and open an input file stream to read from a file called
"Afile.txt" which is located on Drive "C:" at a folder names "Test" */
ifstream file_in("C:/Test/Afile.txt", ifstream::in);
//If opening this file is failed, then end the program
if( file_in.fail() )
{
Pre-Lab Assignment 3 ENGO 333
8
cout << "Error opening file" << endl;
system("pause");
return 0;
}
/*Read the content of the file, line by line until the end of the file. We
supposed that each line has three numbers.*/
double a,b,c;
int counter=0;
while (!file_in.eof()){
counter++;
file_in >> a >>b>>c;
cout<<"content of line "<<counter<<": "<<a<<","<<b<<","<<c<<endl;
}
/*close the file so that we can access it again (either from this program
or any other application)*/
file_in.close();
/*creat and open an output file stream to overwrite the content of a file
"Afile.txt" which is located in the project folder*/
ofstream file_out("Afile.txt", ifstream::out);
//If opening this file is failed, then end the program
if( file_out.fail() )
{
cout << "Error opening file" << endl;
system("pause");
return 0;
}
//Declare and initialize two string variables
string str1="My name is Mozhdeh!";
string str2="I instruct ENGO ";
int cn=333;
/*Write the followings to the file: the value of "str1", a new-line
character, the value of "str2" */
file_out<<str1<<endl<<str2<<cn;
//close the file so that we can access it
file_out.close();
system("pause");
return 0;
};
Source Code 1. Sample code for file I/O
Pre-Lab Assignment 3 ENGO 333
9
2. Tasks
2.1. Learning
Create a test solution/project, and try the source code in Source Code 1 for reading from and writing to
files. You can create different text files and gain experience with file streams. To learn more about file
streams, see these tutorials (on ifstream and on ofstream).
2.2. Programming
? In Microsoft Visual Studio, create a new solution called PreLab3_Solution and a project
named PreLab3_project. You can select the starting directly (Location) of the solution
anywhere on your computer.
? Copy the source files PreLab3_Main.cpp, PreLab3_Functions.cpp,
PreLab3_Header.h to the project directory (folder PreLab3_Project). In VS, from the
solution explorer, add an existing source file, and add the PreLab3_Main.cpp and
PreLab3_Functions.cpp. Also, from the solution explorer, add an existing header file, and
add the file PreLab3_Header.h
? In the project directory (folder PreLab3_Project), create a Text Document
(PeLab3_InputData.txt) and store the data of Table 1 in it. You MUST only save the
numbers. Therefore, your file will have 4 lines (with 4 numbers in each line). You MUST put
blank-space between the numbers in each line.
Task 2.2.1: Write two functions, that receive 4 sides of a building roof, and one returns the area of the
roof and the other returns the perimeter of the roof. The functions MUST have the following prototypes.
Also, the definitions and declarations of these functions MUST be in the non-main source file
(PreLab3_Functions.cpp) and the header file (PreLab3_Header.h), respectively.
double Roof_Area_Calculator(double D1, double D2, double D3, double D4);
double Roof_Perim_Calculator(double D1, double D2, double D3, double D4);
// Parameters:
// D1: first-side length in m
// D2: second-side length in m
// D3: third-side length in m
// D4: forth-side length in m
// return: area of the roof in m2
// return: perimeter of the roof in m
Task 2.2.2: Write a function that receives the scale factor (for example in our case that the scale ratio is
1:900, the function should receive 900) and a distance in millimeters, and returns the scaled distance in
meters. The function MUST have the following prototype. Also, the definition and declaration of this
function MUST be in the non-main source file (PreLab3_Functions.cpp) and the header file
(PreLab3_Header.h).
double Scaling_Dist(unsigned int s, double d);
Pre-Lab Assignment 3 ENGO 333
10
//Parameters:
//s: scale factor
//d: distance in mm
//return: scaled distance in m
In simple words, the task of this function is transforming the values from Table 1 to the values in Table 2.
Task 2.2.3. In the main(), write the code to create an ifstream and open the data text file
(PeLab3_InputData.txt) for reading. You MUST check for failure and force the program to
terminate if the file could not be opened successfully.
Task 2.2.4. In the main(), write the code to create an ofstream and open a text file named
PeLab3_OutputData.txt in the project folder for writing the results. You MUST check for failure
and force the program to terminate if the file could not be opened successfully.
Task 2.2.5. In the main(), write a loop to read the data in the file PeLab3_InputData.txt line by
line until reaching the end of the file. In each iteration of the loop, the following operations MUST be
done:
? Read the lengths of the four sides of a building roof from the file.
? Call the function Scaling_Dist to convert the lengths from millimeters to meters.
? Call the function Roof_Calculator to calculate the area and perimeter of the roof.
? Write the roof calculations to the file PeLab3_OutputData.txt. The format of the file MUST
be as follows:
Building [number]: Area (m2)=[area], Perimeter(m)=[perim]
For instance, after reading the first line of the input file, the first line of the output file should look
like this:
Building 1: Area (m2)=91.1412, Perimeter(m)= 38.34
Task 2.2.6. In the main(), write the code to close both file streams.
2.3. Debugging and Testing
Once you complete the Tasks 2.2 successfully, the solution should be compiled and debugged.
2.4. Reporting
Use the Word template “ENGO333_SampleLabReport_ForCodeOnly” provided on D2L (Content →
Cheat sheets & resources → Lab-report Templates). Write a report for this assignment. In the report, you
should state the objective of the lab in your own words. Only focus on the programming objectives and
not the ones related to ortho-images.
Also, in the results section, you have to report what you did to complete each of the tasks 2.2.1 to 2.2.6.
For all these tasks, you MUST copy your source code to the report and explain your code briefly. You
Pre-Lab Assignment 3 ENGO 333
11
should also provide the results of the calculations (i.e. the final content of file
PeLab3_OutputData.txt). You may use visual aids to show the results as well.
What to Submit
Please submit the followings via D2L:
? The report file which MUST be in PDF format.
From the Word Application, go to File→ Save as Adobe PDF.
? The solution folder, which MUST contain the completed source files PreLab3_Main.cpp,
PreLab3_Functions.cpp, PreLab3_Header.h as well as the input data file
PeLab3_InputData.txt.
o The instructions for submitting the solution folder
1. Test your solution using Visual Studio 2017
2. Once the tests are finished, save the solution (from menu File → Save
all) and then clean the solution (from menu Build → Clean Solution). By
doing this, all the “*.exe” files will be removed from the directory of the
solution and subdirectory of the project. Then, close the MSVS software.
3. Go to the Location where the solution is stored. Delete any debug
folders within the solution or the project directories. Delete the hidden
folder “.vs” in the solution folder as well. Right-click on the solution
folder and select “send to → compressed (zipped) folder” (You can also
use 7Zip to create the zipped folder). This way, the whole solution folder
will be zipped as a folder named “PreLab3_Solution.zip”.
4. Upload the zipped folder “PreLab3_Solution.zip” to D2L folder of the
lab (PRELAB 3).
o Important notice: If you submit more than two items (anything in addition to
the required zipped folder and the report PDF file) to the D2L, your assignment
will not be marked.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。