This project has moved and is read-only. For the latest updates, please go here.

Adding colors and opacity to a set of Polygons.

Aug 27, 2016 at 11:57 PM
Edited Aug 28, 2016 at 12:06 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using SharpKml.Base;
using SharpKml.Dom;
using SharpKml.Engine;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var reader = new StreamReader(File.OpenRead(@"C:\Users\esepich\Desktop\temp-nodes.csv"));

            List<string> pID = new List<string>();

            List<double> pLON = new List<double>();

            List<double> pLAT = new List<double>();

            reader.ReadLine();

            while( !reader.EndOfStream )
            {
                var line = reader.ReadLine();

                var values = line.Split(',');

                pID.Add(values[0]);

                pLON.Add(Double.Parse(values[1]));

                pLAT.Add(Double.Parse(values[2]));
            }

            List<Polygon> lPolygon = new List<Polygon>();

            var curID = pID[0];

            Polygon mPolygon = new Polygon();
            OuterBoundary outerBoundary = new OuterBoundary();
            outerBoundary.LinearRing = new LinearRing();
            outerBoundary.LinearRing.Coordinates = new CoordinateCollection();

            for ( var x = 0; x < pID.Count; x++)
            {
                if( pID[x].Equals( curID ) )
                {
                    outerBoundary.LinearRing.Coordinates.Add(new Vector(pLAT[x], pLON[x], 0));
                }
                else
                {
                    curID = pID[x];

                    mPolygon.OuterBoundary = outerBoundary;
                    mPolygon.Id = curID;
                    
                    lPolygon.Add(mPolygon);

                    mPolygon = new Polygon();
                    outerBoundary = new OuterBoundary();
                    outerBoundary.LinearRing = new LinearRing();
                    outerBoundary.LinearRing.Coordinates = new CoordinateCollection();
                }
            }

            



            Document document = new Document();
            document.Name = "Polygon.kml";
            document.Open = false;

            foreach( var mP in lPolygon)
            {
                Placemark placemark = new Placemark();
                placemark.Name = mP.Id;
                placemark.Geometry = mP;
                document.AddFeature(placemark);

            }


            Kml root = new Kml();
            root.Feature = document;

            KmlFile kmlfile = KmlFile.Create(root, false);

            using (FileStream stream = File.OpenWrite(@"C:\Users\esepich\Desktop\placemark.kml"))
            {
                kmlfile.Save(stream);
            }
        }
    }
}
I would like the polygons to come in with a randomly generated color with %50 opacity for the fill area. Does anyone know how to do that? I am trying to apply an inner fill area that is the intent to do for this question.
Aug 28, 2016 at 5:32 AM
Edited Aug 28, 2016 at 5:45 AM
I found that following https://sharpkml.codeplex.com/SourceControl/latest#Examples/CreateIconStyle.cs and using PolygonStyle to set properties that one can add the colors and opacity. I am using a random color scheme. So the problem was that KmlFile encountered an exception writing the KML because of something yet undetermined. The workaround is to use Serializer(). The for loop is skipping maybe one point. Some modification is needed.
Marked as answer by sepoto on 8/27/2016 at 9:32 PM
Sep 1, 2016 at 6:13 AM
sepoto wrote:
So the problem was that KmlFile encountered an exception writing the KML because of something yet undetermined.
Do you know what that exception was by any chance? If you have a small example program that shows the exception could you open an issue and zip up the project+data files and attach it to the issue and I'll try to find the source of the exception.

Thanks,
Sam