最近在研究Kml怎么转化为Shape文件,因为客户中很多在原来采集了一部分数据都是在google Earth中,而我们的应用中特别需要这份数据,所以打算先在GE中把这份数据导出为Kml或Kmz文件,然后我们的应用系统把Kml或Kmz数据转化为Shape,再导入到系统中。

这样做优点是:导出的Shape数据能够在很多GIS平台中读取,缺点是,会丢失Kml或Kmz的符号等信息;

我们第一阶段的目标是:只解析Kml中的点、线、面,对于贴地图片什么的暂时不考虑;

在以上的目的上我们选择了GDAL作为GIS的平台,之所以选择他是因为他不需要安装直接部署就能使用(绿色),查找一些资料发现OGR可以解析Kml数据,但是我却没有找到相关的资料方法,所以索性,自己解析Kml,要解析Kml看了好些Kml的编码资料,大概找出了编码规则开始着手解析;废话少说,直接补充解析的代码:

1.KMLPlacemarker接口,包含了Kml中Placemarker下的信息

/// <summary>
    /// IKMLPlacemarker
    /// </summary>
    public interface IKMLPlacemarker
    {
        #region 成员属性

/// <summary>
        /// 名称
        /// </summary>
        string Name { set; get; }

/// <summary>
        /// 描述信息
       
        string Description { set; get; }

/// <summary>
        /// 样式路径
        /// </summary>
        string StyleURL { set; get; }

/// <summary>
        /// 图形类型
        /// </summary>
        enumKmlGeometryType GeometryType { set; get; }

/// <summary>
        /// KMLGeometry
        /// </summary>
        IKMLGeometry Geometry { set; get; }

#endregion

#region 成员方法

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="placemarkerNode">PlacemarkerNode</param>
        /// <returns>是否成功</returns>
        bool Initial(System.Xml.XmlNode  placemarkerNode)   ;
                  
        #endregion
    }

2.KMLGeometry空接口,作为KMLPlacemarker的一个属性

/// <summary>
    /// KMLGeometry
    /// </summary>
    public interface IKMLGeometry
    {
    }

3.KMLPoint接口 KML中点

/// <summary>
    /// IKMLPoint
    /// </summary>
    public interface IKMLPoint
    {
        #region 成员属性

/// <summary>
        /// 坐标信息
        /// </summary>
        string Coordinates { set; get; }

#endregion

#region 成员方法

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="pointXmlNode">point节点</param>
        /// <returns>是否成功</returns>
        bool InitialPoint(System.Xml.XmlNode pointXmlNode);

#endregion
    }

4. KMLPolyline接口 KML中线类型

/// <summary>
    /// IKMLPolyLine
    /// </summary>
    public interface IKMLPolyline
    {
        #region 成员属性

/// <summary>
        /// 坐标信息
        /// </summary>
        string Coordinates { set; get; }

#endregion

#region 成员方法

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="polylineXmlNode">polyLine节点</param>
        /// <returns>是否成功</returns>
        bool InitialPolyline(System.Xml.XmlNode polylineXmlNode);

#endregion
    }

5.KMLPolygon接口 Kml中的面类型(不是多面,是面)

/// <summary>
    /// IKMLPolygon
    /// </summary>
    public interface IKMLPolygon
    {
        #region 成员属性

/// <summary>
        /// 坐标信息-内轮廓
        /// </summary>
        string Coordinates_InnerBoundary { set; get; }

/// <summary>
        /// 坐标信息-外轮廓
        /// </summary>
        string Coordinates_OuterBoundary { set; get; }

#endregion

#region 成员方法

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="polygonXmlNode">polyLine节点</param>
        /// <returns>是否成功</returns>
        bool InitialPolygon(System.Xml.XmlNode polygonXmlNode);

#endregion
    }

6. KMLPoint的实现

