milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

KML example

Details
Written by: Stanko Milosev
Category: C#
Published: 02 June 2022
Last Updated: 04 June 2022
Hits: 51
  • xml
Here I wrothe the example of displaying KML files on google maps, and here is one my example of creating KML. I was using sample from here. The model:
using System.Xml.Serialization;

namespace CreateKmlFromFiles
{
  public class KmlModel
  {
    [XmlRoot("kml", Namespace = "http://www.opengis.net/kml/2.2")]
    public class Kml
    {
      public Document Document { get; set; }
    }

    public class Document
    {
      public string name { get; set; }

      public string description { get; set; }

      public Style Style { get; set; }

      public Placemark Placemark { get; set; }
    }

    public class Style
    {
      [XmlAttribute("id")]
      public string id { get; set; }

      public LineStyle LineStyle { get; set; }
      public PolyStyle PolyStyle { get; set; }
    }

    public class LineStyle
    {
      public string color { get; set; }
      public string width { get; set; }
    }

    public class PolyStyle
    {
      public string color { get; set; }
    }

    public class Placemark
    {
      public string name { get; set; }
      public string description { get; set; }
      public string styleUrl { get; set; }
      public LineString LineString { get; set; }
    }

    public class LineString
    {
      public string extrude { get; set; }
      public string tessellate { get; set; }
      public string altitudeMode { get; set; }
      public string coordinates { get; set; }
    }
  }
}
The code:
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace CreateKmlFromFiles
{
  class Program
  {
    static void Main(string[] args)
    {
      StringBuilder sb = new StringBuilder();
      sb.Append("7.0971881, 50.7359953, 2357");

      KmlModel.Kml kmlModel = new KmlModel.Kml
      {
        Document = new KmlModel.Document
        {
          name = "Paths"
          , description = "test"
          , Style = new KmlModel.Style
          {
            id = "red"
            , LineStyle = new KmlModel.LineStyle
            {
              color = "7f0000ff"
              , width = "4"
            }
            , PolyStyle = new KmlModel.PolyStyle
            {
              color = "7f0000ff"
            }
          }
          , Placemark = new KmlModel.Placemark
          {
            name = "Absolute Extruded"
            , description = "Transparent green wall with yellow outlines"
            , styleUrl = "#yellowLineGreenPoly"
            , LineString = new KmlModel.LineString
            {
              extrude = "1"
              , tessellate = "1"
              , altitudeMode = "absolute"
              , coordinates = sb.ToString()
            }
          }
        }
      };

      TextWriter txtWriter = new StreamWriter
      (
        Path.ChangeExtension
        (
          System.Reflection.Assembly.GetEntryAssembly().Location
          , ".kml"
        ));

      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

      ns.Add("", "http://www.opengis.net/kml/2.2");

      XmlSerializer xmlSerializer = new XmlSerializer(typeof(KmlModel.Kml));
      xmlSerializer.Serialize(txtWriter, kmlModel, ns);

      txtWriter.Close();
    }
  }
}

Difference between Path.Join and Path.Combine

Details
Written by: Stanko Milosev
Category: C#
Published: 07 May 2022
Last Updated: 07 May 2022
Hits: 105
Example Path.Combine:
Debug.WriteLine(Path.Combine("test1", @"\test2"));
Will give the result:

\test2

Debug.WriteLine(Path.Join("test1", @"\test2"));
Gives the result:

test1\test2

GetCurrentDirectory

Details
Written by: Stanko Milosev
Category: C#
Published: 30 April 2022
Last Updated: 30 April 2022
Hits: 115
Here and here I gave examples on how to get the exe path, but sometimes that is not enough, sometimes I need to find path of our class library, for example, that is why sometimes I need Directory.GetCurrentDirectory()

Copy files but keep path structure

Details
Written by: Stanko Milosev
Category: C#
Published: 09 April 2022
Last Updated: 09 April 2022
Hits: 183
Here is one my tool to copy files which are listed in a file, and copy them to a location but keep original path tree. Usefull for TortoiseSVN usually I do it when I need to create the patch but I also want to be sure that if patch doesn't work that I have at least some kind of backup.

For example, you want to save files from c:\folder\subfolderOne\file1.txt and c:\folder\subfolderTwo\file1.txt to c:\copyFolder and to keep structure like c:\copyFolder\subfolderOne\file1.txt and c:\copyFolder\subfolderTwo\file1.txt

Here is the code:

using System;
using System.IO;

namespace CopyFilesKeepPathStructure
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Root source path");
            string rootSourcePath = Console.ReadLine();
            //string rootSourcePath = @" c:\folder";

            Console.WriteLine("Root destination path");
            string rootDestinationPath = Console.ReadLine();
            //string rootDestinationPath = @"c:\copyFolder";

            Console.WriteLine($"File with list of files to be copied:");
            string fileWithListOfFilesToBeCopied = Console.ReadLine();
            if (!(fileWithListOfFilesToBeCopied is null) && !(rootSourcePath is null))
            {
                string[] listOfSourceFiles = File.ReadAllLines(fileWithListOfFilesToBeCopied);
                //string[] listOfSourceFiles = File.ReadAllLines(@"C:\files.txt");

                foreach (string sourceFile in listOfSourceFiles)
                {
                    string folderStructure = sourceFile.Substring(rootSourcePath.Length,
                        sourceFile.Length - rootSourcePath.Length);
                    string destinationFile = rootDestinationPath + folderStructure;
                    Directory.CreateDirectory(Path.GetDirectoryName(destinationFile) ?? string.Empty);
                    File.Copy(sourceFile, destinationFile, true);
                    Console.WriteLine($"Source: {sourceFile}, destination: {destinationFile}");
                }
            }

            Console.WriteLine($"Press any key...");
            Console.ReadKey();
        }
    }
}
Download from here.
  1. Creating valid HTTP URI
  2. Sitemap example
  3. Await and async
  4. Master - Detail example

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 1 of 137

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10