Пример #1
0
        /**
         * Create the wptColors array when the mode is KmlColorMode.COLORSET.
         *
         * @param kmlOptions
         */
        private static void createWptColorSetColors(KmlOptions kmlOptions)
        {
            // Use the hard-coded colorset colors with alpha prepended
            int nColors = colorSetColors.Length;

            wptColors = new String[nColors];
            String alpha = kmlOptions.WptAlpha;

            // Insure alpha has two characters
            if (alpha.Length == 0)
            {
                alpha = "00";
            }
            else if (alpha.Length == 1)
            {
                alpha = "0" + alpha;
            }
            else if (alpha.Length > 2)
            {
                alpha = alpha.Substring(0, 2);
            }
            for (int i = 0; i < nColors; i++)
            {
                wptColors[i] = alpha + colorSetColors[i];
            }
        }
Пример #2
0
        /**
         * Create the wptColors array when the mode is KmlColorMode.RAINBOW.
         *
         * @param kmlOptions
         */
        private static void createWptRainbowColors(KmlOptions kmlOptions,
                                                   GpxFileSetModel fileSetModel)
        {
            // Make a color for each waypoint
            int nColors = 0;
            List <GpxFileModel> fileModels = fileSetModel.Files;

            foreach (GpxFileModel fileModel in fileModels)
            {
                if (!fileModel.Checked)
                {
                    continue;
                }
                List <GpxWaypointModel> waypointModels = fileModel.Waypoints;
                foreach (GpxWaypointModel waypointModel in waypointModels)
                {
                    if (!waypointModel.Checked)
                    {
                        continue;
                    }
                    nColors++;
                }
            }
            wptColors = new String[nColors];

            // Calculate the wptColors
            Color  color;
            int    red, green, blue;
            String alpha = kmlOptions.WptAlpha;

            // Insure alpha has two characters
            if (alpha.Length == 0)
            {
                alpha = "00";
            }
            else if (alpha.Length == 1)
            {
                alpha = "0" + alpha;
            }
            else if (alpha.Length > 2)
            {
                alpha = alpha.Substring(0, 2);
            }
            for (int i = 0; i < nColors; i++)
            {
                color        = RainbowColorScheme.defineColor(i, nColors);
                red          = color.R;
                green        = color.G;
                blue         = color.B;
                wptColors[i] = String.Format("{0}{1:X2}{2:X2}{3:X2}", alpha, blue, green,
                                             red);
                // DEBUG
                // System.out.println(wptColors[i]);
            }
            // DEBUG
            // System.out.println("nColors=" + nColors);
        }
Пример #3
0
        /**
         * Create the trkColors array when the mode is KmlColorMode.RAINBOW.
         *
         * @param kmlOptions
         */
        private static void createTrkRainbowColors(KmlOptions kmlOptions,
                                                   GpxFileSetModel fileSetModel)
        {
            // Make a color for each track
            int nColors = 0;
            List <GpxFileModel> fileModels = fileSetModel.Files;

            foreach (GpxFileModel fileModel in fileModels)
            {
                if (!fileModel.Checked)
                {
                    continue;
                }
                List <GpxTrackModel> trackModels = fileModel.Tracks;
                foreach (GpxTrackModel trackModel in trackModels)
                {
                    if (!trackModel.Checked)
                    {
                        continue;
                    }
                    nColors++;
                }
            }
            trkColors = new String[nColors];

            // Calculate the trkColors
            Color  color;
            int    red, green, blue;
            String alpha = kmlOptions.TrkAlpha;

            // Insure alpha has two characters
            if (alpha.Length == 0)
            {
                alpha = "00";
            }
            else if (alpha.Length == 1)
            {
                alpha = "0" + alpha;
            }
            else if (alpha.Length > 2)
            {
                alpha = alpha.Substring(0, 2);
            }
            for (int i = 0; i < nColors; i++)
            {
                color        = RainbowColorScheme.defineColor(i, nColors);
                red          = color.R;
                green        = color.G;
                blue         = color.B;
                trkColors[i] = String.Format("{0}{1:X2}{2:X2}{3:X2}", alpha, blue, green,
                                             red);
            }
        }
