Example #1
0
        private void OnCalibrationClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Calibration Files|*.calib"
                                    + "|All files|*.*";
            openFileDialog.FilterIndex      = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.CheckFileExists  = true;
            openFileDialog.CheckPathExists  = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string calibFileName = openFileDialog.FileName;
                try {
                    MapCalibration = new MapCalibration();
                    MapCalibration.read(calibFileName);
                    redrawLines();
                    Properties.Settings.Default.LastCalibFile = calibFileName;
                    Properties.Settings.Default.Save();
                } catch (Exception ex) {
                    Utils.excMsg("Error opening file:" + NL + calibFileName, ex);
                    return;
                }
            }
        }
Example #2
0
        private void OnFormShown(object sender, EventArgs e)
        {
            // This needs to be none in Shown not Load as the sizes aren't
            // right then.
            string imageFileName = Properties.Settings.Default.LastImageFile;
            string calibFileName = Properties.Settings.Default.LastCalibFile;

            try {
                // Load last image
                if (File.Exists(imageFileName))
                {
                    resetImage(imageFileName, true);
                }
            } catch (Exception ex) {
                Utils.excMsg("Error opening last image file:" + NL
                             + imageFileName, ex);
                return;
            }
            try {
                // Load last calibration
                if (File.Exists(calibFileName))
                {
                    MapCalibration = new MapCalibration();
                    MapCalibration.read(calibFileName);
                }
            } catch (Exception ex) {
                Utils.excMsg("Error opening last calib file:" + NL
                             + calibFileName, ex);
                return;
            }
        }
