示例#1
0
        public XLayer ReadFile(string filename)
        {
            FileStream   fsr = new FileStream(filename, FileMode.Open);
            BinaryReader br  = new BinaryReader(fsr);
            //读文件头
            MyFileHeader mfh = (MyFileHeader)XTools.FromBytes(br, typeof(MyFileHeader));
            //读图层名称
            string name = XTools.ReadString(br);
            //读属性字段结构
            List <XField> fields = ReadFields(mfh.FieldCount, br);
            //定义图层
            SHAPETYPE ShapeType =
                (SHAPETYPE)Enum.Parse(typeof(SHAPETYPE),
                                      mfh.ShapeType.ToString());
            XExtent extent = new XExtent(
                new XVertex(mfh.MinX, mfh.MinY),
                new XVertex(mfh.MaxX, mfh.MaxY));
            XLayer layer = new XLayer(name, ShapeType, extent, fields);

            //读空间对象类型
            for (int i = 0; i < mfh.FeatureCount; i++)
            {
                XSpatial   spatial   = ReadSpatial(ShapeType, br);
                XAttribute attribute = ReadAttribute(br, fields);
                layer.AddFeature(new XFeature(spatial, attribute));
            }
            //关闭文件并返回结果
            br.Close();
            fsr.Close();
            return(layer);
        }
示例#2
0
        public XLayer ReadShapefile(string shpfilename)
        {
            //打开文件和读取工具
            FileStream   fsr = new FileStream(shpfilename, FileMode.Open);
            BinaryReader br  = new BinaryReader(fsr);
            //读取文件头
            ShapefileHeader sfh = (ShapefileHeader)
                                  XTools.FromBytes(br, typeof(ShapefileHeader));
            //获得空间对象类型
            SHAPETYPE ShapeType = (SHAPETYPE)Enum.Parse(typeof(SHAPETYPE), sfh.ShapeType.ToString());
            //获得空间范围
            XExtent extent = new XExtent(new XVertex(sfh.Xmin, sfh.Ymin),
                                         new XVertex(sfh.Xmax, sfh.Ymax));
            //读dbf
            string            dbfFileName = shpfilename.Replace(".shp", ".dbf");
            List <XField>     fields      = new List <XField>();
            List <XAttribute> attributes  = new List <XAttribute>();

            ReadDBFFile(dbfFileName, fields, attributes);
            //构建图层
            XLayer layer = new XLayer(shpfilename, ShapeType, extent, fields);
            int    index = 0;

            while (br.PeekChar() != -1)
            {
                //读记录头
                RecordHeader rh = (RecordHeader)
                                  XTools.FromBytes(br, typeof(RecordHeader));
                int    RecordLength  = FromBigToLittle(rh.RecordLength) * 2 - 4;
                byte[] RecordContent = br.ReadBytes(RecordLength);
                //开始读实际的空间数据
                if (ShapeType == SHAPETYPE.point)
                {
                    XPointSpatial point = ReadPoint(RecordContent);
                    layer.AddFeature(new XFeature(point, attributes[index]));
                }
                else if (ShapeType == SHAPETYPE.line)
                {
                    List <XLineSpatial> lines = ReadLines(RecordContent);
                    foreach (XLineSpatial line in lines)
                    {
                        layer.AddFeature(new XFeature(line, new XAttribute(attributes[index])));
                    }
                }
                else if (ShapeType == SHAPETYPE.polygon)
                {
                    List <XPolygonSpatial> polygons = ReadPolygons(RecordContent);
                    foreach (XPolygonSpatial polygon in polygons)
                    {
                        layer.AddFeature(new XFeature(polygon, new XAttribute(attributes[index])));
                    }
                }
                index++;
            }
            //关闭读取工具和文件
            br.Close();
            fsr.Close();
            return(layer);
        }
示例#3
0
        void WriteFileHeader(XLayer layer, BinaryWriter bw)
        {
            MyFileHeader mfh = new MyFileHeader();

            mfh.MinX         = layer.Extent.GetMinX();
            mfh.MinY         = layer.Extent.GetMinY();
            mfh.MaxX         = layer.Extent.GetMaxX();
            mfh.MaxY         = layer.Extent.GetMaxY();
            mfh.FeatureCount = layer.FeatureCount();
            mfh.ShapeType    = (int)(layer.ShapeType);
            mfh.FieldCount   = layer.Fields.Count;
            bw.Write(XTools.ToBytes(mfh));
        }
示例#4
0
        private void BReadMyFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            XMyFile myFile = new XMyFile();

            layer = myFile.ReadFile(openFileDialog.FileName);
            MessageBox.Show("读入" + layer.FeatureCount() + "个实体");
            view.UpdateExtent(layer.Extent);
            UpdateMap();
        }
示例#5
0
 public FormAttribute(XLayer _Layer)
 {
     InitializeComponent();
     for (int i = 0; i < _Layer.Fields.Count; i++)
     {
         dgvAttribute.Columns.Add(_Layer.Fields[i].name, _Layer.Fields[i].name);
     }
     for (int i = 0; i < _Layer.FeatureCount(); i++)
     {
         dgvAttribute.Rows.Add();
         for (int j = 0; j < _Layer.Fields.Count; j++)
         {
             dgvAttribute.Rows[i].Cells[j].Value = _Layer.GetFeature(i).GetAttribute(j);
         }
     }
 }
