CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Aug 2008
    Posts
    9

    streamwriter and datagridview

    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>
    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
    the issue i have is with the second part of the syntax:
    Code:
    						</coordinates>
    					</LinearRing>
    				</outerBoundaryIs>
    			</Polygon>
    		</Placemark>
    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");
    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
    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();
            }
    thanks in advance

    Jonathan
    Last edited by jkessous; November 12th, 2013 at 08:21 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured