C#在Google Earth中使用kml进行圈子 [英] C# Circles With kml in Google Earth

查看:75
本文介绍了C#在Google Earth中使用kml进行圈子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我需要您的一点帮助,我试图在Google Earth中围绕一个点(经/纬度)绘制一个圆,我发现了一些圆生成器的php示例,其中他们使用公式按半径获取圆的te线串点.在php示例中,它绘制了一个完美的圆,但是我需要在c#winform应用中实现的公式.所以我修改了它,并绘制了它,但是它画出了一个椭圆:confused:不是一个圆圈.

这是我的代码:

Hello Everybody,


i need a little help from you, im trying to draw a circle around a point (lat/long) in Google Earth, i found some php examples of a circle generator where they use a formula to get te linestring points for the circle by radius. in the php example, it draws a perfect circle but i need the formula implemented in a c# winform app. so i adapted it, and its draws, but it dras an elipse :confused: not a circle.

here is my code:

<br />
<pre lang="cs"><br />
  public void Save()<br />
        {<br />
            string FullFilePath = _KMLFilePath + _KMLFileName;<br />
<br />
            XmlTextWriter xtr = new XmlTextWriter(FullFilePath, Encoding.UTF8);<br />
<br />
            xtr.WriteStartDocument();<br />
<br />
            xtr.WriteStartElement("kml");//start kml<br />
            xtr.WriteStartAttribute("xmlns");<br />
            xtr.WriteString("http://earth.google.com/kml/2.2");<br />
            xtr.WriteEndAttribute();<br />
            //xtr.WriteString();<br />
<br />
            {<br />
                xtr.WriteStartElement("Document");//start document<br />
                {<br />
                    <br />
                        xtr.WriteStartElement("name");//start name<br />
                        {<br />
                            xtr.WriteString("Posiciones Fijas");<br />
                        }<br />
                        xtr.WriteEndElement();//end name<br />
<br />
<br />
                        #region calcular cada punto con su circulo<br />
                        for (int i = 0; i < _Coords.Count; i++)<br />
                        {<br />
                            xtr.WriteStartElement("Placemark");//start placemark<br />
                            {<br />
                                xtr.WriteStartElement("name");//start name<br />
                                {<br />
                                    xtr.WriteString(_Names[i]);<br />
                                }<br />
                                xtr.WriteEndElement();//end name<br />
                                xtr.WriteStartElement("description");//start description<br />
                                {<br />
                                    xtr.WriteString(_Description[i]);<br />
                                }<br />
                                xtr.WriteEndElement();//end description<br />
                                xtr.WriteStartElement("visibility");//start vis<br />
                                {<br />
                                    xtr.WriteString("1");<br />
                                }<br />
                                xtr.WriteEndElement();//end vis<br />
                                xtr.WriteStartElement("Style");//start Style<br />
                                {<br />
<br />
                                    xtr.WriteStartElement("geomColor");//start<br />
                                    {<br />
                                        xtr.WriteString("FFffffFF");<br />
                                    }<br />
                                    xtr.WriteEndElement();//end<br />
                                    xtr.WriteStartElement("geomScale");//start<br />
                                    {<br />
                                        xtr.WriteString("1");<br />
                                    }<br />
                                    xtr.WriteEndElement();//end<br />
                                }<br />
                                xtr.WriteEndElement();//end Style<br />
                                xtr.WriteStartElement("LineString");<br />
                                {<br />
                                    xtr.WriteStartElement("coordinates");<br />
                                    {<br />
                                        #region calcular circulo<br />
                                        double Latitud = deg2rad(double.Parse(_Coords[i].Latitude.Replace(".", ",")));<br />
                                        double Longitud = deg2rad(double.Parse(_Coords[i].Longitude.Replace(".", ",")));<br />
                                        double d_rad = (_radius / 6378137);<br />
                                        for (int j = 0; j <= 360; j++)<br />
                                        {<br />
                                            double radial = (Math.PI*j) / 180;<br />
                                            double lat_rad = Math.Asin((Math.Sin(Latitud) * Math.Cos(d_rad)) + (Math.Cos(Latitud) * Math.Sin(d_rad) * Math.Cos(radial)));<br />
                                            double dlon_rad = Math.Atan2(Math.Sin(radial) * Math.Sin(d_rad) * Math.Cos(Latitud), Math.Cos(d_rad) - Math.Sin(Latitud) * Math.Sin(lat_rad));<br />
                                            double lon_rad = ((Longitud + dlon_rad + Math.PI) %( 2 * Math.PI)) - Math.PI;<br />
                                            xtr.WriteString(rad2deg(lat_rad).ToString().Replace(",", ".") + ", " + rad2deg(lon_rad).ToString().Replace(",", ".") + ", 0 ");<br />
                                        }<br />
                                        #endregion<br />
                                    }<br />
                                    xtr.WriteEndElement();//end coordinates<br />
                                }<br />
                                xtr.WriteEndElement();//end linestring<br />
                            }<br />
                            xtr.WriteEndElement();//end placemark<br />
                        }<br />
                        #endregion<br />
                <br />
                }<br />
                xtr.WriteEndElement(); //end document<br />
            }<br />
            xtr.WriteEndElement();//end kml<br />
<br />
            xtr.WriteEndDocument();<br />
<br />
            xtr.Close();<br />
            <br />
        }<br />
