/// <summary> /// Read a shapefile record. /// </summary> /// <param name="stream">Input stream.</param> public ShapeFileRecord ReadShapeFileRecord(Stream stream) { ShapeFileRecord record = new ShapeFileRecord(); // Record Header. record.RecordNumber = ShapeFile.ReadInt32_BE(stream); record.ContentLength = ShapeFile.ReadInt32_BE(stream); // Shape Type. record.ShapeType = ShapeFile.ReadInt32_LE(stream); // Read the shape geometry, depending on its type. switch (record.ShapeType) { case (int)ShapeType.NullShape: // Do nothing. break; case (int)ShapeType.Point: ShapeFile.ReadPoint(stream, record); break; case (int)ShapeType.PolyLine: // PolyLine has exact same structure as Polygon in shapefile. ShapeFile.ReadPolygon(stream, record); break; case (int)ShapeType.Polygon: ShapeFile.ReadPolygon(stream, record); break; case (int)ShapeType.Multipoint: ShapeFile.ReadMultipoint(stream, record); break; default: { string msg = String.Format(System.Globalization.CultureInfo.InvariantCulture, "ShapeType {0} is not supported.", (int)record.ShapeType); throw new ApplicationException(msg); } } // Add the record to our internal list. this.records.Add(record); return(record); }
/// <summary> /// Read a shapefile MultiPoint record. /// </summary> /// <param name="stream">Input stream.</param> /// <param name="record">Shapefile record to be updated.</param> private static void ReadMultipoint(Stream stream, ShapeFileRecord record) { // Bounding Box. record.XMin = ShapeFile.ReadDouble64_LE(stream); record.YMin = ShapeFile.ReadDouble64_LE(stream); record.XMax = ShapeFile.ReadDouble64_LE(stream); record.YMax = ShapeFile.ReadDouble64_LE(stream); // Num Points. int numPoints = ShapeFile.ReadInt32_LE(stream); // Points. for (int i = 0; i < numPoints; i++) { PointF p = new PointF(); p.X = (float)ShapeFile.ReadDouble64_LE(stream); p.Y = (float)ShapeFile.ReadDouble64_LE(stream); record.Points.Add(p); } }
private void HandlePolygon(StringBuilder sb, float xOffset, float yOffset, ShapeFileRecord sfr) { // we'll use this key for all the polygons StringBuilder keys = new StringBuilder(); keys.Append("<Keys>"); int len = sfr.Attributes.ItemArray.GetLength(0); for (int j = 0; j < len; j++) { string key = sfr.Attributes.ItemArray[j].ToString(); if (key.Length == 0) { continue; } try { float.Parse(key); continue; } catch { } // not a number keys.Append(key); if (j + 1 < len) { keys.Append(", "); } } if (keys.ToString().EndsWith(", ")) { keys.Remove(keys.Length - 2, 2); } keys.Append("</Keys>"); string skeys = keys.ToString(); for (int i = 0; i < sfr.NumberOfParts; i++) { sb.Append("<Polygon>"); sb.Append("<Points>"); int oldx = int.MaxValue; int oldy = int.MaxValue; int cp = 0; // Determine the starting index and the end index // into the points array that defines the figure. int start = sfr.Parts[i]; int end; if (sfr.NumberOfParts > 1 && i != (sfr.NumberOfParts - 1)) { end = sfr.Parts[i + 1]; } else { end = sfr.NumberOfPoints; } // Add line segments to the figure. for (int j = start; j < end; j++) { PointF ll = sfr.Points[j]; //PointF ll = MercatorConversion(sfr.Points[j]); int x = (int)((ll.X + xOffset) * 4); int y = (int)(((-ll.Y) + yOffset) * 4); if (x == oldx && y == oldy) // we're truncating the data so some points are redundant { continue; } cp++; oldx = x; oldy = y; sb.AppendFormat("{0},{1}", x, y); if (j + 1 < sfr.Points.Count) { sb.Append(","); } } if (cp == 1) { sb.AppendFormat(",{0},{1}", oldx, oldy); } sb.Append("</Points>"); sb.Append(skeys); sb.Append("</Polygon>"); } }
private void HandlePolygon(StringBuilder sb, float xOffset, float yOffset, ShapeFileRecord sfr) { // we'll use this key for all the polygons StringBuilder keys = new StringBuilder(); keys.Append("<Keys>"); int len = sfr.Attributes.ItemArray.GetLength(0); for (int j = 0; j < len; j++) { string key = sfr.Attributes.ItemArray[j].ToString(); if (key.Length == 0) continue; try { float.Parse(key); continue; } catch { } // not a number keys.Append(key); if (j + 1 < len) keys.Append(", "); } if (keys.ToString().EndsWith(", ")) keys.Remove(keys.Length - 2, 2); keys.Append("</Keys>"); string skeys = keys.ToString(); for (int i = 0; i < sfr.NumberOfParts; i++) { sb.Append("<Polygon>"); sb.Append("<Points>"); int oldx = int.MaxValue; int oldy = int.MaxValue; int cp = 0; // Determine the starting index and the end index // into the points array that defines the figure. int start = sfr.Parts[i]; int end; if (sfr.NumberOfParts > 1 && i != (sfr.NumberOfParts - 1)) end = sfr.Parts[i + 1]; else end = sfr.NumberOfPoints; // Add line segments to the figure. for (int j = start; j < end; j++) { PointF ll = sfr.Points[j]; //PointF ll = MercatorConversion(sfr.Points[j]); int x = (int)((ll.X + xOffset) * 4); int y = (int)(((-ll.Y) + yOffset) * 4); if (x == oldx && y == oldy) // we're truncating the data so some points are redundant continue; cp++; oldx = x; oldy = y; sb.AppendFormat("{0},{1}", x, y); if (j + 1 < sfr.Points.Count) sb.Append(","); } if (cp == 1) sb.AppendFormat(",{0},{1}", oldx, oldy); sb.Append("</Points>"); sb.Append(skeys); sb.Append("</Polygon>"); } }
/// <summary> /// Read a shapefile Polygon record. /// </summary> /// <param name="stream">Input stream.</param> /// <param name="record">Shapefile record to be updated.</param> private static void ReadPolygon(Stream stream, ShapeFileRecord record) { // Bounding Box. record.XMin = ShapeFile.ReadDouble64_LE(stream); record.YMin = ShapeFile.ReadDouble64_LE(stream); record.XMax = ShapeFile.ReadDouble64_LE(stream); record.YMax = ShapeFile.ReadDouble64_LE(stream); // Num Parts and Points. int numParts = ShapeFile.ReadInt32_LE(stream); int numPoints = ShapeFile.ReadInt32_LE(stream); // Parts. for (int i = 0; i < numParts; i++) { record.Parts.Add(ShapeFile.ReadInt32_LE(stream)); } // Points. for (int i = 0; i < numPoints; i++) { PointF p = new PointF(); p.X = (float)ShapeFile.ReadDouble64_LE(stream); p.Y = (float)ShapeFile.ReadDouble64_LE(stream); record.Points.Add(p); } }
/// <summary> /// Read a shapefile Point record. /// </summary> /// <param name="stream">Input stream.</param> /// <param name="record">Shapefile record to be updated.</param> private static void ReadPoint(Stream stream, ShapeFileRecord record) { // Points - add a single point. PointF p = new PointF(); p.X = (float)ShapeFile.ReadDouble64_LE(stream); p.Y = (float)ShapeFile.ReadDouble64_LE(stream); record.Points.Add(p); // Bounding Box. record.XMin = p.X; record.YMin = p.Y; record.XMax = record.XMin; record.YMax = record.YMin; }
/// <summary> /// Read a shapefile record. /// </summary> /// <param name="stream">Input stream.</param> public ShapeFileRecord ReadShapeFileRecord(Stream stream) { ShapeFileRecord record = new ShapeFileRecord(); // Record Header. record.RecordNumber = ShapeFile.ReadInt32_BE(stream); record.ContentLength = ShapeFile.ReadInt32_BE(stream); // Shape Type. record.ShapeType = ShapeFile.ReadInt32_LE(stream); // Read the shape geometry, depending on its type. switch (record.ShapeType) { case (int)ShapeType.NullShape: // Do nothing. break; case (int)ShapeType.Point: ShapeFile.ReadPoint(stream, record); break; case (int)ShapeType.PolyLine: // PolyLine has exact same structure as Polygon in shapefile. ShapeFile.ReadPolygon(stream, record); break; case (int)ShapeType.Polygon: ShapeFile.ReadPolygon(stream, record); break; case (int)ShapeType.Multipoint: ShapeFile.ReadMultipoint(stream, record); break; default: { string msg = String.Format(System.Globalization.CultureInfo.InvariantCulture, "ShapeType {0} is not supported.", (int)record.ShapeType); throw new ApplicationException(msg); } } // Add the record to our internal list. this.records.Add(record); return record; }