示例#6
0
 void ReadJSONField(Newtonsoft.Json.Linq.JToken properties, XLayer layer)
 {
     if (properties != null)
     {
         foreach (Newtonsoft.Json.Linq.JProperty fa in properties)
         {
             //Newtonsoft.Json.Linq.JProperty fieldname = (Newtonsoft.Json.Linq.JProperty)fa.ElementAt(0);
             //var fieldname = (string)fa.ElementAt(0);
             string name     = fa.Name;
             XField newfield = new XField(typeof(String), name);
             if (!(layer.Fields.Contains(newfield)))//图层若不存在该field则加入该field
             {
                 layer.Fields.Add(newfield);
             }
         }
     }
 }
示例#7
0
        private void BOpenShapeFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter           = "Shapefile文件|*.shp";
            openFileDialog.RestoreDirectory = false;
            openFileDialog.FilterIndex      = 1;
            openFileDialog.Multiselect      = false;
            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            XShapeFile sf = new XShapeFile();

            layer = sf.ReadShapefile(openFileDialog.FileName);
            layer.DrawAttributeOrNot = false;
            //MessageBox.Show("读入" + layer.FeatureCount() + "个实体");
            view.UpdateExtent(layer.Extent);
            UpdateMap();
        }
示例#8
0
        public void WriteFile(XLayer layer, string filename)
        {
            FileStream   fsr = new FileStream(filename, FileMode.Create);
            BinaryWriter bw  = new BinaryWriter(fsr);

            //写文件头
            WriteFileHeader(layer, bw);
            //写图层名称
            XTools.WriteString(layer.Name, bw);
            //写属性字段结构
            foreach (XField field in layer.Fields)
            {
                XTools.WriteString(field.datatype.ToString(), bw);
                XTools.WriteString(field.name, bw);
            }
            //写空间对象类型
            foreach (XFeature f in layer.Features)
            {
                WriteFeature(f, bw);
            }
            //关闭文件
            bw.Close();
            fsr.Close();
        }
示例#9
0
        public void WriteJSONFile(XLayer layer, string filename)
        {
            FileStream   fsr    = new FileStream(filename, FileMode.Create);
            StreamWriter writer = new StreamWriter(fsr);

            writer.Write("{");
            writer.Write("\"type\": \"" + "FeatureCollection" + "\",");
            writer.Write("\"features\": [");
            //开始写入所有的feature
            foreach (XFeature feature in layer.Features)
            {
                writer.Write("{");
                writer.Write("\"type\": \"" + "Feature" + "\",");
                //写字段与属性
                writer.Write("\"properties\": {");
                for (int fcount = 0; fcount < layer.Fields.Count; fcount++)
                {
                    writer.Write("\"" + layer.Fields[fcount].name + "\": \"" +
                                 (feature.Attribute.Values[fcount]).ToString() + "\"");
                    if (fcount != layer.Fields.Count - 1)
                    {
                        writer.Write(",");
                    }
                }

                writer.Write("},");
                //写地理信息
                writer.Write("\"geometry\": {");
                string ftype = null;
                if ((int)layer.ShapeType == 1)
                {
                    ftype = "Point";
                }
                else if ((int)layer.ShapeType == 3)
                {
                    ftype = "LineString";
                }
                else if ((int)layer.ShapeType == 5)
                {
                    ftype = "Polygon";
                }

                writer.Write("\"type\": \"" + ftype + "\",");
                writer.Write("\"coordinates\": [");
                if ((int)layer.ShapeType == 1)//如果是点,则按照写点坐标的方式写
                {
                    writer.Write((feature.Spatial.Centroid.X).ToString() + ","
                                 + (feature.Spatial.Centroid.Y).ToString());
                }
                else if ((int)layer.ShapeType == 3)//如果是线,则按照写线中点坐标的方式写
                {
                    for (int i = 0; i < ((XLineSpatial)feature.Spatial).AllVertexes.Count; i++)
                    {
                        writer.Write("[" + (((XLineSpatial)feature.Spatial).AllVertexes[i].X).ToString() + ","
                                     + (((XLineSpatial)feature.Spatial).AllVertexes[i].Y).ToString() + "]");
                        if (i < ((XLineSpatial)feature.Spatial).AllVertexes.Count - 1)
                        {
                            writer.Write(",");
                        }
                    }
                }
                else if ((int)layer.ShapeType == 5)//如果是面,则按照写面中点坐标的方式写
                {
                    writer.Write("[");
                    for (int i = 0; i < ((XPolygonSpatial)feature.Spatial).AllVertexes.Count; i++)
                    {
                        writer.Write("[" + (((XPolygonSpatial)feature.Spatial).AllVertexes[i].X).ToString() + ","
                                     + (((XPolygonSpatial)feature.Spatial).AllVertexes[i].Y).ToString() + "]");
                        if (i < ((XPolygonSpatial)feature.Spatial).AllVertexes.Count - 1)
                        {
                            writer.Write(",");
                        }
                    }
                    writer.Write("]");
                }

                writer.Write("]");

                writer.Write("}");

                writer.Write("}");
                if (feature != layer.Features[layer.Features.Count - 1])
                {
                    writer.Write(",");
                }
            }

            writer.Write("]");
            writer.Write("}");
            writer.Close();
            fsr.Close();
        }
示例#10
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);
        }