Example #3
0
        private void OnOpenImageClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Image Files|*.png;*.bmp;*.jpg;*.jpeg;*.jpe;*.jfif;*.tif;*.tiff;*.gif"
                                    + "|JPEG|*.jpg;*.jpeg;*.jpe"
                                    + "|PNG|*.png"
                                    + "|All files|*.*";
            openFileDialog.FilterIndex      = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.CheckFileExists  = true;
            openFileDialog.CheckPathExists  = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                try {
                    resetImage(fileName, true);
                    Properties.Settings.Default.LastImageFile = fileName;
                    Properties.Settings.Default.Save();
                } catch (Exception ex) {
                    Utils.excMsg("Error opening file:" + NL + fileName, ex);
                    return;
                }
                // Check if there is a calibration file with the same name
                string calibFileName = Path.ChangeExtension(fileName, ".calib");
                if (File.Exists(calibFileName))
                {
                    DialogResult res = Utils.confirmMsg(
                        "There is a calibration file with the same name." + NL
                        + "Would you like to open it also?");
                    if (res == DialogResult.Yes)
                    {
                        try {
                            MapCalibration = new MapCalibration();
                            MapCalibration.read(calibFileName);
                            redrawLines();
                            Properties.Settings.Default.LastCalibFile = calibFileName;
                            Properties.Settings.Default.Save();
                        } catch (Exception ex) {
                            Utils.excMsg("Error opening calib file:" + NL
                                         + calibFileName, ex);
                            return;
                        }
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Reads lines from a GPX file and adds them to the current lines.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="mapCalibration"></param>
        /// <returns>The number of trackpoints read.</returns>
        public int readGpxLines(string fileName, MapCalibration mapCalibration)
        {
            XDocument document = XDocument.Load(fileName);
            XElement  root = document.Root;
            int       nTrkPoints = 0, nTrkSeg = 0;
            double    lat, lon;
            string    strVal;
            Point     point;
            Line      line = null;

            try {
                foreach (XElement elemSeg in from item in root.Descendants().
                         Where(elemSeg => elemSeg.Name.LocalName == "trkseg")
                         select item)
                {
                    if (elemSeg.Name.LocalName == "trkseg")
                    {
                        nTrkSeg++;
                        line      = new Line();
                        line.Desc = "Seg " + nTrkSeg + " from " + fileName;
                        foreach (XElement elemTkpt in from item in elemSeg.Descendants().
                                 Where(elemSeg => elemSeg.Name.LocalName == "trkpt")
                                 select item)
                        {
                            nTrkPoints++;
                            strVal = elemTkpt.Attribute("lat").Value;
                            lat    = Double.Parse(strVal);
                            strVal = elemTkpt.Attribute("lon").Value;
                            lon    = Double.Parse(strVal);
                            point  = mapCalibration.inverse(lon, lat);
                            line.addPoint(point);
                        }
                    }
                    if (line != null && line.NPoints > 0)
                    {
                        addLine(line);
                    }
                }
            } catch (Exception ex) {
                Utils.excMsg("Error reading GPX lines", ex);
            }
            return(nTrkPoints);
        }
Example #5
0
        public void writeGpxFile(String fileName, MapCalibration mapCalibration)
        {
            if (LinesList == null)
            {
                return;
            }
            // Prompt for start time and avg speed
            double      speed = DEFAULT_SPEED;
            InputDialog dlg   = new InputDialog("Write GPX",
                                                "Enter the average speed to use:", $"{DEFAULT_SPEED:N1}");
            DialogResult res = dlg.ShowDialog();

            if (res == DialogResult.OK)
            {
                string val = dlg.Value;
                try {
                    speed = Convert.ToDouble(val);
                } catch (Exception ex) {
                    Utils.excMsg("Failed to get a valid speed, using "
                                 + DEFAULT_SPEED + " mph", ex);
                }
            }
            DateTime startTime = DateTime.Now;

            dlg = new InputDialog("Write GPX",
                                  "Enter the local start time:", startTime.ToString());
            res = dlg.ShowDialog();
            if (res == DialogResult.OK)
            {
                string val = dlg.Value;
                try {
                    startTime = Convert.ToDateTime(val);
                } catch (Exception ex) {
                    Utils.excMsg("Failed to get a valid starttime, " +
                                 "using the current time", ex);
                }
            }
            // Convert to UTC for writing the GPX file
            startTime = startTime.ToUniversalTime();
            DateTime newTime = startTime;
            Point    point;

            try {
                using (StreamWriter outputFile = File.CreateText(fileName)) {
                    // Write header
                    outputFile.WriteLine(
                        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>");
                    outputFile.WriteLine("<gpx");
                    outputFile.WriteLine(" creator=\"MapLines\"");
                    outputFile.WriteLine(" version=\"1.1\"");
                    outputFile.WriteLine(" xmlns=\"http://www.topografix.com/GPX/1/1\"");
                    outputFile.WriteLine(
                        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
                    outputFile.WriteLine(
                        " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 ");
                    outputFile.WriteLine("   http://www.topografix.com/GPX/1/1/gpx.xsd\">");

                    // Write metadata
                    outputFile.WriteLine("  <metadata>");
                    outputFile.WriteLine("    <time>" + startTime.ToString(UTC_FORMAT) + "</time>");
                    outputFile.WriteLine("  </metadata>");

                    // Write lines
                    outputFile.WriteLine("  <trk>");
                    outputFile.WriteLine("    <extensions>");
                    outputFile.WriteLine(
                        "      <gpxx:TrackExtension xmlns:gpxx=" +
                        "\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\">");
                    outputFile.WriteLine(
                        "        <gpxx:DisplayColor>Blue</gpxx:DisplayColor>");
                    outputFile.WriteLine("      </gpxx:TrackExtension>");
                    outputFile.WriteLine("    </extensions>");
                    double lon0 = 0, lat0 = 0;
                    double dist    = 0;
                    double distTot = 0;
                    long   time    = 0;
                    foreach (Line line in LinesList)
                    {
                        outputFile.WriteLine("    <trkseg>");
                        outputFile.WriteLine("      <name>" + line.Desc + "</name>");
                        for (int j = 0; j < line.NPoints; j++)
                        {
                            point = line.Points[j];
                            double[] vals = mapCalibration.transform(point.X,
                                                                     point.Y);
                            outputFile.WriteLine($"      <trkpt lat=\"{vals[1]:N6}\"" +
                                                 $" lon=\"{vals[0]:N6}\">");
                            outputFile.WriteLine("        <ele>0.0</ele>");
                            // Increment the time assuming a default speed
                            if (j == 0)
                            {
                                lon0 = vals[0];
                                lat0 = vals[1];
                            }
                            else
                            {
                                dist = Math.Abs(Gps.M2MI *
                                                Gps.greatCircleDistance(lat0, lon0, vals[1], vals[0]));
                                time    = (long)(dist / speed * 3600.0 * 1000.0);
                                newTime = newTime.AddMilliseconds(time);
                                lon0    = vals[0];
                                lat0    = vals[1];
                            }
                            distTot += dist;
                            outputFile.WriteLine("        <time>"
                                                 + newTime.ToString(UTC_FORMAT) + "</time>");
                            outputFile.WriteLine("      </trkpt>");
                        }
                        outputFile.WriteLine("    </trkseg>");
                    }
                    outputFile.WriteLine("  </trk>");
                    outputFile.WriteLine("</gpx>");
                }
            } catch (Exception ex) {
                Utils.excMsg("Error writing" + fileName, ex);
                return;
            }
        }