65.9K
CodeProject 正在变化。 阅读更多。
Home

向你的 .NET 应用程序添加 GIS 和地图功能

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (14投票s)

2014年9月28日

CPOL

3分钟阅读

viewsIcon

78346

downloadIcon

12172

向你的 .NET 应用程序添加 GIS 和地图功能

Sample Image - maximum width is 600 pixels Sample Image - maximum width is 600 pixels

引言

我假设你对 ASP.NET 和 C#.NET 等有基本的了解。
Easy GIS .NET 是一套 .NET GIS 地图工具和控件,让开发人员可以轻松地将 GIS 功能集成到他们自己的应用程序中。桌面版包含一个独立的查看应用程序,可用于打开和查看 ESRI shapefiles。此应用程序使用相同的 .NET 控件构建,这些控件可以包含在你自己的应用程序中。Easy GIS .NET 是一个在 Lesser General Public License 下发布的免费软件。你可以在个人和商业软件中自由使用它。

背景

目前,我正在进行一个项目,我需要根据孟加拉国地图上的各种分布生成一些地图。 我正在使用 QGIS(一个免费和开源的地理信息系统),它是一个生成复杂地图的优秀工具。 然后我想根据我的数据库值生成一些地图,所以我搜索了谷歌,Easy GIS 引起了我的注意。 经过一些最初的努力,我能够生成我认为许多人需要的东西。

如何配置

步骤 1

这里 下载所需的 .dll

第二步

启动一个新项目作为 ASP.NET Web 应用程序; 将名称命名为 WebExample1。它会给你一个 Default.aspx.cs 文件。

步骤 3

现在添加这些引用

  • EGIS.ShapeFileLib.dll
  • EGIS.Web.Controls.dll
  • geomutil_lib.dll
  • geomutil_libx64.dll

步骤 4

Web.config 文件的 "system.web" 部分下添加下面提到的代码

        <httpHandlers>
   <add path="egismap.axd" verb="*" 
   type="EGIS.Web.Controls.SFMapImageProvider, EGIS.Web.Controls, 
   Version=4.3.0.0, Culture=neutral, PublicKeyToken=05b98c869b5ffe6a"
    validate="true" />
  </httpHandlers>

步骤 5

这里 下载孟加拉国的 shapefile。

步骤 6

将文件夹添加到你的项目中。

步骤 7

这里 下载 bangladesh.egp

步骤 8

将文件添加到你的项目中

步骤 9

现在你的解决方案资源管理器将如下所示

Using the Code

现在注意 "bangladesh.egp" 文件

<sfproject version="1.0">
  <MapBackColor>buttonface</MapBackColor>
  <layers>
    <shapefile>
      <name>BGD_adm3.shp</name>
      <path>bangladesh/BGD_adm3</path>
      <renderer>
        <MinZoomLevel>-1</MinZoomLevel>
        <MaxZoomLevel>-1</MaxZoomLevel>
        <MinRenderLabelZoom>-1</MinRenderLabelZoom>
        <FieldName>NAME_3</FieldName>
        <Font Size="10" Name="Microsoft Sans Serif" Style="Regular">
        </Font>
        <FontColor>Black</FontColor>
        <FillColor>#FCFCFC</FillColor>
        <OutlineColor>#969696</OutlineColor>
        <PenWidthScale>0.0001355183</PenWidthScale>
        <FillInterior>True</FillInterior>
        <Selectable>False</Selectable>
        <LineType>Outline</LineType>
        <ShadowText>True</ShadowText>
        <PointSize>5</PointSize>
        <UseToolTip>True</UseToolTip>
        <ToolTipFieldName>NAME_3</ToolTipFieldName>
      </renderer>
    </shapefile>
  </layers>
</sfproject>
  1. <name> 字段包含 shapefile 名称,因此请相应地更改它。
  2. <path> 字段包含 shapefile 的位置,因此请相应地更改它。
  3. 相应地更改其余字段。

现在编译并运行该项目。你将看到你分配的地图。

现在,我们希望显示一些区域分布级别的数据。
为此,我在 BGD_adm3.shp 文件中添加了一个名为 "popu_mis" 的列,其中包含区域分布人口。

什么是 shapefile

shapefile 格式现在是存储 GIS 数据的常用格式。 Shapefiles 存储非拓扑矢量数据以及相关的属性数据。 Shapefiles 由 Esri 开发,可以被许多 GIS 软件程序(如 ArcGIS 和 QGIS)直接读取。 shapefile 实际上是至少三个基本文件的集合:.shp.shx.dbf。 所有三个文件必须存在于同一个目录中才能查看。 可能还有其他文件,例如包含 shapefile 投影信息的 .prj。 通常,shapefiles 会被压缩在 .zip 文件中以进行传输,例如通过电子邮件作为附件或通过网站下载。

如果你想了解更多关于 shapefile 的信息,请点击这里
如果你想下载 shapefile 查看器,请点击这里

使用数据渲染

private void SetupPopulation()
{
    TooltipHeaderFieldNamePair[] tooltipPairs;
    tooltipPairs = new TooltipHeaderFieldNamePair[] { 
            new TooltipHeaderFieldNamePair("District: ", "NAME_3"),
            new TooltipHeaderFieldNamePair("Population: ", "popu_mis")};
    SetupCustomRenderSettings("popu_mis", 0, tooltipPairs);
}

"NAME_3" 是区域字段,"popu_mis" 是人口字段。 它将创建一个工具提示文本。

double[] samples = new double[numRecords];
//find the range of population values and obtain the quintile quantiles
for (int n = 0; n < numRecords; n++)
{
    if (String.IsNullOrEmpty(dbfReader.GetField(n, fieldIndex)) == false)
    {
        string tmp = dbfReader.GetField(n, fieldIndex);
        int value;
        if (int.TryParse(tmp, out value))
        {
            double d = double.Parse(dbfReader.GetField(n, fieldIndex), System.Globalization.CultureInfo.InvariantCulture);
            samples[n] = d;
        }
    }
}

此代码从 shapefile 创建一个人口分布值数组。

private static double[] GetQuintiles(double[] samples)
{
    Array.Sort(samples);
    double[] quintiles = new double[4];

    quintiles[0] = samples[10];
    quintiles[1] = samples[30];
    quintiles[2] = samples[50];
    quintiles[3] = samples[60];
    return quintiles;
}

此代码创建一系列范围值。 例如,10000-20000、20000-40000、40000-80000 等。

Color[] cols = new Color[] { 
        Color.FromArgb(96,169, 108), 
        Color.FromArgb(201, 200, 123), 
        Color.FromArgb(157, 135, 151), 
        Color.FromArgb(112, 155, 235),
        Color.FromArgb(192,94,113)};
}

此代码创建一系列颜色。 颜色数量将比分位数中的元素数量多 1 个。

运行代码后,你将看到下面的地图,其中包含工具提示人口和区域名称值。

参考文献

结论

Easy GIS 对于基本的地图功能来说,快速且易于实现。 但我仍然无法显示图例。

 

 

© . All rights reserved.