private void DrawCutomFeature(ref Graphics g, ref Matrix mTransMatrix, Shape _Shape, ref ArrayList oFICol, PointF DrawPoint) { int i; int j; int iSpacer = 2; GraphicsPath gpShp = new GraphicsPath(); StringFormat _StringFormat = new StringFormat(); _StringFormat.Alignment = StringAlignment.Center; _StringFormat.LineAlignment = StringAlignment.Center; _StringFormat.FormatFlags = StringFormatFlags.NoClip; FeatureInfo ofi; float iSymbolWidth = 0; float iSymbolOffset; PointF SymPoint = DrawPoint; float iLabelOffset = 0; SizeF szChar; for (i = 0; i <= oFICol.Count - 1; i++) { ofi = (FeatureInfo)oFICol[i]; if (ofi.SymbolSet == true) { if ((ofi.SymbolImage == null)) { szChar = g.MeasureString(ofi.SymbolChar.ToString(), ofi.SymbolFont); iSymbolWidth += szChar.Width + iSpacer; iLabelOffset = Math.Max(iLabelOffset, szChar.Height / 2); } else { iSymbolWidth += ofi.SymbolImage.Width + iSpacer; iLabelOffset = Math.Max(iLabelOffset, ofi.SymbolImage.Height / 2); } } } iSymbolOffset = iSymbolWidth / 2; SymPoint.X = SymPoint.X - iSymbolOffset; for (i = 0; i <= oFICol.Count - 1; i++) { ofi = (FeatureInfo)oFICol[i]; //*** Render Symbol if (ofi.SymbolSet == true) { _StringFormat.Alignment = StringAlignment.Near; _StringFormat.LineAlignment = StringAlignment.Center; if ((ofi.SymbolImage == null)) { SymPoint.Y = DrawPoint.Y; g.DrawString(ofi.SymbolChar.ToString(), ofi.SymbolFont, new SolidBrush(ofi.SymbolColor), SymPoint.X, SymPoint.Y, _StringFormat); SymPoint.X += g.MeasureString(ofi.SymbolChar.ToString(), ofi.SymbolFont).Width + iSpacer; } else { SymPoint.Y = DrawPoint.Y - ofi.SymbolImage.Height / 2; g.DrawImage(ofi.SymbolImage, SymPoint); SymPoint.X += ofi.SymbolImage.Width + iSpacer; } } //*** Render Label if (ofi.LabelSet == true) { //*** Place label on top of images _StringFormat.Alignment = StringAlignment.Center; _StringFormat.LineAlignment = StringAlignment.Far; g.DrawString(ofi.Caption, ofi.LabelFont, new SolidBrush(ofi.LabelColor), DrawPoint.X, DrawPoint.Y - iLabelOffset, _StringFormat); } //*** Render Fill Style if (ofi.FillSet == true) { gpShp.Reset(); for (j = 0; j <= _Shape.Parts.Count - 1; j++) { gpShp.AddPolygon((PointF[])_Shape.Parts[j]); } Pen PnFill = new Pen(ofi.BorderColor, ofi.BorderSize); Brush BrFill; if (ofi.BorderSize == 0) PnFill.Color = Color.Transparent; //??? Strange that setting Pen width = 0 gives effect of Pen width 1. So this workaround PnFill.DashStyle = ofi.BorderStyle; if (ofi.FillStyle == FillStyle.Solid) { BrFill = new SolidBrush(ofi.FillColor); } else if (ofi.FillStyle == FillStyle.Transparent) { BrFill = new SolidBrush(Color.Transparent); } else { BrFill = new HatchBrush((HatchStyle)ofi.FillStyle, ofi.FillColor, Color.Transparent); } gpShp.Transform(mTransMatrix); g.FillPath(BrFill, gpShp); try { g.DrawPath(PnFill, gpShp); } catch (Exception ex) { Debug.Write(ex.Message); //*** Gives error sometimes when Pen border size > 1 //http://msdn2.microsoft.com/en-us/library/ms969928.aspx } PnFill.Dispose(); PnFill = null; BrFill.Dispose(); BrFill = null; } } _StringFormat.Dispose(); gpShp.Dispose(); }
public string GetLabel(Layer _Layer, ref Shape _Shape, ref Theme _Theme, CustomLabel _CustomLabel) { Legend _Legend = null; return GetLabel(_Layer, ref _Shape, ref _Theme, _CustomLabel, ref _Legend); }
public string GetLabel(Layer _Layer, ref Shape _Shape, ref Theme _Theme, CustomLabel _CustomLabel, ref Legend _Legend) { int i; int j; string Label = ""; //*** BugFix 06 Feb 2006 No label field selected string Delimiter = " "; int IndentSpace = 3; char[] Fields; bool Indented; if ((_Theme == null)) { //*** Custom and Feature Layers if (_CustomLabel == null) { if (_Layer.LabelMultirow == true) Delimiter = "\r\n"; //initially it was: ControlChars.CrLf (line feed) Fields = _Layer.LabelField.Replace(",", "").ToCharArray(); } else { if (_CustomLabel.MultiRow == true) Delimiter = "\r\n"; //initially it was: ControlChars.CrLf (line feed) Fields = _CustomLabel.LabelField.Replace(",", "").ToCharArray(); } if (Array.IndexOf(Fields, "0"[0]) > -1) Label = _Shape.AreaId + Delimiter; if (Array.IndexOf(Fields, char.Parse("1")) > -1) { if (_Layer.LabelMultirow == true) { if (Label == "") { Label += _Shape.AreaName; } else { if (_Layer.LabelIndented == true) Label += new string(' ', IndentSpace); Label += _Shape.AreaName; } } else { Label += _Shape.AreaName; } } } else { //*** Base Layers if (_CustomLabel == null) { if (_Legend == null) { if (_Layer.LabelMultirow == true) Delimiter = "\r\n"; //initially it was: ControlChars.CrLf (line feed) Fields = _Layer.LabelField.Replace(",", "").ToCharArray(); Indented = _Layer.LabelIndented; } else { if (_Legend.LabelMultiRow == true) Delimiter = "\r\n"; //initially it was: ControlChars.CrLf (line feed) Fields = _Legend.LabelField.Replace(",", "").ToCharArray(); Indented = _Legend.LabelIndented; } } else { if (_CustomLabel.MultiRow == true) Delimiter = "\r\n"; //initially it was: ControlChars.CrLf (line feed) Fields = _CustomLabel.LabelField.Replace(",", "").ToCharArray(); Indented = _CustomLabel.Indent; } if (_Theme.AreaIndexes.ContainsKey(_Shape.AreaId)) { j = 0; for (i = 0; i <= Fields.Length - 1; i++) { switch (Fields[i]) { case '0': Label += _Shape.AreaId + Delimiter; j = j + 1; break; case '1': if (Indented == true) Label += new string(' ', j * IndentSpace); Label += ((AreaInfo)_Theme.AreaIndexes[_Shape.AreaId]).AreaName + Delimiter; j = j + 1; break; case '2': if (Indented == true) Label += new string(' ', j * IndentSpace); //Label += ((AreaInfo)_Theme.AreaIndexes[_Shape.AreaId]).DataValue + Delimiter; //Textual dataValue is also displayed on Map Label, so property AreaInfo.DisplayInfo is used. Label += ((AreaInfo)_Theme.AreaIndexes[_Shape.AreaId]).DisplayInfo + Delimiter; j = j + 1; break; case '3': if (Indented == true) Label += new string(' ', j * IndentSpace); Label += _Theme.UnitName + Delimiter; j = j + 1; break; case '4': if (Indented == true) Label += new string(' ', j * IndentSpace); //Label += _Theme.SubgroupName(0) & Delimiter //*** BugFix 21 Sep 2006 Subgroup label error Label += ((AreaInfo)_Theme.AreaIndexes[_Shape.AreaId]).Subgroup + Delimiter; j = j + 1; break; case '5': if (Indented == true) Label += new string(' ', j * IndentSpace); Label += ((AreaInfo)_Theme.AreaIndexes[_Shape.AreaId]).Time + Delimiter; break; } } Label = Label.Trim(Delimiter.ToCharArray()); } else { if (this._ShowLabelWhereDataExists) { //- If label is not required for Areas NOT having Data, // then set blank label. Label = string.Empty; } else { Hashtable AreaNames = _Layer.AreaNames; string AreaName; if (Array.IndexOf(Fields, "0"[0]) > -1) Label = _Shape.AreaId + Delimiter; if (Array.IndexOf(Fields, char.Parse("1")) > -1) { if (AreaNames.ContainsKey(_Shape.AreaId)) { //Get language specific AreaName from Database if it exists AreaName = (string)AreaNames[_Shape.AreaId]; } else { //Get Shapefile Area Name AreaName = _Shape.AreaName; } if (_Layer.LabelMultirow == true) { if (Label == "") { Label += AreaName; } else { if (_Layer.LabelIndented == true) Label += new string(' ', IndentSpace); Label += AreaName; } } else { Label += AreaName; } } } } } return Label; }
public Layer CreateBufferLayer(Layer SrcLayer, float BufferRadius) { int i; Layer BufferLayer = new Layer(); Shape SrcShape; IDictionaryEnumerator dicEnumerator = SrcLayer.GetRecords(SrcLayer.LayerPath + "\\" + SrcLayer.ID).GetEnumerator(); switch (SrcLayer.LayerType) { case ShapeType.Point: case ShapeType.PointCustom: case ShapeType.PointFeature: GraphicsPath TempGp = new GraphicsPath(); PointF Pt; while (dicEnumerator.MoveNext()) { Shape TempShp = new Shape(); SrcShape = (Shape)dicEnumerator.Value; Pt = (PointF)SrcShape.Parts[0]; TempShp.AreaId = SrcShape.AreaId; TempShp.AreaName = SrcShape.AreaName; TempShp.Centroid = Pt; PointF[] Pts = GetPointBuffer(Pt, BufferRadius); TempGp.Reset(); TempGp.AddPolygon(Pts); TempShp.Extent = TempGp.GetBounds(); TempShp.Parts.Add(Pts); BufferLayer.Records.Add(TempShp.AreaId, TempShp); if (BufferLayer.Extent.IsEmpty) { BufferLayer.Extent = TempShp.Extent; } else { BufferLayer.Extent = RectangleF.Union(BufferLayer.Extent, TempShp.Extent); } TempShp = null; } TempGp.Dispose(); break; case ShapeType.PolyLine: case ShapeType.PolyLineCustom: case ShapeType.PolyLineFeature: TempGp = new GraphicsPath(); while (dicEnumerator.MoveNext()) { Shape TempShp = new Shape(); SrcShape = (Shape)dicEnumerator.Value; TempShp.AreaId = SrcShape.AreaId; TempShp.AreaName = SrcShape.AreaName; //TempShp.Centroid = SrcShape.Parts(j) for (i = 0; i <= SrcShape.Parts.Count - 1; i++) { PointF[] Pts = (PointF[])SrcShape.Parts[i]; PointF[] PolyPts = GetLineBuffer(Pts, BufferRadius); TempShp.Centroid = Pts[(int)Pts.Length / 2]; TempShp.Parts.Add(PolyPts); TempGp.Reset(); TempGp.AddPolygon(PolyPts); if (TempShp.Extent.IsEmpty) { TempShp.Extent = TempGp.GetBounds(); } else { TempShp.Extent = RectangleF.Union(TempShp.Extent, TempGp.GetBounds()); } Pts = null; } BufferLayer.Records.Add(TempShp.AreaId, TempShp); if (BufferLayer.Extent.IsEmpty) { BufferLayer.Extent = TempShp.Extent; } else { BufferLayer.Extent = RectangleF.Union(BufferLayer.Extent, TempShp.Extent); } TempShp = null; } TempGp.Dispose(); break; } BufferLayer.LayerType = ShapeType.PolygonBuffer; BufferLayer.FillColor = Color.FromArgb(40, 255, 0, 0); BufferLayer.BorderColor = Color.Transparent; return BufferLayer; }
public Layer CreateBufferLayer(PointF[] moBufferPts, float BufferRadius, BufferStyle eBufferStyle, string BufferName) { int i; //*** BugFix 11 Feb 2006 Error on Double clicking on same point if (moBufferPts.Length > 1) { if (moBufferPts[moBufferPts.Length - 1].Equals(moBufferPts[moBufferPts.Length - 2])) { // ERROR: Not supported in C#: ReDimStatement } } //*** BugFix 02 Feb 2006 If user double click on first point itself for line buffer then create point buffer if (eBufferStyle == BufferStyle.Line & moBufferPts.Length == 1) eBufferStyle = BufferStyle.Point; //*** Transform Buffer Points to Latitude and Longitude for (i = 0; i <= moBufferPts.Length - 1; i++) { moBufferPts[i] = PointToClient(moBufferPts[i]); } //*** Create buffer Shape TempShp = new Shape(); Layer BufferLayer = new Layer(); GraphicsPath TempGp = new GraphicsPath(); PointF[] PolyPts; if (eBufferStyle == BufferStyle.Point) { //*** Point Buffer GraphicsPath LyrGp = new GraphicsPath(); for (i = 0; i <= moBufferPts.Length - 1; i++) { TempShp = new Shape(); TempShp.AreaId = BufferName + "_" + i + 1; TempShp.AreaName = BufferName + "_" + i + 1; TempShp.Centroid = moBufferPts[i]; PolyPts = GetPointBuffer(moBufferPts[i], BufferRadius); TempShp.Parts.Add(PolyPts); TempGp.Reset(); TempGp.AddPolygon(PolyPts); TempShp.Extent = TempGp.GetBounds(); BufferLayer.Records.Add(TempShp.AreaId, TempShp); LyrGp.AddPolygon(PolyPts); } BufferLayer.Extent = LyrGp.GetBounds(); LyrGp.Dispose(); } else { //*** Line Buffer TempShp.AreaId = BufferName; TempShp.AreaName = BufferName; TempShp.Centroid = moBufferPts[(int)moBufferPts.Length / 2]; PolyPts = GetLineBuffer(moBufferPts, BufferRadius); TempShp.Parts.Add(PolyPts); TempGp.AddPolygon(PolyPts); TempShp.Extent = TempGp.GetBounds(); BufferLayer.Extent = TempGp.GetBounds(); BufferLayer.Records.Add(TempShp.AreaId, TempShp); } TempGp.Dispose(); TempShp = null; BufferLayer.LayerType = ShapeType.PolygonBuffer; BufferLayer.FillColor = Color.FromArgb(40, 255, 0, 0); BufferLayer.BorderColor = Color.Transparent; return BufferLayer; }
public static void CreateShapefile(Layer _Layer, string FilePath, string FileName) { System.Text.Encoding FileEncoding = System.Text.Encoding.Default; FileHeader SHPFileHeader = new FileHeader(); FileStream oShpStream = new FileStream(FilePath + "\\" + FileName + ".shp", FileMode.Create); BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream); FileStream oShxStream = new FileStream(FilePath + "\\" + FileName + ".shx", FileMode.Create); BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream); FileStream oDbfStream = new FileStream(FilePath + "\\" + FileName + ".dbf", FileMode.Create); BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding); SHPFileHeader.FileCode = BigToLittleEndian(9994); SHPFileHeader.Version = 1000; switch (_Layer.LayerType) { case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Point: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PointCustom: SHPFileHeader.ShapeType = (int)ShapeType.Point; break; case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLine: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLineCustom: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLineFeature: SHPFileHeader.ShapeType = (int)ShapeType.PolyLine; break; case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Polygon: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonBuffer: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonCustom: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonFeature: SHPFileHeader.ShapeType = (int)ShapeType.Polygon; break; } SHPFileHeader.BoundingBoxXMin = _Layer.Extent.X; SHPFileHeader.BoundingBoxYMin = _Layer.Extent.Y; SHPFileHeader.BoundingBoxXMax = _Layer.Extent.X + _Layer.Extent.Width; SHPFileHeader.BoundingBoxYMax = _Layer.Extent.Y + _Layer.Extent.Height; ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader); //*** Write .Shp File Header ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader); //*** Write .Shx File Header DbaseFileHeaderWriter(ref oDbfBinaryWriter, _Layer.RecordCount, FileEncoding); //*** Write .dbf File Header int i; Shape _Shape = new Shape(); PointF[] Pts; int Offset = 50; int RecordNumber = 0; int ContentLength; int Points; IDictionaryEnumerator dicEnumerator = _Layer.Records.GetEnumerator(); switch (SHPFileHeader.ShapeType) { case 1: PointF Pt; ContentLength = 10; while (dicEnumerator.MoveNext()) { _Shape = (Shape)dicEnumerator.Value; Pt = (PointF)_Shape.Parts[0]; RecordNumber += 1; oShpBinaryWriter.Write(BigToLittleEndian(RecordNumber)); oShpBinaryWriter.Write(BigToLittleEndian(ContentLength)); oShpBinaryWriter.Write((int)1); //Shape Type oShpBinaryWriter.Write((double)Pt.X); oShpBinaryWriter.Write((double)Pt.Y); IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength); //*** Write SHX Record Offset += (4 + ContentLength); //*** Add 4 for record header '*** Set Offset for next SHX record DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName); //*** Write DBF Record } break; case 3: case 5: while (dicEnumerator.MoveNext()) { _Shape = (Shape)dicEnumerator.Value; int[] NumPoints = new int[_Shape.Parts.Count]; Points = 0; for (i = 0; i <= _Shape.Parts.Count - 1; i++) { Pts = (PointF[])_Shape.Parts[i]; NumPoints[i] = Pts.Length; Points += Pts.Length; } ContentLength = ((11 + _Shape.Parts.Count) * 2) + (Points * 8); RecordNumber += 1; PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, Points, NumPoints, _Shape, SHPFileHeader.ShapeType); IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength); //*** Write SHX Record Offset += (4 + ContentLength); //*** Add 4 for record header '*** Set Offset for next SHX record DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName); //*** Write DBF Record } break; } //*** Reset FileLength in Shp File Header oShpStream.Position = 24; oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2)); //*** Reset FileLength in Shx File Header oShxStream.Position = 24; oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2)); //*** Dispose oShpBinaryWriter.Close(); oShxBinaryWriter.Close(); oDbfBinaryWriter.Close(); oShpStream.Close(); oShxStream.Close(); oDbfStream.Close(); oShpBinaryWriter = null; oShxBinaryWriter = null; oDbfBinaryWriter = null; oShpStream = null; oShxStream = null; oDbfStream = null; dicEnumerator = null; }
public static void CreateBlockFile(string sBlockId, string sBlockName, Hashtable Area_Shp, string DestFilePath, string DestFileName, ShapeType ShapeType) { Hashtable Shp_Area = new Hashtable(); //- Get Encoding used in Current CultureInfo of OS. System.Text.Encoding FileEncoding = System.Text.Encoding.Default; //*** Build ShapeFile - AreaId Collection with unique Shape and comma delimited Areaids IDictionaryEnumerator dicEnumerator = Area_Shp.GetEnumerator(); while (dicEnumerator.MoveNext()) { if (Shp_Area.ContainsKey(dicEnumerator.Value)) { Shp_Area[dicEnumerator.Value] = Shp_Area[dicEnumerator.Value].ToString() + "," + dicEnumerator.Key.ToString(); } else { Shp_Area.Add(dicEnumerator.Value, dicEnumerator.Key); } } FileHeader SHPFileHeader = new FileHeader(); FileStream oShpStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shp", FileMode.Create); BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream); FileStream oShxStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shx", FileMode.Create); BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream); FileStream oDbfStream = new FileStream(DestFilePath + "\\" + DestFileName + ".dbf", FileMode.Create); BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding); SHPFileHeader.FileCode = BigToLittleEndian(9994); SHPFileHeader.Version = 1000; SHPFileHeader.ShapeType = (int)ShapeType; ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader); //*** Write .Shp File Header ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader); //*** Write .Shx File Header DbaseFileHeaderWriter(ref oDbfBinaryWriter, 1, FileEncoding); //Area_Shp.Count '*** Write .dbf File Header int i; int j; string[] AreaId; string SrcFile; string SrcFilePath; string SrcFileName; ShapeInfo ShpInfo; Shape _Shape = new Shape(); Shape _Block = new Shape(); RectangleF _Extent = new RectangleF(); PointF[] Pts; int Offset = 50; int RecordNumber; int ContentLength; int Points; int BlockPointCount = 0; int[] BlockPartOffset = null; // Iterate for all distinct shape files to // set single composite multipart polygon containing all distinct shapes in refrred shapefiles // set composite extent dicEnumerator = Shp_Area.GetEnumerator(); while (dicEnumerator.MoveNext()) { SrcFile = dicEnumerator.Key.ToString(); AreaId = dicEnumerator.Value.ToString().Split(","[0]); if (File.Exists(SrcFile)) { SrcFilePath = Path.GetDirectoryName(SrcFile); SrcFileName = Path.GetFileNameWithoutExtension(SrcFile); ShpInfo = GetShapeInfo(SrcFilePath, SrcFileName); switch (ShapeType) { case ShapeType.Point: //TODO break; case ShapeType.Polygon: case ShapeType.PolyLine: // Iterate for each shape IDictionaryEnumerator dicShapeEnumerator = ShpInfo.Records.GetEnumerator(); while (dicShapeEnumerator.MoveNext()) { _Shape = (Shape)dicShapeEnumerator.Value; if (_Extent.IsEmpty) { _Extent = _Shape.Extent; } else { _Extent = RectangleF.Union(_Extent, _Shape.Extent); } int iPrevPartsCount = _Block.Parts.Count; int[] temp = new int[iPrevPartsCount + _Shape.Parts.Count]; if (BlockPartOffset != null) Array.Copy(BlockPartOffset, temp, Math.Min(BlockPartOffset.Length, temp.Length)); BlockPartOffset = temp; Points = 0; // Iterate for each part for (i = 0; i <= _Shape.Parts.Count - 1; i++) { Pts = (PointF[])_Shape.Parts[i]; _Block.Parts.Add(Pts); BlockPartOffset[iPrevPartsCount + i] = Pts.Length; Points += Pts.Length; BlockPointCount += Pts.Length; } } break; } } } _Block.Extent = _Extent; RecordNumber = 1; //*** All Shape will be added as parts of a single block ContentLength = ((11 + _Block.Parts.Count) * 2) + (BlockPointCount * 8); PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, BlockPointCount, BlockPartOffset, _Block, SHPFileHeader.ShapeType); IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength); //*** Write SHX Record DbaseRecordWriter(ref oDbfBinaryWriter, sBlockId, sBlockName); //*** Write DBF Record if (ShapeType == ShapeType.Polygon | ShapeType == ShapeType.PolyLine) { SHPFileHeader.BoundingBoxXMin = _Extent.X; SHPFileHeader.BoundingBoxYMin = _Extent.Y; SHPFileHeader.BoundingBoxXMax = _Extent.X + _Extent.Width; SHPFileHeader.BoundingBoxYMax = _Extent.Y + _Extent.Height; } //*** Reset FileLength and Extent in Shp File Header oShpStream.Position = 24; oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2)); oShpStream.Position = 36; oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin); oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin); oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax); oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax); //*** Reset FileLength and Extent in Shx File Header oShxStream.Position = 24; oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2)); oShxStream.Position = 36; oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin); oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin); oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax); oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax); //*** Dispose oShpBinaryWriter.Close(); oShxBinaryWriter.Close(); oDbfBinaryWriter.Close(); oShpStream.Close(); oShxStream.Close(); oDbfStream.Close(); oShpBinaryWriter = null; oShxBinaryWriter = null; oDbfBinaryWriter = null; // _Extent = null; ShpInfo = null; }
//public static void CreateBlockFile(string sBlockId, string sBlockName, Hashtable Area_Shp, string DestFilePath, string DestFileName, ShapeType ShapeType) //{ // Hashtable Shp_Area = new Hashtable(); // //- Get Encoding used in Current CultureInfo of OS. // System.Text.Encoding FileEncoding = System.Text.Encoding.Default; // //*** Build ShapeFile - AreaId Collection with unique Shape and comma delimited Areaids // IDictionaryEnumerator dicEnumerator = Area_Shp.GetEnumerator(); // while (dicEnumerator.MoveNext()) // { // if (Shp_Area.ContainsKey(dicEnumerator.Value)) // { // Shp_Area[dicEnumerator.Value] = Shp_Area[dicEnumerator.Value].ToString() + "," + dicEnumerator.Key.ToString(); // } // else // { // Shp_Area.Add(dicEnumerator.Value, dicEnumerator.Key); // } // } // FileHeader SHPFileHeader = new FileHeader(); // FileStream oShpStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shp", FileMode.Create); // BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream); // FileStream oShxStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shx", FileMode.Create); // BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream); // FileStream oDbfStream = new FileStream(DestFilePath + "\\" + DestFileName + ".dbf", FileMode.Create); // BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding); // SHPFileHeader.FileCode = BigToLittleEndian(9994); // SHPFileHeader.Version = 1000; // SHPFileHeader.ShapeType = (int)ShapeType; // ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader); // //*** Write .Shp File Header // ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader); // //*** Write .Shx File Header // DbaseFileHeaderWriter(ref oDbfBinaryWriter, 1, FileEncoding); // //Area_Shp.Count '*** Write .dbf File Header // int i; // int j; // string[] AreaId; // string SrcFile; // string SrcFilePath; // string SrcFileName; // ShapeInfo ShpInfo; // Shape _Shape = new Shape(); // Shape _Block = new Shape(); // RectangleF _Extent = new RectangleF(); // PointF[] Pts; // int Offset = 50; // int RecordNumber; // int ContentLength; // int Points; // int BlockPointCount = 0; // int[] BlockPartOffset = null; // dicEnumerator = Shp_Area.GetEnumerator(); // while (dicEnumerator.MoveNext()) // { // SrcFile = dicEnumerator.Key.ToString(); // AreaId = dicEnumerator.Value.ToString().Split(","[0]); // if (File.Exists(SrcFile)) // { // SrcFilePath = Path.GetDirectoryName(SrcFile); // SrcFileName = Path.GetFileNameWithoutExtension(SrcFile); // ShpInfo = GetShapeInfo(SrcFilePath, SrcFileName); // switch (ShapeType) // { // case ShapeType.Point: // break; // case ShapeType.Polygon: // case ShapeType.PolyLine: // for (j = 0; j <= AreaId.Length - 1; j++) // { // if (ShpInfo.Records.ContainsKey(AreaId[j])) // { // _Shape = (Shape)ShpInfo.Records[AreaId[j]]; // if (_Extent.IsEmpty) // { // _Extent = _Shape.Extent; // } // else // { // _Extent = RectangleF.Union(_Extent, _Shape.Extent); // } // int iPrevPartsCount = _Block.Parts.Count; // //ReDim Preserve BlockPartOffset(iPrevPartsCount + _Shape.Parts.Count - 1) // //int[] YourArray; // //... // //int[] temp = new int[i + 1]; // //if (YourArray != null) // // Array.Copy(YourArray, temp, Math.Min(YourArray.Length, temp.Length)); // //YourArray = temp; // int[] temp = new int[iPrevPartsCount + _Shape.Parts.Count]; // if (BlockPartOffset != null) // Array.Copy(BlockPartOffset, temp, Math.Min(BlockPartOffset.Length, temp.Length)); // BlockPartOffset = temp; // Points = 0; // for (i = 0; i <= _Shape.Parts.Count - 1; i++) // { // Pts = (PointF[])_Shape.Parts[i]; // _Block.Parts.Add(Pts); // BlockPartOffset[iPrevPartsCount + i] = Pts.Length; // Points += Pts.Length; // BlockPointCount += Pts.Length; // } // } // } // break; // } // } // } // _Block.Extent = _Extent; // RecordNumber = 1; // //*** All Shape will be added as parts of a single block // ContentLength = ((11 + _Block.Parts.Count) * 2) + (BlockPointCount * 8); // PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, BlockPointCount, BlockPartOffset, _Block, SHPFileHeader.ShapeType); // IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength); // //*** Write SHX Record // DbaseRecordWriter(ref oDbfBinaryWriter, sBlockId, sBlockName); // //*** Write DBF Record // if (ShapeType == ShapeType.Polygon | ShapeType == ShapeType.PolyLine) // { // SHPFileHeader.BoundingBoxXMin = _Extent.X; // SHPFileHeader.BoundingBoxYMin = _Extent.Y; // SHPFileHeader.BoundingBoxXMax = _Extent.X + _Extent.Width; // SHPFileHeader.BoundingBoxYMax = _Extent.Y + _Extent.Height; // } // //*** Reset FileLength and Extent in Shp File Header // oShpStream.Position = 24; // oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2)); // oShpStream.Position = 36; // oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin); // oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin); // oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax); // oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax); // //*** Reset FileLength and Extent in Shx File Header // oShxStream.Position = 24; // oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2)); // oShxStream.Position = 36; // oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin); // oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin); // oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax); // oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax); // //*** Dispose // oShpBinaryWriter.Close(); // oShxBinaryWriter.Close(); // oDbfBinaryWriter.Close(); // oShpStream.Close(); // oShxStream.Close(); // oDbfStream.Close(); // oShpBinaryWriter = null; // oShxBinaryWriter = null; // oDbfBinaryWriter = null; // // _Extent = null; // ShpInfo = null; //} ////*** Create Shape File for selected polygon from multiple shape file //[Obsolete] //public static void CreateShapeFile(Hashtable Area_Shp, string DestFilePath, string DestFileName, ShapeType ShapeType) //{ // Hashtable Shp_Area = new Hashtable(); // System.Text.Encoding FileEncoding = System.Text.Encoding.Default; // //*** Build ShapeFile - AreaId Collection with unique Shape and comma delimited Areaids // IDictionaryEnumerator dicEnumerator = Area_Shp.GetEnumerator(); // while (dicEnumerator.MoveNext()) // { // if (Shp_Area.ContainsKey(dicEnumerator.Value)) // { // Shp_Area[dicEnumerator.Value] = Shp_Area[dicEnumerator.Value].ToString() + "," + dicEnumerator.Key.ToString(); // } // else // { // Shp_Area.Add(dicEnumerator.Value, dicEnumerator.Key); // } // } // FileHeader SHPFileHeader = new FileHeader(); // FileStream oShpStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shp", FileMode.Create); // BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream); // FileStream oShxStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shx", FileMode.Create); // BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream); // FileStream oDbfStream = new FileStream(DestFilePath + "\\" + DestFileName + ".dbf", FileMode.Create); // BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding); // SHPFileHeader.FileCode = BigToLittleEndian(9994); // SHPFileHeader.Version = 1000; // SHPFileHeader.ShapeType = (int)ShapeType; // ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader); // //*** Write .Shp File Header // ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader); // //*** Write .Shx File Header // DbaseFileHeaderWriter(ref oDbfBinaryWriter, Area_Shp.Count, FileEncoding); // //*** Write .dbf File Header // int i; // int j; // string[] AreaId; // string SrcFile; // string SrcFilePath; // string SrcFileName; // ShapeInfo ShpInfo; // Shape _Shape = new Shape(); // RectangleF _Extent = new RectangleF(); // PointF[] Pts; // int Offset = 50; // int RecordNumber = 0; // int ContentLength; // int Points; // dicEnumerator = Shp_Area.GetEnumerator(); // while (dicEnumerator.MoveNext()) // { // SrcFile = dicEnumerator.Key.ToString(); // AreaId = dicEnumerator.Value.ToString().Split(","[0]); // if (File.Exists(SrcFile)) // { // SrcFilePath = Path.GetDirectoryName(SrcFile); // SrcFileName = Path.GetFileNameWithoutExtension(SrcFile); // ShpInfo = GetShapeInfo(SrcFilePath, SrcFileName); // switch (ShapeType) // { // case ShapeType.Point: // PointF Pt; // ContentLength = 10; // for (j = 0; j <= AreaId.Length - 1; j++) // { // if (ShpInfo.Records.ContainsKey(AreaId[j])) // { // _Shape = (Shape)ShpInfo.Records[AreaId[j]]; // Pt = (PointF)_Shape.Parts[0]; // if (RecordNumber == 0) // { // SHPFileHeader.BoundingBoxXMin = Pt.X; // SHPFileHeader.BoundingBoxYMin = Pt.Y; // SHPFileHeader.BoundingBoxXMax = Pt.X; // SHPFileHeader.BoundingBoxYMax = Pt.Y; // } // else // { // SHPFileHeader.BoundingBoxXMin = Math.Min(SHPFileHeader.BoundingBoxXMin, Pt.X); // SHPFileHeader.BoundingBoxYMin = Math.Min(SHPFileHeader.BoundingBoxYMin, Pt.Y); // SHPFileHeader.BoundingBoxXMax = Math.Max(SHPFileHeader.BoundingBoxXMax, Pt.X); // SHPFileHeader.BoundingBoxYMax = Math.Max(SHPFileHeader.BoundingBoxYMax, Pt.Y); // } // RecordNumber += 1; // oShpBinaryWriter.Write(BigToLittleEndian(RecordNumber)); // oShpBinaryWriter.Write(BigToLittleEndian(ContentLength)); // oShpBinaryWriter.Write((int)1); // //Shape Type // oShpBinaryWriter.Write((double)Pt.X); // oShpBinaryWriter.Write((double)Pt.Y); // IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength); // //*** Write SHX Record // Offset += (4 + ContentLength); // //*** Add 4 for record header '*** Set Offset for next SHX record // DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName); // //*** Write DBF Record // } // } // // Pt = null; // break; // case ShapeType.Polygon: // case ShapeType.PolyLine: // for (j = 0; j <= AreaId.Length - 1; j++) // { // if (ShpInfo.Records.ContainsKey(AreaId[j])) // { // _Shape = (Shape)ShpInfo.Records[AreaId[j]]; // if (_Extent.IsEmpty) // { // _Extent = _Shape.Extent; // } // else // { // _Extent = RectangleF.Union(_Extent, _Shape.Extent); // } // int[] NumPoints = new int[_Shape.Parts.Count]; // Points = 0; // for (i = 0; i <= _Shape.Parts.Count - 1; i++) // { // Pts = (PointF[])_Shape.Parts[i]; // NumPoints[i] = Pts.Length; // Points += Pts.Length; // } // ContentLength = ((11 + _Shape.Parts.Count) * 2) + (Points * 8); // RecordNumber += 1; // PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, Points, NumPoints, _Shape, SHPFileHeader.ShapeType); // IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength); // //*** Write SHX Record // Offset += (4 + ContentLength); // //*** Add 4 for record header '*** Set Offset for next SHX record // DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName); // //*** Write DBF Record // } // } // break; // } // } // } // if (ShapeType == ShapeType.Polygon | ShapeType == ShapeType.PolyLine) // { // SHPFileHeader.BoundingBoxXMin = _Extent.X; // SHPFileHeader.BoundingBoxYMin = _Extent.Y; // SHPFileHeader.BoundingBoxXMax = _Extent.X + _Extent.Width; // SHPFileHeader.BoundingBoxYMax = _Extent.Y + _Extent.Height; // } // //*** Reset FileLength and Extent in Shp File Header // oShpStream.Position = 24; // oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2)); // oShpStream.Position = 36; // oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin); // oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin); // oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax); // oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax); // //*** Reset FileLength and Extent in Shx File Header // oShxStream.Position = 24; // oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2)); // oShxStream.Position = 36; // oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin); // oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin); // oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax); // oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax); // //*** Dispose // oShpBinaryWriter.Close(); // oShxBinaryWriter.Close(); // oDbfBinaryWriter.Close(); // oShpStream.Close(); // oShxStream.Close(); // oDbfStream.Close(); // oShpBinaryWriter = null; // oShxBinaryWriter = null; // oDbfBinaryWriter = null; // // _Extent = null; // ShpInfo = null; //} //[Obsolete] //public static void CreateShapeFile(Hashtable Area_Shp, string DestFilePath, string DestFileName) //{ // ShapeType ShapeType = ShapeType.Polygon; // CreateShapeFile(Area_Shp, DestFilePath, DestFileName, ShapeType); //} //*** Create Shape File for selected polygon from single shape file - Used for Split process public static void CreateShapeFile(string AreaIds, string SrcFilePath, string SrcFileName, string DestFilePath, string DestFileName) { ShapeInfo ShpInfo = GetShapeInfo(SrcFilePath, SrcFileName); string[] AreaId; AreaId = AreaIds.Split(","[0]); FileHeader SHPFileHeader = new FileHeader(); FileStream oShpStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shp", FileMode.Create); BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream); FileStream oShxStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shx", FileMode.Create); BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream); FileStream oDbfStream = new FileStream(DestFilePath + "\\" + DestFileName + ".dbf", FileMode.Create); //- Get Encoding used in source Shape file (.dbf) System.Text.Encoding FileEncoding = ShapeFileReader.GetEncodingInDBF(SrcFilePath + "\\" + SrcFileName + ".dbf"); BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding); //System.Text.Encoding.ASCII SHPFileHeader.FileCode = BigToLittleEndian(9994); SHPFileHeader.Version = 1000; switch (ShpInfo.ShapeType) { case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Point: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PointCustom: SHPFileHeader.ShapeType = (int)ShapeType.Point; break; case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLine: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLineCustom: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLineFeature: SHPFileHeader.ShapeType = (int)ShapeType.PolyLine; break; case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Polygon: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonBuffer: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonCustom: case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonFeature: SHPFileHeader.ShapeType = (int)ShapeType.Polygon; break; } ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader); //*** Write .Shp File Header ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader); //*** Write .Shx File Header DbaseFileHeaderWriter(ref oDbfBinaryWriter, AreaId.Length, FileEncoding); //*** Write .dbf File Header int i; int j; Shape _Shape = new Shape(); PointF[] Pts; int Offset = 50; int RecordNumber = 0; int ContentLength; int Points; switch (SHPFileHeader.ShapeType) { case 1: PointF Pt; ContentLength = 10; for (j = 0; j <= AreaId.Length - 1; j++) { if (ShpInfo.Records.ContainsKey(AreaId[j])) { _Shape = (Shape)ShpInfo.Records[AreaId[j]]; Pt = (PointF)_Shape.Parts[0]; if (j == 0) { SHPFileHeader.BoundingBoxXMin = Pt.X; SHPFileHeader.BoundingBoxYMin = Pt.Y; SHPFileHeader.BoundingBoxXMax = Pt.X; SHPFileHeader.BoundingBoxYMax = Pt.Y; } else { SHPFileHeader.BoundingBoxXMin = Math.Min(SHPFileHeader.BoundingBoxXMin, Pt.X); SHPFileHeader.BoundingBoxYMin = Math.Min(SHPFileHeader.BoundingBoxYMin, Pt.Y); SHPFileHeader.BoundingBoxXMax = Math.Max(SHPFileHeader.BoundingBoxXMax, Pt.X); SHPFileHeader.BoundingBoxYMax = Math.Max(SHPFileHeader.BoundingBoxYMax, Pt.Y); } RecordNumber += 1; oShpBinaryWriter.Write(BigToLittleEndian(RecordNumber)); oShpBinaryWriter.Write(BigToLittleEndian(ContentLength)); oShpBinaryWriter.Write((int)1); //Shape Type oShpBinaryWriter.Write((double)Pt.X); oShpBinaryWriter.Write((double)Pt.Y); IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength); //*** Write SHX Record Offset += (4 + ContentLength); //*** Add 4 for record header '*** Set Offset for next SHX record DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName); //*** Write DBF Record } } break; case 3: case 5: RectangleF _Extent = new RectangleF(); for (j = 0; j <= AreaId.Length - 1; j++) { if (ShpInfo.Records.ContainsKey(AreaId[j])) { _Shape = (Shape)ShpInfo.Records[AreaId[j]]; if (_Extent.IsEmpty) { _Extent = _Shape.Extent; } else { _Extent = RectangleF.Union(_Extent, _Shape.Extent); } int[] NumPoints = new int[_Shape.Parts.Count]; Points = 0; for (i = 0; i <= _Shape.Parts.Count - 1; i++) { Pts = (PointF[])_Shape.Parts[i]; NumPoints[i] = Pts.Length; Points += Pts.Length; } ContentLength = ((11 + _Shape.Parts.Count) * 2) + (Points * 8); RecordNumber += 1; PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, Points, NumPoints, _Shape, SHPFileHeader.ShapeType); IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength); //*** Write SHX Record Offset += (4 + ContentLength); //*** Add 4 for record header '*** Set Offset for next SHX record DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName); //*** Write DBF Record } } SHPFileHeader.BoundingBoxXMin = _Extent.X; SHPFileHeader.BoundingBoxYMin = _Extent.Y; SHPFileHeader.BoundingBoxXMax = _Extent.X + _Extent.Width; SHPFileHeader.BoundingBoxYMax = _Extent.Y + _Extent.Height; // _Extent = null; break; } //*** Reset FileLength and Extent in Shp File Header oShpStream.Position = 24; oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2)); oShpStream.Position = 36; oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin); oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin); oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax); oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax); //*** Reset FileLength and Extent in Shx File Header oShxStream.Position = 24; oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2)); oShxStream.Position = 36; oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin); oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin); oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax); oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax); //*** Dispose oShpBinaryWriter.Close(); oShxBinaryWriter.Close(); oDbfBinaryWriter.Close(); oShpStream.Close(); oShxStream.Close(); oDbfStream.Close(); oShpBinaryWriter = null; oShxBinaryWriter = null; oDbfBinaryWriter = null; ShpInfo = null; }
private static void PolygonRecordWriter(ref System.IO.BinaryWriter bWriter, int RecordNumber, int ContentLength, int Points, int[] NumPoints, Shape _Shape, int ShapeType) { int i; int j; PointF[] Pt; int Parts; //*** Write Record Header //Position Field Value Type Byte Order //Byte 0 Record Number Record Number Integer Big //Byte 4 Content Length Content Length Integer Big bWriter.Write(BigToLittleEndian(RecordNumber)); bWriter.Write(BigToLittleEndian(ContentLength)); //*** Write Record Content //Position Field Value Type Number Byte Order //Byte 0 Shape Type 5 Integer 1 Little //Byte 4 Box Box Double 4 Little //Byte 36 NumParts NumParts Integer 1 Little //Byte 40 NumPoints NumPoints Integer 1 Little //Byte 44 Parts Parts Integer NumParts Little //Byte X Points Points Point NumPoints Little bWriter.Write(ShapeType); //Shape Type bWriter.Write((double)_Shape.Extent.Left); //Bounding Box bWriter.Write((double)_Shape.Extent.Top); bWriter.Write((double)_Shape.Extent.Left + _Shape.Extent.Width); bWriter.Write((double)_Shape.Extent.Top + _Shape.Extent.Height); bWriter.Write(_Shape.Parts.Count); //NumParts bWriter.Write(Points); //NumPoints Parts = 0; bWriter.Write(Parts); //Parts for (i = 1; i <= NumPoints.Length - 1; i++) { Parts += NumPoints[i - 1]; bWriter.Write(Parts); //Parts } for (i = 0; i <= _Shape.Parts.Count - 1; i++) { Pt = (PointF[])_Shape.Parts[i]; for (j = 0; j <= Pt.Length - 1; j++) { bWriter.Write((double)Pt[j].X); //Points bWriter.Write((double)Pt[j].Y); } } }
public static ShapeInfo GetShapeInfo(string p_SourceDir, string p_FileName) { ShapeInfo RetVal = null; //-- Check for shape file set existence if (File.Exists(p_SourceDir + "\\" + p_FileName + ".shp") && File.Exists(p_SourceDir + "\\" + p_FileName + ".shx") && File.Exists(p_SourceDir + "\\" + p_FileName + ".dbf")) { FileHeader SHPFileHeader = new FileHeader(); SHPRecordHeader SHPRecordHeader = new SHPRecordHeader(); FileStream objSHPStream = null; BinaryReader objSHPBinaryReader = null; FileHeader SHXFileHeader = new FileHeader(); SHXRecordHeader SHXRecordHeader = new SHXRecordHeader(); FileStream objSHXStream = null; BinaryReader objSHXBinaryReader = null; FileStream dbfStream = null; BinaryReader dbfReader = null; System.Text.Encoding FileEncoding; // To hold encoding type on the basis of code Page value defined in .dbf header. int i; int j; int k; try { objSHPStream = File.OpenRead(p_SourceDir + "\\" + p_FileName + ".shp"); objSHPBinaryReader = new BinaryReader(objSHPStream); objSHXStream = File.OpenRead(p_SourceDir + "\\" + p_FileName + ".shx"); objSHXBinaryReader = new BinaryReader(objSHXStream); //-- get Encoding used to create dbf file. This PageCode information is in 29th position of dbfReader. FileEncoding = ShapeFileReader.GetEncodingInDBF(p_SourceDir + "\\" + p_FileName + ".dbf"); dbfStream = File.OpenRead(p_SourceDir + "\\" + p_FileName + ".dbf"); //dbfReader = new BinaryReader(dbfStream, System.Text.Encoding.GetEncoding(65001)); // 65001 is the code page for Unicode UTF-8 encoding taken as default' dbfReader = new BinaryReader(dbfStream, FileEncoding); SHPFileHeader = ReadHeaderInfo(ref objSHPBinaryReader); SHXFileHeader = ReadHeaderInfo(ref objSHXBinaryReader); ShapeInfo _ShapeInfo = new ShapeInfo(); _ShapeInfo.ID = p_FileName; _ShapeInfo.Extent = new RectangleF((float)SHPFileHeader.BoundingBoxXMin, (float)SHPFileHeader.BoundingBoxYMin, (float)Math.Abs(SHPFileHeader.BoundingBoxXMax - SHPFileHeader.BoundingBoxXMin), (float)Math.Abs(SHPFileHeader.BoundingBoxYMax - SHPFileHeader.BoundingBoxYMin)); _ShapeInfo.RecordCount = (int)(SHXFileHeader.FileLength - 50) / 4; _ShapeInfo.ShapeType = (DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType)SHPFileHeader.ShapeType; if ((int)(_ShapeInfo.ShapeType) == (int)(ShapeFileReader.ShapeType.PointZ)) { _ShapeInfo.ShapeType = DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Point; } //*** Read dbf header and set Id and Label Field string sFieldId = ""; string sFieldLabel = ""; byte dbfVersion = dbfReader.ReadByte(); //3 byte updateYear = dbfReader.ReadByte(); byte updateMonth = dbfReader.ReadByte(); byte updateDay = dbfReader.ReadByte(); Int32 numberRecords = dbfReader.ReadInt32(); // short headerLength = dbfReader.ReadInt16(); // short recordLength = dbfReader.ReadInt16(); //**** In Reserved Bytes, there is a byte (flag) at 29th postion of stream which represent code page information, that can be used to get Encoding type. dbfStream.Seek(29, SeekOrigin.Begin); // Seek to 29th postion to fetch code page . FileEncoding = GetDbaseEncodingType(dbfReader.ReadByte()); // Reads code page and gets the corresponding encoding type. dbfStream.Seek(32, SeekOrigin.Begin); // Move to past the reserved bytes. // byte[] reserved = dbfReader.ReadBytes(20); Int32 numberFields = (int)(headerLength - 33) / 32; DBF_Field_Header[] fieldHeaders = new DBF_Field_Header[numberFields]; string FieldName; for (i = 0; i <= numberFields - 1; i++) { //byte[] testBytes = FileEncoding.GetBytes(dbfReader.ReadChars(10)); ////dbfReader.BaseStream.Position -= 10; //testBytes = System.Text.Encoding.Convert(System.Text.Encoding.UTF8, FileEncoding, testBytes); try { char[] fieldNameChars = dbfReader.ReadChars(10); char fieldNameTerminator = dbfReader.ReadChar(); FieldName = new string(fieldNameChars).ToUpper(); fieldHeaders[i].FieldName = FieldName; fieldHeaders[i].FieldType = dbfReader.ReadChar(); if (FieldName.Length >= 3 && fieldHeaders[i].FieldName.Replace("\0","").Trim() == ID_FIELD_NAME) { if (FieldName[0] == 'I' & FieldName[1] == 'D' & FieldName[2] == '_') sFieldId = fieldHeaders[i].FieldName; } if (FieldName.Length >= 6 && fieldHeaders[i].FieldName.Replace("\0", "").Trim() == NAME1_FIELD_NAME) { if (FieldName[0] == 'N' & FieldName[1] == 'A' & FieldName[2] == 'M' & FieldName[3] == 'E' & FieldName[4] == '1' & FieldName[5] == '_') sFieldLabel = fieldHeaders[i].FieldName; } byte[] reserved1 = dbfReader.ReadBytes(4); fieldHeaders[i].FieldLength = dbfReader.ReadByte(); byte[] reserved2 = dbfReader.ReadBytes(15); } catch (Exception ex) { Console.Write(ex.Message); } } byte headerTerminator = dbfReader.ReadByte(); //13 if (sFieldId == "") sFieldId = fieldHeaders[0].FieldName; if (sFieldLabel == "") { if (fieldHeaders.Length > 1) { sFieldLabel = fieldHeaders[1].FieldName; } else { sFieldLabel = fieldHeaders[0].FieldName; } } int numParts; int numPoints; int[] theParts; PointF[] Pt; float Ydiff = 0; //*** Add Ydiff to make all Y coordinates positve for area calculation. if extent of polygon goes below equator //*** Special Handling for centroids of areas lying in 3rd quadrent //*** http://astronomy.swin.edu.au/~pbourke/geometry/polyarea/ //*** http://www.geog.ubc.ca/courses/klink/gis.notes/ncgia/u33.html //*** X = S ((y(i) - y(i+1)) (x(i)2 + x(i)x(i+1) + x(i+1)2)/6A) Y = S ((x(i+1) - x(i)) (y(i)2 + y(i)y(i+1) + y(i+1)2)/6A) where A is the area of the polygon //*** note: as with the area algorithm, the polygon must be coded clockwise and all y coordinates must be non- negative for (i = 0; i <= _ShapeInfo.RecordCount - 1; i++) { SHXRecordHeader.Offset = BigToLittleEndian(objSHXBinaryReader.ReadInt32()); SHXRecordHeader.ContentLength = BigToLittleEndian(objSHXBinaryReader.ReadInt32()); objSHPStream.Seek(SHXRecordHeader.Offset * 2, SeekOrigin.Begin); SHPRecordHeader.RecordNumber = BigToLittleEndian(objSHPBinaryReader.ReadInt32()); SHPRecordHeader.ContentLength = BigToLittleEndian(objSHPBinaryReader.ReadInt32()); objSHPBinaryReader.ReadInt32(); //ShapeType byte isValid = dbfReader.ReadByte(); //32 Shape _Shape = new Shape(); RectangleF _Extent = new RectangleF(); { for (j = 0; j <= fieldHeaders.Length - 1; j++) { char[] fieldDataChars; if (fieldHeaders[j].FieldName == sFieldId) { //Fetch AreaID from .dbf file using default encoding which is set to UTF-8. fieldDataChars = dbfReader.ReadChars(fieldHeaders[j].FieldLength); _Shape.AreaId = new string(fieldDataChars).Trim(); } else if (fieldHeaders[j].FieldName == sFieldLabel) { //Fetch the value from "NAME1_" column in its original encoded characters using extracted encoding type. fieldDataChars = FileEncoding.GetString(dbfReader.ReadBytes(fieldHeaders[j].FieldLength)).Replace("\0", "").Trim().ToCharArray(); _Shape.AreaName = new string(fieldDataChars).Trim(); } else { fieldDataChars = dbfReader.ReadChars(fieldHeaders[j].FieldLength); } //string fieldData = new string(fieldDataChars).Trim(); // unused statement } switch (SHPFileHeader.ShapeType) { case (int)ShapeType.Point: case (int)(ShapeFileReader.ShapeType.PointZ): PointF _Pt = new PointF(); _Pt.X = (float)objSHPBinaryReader.ReadDouble(); _Pt.Y = (float)objSHPBinaryReader.ReadDouble(); _Shape.Parts.Add(_Pt); break; case (int)ShapeType.PolyLine: _Extent.X = (float)objSHPBinaryReader.ReadDouble(); //Byte 04 Minx _Extent.Y = (float)objSHPBinaryReader.ReadDouble(); //Byte 12 MinY _Extent.Width = (float)Math.Abs(_Extent.X - objSHPBinaryReader.ReadDouble()); //Byte 20 MaxX _Extent.Height = (float)Math.Abs(_Extent.Y - objSHPBinaryReader.ReadDouble()); //Byte 28 MaxY _Shape.Extent = _Extent; numParts = objSHPBinaryReader.ReadInt32(); //Byte 36 Num Parts numPoints = objSHPBinaryReader.ReadInt32(); //Byte 40 Num Points theParts = new int[numParts]; for (j = 0; j <= numParts - 1; j++) { theParts[j] = objSHPBinaryReader.ReadInt32(); } for (j = 0; j <= numParts - 1; j++) { if ((j != numParts - 1)) { Pt = new PointF[theParts[j + 1] - theParts[j]]; for (k = theParts[j]; k <= theParts[j + 1] - 1; k++) { Pt[k - theParts[j]].X = (float)objSHPBinaryReader.ReadDouble(); Pt[k - theParts[j]].Y = (float)objSHPBinaryReader.ReadDouble(); } } else { Pt = new PointF[numPoints - theParts[j]]; for (k = theParts[j]; k <= numPoints - 1; k++) { Pt[k - theParts[j]].X = (float)objSHPBinaryReader.ReadDouble(); Pt[k - theParts[j]].Y = (float)objSHPBinaryReader.ReadDouble(); } } _Shape.Parts.Add(Pt); } break; case (int)ShapeType.Polygon: //*** Bugfix 27 Sep 2006 Centroid calulation for very small polygon 'Mayanmar problem double second_factor; //*** For centroid calculation double[] polygon_area; //*** SignedPolygonArea double[] Cx; double[] Cy; _Extent.X = (float)objSHPBinaryReader.ReadDouble(); //Byte 04 Minx _Extent.Y = (float)objSHPBinaryReader.ReadDouble(); //Byte 12 MinY _Extent.Width = (float)Math.Abs(_Extent.X - objSHPBinaryReader.ReadDouble()); //Byte 20 MaxX _Extent.Height = (float)Math.Abs(_Extent.Y - objSHPBinaryReader.ReadDouble()); //Byte 28 MaxY _Shape.Extent = _Extent; if (_Extent.Y < 0) Ydiff = -(_Extent.Y); numParts = objSHPBinaryReader.ReadInt32(); //Byte 36 Num Parts numPoints = objSHPBinaryReader.ReadInt32(); //Byte 40 Num Points theParts = new int[numParts]; for (j = 0; j <= numParts - 1; j++) { theParts[j] = objSHPBinaryReader.ReadInt32(); } Cx = new double[numParts]; Cy = new double[numParts]; polygon_area = new double[numParts]; for (j = 0; j <= numParts - 1; j++) { if ((j != numParts - 1)) { Pt = new PointF[theParts[j + 1] - theParts[j]]; for (k = theParts[j]; k <= theParts[j + 1] - 1; k++) { Pt[k - theParts[j]].X = (float)objSHPBinaryReader.ReadDouble(); Pt[k - theParts[j]].Y = (float)objSHPBinaryReader.ReadDouble(); if (k > theParts[j]) { polygon_area[j] = polygon_area[j] + (Pt[k - theParts[j]].X - Pt[k - theParts[j] - 1].X) * ((Pt[k - theParts[j]].Y + Ydiff) + (Pt[k - theParts[j] - 1].Y + Ydiff)) / 2; second_factor = Pt[k - theParts[j] - 1].X * (Pt[k - theParts[j]].Y + Ydiff) - Pt[k - theParts[j]].X * (Pt[k - theParts[j] - 1].Y + Ydiff); Cx[j] = Cx[j] + (Pt[k - theParts[j] - 1].X + Pt[k - theParts[j]].X) * second_factor; Cy[j] = Cy[j] + ((Pt[k - theParts[j] - 1].Y + Ydiff) + (Pt[k - theParts[j]].Y + Ydiff)) * second_factor; } } } else { Pt = new PointF[numPoints - theParts[j]]; for (k = theParts[j]; k <= numPoints - 1; k++) { Pt[k - theParts[j]].X = (float)objSHPBinaryReader.ReadDouble(); Pt[k - theParts[j]].Y = (float)objSHPBinaryReader.ReadDouble(); if (k > theParts[j]) { polygon_area[j] = polygon_area[j] + (Pt[k - theParts[j]].X - Pt[k - theParts[j] - 1].X) * ((Pt[k - theParts[j]].Y + Ydiff) + (Pt[k - theParts[j] - 1].Y + Ydiff)) / 2; second_factor = Pt[k - theParts[j] - 1].X * (Pt[k - theParts[j]].Y + Ydiff) - Pt[k - theParts[j]].X * (Pt[k - theParts[j] - 1].Y + Ydiff); Cx[j] = Cx[j] + (Pt[k - theParts[j] - 1].X + Pt[k - theParts[j]].X) * second_factor; Cy[j] = Cy[j] + ((Pt[k - theParts[j] - 1].Y + Ydiff) + (Pt[k - theParts[j]].Y + Ydiff)) * second_factor; } } } _Shape.Parts.Add(Pt); if (polygon_area[j] == 0) { Cx[j] = 0; //*** Indonesia problem Cy[j] = 0; } else { Cx[j] = Cx[j] / 6 / polygon_area[j]; // Divide by 6 times the polygon's area. Cy[j] = Cy[j] / 6 / polygon_area[j]; } } double SumCx = 0; double SumCy = 0; double SumA = 0; for (j = 0; j <= numParts - 1; j++) { SumCx = SumCx + Cx[j] * polygon_area[j]; SumCy = SumCy + Cy[j] * polygon_area[j]; SumA = SumA + polygon_area[j]; } SumCx = SumCx / SumA; SumCy = SumCy / SumA; if (SumCx < 0 | SumCy < 0) { //*** If the values are negative, the polygon is oriented counterclockwise. Reverse the signs. SumCx = -SumCx; SumCy = -SumCy; } _Shape.Centroid = new PointF((float)SumCx, (float)SumCy - Ydiff); break; } _ShapeInfo.Records.Add(_Shape.AreaId, _Shape); _Shape = null; } } RetVal = _ShapeInfo; } catch (Exception ex) { Console.Write(ex.Message); } finally { //-- Deallocate the memory used for garbage collector to remove the memory utilized objSHPStream.Close(); objSHPStream = null; objSHXStream.Close(); objSHXStream = null; dbfStream.Close(); dbfStream = null; objSHPBinaryReader.Close(); objSHPBinaryReader = null; objSHXBinaryReader.Close(); objSHXBinaryReader = null; dbfReader.Close(); dbfReader = null; // SHPFileHeader = null; // SHXFileHeader = null; // SHXRecordHeader = null; // SHPRecordHeader = null; } } return RetVal; }