示例#1
0
 private void WriteFeature(XFeature feature, BinaryWriter bw)
 {
     if (feature.Spatial is XPointSpatial)
     {
         ((XPointSpatial)feature.Spatial).Centroid.WriteVertex(bw);
     }
     if (feature.Spatial is XLineSpatial)
     {
         WriteMultipleVertexes(((XLineSpatial)feature.Spatial).
                               AllVertexes, bw);
     }
     if (feature.Spatial is XPolygonSpatial)
     {
         WriteMultipleVertexes(((XPolygonSpatial)feature.Spatial).
                               AllVertexes, bw);
     }
     WriteAttribute(feature.Attribute, bw);
 }
示例#2
0
 public void AddFeature(XFeature feature)
 {
     Features.Add(feature);
 }
示例#3
0
        public List <XLayer> ReadJSONFile(string filename)
        {
            List <XLayer> layers = new List <XLayer>();
            StreamReader  sr     = new StreamReader(filename, Encoding.UTF8);

            Newtonsoft.Json.Linq.JObject       o        = Newtonsoft.Json.Linq.JObject.Parse(sr.ReadToEnd()); //读入一个json文件
            Newtonsoft.Json.Linq.JToken        features = o["features"];                                      //读取所有空间对象数组
            List <Newtonsoft.Json.Linq.JToken> flst     = features.ToList();
            List <XField> fields       = new List <XField>();                                                 /*ReadFields(mfh.FieldCount, br);*/
            XLayer        pointlayer   = new XLayer(filename, SHAPETYPE.point, null, fields);
            XLayer        linelayer    = new XLayer(filename, SHAPETYPE.line, null, fields);
            XLayer        polygonlayer = new XLayer(filename, SHAPETYPE.polygon, null, fields);
            double        maxx         = Double.MinValue;
            double        minx         = Double.MaxValue;
            double        maxy         = Double.MinValue;
            double        miny         = Double.MaxValue;

            for (int index = 0; index < flst.Count; index++)
            {
                List <XVertex> _Vertexes               = new List <XVertex>();                      //记录feature的节点
                string         featuretype             = (string)(flst[index]["geometry"]["type"]); //读取对象类型 包括Point LineString Polygon
                Newtonsoft.Json.Linq.JToken properties = flst[index]["properties"];                 //读取对象的所有字段与属性

                //if (featuretype == "Polygon") flst[index]["geometry"]["coordinates"][0]
                if (featuretype == "Point")//如果是点对象 则按点对象的数据结构进行处理
                {
                    var    location = flst[index]["geometry"]["coordinates"];
                    double lon      = (double)location.First;
                    double lat      = (double)location.Last;
                    Console.WriteLine(lon);
                    if (lon > maxx)
                    {
                        maxx = lon;
                    }
                    else if (lon < minx)
                    {
                        minx = lon;
                    }

                    if (lat > maxy)
                    {
                        maxy = lat;
                    }
                    else if (lat < miny)
                    {
                        miny = lat;
                    }

                    _Vertexes.Add(new XVertex(lon, lat));
                    ReadJSONField(properties, pointlayer);//右边返回一个Fields泛型赋给左边图层字段
                }

                else if (featuretype == "LineString")//如果是线对象 则按线对象的数据结构进行处理
                {
                    foreach (var location in flst[index]["geometry"]["coordinates"])
                    {
                        double lon = (double)location.First;
                        double lat = (double)location.Last;
                        //Console.WriteLine(lon);
                        if (lon > maxx)
                        {
                            maxx = lon;
                        }
                        else if (lon < minx)
                        {
                            minx = lon;
                        }

                        if (lat > maxy)
                        {
                            maxy = lat;
                        }
                        else if (lat < miny)
                        {
                            miny = lat;
                        }

                        _Vertexes.Add(new XVertex(lon, lat));
                        ReadJSONField(properties, linelayer);//右边返回一个Fields泛型赋给左边图层字段
                    }
                }

                else if (featuretype == "Polygon")//如果是多边形对象 则按多边形对象的数据结构进行处理
                {
                    foreach (var singlepoly in flst[index]["geometry"]["coordinates"])
                    {
                        foreach (var location in singlepoly)
                        {
                            double lon = (double)location.First;
                            double lat = (double)location.Last;
                            Console.WriteLine(lon);
                            if (lon > maxx)
                            {
                                maxx = lon;
                            }
                            else if (lon < minx)
                            {
                                minx = lon;
                            }

                            if (lat > maxy)
                            {
                                maxy = lat;
                            }
                            else if (lat < miny)
                            {
                                miny = lat;
                            }

                            _Vertexes.Add(new XVertex(lon, lat));
                            ReadJSONField(properties, polygonlayer);//右边返回一个Fields泛型赋给左边图层字段
                        }
                    }
                }


                if (featuretype == "Point")
                {
                    XFeature onefeature = new XFeature(new XPointSpatial(_Vertexes[0]), null); //暂时默认无属性
                    pointlayer.AddFeature(onefeature);                                         //给线图层添加对象
                }
                else if (featuretype == "LineString")
                {
                    XFeature onefeature = new XFeature(new XLineSpatial(_Vertexes), null); //暂时默认无属性
                    linelayer.AddFeature(onefeature);                                      //给线图层添加对象
                }
                else if (featuretype == "Polygon")
                {
                    XFeature onefeature = new XFeature(new XPolygonSpatial(_Vertexes), null); //暂时默认无属性
                    polygonlayer.AddFeature(onefeature);                                      //给线图层添加对象
                }
            }
            //Console.WriteLine(minx.ToString(), miny.ToString(), maxx.ToString(), maxy.ToString());
            XExtent layersextent = new XExtent(new XVertex(minx, miny), new XVertex(maxx, maxy));

            layers.Add(polygonlayer);
            layers.Add(linelayer);
            layers.Add(pointlayer);
            foreach (XLayer ll in layers)
            {
                ll.Extent             = layersextent;//给三种layer赋上共同的extent
                ll.DrawAttributeOrNot = false;
            }
            sr.Close();
            return(layers);
        }