/// <summary>
    /// KMLPoint
    /// </summary>
    public class KMLPoint : IKMLPoint, IKMLGeometry
    {
        #region IKMLPoint 成员

#region 成员属性

/// <summary>
        /// 坐标信息
        /// </summary>
        public string Coordinates { set; get; }

#endregion

#region 成员方法

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="pointXmlNode">point节点</param>
        /// <returns>是否成功</returns>
        public bool InitialPoint(System.Xml.XmlNode pointXmlNode)
        {
            System.Xml.XmlNode childNode = null;
            for (int k = 0; k < pointXmlNode.ChildNodes.Count; k++)
            {
                childNode = pointXmlNode.ChildNodes[k];
                switch (childNode.Name)
                {
                    case "coordinates":
                        {
                            this.Coordinates = childNode.InnerText;
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }

return true;
        }

#endregion

#endregion
    }

7.KMLPolyline的实现

/// <summary>
    /// KMLPolyline
    /// </summary>
    public class KMLPolyline : IKMLPolyline, IKMLGeometry
    {
        #region IKMLPolyline 成员

#region 成员属性

/// <summary>
        /// 坐标信息
        /// </summary>
        public string Coordinates { set; get; }

#endregion

#region 成员方法

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="polylineXmlNode">polyLine节点</param>
        /// <returns>是否成功</returns>
        public bool InitialPolyline(System.Xml.XmlNode polylineXmlNode)
        {
            System.Xml.XmlNode childNode = null;
            for (int k = 0; k < polylineXmlNode.ChildNodes.Count; k++)
            {
                childNode = polylineXmlNode.ChildNodes[k];
                switch (childNode.Name)
                {
                    case "coordinates":
                        {
                            this.Coordinates = childNode.InnerText;
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }

return true;
        }

#endregion

#endregion
    }

8. KMLPolygon的实现

/// <summary>
    /// KMLPolygon
    /// </summary>
    public class KMLPolygon : IKMLPolygon, IKMLGeometry
    {
        #region IKMLPolygon 成员

#region 成员属性

/// <summary>
        /// 坐标信息-内轮廓
        /// </summary>
        public string Coordinates_InnerBoundary { set; get; }

/// <summary>
        /// 坐标信息-外轮廓
        /// </summary>
        public string Coordinates_OuterBoundary { set; get; }

#endregion

#region 成员方法

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="polygonXmlNode">polyLine节点</param>
        /// <returns>是否成功</returns>
        public bool InitialPolygon(System.Xml.XmlNode polygonXmlNode)
        {
            System.Xml.XmlNode childNode = null;
            for (int i = 0; i < polygonXmlNode.ChildNodes.Count; i++)
            {
                childNode = polygonXmlNode.ChildNodes[i];
                switch (childNode.Name)
                {
                    case "outerBoundaryIs":
                        {
                            this.Coordinates_OuterBoundary = GetCoordinates(childNode);
                            break;
                        }
                    case "innerBoundaryIs":
                        {
                            this.Coordinates_InnerBoundary = GetCoordinates(childNode);
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }

return true;
        }

#endregion

#endregion

#region 私有方法

/// <summary>
        /// 获得轮廓线坐标集
        /// </summary>
        /// <param name="boundaryNode">轮廓线节点</param>
        /// <returns>坐标集</returns>
        private string GetCoordinates(System.Xml.XmlNode boundaryNode)
        {
            System.Xml.XmlNode childNodeBoundary = null;
            System.Xml.XmlNode childNodeLinering = null;

for (int k = 0; k < boundaryNode.ChildNodes.Count; k++)
            {
                childNodeBoundary = boundaryNode.ChildNodes[k];
                if(childNodeBoundary.Name!="LinearRing") continue;
                for (int j = 0; j < childNodeBoundary.ChildNodes.Count; j++)
                {
                      childNodeLinering= childNodeBoundary.ChildNodes[j];
                      switch (childNodeLinering.Name)
                      {
                          case "coordinates":
                              {
                                  return childNodeLinering.InnerText;
                              }
                          default:
                              {
                                  break;
                              }
                      }
                }
            }

return string.Empty; ;
        }

#endregion
    }

9.KmlGeometryType枚举类型

/// <summary>
    /// KmlGeometry类型枚举
    /// </summary>
    public enum enumKmlGeometryType
    {
        /// <summary>
        /// 未知
        /// </summary>
        KMLNull=0,

/// <summary>
        /// 点
        /// </summary>
        KMLPoint=1,

/// <summary>
        /// 线
        /// </summary>
        KMLPolyline=2,

/// <summary>
        /// 面
        /// </summary>
        KMLPolygon=3
    }

10.KMLPlacemarker的实现

/// <summary>
    /// KMLPlacemarker
    /// </summary>
    public class KMLPlacemarker:IKMLPlacemarker
    {
        #region IKMLPlacemarker 成员

#region 成员属性

/// <summary>
        /// 名称
        /// </summary>
        public string Name { set; get; }

/// <summary>
        /// 描述信息

public string Description { set; get; }

/// <summary>
        /// 样式路径
        /// </summary>
        public string StyleURL { set; get; }

/// <summary>
        /// 图形类型
        /// </summary>
        public enumKmlGeometryType GeometryType { set; get; }

/// <summary>
        /// KMLGeometry
        /// </summary>
        public IKMLGeometry Geometry { set; get; }

#endregion

#region 成员方法

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="placemarkerNode">PlacemarkerNode</param>
        /// <returns>是否成功</returns>
        public bool Initial(System.Xml.XmlNode placemarkerNode)
        {
            if (placemarkerNode == null) return false;
            if (placemarkerNode.ChildNodes.Count < 1) return false;

System.Xml.XmlNode childNode = null;
            for (int i = 0; i < placemarkerNode.ChildNodes.Count; i++)
            {
                childNode = placemarkerNode.ChildNodes[i];
                switch (childNode.Name)
                {
                    case "name":
                        {
                            this.Name = childNode.InnerText;
                            break;
                        }
                    case "description":
                        {
                            this.Description = childNode.InnerText;
                            break;
                        }
                    case "styleUrl":
                        {
                            this.StyleURL = childNode.InnerText;
                            break;
                        }
                    case "Point":
                        {
                            this.GeometryType = enumKmlGeometryType.KMLPoint;
                            this.Geometry = new KMLPoint();
                            (this.Geometry as IKMLPoint).InitialPoint(childNode);
                            break;
                        }
                    case "LineString":
                        {
                            this.GeometryType = enumKmlGeometryType.KMLPolyline;
                            this.Geometry = new KMLPolyline();
                            (this.Geometry as IKMLPolyline).InitialPolyline(childNode);
                            break;
                        }
                    case "Polygon":
                        {
                            this.GeometryType = enumKmlGeometryType.KMLPolygon;
                            this.Geometry = new KMLPolygon();
                            (this.Geometry as IKMLPolygon).InitialPolygon(childNode);
                            break;
                        }
                    default:
                        break;
                }
            }

return true;
        }

#endregion

#endregion
    }

11.1KML解析为KMLPlacemarker的方法:(后续会补充上,这只是一部分)

/// <summary>
        /// 转换KML为Shape文件
        /// </summary>
        /// <param name="kmlFilePath">kml文件路径</param>
        /// <param name="shapeDirectoryPath">shape文件夹路径</param>
        /// <returns></returns>
        public bool ConvertKMLToShape(string kmlFilePath, string shapeDirectoryPath)
        {
            // 判断
            if (string.IsNullOrEmpty(kmlFilePath)) return false;
            if (string.IsNullOrEmpty(shapeDirectoryPath)) return false;
            if (!System.IO.File.Exists(kmlFilePath)) return false;
            if (!System.IO.Directory.Exists(shapeDirectoryPath)) { System.IO.Directory.CreateDirectory(shapeDirectoryPath); };

string kmlFileName = System.IO.Path.GetFileNameWithoutExtension(kmlFilePath);
            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.Load(kmlFilePath);

System.Xml.XmlNodeList placeMarkerNodeList = xmlDocument.GetElementsByTagName("Placemark");
            System.Xml.XmlNode placeMarkerNode = null;

IList<IKMLPlacemarker> kmlPlaceMarkers_Point = new List<IKMLPlacemarker>(); ;
            IList<IKMLPlacemarker> kmlPlaceMarkers_Polyline = new List<IKMLPlacemarker>(); ;
            IList<IKMLPlacemarker> kmlPlaceMarkers_Polygon = new List<IKMLPlacemarker>(); ;

IKMLPlacemarker kmlPlacemarker = null;
            string coordinates = string.Empty;
            int i = 0;
            for (i = 0; i < placeMarkerNodeList.Count; i++)
            {
                placeMarkerNode = placeMarkerNodeList.Item(i);
                kmlPlacemarker = new KMLPlacemarker();
                kmlPlacemarker.Initial(placeMarkerNode);
                switch (kmlPlacemarker.GeometryType)
                {
                    case enumKmlGeometryType.KMLPoint:
                        kmlPlaceMarkers_Point.Add(kmlPlacemarker);
                        break;
                    case enumKmlGeometryType.KMLPolyline:
                        kmlPlaceMarkers_Polyline.Add(kmlPlacemarker);
                        break;
                    case enumKmlGeometryType.KMLPolygon:
                        kmlPlaceMarkers_Polygon.Add(kmlPlacemarker);
                        break;
                    default:
                        break;
                }
            }

    ////////////////////////////////////////隐掉了部分生成Shape的代码/////////////////////////////////////////

return true;
        }

这时候就能把Kml文件解析为自己定义的KmlPlacemarker,它包含了Kml中Placemarker中的所有信息;

利用Ogr将Kml转为Shape【1】的更多相关文章

  1. python 利用 ogr 写入shp文件,数据格式

    python 利用 ogr 写入 shp 文件, 定义shp文件中的属性字段(field)的数据格式为: OFTInteger # 整型 OFTIntegerList # 整型list OFTReal ...

  2. .net 利用Emit将object转为DbParameter,DataTable转为List<>

    先放测试结果图,测试的方法是拷贝了老赵的一个简单的性能计数器:CodeTimer.发现速度还是比利用反射来获取快了2倍左右的,将object转为DbParameter的反射方法我没写.         ...

  3. ArcGIS中KML转为shp文件

    问题:如何将KML转为shp文件? 方法: 1.打开ArcMap -> ArcToolbox: 2.在ArcToolbox中选择“转换工具”-> “由KML转出” -> “KML转图 ...

  4. java利用反射将pojo转为json对象

    最近做以太坊钱包项目需要与前台进行json交互,写一个工具类,经普通javaBean转为json对象 package util; import java.lang.reflect.Field; imp ...

  5. 利用css将英文转为大写或小写

    项目需要在后台接收的字段值为小写,但在页面上显示大写英文,但操作页面之后,最终传给后台的依旧是小写,所以就需要使用css转化一下即可 <li>This.is.a.book,全部转为大写:& ...

  6. 利用PhotoShop将Font-Awesome转为图片格式

    介绍如何将Font-Awesome等字体图标转换为图片格式,使用PHOTPSHOP很简单. 网上找了很多,都比较麻烦.别问为什么要这么做,因为你还没遇到需要的时候. 下载Font-Awesome字体库 ...

  7. 利用itext将html转为pdf

    亲测代码没有问题,需要注意细节已经标注:需要jar包:iText-2.0.8.jar:core-renderer-R8.jar: core-renderer-R8.jar下载地址:http://cen ...

  8. json处理三部曲之第三曲:利用Gson处理json

    需要导入gson-xxx.jar包 <dependency> <groupId>com.google.code.gson</groupId> <artifac ...

  9. shp的基本操作

    本节将介绍如何利用python完成对shp的基本操作 1.读取shp四至 import shapefile sf = shapefile.Reader(r"E:\shp\1.shp" ...

随机推荐

  1. 2016级算法第六次上机-B.ModricWang's FFT : EASY VERSION

    1114 ModricWang's FFT EASY VERSION 思路 利用FFT做大整数乘法,实际上是把大整数变成多项式,然后做多项式乘法. 例如,对于\(1234\),改写成\(f(x)=1* ...

  2. js数字动画

    项目中需要的数字动画效果,网上找不到需要的效果,所以自己写了一个. 不多说,直接上干货:

  3. c# 获取项目根目录方法

    编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径 ...

  4. (转)DB2性能优化 – 如何通过调整锁参数优化锁升级

    原文:http://blog.51cto.com/5063935/2074306 1.概念描述 所谓的锁升级(lock escalation),是数据库的一种作用机制,为了节约内存的开销, 其会将为数 ...

  5. ubuntu安装ntp时间服务器

    1.安装ntp软件 sudo apt-get install ntp2.修改配置文件      sudo vim /etc/ntp.conf driftfile /var/lib/ntp/ntp.dr ...

  6. 【Qt开发】常用控件--QLineEdit

    QLineEdit是单行文本编辑控件.比如用户名,密码等输入框可以使用该控件. 所属头文件<QLineEdit> 常用方法 1.void setText(const QString &am ...

  7. hr中间插入字体

    修饰CSS:hr:before { content: "??";} hr:after { content: " This is an <hr> element ...

  8. tmux快捷键汇总(常用)

    会话外操作: tmux new -s <name-of-my-session> 在会话外创建一个新的会话 tmux ls 在会话外获取会话列表 tmux a(attach) -t < ...

  9. Golang真言

    Don't communicate by sharing memory, share memory by communicating. Concurrency is not parallelism. ...

  10. java并发编程(9)内存模型

    JAVA内存模型 在多线程这一系列中,不去探究内存模型的底层 一.什么是内存模型,为什么需要它 在现代多核处理器中,每个处理器都有自己的缓存,定期的与主内存进行协调: 想要确保每个处理器在任意时刻知道 ...