using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing;
using System.IO;
namespace GpsTracer
{
class Mapper
{
private List<Point> m_points;
private Point m_center;
int m_drawded = 1;
private Graphics m_g;
private Rectangle m_clip;
static private Pen m_bgPen = new Pen(Color.LightBlue);
static private Pen m_gridPen = new Pen(Color.LightGray);
static private Brush m_bgBrush = new SolidBrush(m_bgPen.Color);
static private Pen m_linePen = new Pen(Color.MediumBlue);
static private Pen m_pointPen = new Pen(Color.Blue);
static private Pen m_lastPointPen = new Pen(Color.Red);
static private Font m_font = new Font("Tahoma", 8, FontStyle.Regular);
static private Brush m_fontBrush = new SolidBrush(Color.Blue);
public Mapper(Graphics g, int x, int y, int width, int height)
{
m_points = new List<Point>();
m_center = new Point(x + width / 2, y + height / 2);
m_g = g;
m_clip = new Rectangle(x, y, width, height);
m_g.Clip = new Region(m_clip);
clearAndDraw();
}
public void parseAndDraw(string s)
{
string[] Words = s.Split(',');
m_g.FillRectangle(m_bgBrush, new Rectangle(m_clip.X, m_clip.Y + 5, m_clip.Width, 15));
m_g.DrawString(s, m_font,m_fontBrush, new RectangleF(m_clip.X , m_clip.Y + 5, m_clip.Width, 15));
switch (Words[0])
{
case "$GPRMC":// $GPRMC,170111,A,4338.5810,N,07015.1010,W,000.0,360.0,060199,017.5,W*73
// RMC - Recommended minimum specific GPS/Transit data
if (Words[3].Length > 0 && Words[5].Length > 0)
{
drawLatLong(Words[3], Words[5]);
}
break;
case "$GPGSV":// $GPGSV,2,1,08,03,17,171,42,06,21,047,44,14,28,251,45,16,25,292,44*71
// GSV - Satellites in view
break;
case "$GPGSA":// $GPGGA,170111,4338.581,N,07015.101,W,1,00,2.0,1.1,M,-31.8,M,,*71
//GSA - GPS dilution of precision and active satellites
break;
default:
break;
}
}
public void drawLatLong(string latitude, string longitude)
{
Point aPoint = new Point();
aPoint.X = (Convert.ToInt32(latitude.Substring(latitude.Length - 4, 4)));
aPoint.Y = (Convert.ToInt32(longitude.Substring(longitude.Length - 4, 4)));
aPoint.X += (Convert.ToInt32(latitude.Substring(latitude.Length - 7, 2))) * 60;
aPoint.Y += (Convert.ToInt32(longitude.Substring(longitude.Length - 7, 2))) * 60;
aPoint.X += (Convert.ToInt32(latitude.Substring(latitude.Length - 9, 2))) * 3600;
aPoint.Y += (Convert.ToInt32(longitude.Substring(longitude.Length - 9, 2))) * 3600;
m_points.Add(aPoint);
draw();
}
public void draw()
{
float xTo = 0;
float xFrom = 0;
float yTo = 0;
float yFrom = 0;
for (int i = m_drawded; i < m_points.Count; i++)
{
xTo = (m_points[i].X - m_points[0].X) / m_scale + m_center.X;
xFrom = (m_points[i - 1].X - m_points[0].X) / m_scale + m_center.X;
yTo = (m_points[i].Y - m_points[0].Y) / m_scale + m_center.Y;
yFrom = (m_points[i - 1].Y - m_points[0].Y) / m_scale + m_center.Y;
m_g.DrawLine(m_linePen, (int)xTo, (int)yTo, (int)xFrom, (int)yFrom);
m_g.DrawEllipse(m_pointPen, new Rectangle((int)xFrom - 2, (int)yFrom - 2, 4, 4));
}
m_g.DrawEllipse(m_lastPointPen, new Rectangle((int)xTo - 2, (int)yTo - 2, 4, 4));
m_drawded++;
}
private float m_scale = 10;
public float Scale
{
get { return m_scale; }
set { m_scale = value; }
}
public void clearAndDraw()
{
m_g.FillRectangle(m_bgBrush, m_clip);
m_drawded = 1;
draw();
}
public void moveCenter(int x, int y)
{
m_center.X -= x;
m_center.Y -= y;
}
public void centerInTheMiddle()
{
int n =m_points.Count;
m_center.X = (int)((m_points[n - 1].X - m_points[0].X) / m_scale);
m_center.Y = (int)((m_points[n - 1].Y -m_points[0].Y) / m_scale);
clearAndDraw();
}
public void loatPath(String filename)
{
StreamReader sr = new StreamReader(filename);
m_points.Clear();
int n = 0;
Point p = new Point();
while (!sr.EndOfStream)
{
String readed = sr.ReadLine();
p.X = Convert.ToInt32(readed);
readed = sr.ReadLine();
p.Y = Convert.ToInt32(readed);
m_points.Add(p);
n++;
}
m_drawded = 1;
sr.Close();
clearAndDraw();
}
public void savePath(String filename)
{
StreamWriter sw = new StreamWriter(filename);
foreach (Point p in m_points)
{
sw.WriteLine(Convert.ToString(p.X));
sw.WriteLine(Convert.ToString(p.Y));
}
sw.Flush();
sw.Close();
}
}
}