Skip to content

Instantly share code, notes, and snippets.

@lansalot
Created December 24, 2022 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lansalot/bc2a0da7bee786269ff5950fb13a065e to your computer and use it in GitHub Desktop.
Save lansalot/bc2a0da7bee786269ff5950fb13a065e to your computer and use it in GitHub Desktop.
rivate void btnSelectShapeFile_Click(object sender, EventArgs e)
{
if (dlgShapefile.ShowDialog(this) == DialogResult.OK)
{
shapeFileName = dlgShapefile.FileName;
if (shapeFileName != null)
{
using (Shapefile shapefile = new Shapefile(shapeFileName))
foreach (Shape shape in shapefile)
{
switch (shape.Type)
{
case ShapeType.Point:
// a point is just a single x/y point
ShapePoint shapePoint = shape as ShapePoint;
//Console.WriteLine("Point={0},{1}", shapePoint.Point.X, shapePoint.Point.Y);
break;
case ShapeType.Polygon:
// a polygon contains one or more parts - each part is a list of points which
// are clockwise for boundaries and anti-clockwise for holes
// see http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
ShapePolygon shapePolygon = shape as ShapePolygon;
foreach (PointD[] part in shapePolygon.Parts)
{
var points = new List<GeoPoint>();
var polygon = new Polygon(PolygonStyle.Default);
foreach (PointD point in part)
{
points.Add(new GeoPoint((float)point.X, (float)point.Y));
}
polygon.AddRange(points);
mapControl.Polygons.Add(polygon);
}
break;
default:
// and so on for other types...
break;
}
mapControl.Refresh();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment