C#实现SHP文件读取与地图显示的完整教程
作者:almighty27
在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平移缩放操作以及属性表展示,需要的朋友可以参考下
概述
在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式。本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平移缩放操作以及属性表展示。
功能特点
- SHP文件读取与解析
- 点、线、面要素的绘制
- 地图平移与缩放功能
- 属性数据表格展示
- 地图元素选择高亮
核心代码解析

1. 文件读取与初始化
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 清空现有数据
if (listPolygonshp != null) listPolygonshp.Clear();
if (listPointShp != null) listPointShp.Clear();
location = new PointF((this.Width - score) / 2, 25);
// 打开文件对话框
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "shp文件|*.shp";
if (ofd.ShowDialog() == DialogResult.Cancel) return;
// 读取SHP文件
kl.OpenShpFile(ofd.FileName);
myds = kl.GetDataSet();//填充数据集
// 显示文件头信息
textBox1.Text = "";
textBox1.Text += "文件代码:" + kl.GetFileCode() + "\r\n"
+ "文件长度:" + kl.GetFileLength() + "\r\n"
// ... 其他文件头信息
}2. 坐标转换
SHP文件使用地理坐标系,而屏幕使用像素坐标系,需要进行转换:
private void transform(PointF[] points, int lenth)
{
float width = (float)(kl.GetXMax() - kl.GetXMin());
float height = (float)(kl.GetYMax() - kl.GetYMin());
float wh = width / height;
for (int i = 0; i < points.Length; i++)
{
points[i].X = (this.Width - lenth) / 2 +
(points[i].X - (float)kl.GetXMin()) / width * lenth * wh;
points[i].Y = lenth - (points[i].Y - (float)kl.GetYMin()) / height * lenth;
}
}3. 图形绘制
public void DisplayShp()
{
Bitmap bp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bp);
// 绘制点要素
foreach (PointShp p in listPointShp)
{
SolidBrush bru = new SolidBrush(Color.Green);
g.FillEllipse(bru, p.pf.X, p.pf.Y, 10, 10);
// 添加标注
g.DrawString(p.label, new Font("宋体", 9), Brushes.Black,
new PointF(p.pf.X - 25, p.pf.Y));
}
// 绘制面要素
foreach (PolygonShp ls in listPolygonshp)
{
Pen pen = new Pen(Color.Black, 2);
g.DrawPolygon(pen, ls.Points);
// 计算标注位置(面中心)
float w = 0, h = 0;
foreach (PointF pf in ls.Points)
{
w += pf.X;
h += pf.Y;
}
g.DrawString(ls.label, new Font("宋体", 9), Brushes.Green,
new PointF(w / ls.Points.Length - 25, h / ls.Points.Length));
}
pictureBox1.Image = bp;
g.Dispose();
}4. 地图交互功能

缩放功能
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
float zoomnum = e.Delta > 0 ? 1.1f : 0.9f;
foreach(PolygonShp ls in listPolygonshp)
{
zoom(ls.Points, zoomnum);
}
foreach (PointShp p in listPointShp)
{
PointF[] pos = new PointF[1] { p.pf };
zoom(pos, zoomnum);
p.pf = pos[0];
}
DisplayShp();
}
private void zoom(PointF[] points, float zoomnum)
{
for(int i = 0; i < points.Length; i++)
{
points[i].X = location.X + (points[i].X - location.X) * zoomnum;
points[i].Y = location.Y + (points[i].Y - location.Y) * zoomnum;
}
}平移功能
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(moving)
{
foreach(PolygonShp ls in listPolygonshp)
{
for(int i = 0; i < ls.Points.Length; i++)
{
ls.Points[i].X += e.X - xStart;
ls.Points[i].Y += e.Y - yStart;
}
}
foreach (PointShp p in listPointShp)
{
p.pf.X += e.X - xStart;
p.pf.Y += e.Y - yStart;
}
xStart = e.X;
yStart = e.Y;
DisplayShp();
}
}5. 属性表展示

public void FillProptyTable()
{
dataGridView1.DataSource = myds.Tables[0];
}使用说明
- 点击"打开"菜单选择SHP文件
- 查看"地图概况"选项卡了解文件基本信息
- 在"属性表"选项卡查看要素属性数据
- 使用鼠标滚轮进行地图缩放
- 按住鼠标左键拖动进行地图平移
- 点击属性表行头可选择对应地图要素
总结
本文介绍了如何使用C#实现SHP文件的读取和地图显示功能。关键点包括:
- 正确处理地理坐标到屏幕坐标的转换
- 实现点、线、面要素的绘制
- 添加地图交互功能(平移、缩放)
- 展示属性数据并与图形要素关联
以上就是C#实现SHP文件读取与地图显示的完整教程的详细内容,更多关于C# SHP文件读取与地图显示的资料请关注脚本之家其它相关文章!
