Propagation in Rural Scenarios

Detailed Description

This is an example of how to use the WinProp API for predictions in a rural scenario. The full example is distributed with the installation.
#include <stdio.h>
#include <string>
#include <iostream>

#include "rural_propagation.h"

#ifndef API_DATA_FOLDER
#define API_DATA_FOLDER "../../api/winprop/data/"
#endif // !API_DATA_FOLDER

int main(int argc, char** argv)
{
    int                 Error = 0;
    WinProp_ParaMain    GeneralParameters;
    WinProp_ParaRural   RuralParameters;
    WinProp_Antenna     Antenna;
    WinProp_Callback    Callback;
    WinProp_Result      Resultmatrix;

    /****************************************************************************/
    /* Initialization of parameters                                             */
    /****************************************************************************/

    WinProp_Structure_Init_ParameterMain(&GeneralParameters);
    WinProp_Structure_Init_ParameterRural(&RuralParameters);
    WinProp_Structure_Init_Antenna(&Antenna);
    WinProp_Structure_Init_Callback(&Callback);
    WinProp_Structure_Init_Result(&Resultmatrix);

    /****************************************************************************/
    /* Load clutter data                                                        */
    /****************************************************************************/

    Error = InterfaceLoadClutterASC(
        &GeneralParameters.Clutter,
        API_DATA_FOLDER "outdoor/RuralClutter.asc",
        API_DATA_FOLDER "outdoor/RuralClutter.table");
    
    /****************************************************************************/
    /* Load topo data                                                           */
    /****************************************************************************/

    Error = InterfaceLoadTopoASC(
        &GeneralParameters.Topography, API_DATA_FOLDER "outdoor/RuralTopo.asc");

    /****************************************************************************/
    /* Definition of scenario (topo database in WinProp format)                 */
    /****************************************************************************/

    /* Definition of general parameters. */
    GeneralParameters.ScenarioMode = SCENARIOMODE_RURAL;
    GeneralParameters.PredictionModelRural = PREDMODEL_RURAL_2RAY_DET;

    /* Definition of prediction area. */
    GeneralParameters.UrbanLowerLeftX = GeneralParameters.Topography.lowerLeftX;
    GeneralParameters.UrbanLowerLeftY = GeneralParameters.Topography.lowerLeftY;
    GeneralParameters.UrbanUpperRightX = GeneralParameters.Topography.upperRightX;
    GeneralParameters.UrbanUpperRightY = GeneralParameters.Topography.upperRightY;

    /* Copy coordinates to prediction area of second model (not yet used) */
    GeneralParameters.RuralLowerLeftX = GeneralParameters.UrbanLowerLeftX;
    GeneralParameters.RuralLowerLeftY = GeneralParameters.UrbanLowerLeftY;
    GeneralParameters.RuralUpperRightX = GeneralParameters.UrbanUpperRightX;
    GeneralParameters.RuralUpperRightY = GeneralParameters.UrbanUpperRightY;

    double PredictionHeight = 1.5;

    /* Size of matrix with results. */
    GeneralParameters.Resolution = 10.0;                       // Resolution in meter
    GeneralParameters.NrLayers = 1;                            // Number of prediction heights
    GeneralParameters.PredictionHeights = &PredictionHeight;   // Prediction height in meter

    /* No vector buildings but clutter and topo. */
    GeneralParameters.BuildingsMode = 0;
    GeneralParameters.TopographyMode = 1;
    GeneralParameters.ClutterMode = 1;
    GeneralParameters.ClutterConsideration = 0;

    /****************************************************************************/
    /* Definition of antenna.                                                   */
    /****************************************************************************/

    /* Position and configuration. */
    Antenna.Id = 1;
    Antenna.SiteId = 1;
    Antenna.Longitude_X = 321900.0;
    Antenna.Latitude_Y = 4158000.0;
    Antenna.Height = 40.0;
    Antenna.Power = 43.0; // Power in dBm
    Antenna.Frequency = 1800.0; // Frequency in MHz
    char AntennaName[500];
    sprintf(AntennaName, "%s", "Antenna 1");
    Antenna.Name = AntennaName;

    /****************************************************************************/
    /* Definition of callback functions (for progress messages)                 */
    /****************************************************************************/

    /* Two functions: Message output and progress bar update. */
    Callback.Percentage = CallbackProgress;
    Callback.Message = CallbackMessage;
    Callback.Error = CallbackError;

    /****************************************************************************/
    /* Compute prediction now                                                   */
    /****************************************************************************/

    /* Start computation within DLLs. */
    if (Error == 0)
        Error = OutdoorPlugIn_ComputePrediction(
            &Antenna, &GeneralParameters, NULL, 0, NULL, NULL, &RuralParameters, NULL, &Callback, &Resultmatrix, NULL, NULL, NULL);

    /****************************************************************************/
    /* Save prediction result to ASCII file.                                    */
    /****************************************************************************/

    if (Error == 0)
    {
        /* Write result matrix to ASCII file. */
        write_ascii(&Resultmatrix, API_DATA_FOLDER "output/Output Rural.txt");
    }

    /****************************************************************************/
    /* Free matrix for results and buildings                                    */
    /****************************************************************************/

    WinProp_FreeResult(&Resultmatrix);
    InterfaceClutterFree(&GeneralParameters.Clutter);
    InterfaceTopoFree(&GeneralParameters.Topography);

    return Error == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

int _STD_CALL CallbackMessage(const char * Text)
{
	if (Text == nullptr)
		return 0;

	std::cout << "\n" << Text;

	return(0);
}

int _STD_CALL CallbackError(const char * Text, int Error)
{
	if (Text == nullptr)
		return 0;

	std::cout << "\n";

#ifdef __LINUX
	std::cout << "\033[31m" << "Error (" << Error << "): "; // highlight error in red color
#else
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
	std::cout << "Error (" << Error << "): ";
#endif // __LINUX
	std::cout << Text;

#ifdef __LINUX
	std::cout << "\033[0m"; // highlight error in red color
#else
	SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
#endif // __LINUX

	return 0;
}

int _STD_CALL CallbackProgress(int value, const char* text)
{
	char Line[200];

	sprintf(Line, "\n%d%% %s", value, text);
	std::cout << Line;

	return(0);
}

void write_ascii(const WinProp_Result* Resultmatrix, const char* Filename) {
	FILE* OutputFile = fopen(Filename,"w");
	if (OutputFile)
	{
		/* Loop all pixels. */
		for (int x = 0; x < Resultmatrix->Columns; x++)
		{
			for (int y = 0; y < Resultmatrix->Lines; y++)
			{
				/* Compute real coordinates. */
				double Coordinate_X = Resultmatrix->LowerLeftX + ((double)x + 0.5) * Resultmatrix->Resolution;
				double Coordinate_Y = Resultmatrix->LowerLeftY + ((double)y + 0.5) * Resultmatrix->Resolution;

				/* Check if pixel was computed or not */
				if (Resultmatrix->Matrix[0][x][y] > -1000)
					fprintf(OutputFile, "%.2f\t%.2f\t%.2f\n", Coordinate_X, Coordinate_Y, Resultmatrix->Matrix[0][x][y]);
			}
		}

		/* Close file. */
		fclose(OutputFile);
	}
	else
		printf("\nCould not open the File: %s for writing.\n",Filename);
}