Пример #4
0
 /**
  * Create the wptColors array when the mode is KmlColorMode.COLOR.
  *
  * @param kmlOptions
  */
 private static void createWptColorColors(KmlOptions kmlOptions)
 {
     // Make a single element with the options waypoint color
     wptColors    = new String[1];
     wptColors[0] = kmlOptions.WptAlpha + kmlOptions.WptColor;
 }
Пример #5
0
 /**
  * Create the rteColors array when the mode is KmlColorMode.COLOR.
  *
  * @param kmlOptions
  */
 private static void createRteColorColors(KmlOptions kmlOptions)
 {
     // Make a single element with the options route color
     rteColors    = new String[1];
     rteColors[0] = kmlOptions.RteAlpha + kmlOptions.RteColor;
 }
Пример #6
0
 /**
  * Create the trkColors array when the mode is KmlColorMode.COLOR.
  *
  * @param kmlOptions
  */
 private static void createTrkColorColors(KmlOptions kmlOptions)
 {
     // Make a single element with the options track color
     trkColors    = new String[1];
     trkColors[0] = kmlOptions.TrkAlpha + kmlOptions.TrkColor;
 }
Пример #7
0
        /**
         * @param args
         */
        public static void createKml(GpxFileSetModel fileSetModel,
                                     KmlOptions kmlOptions)
        {
            // Generate the KML
            Kml kml = new Kml();

            // Create the Document for this file
            Document document = new Document()
            {
                Name = "GPXViewer",
                Open = true
            };

            kml.Feature = document;

            // Make the Styles for this Document
            // Trk Colors
            switch (kmlOptions.TrkColorMode)
            {
            case KmlColorMode.COLOR:
                createTrkColorColors(kmlOptions);
                break;

            case KmlColorMode.COLORSET:
                createTrkColorSetColors(kmlOptions);
                break;

            case KmlColorMode.RAINBOW:
                createTrkRainbowColors(kmlOptions, fileSetModel);
                break;
            }
            // Rte Colors
            switch (kmlOptions.RteColorMode)
            {
            case KmlColorMode.COLOR:
                createRteColorColors(kmlOptions);
                break;

            case KmlColorMode.COLORSET:
                createRteColorSetColors(kmlOptions);
                break;

            case KmlColorMode.RAINBOW:
                createRteRainbowColors(kmlOptions, fileSetModel);
                break;
            }
            // Wpt Colors
            switch (kmlOptions.WptColorMode)
            {
            case KmlColorMode.COLOR:
                createWptColorColors(kmlOptions);
                break;

            case KmlColorMode.COLORSET:
                createWptColorSetColors(kmlOptions);
                break;

            case KmlColorMode.RAINBOW:
                createWptRainbowColors(kmlOptions, fileSetModel);
                break;
            }
            // Create the color styles
            int nTrkColors = trkColors.Length;

            foreach (String color in trkColors)
            {
                document.AddStyle(new Style()
                {
                    Id   = "trk" + color,
                    Line = new LineStyle()
                    {
                        Color = Color32.Parse(color),
                        Width = kmlOptions.TrkLineWidth,
                    },
                    Icon = new SharpKml.Dom.IconStyle()
                    {
                        Color     = Color32.Parse(color),
                        ColorMode = ColorMode.Normal,
                        Scale     = kmlOptions.IconScale,
                        Icon      = new IconStyle.IconLink(new Uri(kmlOptions.TrkIconUrl))
                    }
                });
            }
            int nRteColors = rteColors.Length;

            foreach (String color in rteColors)
            {
                document.AddStyle(new Style()
                {
                    Id   = "rte" + color,
                    Line = new LineStyle()
                    {
                        Color = Color32.Parse(color),
                        Width = kmlOptions.TrkLineWidth,
                    },
                    Icon = new SharpKml.Dom.IconStyle()
                    {
                        Color     = Color32.Parse(color),
                        ColorMode = ColorMode.Normal,
                        Scale     = kmlOptions.IconScale,
                        Icon      = new IconStyle.IconLink(new Uri(kmlOptions.RteIconUrl))
                    }
                });
            }
            int nWptColors = wptColors.Length;

            foreach (String color in wptColors)
            {
                document.AddStyle(new Style()
                {
                    Id   = "wpt" + color,
                    Line = new LineStyle()
                    {
                        Color = Color32.Parse(color),
                        Width = kmlOptions.TrkLineWidth,
                    },
                    Icon = new SharpKml.Dom.IconStyle()
                    {
                        Color     = Color32.Parse(color),
                        ColorMode = ColorMode.Normal,
                        Scale     = kmlOptions.IconScale,
                        Icon      = new IconStyle.IconLink(new Uri(kmlOptions.WptIconUrl))
                    }
                });
            }

            // Loop over GPX files
            int nTrack    = 0;
            int nRoute    = 0;
            int nWaypoint = 0;
            List <GpxWaypointModel> waypointModels;
            wptType             waypoint;
            Folder              fileFolder;
            Folder              waypointFolder;
            Folder              trackFolder;
            Folder              routeFolder;
            Folder              folder;
            MultipleGeometry    mg;
            Placemark           placemark;
            Placemark           trackPlacemark;
            MultipleTrack       mt;
            Track               track;
            LineString          ls = null;
            double              lat, lon, alt;
            String              fileName;
            DateTime?           when;
            List <GpxFileModel> fileModels = fileSetModel.Files;

            foreach (GpxFileModel fileModel in fileModels)
            {
                if (!fileModel.Checked)
                {
                    continue;
                }
                fileName = fileModel.FileName;
                if (!File.Exists(fileName))
                {
                    Utils.errMsg("File does not exist: " + fileName);
                    continue;
                }
                fileFolder = new Folder()
                {
                    //Name = fileName,
                    Name = Path.GetFileName(fileName),
                    Open = false
                };
                document.AddFeature(fileFolder);
                // Loop over waypoints
                waypointFolder = new Folder()
                {
                    Name = "Waypoints",
                    Open = false
                };
                waypointModels = fileModel.Waypoints;
                if (waypointModels.Count > 0)
                {
                    fileFolder.AddFeature(waypointFolder);
                }
                foreach (GpxWaypointModel waypointModel in waypointModels)
                {
                    if (!waypointModel.Checked)
                    {
                        continue;
                    }
                    waypoint = waypointModel.Waypoint;
                    lat      = decimal.ToDouble(waypoint.lat);
                    lon      = decimal.ToDouble(waypoint.lon);
                    if (waypoint.ele.HasValue)
                    {
                        alt = decimal.ToDouble((decimal)waypoint.ele);
                    }
                    else
                    {
                        alt = 0;
                    }
                    // Make a Placemark
                    waypointFolder.AddFeature(new Placemark()
                    {
                        Name     = waypointModel.Label,
                        StyleUrl = new Uri("#wpt" + wptColors[nWaypoint % nWptColors],
                                           UriKind.Relative),
                        Geometry = new SharpKml.Dom.Point()
                        {
                            Coordinate = new Vector(lat, lon, alt)
                        }
                    });
                    nWaypoint++;
                }

                // Loop over tracks
                trackFolder = new Folder()
                {
                    Name = "Tracks",
                    Open = false
                };
                List <GpxTrackModel> trackModels;
                trackModels = fileModel.Tracks;
                bool useTrackIconFirst;
                bool useTrackTrackFirst;
                if (trackModels.Count > 0)
                {
                    fileFolder.AddFeature(trackFolder);
                }
                trackPlacemark = null;
                mt             = null;
                track          = null;
                foreach (GpxTrackModel trackModel in trackModels)
                {
                    if (!trackModel.Checked)
                    {
                        continue;
                    }
                    // Make a Placemark for the track
                    if (kmlOptions.UseTrkTrack)
                    {
                        trackPlacemark = new Placemark()
                        {
                            Name     = trackModel.Label,
                            Open     = false,
                            StyleUrl = new Uri("#trk" + trkColors[nTrack % nTrkColors],
                                               UriKind.Relative),
                            Snippet = new Snippet()
                        };
                        trackFolder.AddFeature(trackPlacemark);
                        mt = new MultipleTrack();
                        trackPlacemark.Geometry = mt;
                    }
                    // Make a Placemark with MultiGeometry
                    placemark = new Placemark()
                    {
                        Name       = trackModel.Label + " Lines",
                        Visibility = kmlOptions.UseTrkTrack && !kmlOptions.UseTrkLines ?
                                     false:true,
                        StyleUrl = new Uri("#trk" + trkColors[nTrack % nTrkColors],
                                           UriKind.Relative)
                    };
                    trackFolder.AddFeature(placemark);
                    // Need MultiGeometry to handle non-connected segments
                    mg = new MultipleGeometry();
                    placemark.Geometry = mg;
                    useTrackIconFirst  = kmlOptions.UseTrkIcon ? true : false;
                    useTrackTrackFirst = kmlOptions.UseTrkTrack ? true : false;
                    foreach (trksegType trackSegment in trackModel.Track.trkseg)
                    {
                        // Add a LineString to the MultiGeometry
                        ls = new LineString()
                        {
                            Extrude     = false,
                            Tessellate  = true,
                            Coordinates = new CoordinateCollection()
                        };
                        mg.AddGeometry(ls);
                        if (mt != null)
                        {
                            track = new Track();
                            mt.AddTrack(track);
                        }
                        foreach (wptType trackPoint in trackSegment.trkpt)
                        {
                            lat = decimal.ToDouble(trackPoint.lat);
                            lon = decimal.ToDouble(trackPoint.lon);
                            if (trackPoint.ele.HasValue)
                            {
                                alt = decimal.ToDouble((decimal)trackPoint.ele);
                            }
                            else
                            {
                                alt = 0;
                            }
                            if (trackPoint.time != null)
                            {
                                when = (DateTime)trackPoint.time;
                            }
                            else
                            {
                                when = null;
                            }
                            // Add coordinates to the LineString
                            ls.Coordinates.Add(new Vector(lat, lon, alt));
                            if (track != null)
                            {
                                // Add coordinates (and when to the track ?)
                                track.AddCoordinate(new Vector(lat, lon, alt));
                                if (when != null)
                                {
                                    track.AddWhen((DateTime)when);
                                }
                            }
                            // Make a Placemark with an icon at the first point
                            // on the track
                            if (useTrackIconFirst)
                            {
                                useTrackIconFirst = false;
                                trackFolder.AddFeature(new Placemark()
                                {
                                    Name = trackModel.Label,
                                    // The Track has its own icon
                                    Visibility = !kmlOptions.UseTrkTrack,
                                    StyleUrl   = new Uri("#trk" + trkColors[nTrack % nTrkColors],
                                                         UriKind.Relative),
                                    Geometry = new SharpKml.Dom.Point()
                                    {
                                        Coordinate = new Vector(lat, lon)
                                    }
                                });
                            }
                            if (trackPlacemark != null && useTrackTrackFirst)
                            {
                                useTrackTrackFirst         = false;
                                trackPlacemark.Description = new Description()
                                {
                                    Text = trackPoint.time.ToString()
                                };
                            }
                        }
                    }
                    nTrack++;
                }

                // Loop over routes
                folder = new Folder()
                {
                    Name = "Routes",
                    Open = false
                };
                List <GpxRouteModel> routeModels;
                routeModels = fileModel.Routes;
                bool useRteIconFirst;
                if (routeModels.Count > 0)
                {
                    fileFolder.AddFeature(folder);
                }
                foreach (GpxRouteModel routeModel in routeModels)
                {
                    if (!routeModel.Checked)
                    {
                        continue;
                    }
                    routeFolder = (new Folder()
                    {
                        Name = routeModel.Label,
                        Open = false,
                    });
                    folder.AddFeature(routeFolder);
                    // Make a Placemark with MultiGeometry
                    placemark = new Placemark()
                    {
                        Name     = routeModel.Label + " Lines",
                        StyleUrl = new Uri("#rte" + rteColors[nRoute % nRteColors],
                                           UriKind.Relative),
                    };
                    routeFolder.AddFeature(placemark);
                    // Need MultiGeometry to handle non-connected segments
                    mg = new MultipleGeometry();
                    placemark.Geometry = mg;
                    useRteIconFirst    = kmlOptions.UseRteIcon ? true : false;
                    // Add a LineString to the MultiGeometry
                    ls = new LineString()
                    {
                        Extrude    = false,
                        Tessellate = true
                    };
                    mg.AddGeometry(ls);
                    foreach (wptType rtePoint in routeModel.Route.rtept)
                    {
                        lat = decimal.ToDouble(rtePoint.lat);
                        lon = decimal.ToDouble(rtePoint.lon);
                        if (rtePoint.ele.HasValue)
                        {
                            alt = decimal.ToDouble((decimal)rtePoint.ele);
                        }
                        else
                        {
                            alt = 0;
                        }
                        if (useRteIconFirst)
                        {
                            // Make a Placemark with an Icon at the first point
                            // on the route
                            useRteIconFirst = false;
                            routeFolder.AddFeature(new Placemark()
                            {
                                Name     = routeModel.Label,
                                StyleUrl = new Uri("#rte" + rteColors[nRoute % nRteColors],
                                                   UriKind.Relative),
                                Geometry = new SharpKml.Dom.Point()
                                {
                                    Coordinate = new Vector(lat, lon, alt)
                                }
                            });
                        }
                        // Make a Placemark
                        routeFolder.AddFeature(new Placemark()
                        {
                            Name     = rtePoint.name,
                            StyleUrl = new Uri("#rte" + rteColors[nRoute % nRteColors],
                                               UriKind.Relative),
                            Geometry = new SharpKml.Dom.Point()
                            {
                                Coordinate = new Vector(lat, lon, alt)
                            }
                        });
                    }
                }
                nRoute++;
            }

            // Create the file
            String kmlFileName = kmlOptions.KmlFileName;
            string outFile     = Environment.ExpandEnvironmentVariables(kmlFileName);

            if (kmlOptions.PromptToOverwrite && File.Exists(outFile))
            {
                DialogResult dr = MessageBox.Show("File exists: " + outFile
                                                  + "\nOK to overwrite?", "File Exists",
                                                  MessageBoxButtons.YesNoCancel,
                                                  MessageBoxIcon.Information);
                if (dr != DialogResult.Yes)
                {
                    return;
                }
            }

            try {
                Serializer serializer = new Serializer();
                serializer.Serialize(kml);
                File.WriteAllText(outFile, serializer.Xml);
            } catch (Exception ex) {
                Utils.excMsg("Error creating KML file: " + outFile, ex);
            }

            // Send it to Google Earth
            if (kmlOptions.SendToGoogleEarth)
            {
                Process result;
                try {
                    result = Process.Start(outFile);
                    if (result == null)
                    {
                        Utils.errMsg("Failed to send to Google Earth:" + NL
                                     + "    " + outFile);
                    }
                } catch (Exception ex) {
                    Utils.excMsg("Failed to send to Google Earth:" + NL
                                 + "    " + outFile, ex);
                }
            }
        }