c# 从MySQL往sharpmap中加载矢量数据图层(一)

最近在学习使用开源数据库和开源gis开发库来开发一个简易导航系统,选用了MySQL和sharpmap,但是将矢量图层shapefile导入MySQL之后发现sharpmap 1.1版本不支持将SQL数据直接从MySQL导入,本来想换成支持的postgreSQL,但是一同学习的其他小组否决了,于是探寻从MySQL往sharpmap加载数据的办法。

研究发现,sharpmap原本支持的方法就是通过新建一个map类的实例,然后创建一个VectorLayer类的图层,通过VectorLayer类的方法从系统路径中打开shapefile来加载进入图层中,然后把这个图层加载进入map类中,渲染map类的实例得到一个Image的变量,放在图片控件中显示出来。sharpmap还可以通过通过Extensions类,来从不同的数据源加载图层:

Extension Are Cool Cool
Oracle Oracle Spatial datasource provider Data provider Diego Guidi
PostGIS PostgreSQL/PostGIS datasource provider Data provider Volleyknaller , FObermaier
PostGIS2 PostgreSQL/PostGIS datasource provider Data provider Volleyknaller
OGR Provider Adding support for several vector formats through OGR Data Provider Volleyknaller
NetTopologySuite Data Provider Adds NTS functionality to SharpMap Data Provider Diego Guidi
GDAL Raster Layer Adding support for a vast number of raster image formats using GDAL Layer Volleyknaller, FObermaier
SimpleChart Add pie-charts as symbols to your map based on attribute data Example Volleyknaller
SpatiaLite SQLite/SpatiaLite datasource provider Data Provider Bill Dollins,Kev, FObermaier

sharpmap从14年以来就不更新了,据说项目主导者进了公司,对这项目也不上心了,以后估计我都不会用到sharpmap了。
无法从MySQL中直接导入数据,于是采用个折中的办法,从MySQL中储存的记录,构造一张新的shapefile,然后再导入sharpmap中。

上两节讲过如何将shapefile存储进入MySQL中,采用的是GDAL的OGR中的ogr2ogr类,所以想到采用开源的GDAL库来将MySQL表数据写成shapefile文件。找了很久之后没发现c#有直接调用的函数,但是找到了一篇文章例子,改一下就好了。

/******************************************************************************
 * $Id: createdata.cs 35222 2016-08-28 06:06:11Z goatbar $
 *
 * Name:     createdata.cs
 * Project:  GDAL CSharp Interface
 * Purpose:  A sample app to create a spatial data source and a layer.
 * Author:   Tamas Szekeres, [email protected]
 *
 ******************************************************************************
 * Copyright (c) 2007, Tamas Szekeres
 * Copyright (c) 2009-2010, Even Rouault <even dot rouault at mines-paris dot org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *****************************************************************************/


using System;

using OSGeo.OGR;
using OSGeo.OSR;


/**

 * <p>Title: GDAL C# createdata example.</p>
 * <p>Description: A sample app to create a spatial data source and a layer.</p>
 * @author Tamas Szekeres ([email protected])
 * @version 1.0
 */



/// <summary>
/// A C# based sample to create a layer.
/// </summary>
namespace 路径查询程序
{
    class CreateData
    {
        public static void usage()
        {
            Console.WriteLine("usage: createdata {data source name} {layername}");
            System.Environment.Exit(-1);
        }

        public static void Main(string[] args)
        {

            if (args.Length != 2) usage();

         
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值