-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.cs
105 lines (102 loc) · 4.4 KB
/
example.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
void LoadDBF()
{
string path = @"city.dbf";
ShapeDBF sd = new ShapeDBF(path);
DataTable dbfTable = sd.GetInfoFromDBF();
}
void DrawFeatures()
{
string path = @"New_Shapefile.dbf";
ShapeFile sfh = new ShapeFile(path);
sfh.ReadHeader();
sfh.ReadRecord();
//将图层中的所有几何对象绘制到当前绘制区域内
Graphics g = this.CreateGraphics();//这里用Form来绘制几何对象
int width = this.ClientRectangle.Width;
int height = this.ClientRectangle.Height;
double layerWidth = sfh.Envelope.East - sfh.Envelope.West;
double layerHeight = sfh.Envelope.North - sfh.Envelope.South;
System.Diagnostics.Debug.WriteLine(sfh.Envelope.West + "," + sfh.Envelope.East + ","
+ sfh.Envelope.South + "," + sfh.Envelope.North);
Brush polygonBrush = new SolidBrush(Color.Chocolate);//多边形填充色
Pen pen = new Pen(Color.Red);//点和线的颜色
foreach(ShapeRecord sr in sfh.Shapes)
{
if(sr.Polygon!=null)
{
List<PointF> points = sr.Polygon.Points.ToList().Select(item =>
{
PointF point = new PointF();
point.X = (float)((item.X - sfh.Envelope.West) * width / layerWidth);
point.Y = (float)((item.Y - sfh.Envelope.South) * height / layerHeight);
point.Y = (float)height - point.Y;
return point;
}).ToList();
g.FillPolygon(polygonBrush, points.ToArray());
}
else if(sr.Point!=null)
{
PointF point = new PointF();
point.X = (float)((sr.Point.X - sfh.Envelope.West) * width / layerWidth);
point.Y = (float)((sr.Point.Y - sfh.Envelope.South) * height / layerHeight);
point.Y = (float)height - point.Y;
g.FillEllipse(polygonBrush, point.X, point.Y, 2, 2);
}
else if(sr.MultiPoint!=null)
{
sr.MultiPoint.Points.ToList().ForEach(item =>
{
PointF point = new PointF();
point.X = (float)((item.X - sfh.Envelope.West) * width / layerWidth);
point.Y = (float)((item.Y - sfh.Envelope.South) * height / layerHeight);
point.Y = (float)height - point.Y;
g.FillEllipse(polygonBrush, point.X, point.Y, 2, 2);
});
}
else if(sr.PolyLine!=null)
{
if(sr.PolyLine.NumParts==1)
{
List<PointF> points = sr.PolyLine.Points.ToList().Select(item =>
{
PointF point = new PointF();
point.X = (float)((item.X - sfh.Envelope.West) * width / layerWidth);
point.Y = (float)((item.Y - sfh.Envelope.South) * height / layerHeight);
point.Y = (float)height - point.Y;
return point;
}).ToList();
g.DrawLines(pen, points.ToArray());
}
else if(sr.PolyLine.NumParts>1)
{
for(int i=0;i<sr.PolyLine.Parts.Length;i++)
{
List<PointF> points = null;
if (i<sr.PolyLine.Parts.Length-1)
{
points= sr.PolyLine.Points.ToList().GetRange(sr.PolyLine.Parts[i], sr.PolyLine.Parts[i + 1] - sr.PolyLine.Parts[i]).Select(item =>
{
PointF point = new PointF();
point.X = (float)((item.X - sfh.Envelope.West) * width / layerWidth);
point.Y = (float)((item.Y - sfh.Envelope.South) * height / layerHeight);
point.Y = (float)height - point.Y;
return point;
}).ToList();
}
else
{
points = sr.PolyLine.Points.ToList().GetRange(sr.PolyLine.Parts[i], sr.PolyLine.Points.Count() - sr.PolyLine.Parts[i]).Select(item =>
{
PointF point = new PointF();
point.X = (float)((item.X - sfh.Envelope.West) * width / layerWidth);
point.Y = (float)((item.Y - sfh.Envelope.South) * height / layerHeight);
point.Y = (float)height - point.Y;
return point;
}).ToList();
}
g.DrawLines(pen, points.ToArray());
}
}
}
}
}