/// <summary> /// 地理坐标系转投影坐标系 /// </summary> public override void CoordTransGeo2Pro() { EVPoint point = CoordTransform.GeoToPro(new EVPoint(X, Y)); X = point.X; Y = point.Y; }
/// <summary> /// 投影坐标系转地理坐标系 /// </summary> public override void CoordTransPro2Geo() { EVPoint point = CoordTransform.ProToGeo(new EVPoint(X, Y)); X = point.X; Y = point.Y; }
/// <summary> /// 投影坐标系转地理坐标系 /// </summary> public override void CoordTransPro2Geo() { for (int i = 0; i < Points.Count; i++) { EVPoint point = CoordTransform.ProToGeo((EVPoint)Points[i]); Points.RemoveAt(i); Points.Insert(i, point); } }
/// <summary> /// 地理坐标系转投影坐标系 /// </summary> public override void CoordTransGeo2Pro() { for (int i = 0; i < Points.Count; i++) { EVPoint point = CoordTransform.GeoToPro((EVPoint)Points[i]); Points.RemoveAt(i); Points.Insert(i, point); } //更新边界盒 double[] border = BorderUtil.GetBorder(Points); Xmin = border[0]; Ymin = border[1]; Xmax = border[2]; Ymax = border[3]; }
/// <summary> /// 读取record数据 /// 当读取时文件流位置在第一个记录处 /// </summary> /// <param name="reader"></param> public void Read(BinaryReader reader) { //如果header为空先读取header if (mHeader == null) { mHeader.Read(reader); } if (mRecordDic != null) { mRecordDic.Clear(); } else { mRecordDic = new Dictionary <ulong, ShpData>(); } //目前只考虑点线面三种类型 switch (mHeader.GeoType) { case ShapeType.Point: while (reader.PeekChar() != -1) { EVPoint point = new EVPoint(); uint RecordNum = reader.ReadUInt32(); int DataLength = reader.ReadInt32(); ulong uRecordNum = ByteTransUtil.big2little((int)RecordNum); //读取第i个记录 //每一条的记录长度没有考虑RecordNum、DataLength 所以比实际长度少4字 int uDataLength = (int)ByteTransUtil.big2little(DataLength); //以字为单位,一字等于2字节,等于16位 长度不包括uRecordNum和uDataLength int geoType = reader.ReadInt32(); //每一个对象的类型 没有用! point.RecordNum = uRecordNum; point.DataLength = uDataLength; //固定10字 没有考虑RecordNum、DataLength 比实际长度少4字 point.GeoType = ShapeType.Point; point.X = reader.ReadDouble(); point.Y = reader.ReadDouble(); // ShpData item = new ShpData(); item.GeoShape = point; mRecordDic.Add(uRecordNum, item); } break; case ShapeType.PolyLine: while (reader.PeekChar() != -1) { EVPolyLine polyline = new EVPolyLine(); polyline.Parts = new ArrayList(); polyline.Points = new ArrayList(); uint RecordNum = reader.ReadUInt32(); //读取第i个记录 从1开始算 ulong uRecordNum = ByteTransUtil.big2little((int)RecordNum); int DataLength = reader.ReadInt32(); //以字为单位,一字等于2字节,等于16位 //每一条的记录长度没有考虑RecordNum、DataLength 所以比实际长度少4字 int uDataLength = (int)ByteTransUtil.big2little(DataLength); int geoType = reader.ReadInt32();//每一个对象的类型 没有用 polyline.RecordNum = uRecordNum; polyline.DataLength = uDataLength; //没有考虑RecordNum、DataLength 比实际长度少4字 polyline.GeoType = ShapeType.PolyLine; polyline.Xmin = reader.ReadDouble(); polyline.Ymin = reader.ReadDouble(); polyline.Xmax = reader.ReadDouble(); polyline.Ymax = reader.ReadDouble(); polyline.NumParts = reader.ReadInt32(); polyline.NumPoints = reader.ReadInt32(); for (int i = 0; i < polyline.NumParts; i++) { int parts = new int(); parts = reader.ReadInt32(); polyline.Parts.Add(parts); } for (int j = 0; j < polyline.NumPoints; j++) { EVPoint pointtemp = new EVPoint(); pointtemp.X = reader.ReadDouble(); pointtemp.Y = reader.ReadDouble(); polyline.Points.Add(pointtemp); } ShpData item = new ShpData(); item.GeoShape = polyline; mRecordDic.Add(uRecordNum, item); } break; case ShapeType.Polygon: while (reader.PeekChar() != -1) { EVPolygon polygon = new EVPolygon(); polygon.Parts = new ArrayList(); polygon.Points = new ArrayList(); uint RecordNum = reader.ReadUInt32(); int DataLength = reader.ReadInt32(); ulong uRecordNum = ByteTransUtil.big2little((int)RecordNum); //读取第i个记录 //每一条的记录长度没有考虑RecordNum、DataLength 所以比实际长度少4字 //以字为单位,一字等于2字节,等于16位 int uDataLength = (int)ByteTransUtil.big2little(DataLength); int geoType = reader.ReadInt32(); polygon.RecordNum = uRecordNum; polygon.DataLength = uDataLength; polygon.GeoType = ShapeType.Polygon; polygon.Xmin = reader.ReadDouble(); polygon.Ymin = reader.ReadDouble(); polygon.Xmax = reader.ReadDouble(); polygon.Ymax = reader.ReadDouble(); polygon.NumParts = reader.ReadInt32(); polygon.NumPoints = reader.ReadInt32(); for (int j = 0; j < polygon.NumParts; j++) { int parts = new int(); parts = reader.ReadInt32(); polygon.Parts.Add(parts); } for (int j = 0; j < polygon.NumPoints; j++) { EVPoint pointtemp = new EVPoint(); pointtemp.X = reader.ReadDouble(); pointtemp.Y = reader.ReadDouble(); polygon.Points.Add(pointtemp); } ShpData item = new ShpData(); item.GeoShape = polygon; mRecordDic.Add(uRecordNum, item); } break; } }
/// <summary> /// 求包围盒 /// </summary> private void GetBorder() { mHeader.Zmin = 0; mHeader.Zmax = 0; mHeader.Mmin = 0; mHeader.Mmax = 0; double Xmin = 0; double Ymin = 0; double Xmax = 0; double Ymax = 0; switch (mHeader.GeoType) { case ShapeType.Point: EVPoint pP = mRecordDic[1].GeoShape as EVPoint; Xmin = pP.X; Ymin = pP.Y; Xmax = pP.X; Ymax = pP.Y; for (ulong i = 0; i < (ulong)mRecordDic.Count; i++) { EVPoint pont = mRecordDic[i + 1].GeoShape as EVPoint; if (Xmin > pont.X) { Xmin = pont.X; } if (Xmax < pont.X) { Xmax = pont.X; } if (Ymin > pont.Y) { Ymin = pont.Y; } if (Ymax < pont.Y) { Ymax = pont.Y; } } mHeader.Xmin = Xmin; mHeader.Ymin = Ymin; mHeader.Xmax = Xmax; mHeader.Ymax = Ymax; break; case ShapeType.PolyLine: EVPolyLine pL = mRecordDic[1].GeoShape as EVPolyLine; Xmin = pL.Xmin; Ymin = pL.Ymin; Xmax = pL.Xmax; Ymax = pL.Ymax; for (ulong i = 0; i < (ulong)mRecordDic.Count; i++) { EVPolyLine line = mRecordDic[i + 1].GeoShape as EVPolyLine; if (Xmin > line.Xmin) { Xmin = line.Xmin; } if (Xmax < line.Xmax) { Xmax = line.Xmax; } if (Ymin > line.Ymin) { Ymin = line.Ymin; } if (Ymax < line.Ymax) { Ymax = line.Ymax; } } mHeader.Xmin = Xmin; mHeader.Ymin = Ymin; mHeader.Xmax = Xmax; mHeader.Ymax = Ymax; break; case ShapeType.Polygon: EVPolygon pG = mRecordDic[1].GeoShape as EVPolygon; Xmin = pG.Xmin; Ymin = pG.Ymin; Xmax = pG.Xmax; Ymax = pG.Ymax; for (ulong i = 0; i < (ulong)mRecordDic.Count; i++) { EVPolygon gon = mRecordDic[i + 1].GeoShape as EVPolygon; if (Xmin > gon.Xmin) { Xmin = gon.Xmin; } if (Xmax < gon.Xmax) { Xmax = gon.Xmax; } if (Ymin > gon.Ymin) { Ymin = gon.Ymin; } if (Ymax < gon.Ymax) { Ymax = gon.Ymax; } } mHeader.Xmin = Xmin; mHeader.Ymin = Ymin; mHeader.Xmax = Xmax; mHeader.Ymax = Ymax; break; } }
/// <summary> /// 写入record记录 /// 先判断是否获取wkt信息以及写入header /// 写入的同时 计算shx的记录内容 /// </summary> /// <param name="writer"></param> /// <returns>返回shx的文件内容</returns> public Dictionary <ulong, ShxData> Write(BinaryWriter writer) { Dictionary <ulong, ShxData> shxDic = new Dictionary <ulong, ShxData>(); if (isGetWKTInfo) { if (writer.BaseStream.Position == 0) { mHeader.Write(writer); } int offset = 50;//跳过头文件 switch (mHeader.GeoType) { case ShapeType.Point: for (int i = 0; i < mRecordDic.Count; i++) { EVPoint point = mRecordDic[(ulong)(i + 1)].GeoShape as EVPoint; ShxData shx = new ShxData(); shx.ContentLength = point.DataLength; shx.Offset = offset; offset += shx.ContentLength + 4; shxDic.Add(point.RecordNum, shx); //写入shp byte[] lbtRecorderNum = BitConverter.GetBytes(point.RecordNum); byte[] bbtRecorderNum = ByteTransUtil.little2big(lbtRecorderNum); writer.Write(bbtRecorderNum); byte[] lbtDataLength = BitConverter.GetBytes(point.DataLength); byte[] bbtDataLength = ByteTransUtil.little2big(lbtDataLength); writer.Write(bbtDataLength); writer.Write((int)ShapeType.Point); writer.Write(point.X); writer.Write(point.Y); } break; case ShapeType.PolyLine: for (int i = 0; i < mRecordDic.Count; i++) { EVPolyLine line = mRecordDic[(ulong)(i + 1)].GeoShape as EVPolyLine; ShxData shx = new ShxData(); shx.ContentLength = line.DataLength; shx.Offset = offset; offset += shx.ContentLength + 4; shxDic.Add(line.RecordNum, shx); //写入shp byte[] lbtRecorderNum = BitConverter.GetBytes(line.RecordNum); byte[] bbtRecorderNum = ByteTransUtil.little2big(lbtRecorderNum); writer.Write(bbtRecorderNum); byte[] lbtDataLength = BitConverter.GetBytes(line.DataLength); byte[] bbtDataLength = ByteTransUtil.little2big(lbtDataLength); writer.Write(bbtDataLength); writer.Write((int)line.GeoType); writer.Write(line.Xmin); writer.Write(line.Ymin); writer.Write(line.Xmax); writer.Write(line.Ymax); writer.Write(line.NumParts); writer.Write(line.NumPoints); for (int j = 0; j < line.NumParts; j++) { writer.Write((int)line.Parts[j]); } for (int j = 0; j < line.NumPoints; j++) { EVPoint point = line.Points[j] as EVPoint; writer.Write(point.X); writer.Write(point.Y); } } break; case ShapeType.Polygon: for (int i = 0; i < mRecordDic.Count; i++) { EVPolygon gon = mRecordDic[(ulong)(i + 1)].GeoShape as EVPolygon; ShxData shx = new ShxData(); shx.ContentLength = gon.DataLength; shx.Offset = offset; offset += shx.ContentLength + 4; shxDic.Add(gon.RecordNum, shx); //写入shp byte[] lbtRecorderNum = BitConverter.GetBytes(gon.RecordNum); byte[] bbtRecorderNum = ByteTransUtil.little2big(lbtRecorderNum); writer.Write(bbtRecorderNum); byte[] lbtDataLength = BitConverter.GetBytes(gon.DataLength); byte[] bbtDataLength = ByteTransUtil.little2big(lbtDataLength); writer.Write(bbtDataLength); writer.Write((int)gon.GeoType); writer.Write(gon.Xmin); writer.Write(gon.Ymin); writer.Write(gon.Xmax); writer.Write(gon.Ymax); writer.Write(gon.NumParts); writer.Write(gon.NumPoints); for (int j = 0; j < gon.NumParts; j++) { writer.Write((int)gon.Parts[j]); } for (int j = 0; j < gon.NumPoints; j++) { EVPoint point = gon.Points[j] as EVPoint; writer.Write(point.X); writer.Write(point.Y); } } break; } } return(shxDic); }
/// <summary> /// 导入wkt /// </summary> /// <param name="wkt"></param> public override void TransFormWKT(string wkt) { if (wkt.Contains("POLYGON")) { //清除当前 if (Parts != null) { Parts.Clear(); } else { Parts = new ArrayList(); } if (Points != null) { Points.Clear(); } else { Points = new ArrayList(); } Regex coordinateGroupPattern = new Regex("[0-9.]+ [0-9., ]+"); MatchCollection coordinateGroupMatch = coordinateGroupPattern.Matches(wkt); List <string> value = new List <string>(); //如果大于0说明正常 大于1说明有内环 第一个为多边形外环 if (coordinateGroupMatch.Count != 0) { //只考虑外环,内含舍去 //foreach (Match m in coordinateGroupMatch) //{ // value.Add(m.Value); //} char[] sc = { ',' }; char[] scpoint = { ' ' }; string[] cors = coordinateGroupMatch[0].Value.Split(sc, StringSplitOptions.RemoveEmptyEntries);//coordinateGroupMatch[0].Value.Split(','); if (cors.Length > 0) { for (int i = 0; i < cors.Length; i++) { EVPoint point = new EVPoint(); string[] xy = cors[i].Split(scpoint, StringSplitOptions.RemoveEmptyEntries); //cors[i].Split(' '); if (xy.Length < 2) //是2或者三 { continue; } else { point.X = Convert.ToDouble(xy[0]); point.Y = Convert.ToDouble(xy[1]); Points.Add(point); } } } } } if (Points.Count > 0) { Parts.Add(0); NumParts = 1; NumPoints = Points.Count; //求边界盒 double[] border = BorderUtil.GetBorder(Points); Xmin = border[0]; Ymin = border[1]; Xmax = border[2]; Ymax = border[3]; this.GeoType = ShapeType.Polygon; //this.RecordNum //放在外围控制 //记录长度 每条长度不算RecorderNum和ContentLength的4字 NumParts固定为1 this.DataLength = 24 + 8 * NumPoints;//字数 1字=2byte=16位 } else { Parts.Add(0); NumParts = 0; NumPoints = 0; Xmin = 0; Ymin = 0; Xmax = 0; Ymax = 0; this.GeoType = ShapeType.Polygon; this.DataLength = 24 + 8 * NumPoints;//字数 1字=2byte=16位 } }