<br />
<br />
private double deg2rad(double deg)<br />
       {<br />
           return Math.PI* deg / 180;<br />
       }<br />
       private double rad2deg(double rad)<br />
       {<br />
           return rad *(180 / Math.PI);<br />
       }<br />
</pre><br />



我不知道到底出了什么问题

有人可以帮助我吗,我有点盲目,

php的原始源代码位于:

http://dev.bt23.org/keyhole/circlegen/output.phps [ ^ ]



i dont know what exactly is going wrong

can anybody help me im a little bit blind,

The original source code for php is located here:

http://dev.bt23.org/keyhole/circlegen/output.phps[^]

推荐答案

您好,

较短距离处的度或多或少是笛卡尔坐标.如果是大圆圈,则需要担心球形.

从您的代码看来,您的半径很小.您可以使用法线画圆方程.

如果StartX和startY是您的参考十进制度(中心点),则

Hi,

The degrees at shorter distance is more or less Cartesian coordinates. If big circles then need to worry about spherical.

From you code it seems your radius is small. The you can use normal circle drawing equation.

if StartX and startY is your reference decimal degrees (center point) then

for (int j = 0; j <= 360; j++)
{
    endX = startX + radius * Math.Cos(deg2rad(j));
    endY = startY + radius * Math.Sin(deg2rad(j));
}



endX和endY(以十进制度为单位)在圆周上为360点.半径也以十进制度数表示.

但是,如果您的圆超过约0.1 dec度(大于1000米),则此方法将无效.它可能适用于较小的半径



endX and endY (in decimal degrees) are 360 points in the circumference. Here radius also in decimal degrees.

However this will not work if your circle is more than approx 0.1 dec degrees (>1000 meters). It could work for smaller radius


我尚未对您的代码进行完整的分析,但看来您是通过在屏幕上绘制360个点来完成此操作的.不要忘记,在大多数屏幕上,两个轴上的像素距离都不相同,因此您需要调整点位置以考虑到这一事实.我希望更简单的解决方案是使用 Ellipse [ ^ ]类,它将为您绘制圆.
I have not done a complete analysis of your code but it would appear that you are doing this by drawing 360 dots on the screen. Don''t forget that on most screens the pixel distances are not the same in both axes so you need to adjust the point locations to take account of that fact. I would expect a simpler solution would be to use the Ellipse[^] class, which will draw the circle for you.


尝试了两种解决方案,即使在大圆(100km)和小圆(150m)的情况下,仍然绘制椭圆,令我感到困扰的是,PHP代码可以完美地工作,我不知道自己在修改该代码时做错了什么.
Tried both Solution, even with big(100km) and smal circles (150m) and still drawing an elipse, what bugs me is that the PHP code works perfectly, i dont know what i´ve done wrong adapting it.


这篇关于C#在Google Earth中使用kml进行圈子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