j k

j k

  • NA
  • 34
  • 0

streamwriter and datagridview

Nov 12 2013 8:10 AM

Hi All,
i need your assistance with the following:
i have built an application that fills inputs from a database to a datagridview.
the applicaiton then takes the inputs from the datagridview and writes it down to a file (kml --> google earth).
i know how to write to a file the issue is the order.
the output of the file should be:

[CODE]
<Placemark>
   <name>Alarm Zone 2</name>
   <styleUrl>#m_ylw-pushpin</styleUrl>
   <Polygon>
    <tessellate>1</tessellate>
    <outerBoundaryIs>
     <LinearRing>
      <coordinates>
       -101.2023177342566,35.92269094626265,0 -101.2022793432744,35.92253750459201,0 -101.2020270554968,35.92258752157488,0 -101.2020657349624,35.92275793800641,0 -101.2023177342566,35.92269094626265,0
      </coordinates>
     </LinearRing>
    </outerBoundaryIs>
   </Polygon>

[/CODE]

currently i managed to wirte the first part of the file
[CODE]
<Placemark>
   <name>Alarm Zone 2</name>
   <styleUrl>#m_ylw-pushpin</styleUrl>
   <Polygon>
    <tessellate>1</tessellate>
    <outerBoundaryIs>
     <LinearRing>
      <coordinates>
       -101.2023177342566,35.92269094626265,0 -101.2022793432744,35.92253750459201,0 -101.2020270554968,35.92258752157488,0 -101.2020657349624,35.92275793800641,0 -101.2023177342566,35.92269094626265,0
[/CODE]

the issue i have is with the second part of the syntax:
[CODE]
      </coordinates>
     </LinearRing>
    </outerBoundaryIs>
   </Polygon>
  </Placemark>
[/CODE]

no matter where i put
[CODE]
                KMLwriter1.WriteLine("\r\n");
                KMLwriter1.WriteLine("</coordinates>" + "\r\n");
                KMLwriter1.WriteLine("</LinearRing>" + "\r\n");
                KMLwriter1.WriteLine("</outerBoundaryIs>" + "\r\n");
                KMLwriter1.WriteLine("</Polygon>" + "\r\n");
                KMLwriter1.WriteLine("</Placemark>" + "\r\n");
[/CODE]

i mess up the file and i get something like this
[CODE]
<Placemark>

<name>External_1_AL</name>

<styleUrl>#m_ylw-pushpin</styleUrl>

<Polygon>

<tessellate>1</tessellate>

<outerBoundaryIs>

<LinearRing>

<coordinates>

-101.2023177342566,35.92269094626265,0

</coordinates>

</LinearRing>

</outerBoundaryIs>

</Polygon>

</Placemark>

 -101.2022793432744,35.92253750459201,0
[/CODE]

below you can find my current code.

[CODE]
      #region Class LatLongPair
        class LatLongPair
        {
            public double  Lat { get;set; }
            public double  Lon { get;set; }
        }
        #endregion


        private void button3_Click(object sender, EventArgs e)
        {

            StreamWriter KMLwriter1 = new System.IO.StreamWriter(KMLDestinationTB.Text + "\\" + sitenameTB.Text + ".kml", false);

            #region KML Header Writings



            #endregion KML Header Writing



            #region testing new code from web

            // this is a new code from the web

            Dictionary<string, List<LatLongPair>> LatLongCollectionByName = new Dictionary<string, List<LatLongPair>>();

            List<LatLongPair> lst = null;
            LatLongPair lPair = null;
            string matchHandle = string.Empty;

            for (int i = 0; i < LayersGrid.Rows.Count; i++)
            {
                try
                {
                    if (LayersGrid.Rows[i].Cells[0].Value.ToString() == matchHandle)
                    {
                        lPair.Lat = Convert.ToDouble(LayersGrid.Rows[i].Cells[2].Value);
                        lPair.Lon = Convert.ToDouble(LayersGrid.Rows[i].Cells[1].Value);
                        lst.Add(lPair);
                    }

                    else
                    {
                        matchHandle = LayersGrid.Rows[i].Cells[0].Value.ToString();
                        lPair = new LatLongPair();
                        lPair.Lat = Convert.ToDouble(LayersGrid.Rows[i].Cells[2].Value);
                        lPair.Lon = Convert.ToDouble(LayersGrid.Rows[i].Cells[1].Value);

                        lst = new List<LatLongPair>();
                        lst.Add(lPair);

                        LatLongCollectionByName.Add(matchHandle, lst);

                        #region KML Placemark Writing

                        KMLwriter1.WriteLine("<Placemark>" + "\r\n");
                        KMLwriter1.WriteLine("<name>" + matchHandle + "</name>" + "\r\n");
                        KMLwriter1.WriteLine("<styleUrl>#m_ylw-pushpin</styleUrl>" + "\r\n");
                        KMLwriter1.WriteLine("<Polygon>" + "\r\n");
                        KMLwriter1.WriteLine("<tessellate>1</tessellate>" + "\r\n");
                        KMLwriter1.WriteLine("<outerBoundaryIs>" + "\r\n");
                        KMLwriter1.WriteLine("<LinearRing>" + "\r\n");
                        KMLwriter1.WriteLine("<coordinates>" + "\r\n");


                    

                    }

                    foreach (KeyValuePair<string, List<LatLongPair>> item in LatLongCollectionByName)
                    {
                        KMLwriter1.WriteLine(lPair.Lon + "," + lPair.Lat + ",0");

                    }

                }


                catch
                {
                    MessageBox.Show("error");
                }

                        #endregion


                  
              //  }

                KMLwriter1.WriteLine("\r\n");
                KMLwriter1.WriteLine("</coordinates>" + "\r\n");
                KMLwriter1.WriteLine("</LinearRing>" + "\r\n");
                KMLwriter1.WriteLine("</outerBoundaryIs>" + "\r\n");
                KMLwriter1.WriteLine("</Polygon>" + "\r\n");
                KMLwriter1.WriteLine("</Placemark>" + "\r\n");


                //end of new code from web

            #endregion


            }
            KMLwriter1.Close();
        }

[/CODE]

thanks in advance

Jonathan


Answers (1)