首页 > 其他分享 >GDAL在Winform下使用

GDAL在Winform下使用

时间:2024-02-23 12:00:14浏览次数:20  
标签:Ogr static GDAL 使用 var Path Gdal Winform

 

注册语句 OSGeo.GDAL.Gdal.AllRegister()前加上:

    public Form1()
    {
        InitializeComponent();
        SharpMap.GdalConfiguration.ConfigureGdal();
        SharpMap.GdalConfiguration.ConfigureOgr();


        OSGeo.GDAL.Gdal.AllRegister();

        OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
    }
View Code

GdalConfiguration.cs

/******************************************************************************
 *
 * Name:     GdalConfiguration.cs.pp
 * Project:  GDAL CSharp Interface
 * Purpose:  A static configuration utility class to enable GDAL/OGR.
 * Author:   Felix Obermaier
 *
 ******************************************************************************
 * Copyright (c) 2012, Felix Obermaier
 *
 * 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 System.IO;
using System.Reflection;
using Gdal = OSGeo.GDAL.Gdal;
using Ogr = OSGeo.OGR.Ogr;

namespace SharpMap
{
    public static partial class GdalConfiguration
    {
        private static bool _configuredOgr;
        private static bool _configuredGdal;

        /// <summary>
        /// Function to determine which platform we're on
        /// </summary>
        private static string GetPlatform()
        {
            return IntPtr.Size == 4 ? "x86" : "x64";
        }


        /// <summary>
        /// Construction of Gdal/Ogr
        /// </summary>
        static GdalConfiguration()
        {
            var executingAssemblyFile = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
            var executingDirectory = Path.GetDirectoryName(executingAssemblyFile);

            if (string.IsNullOrEmpty(executingDirectory))
                throw new InvalidOperationException("cannot get executing directory");


            var gdalPath = Path.Combine(executingDirectory, "gdal");
            var nativePath = Path.Combine(gdalPath, GetPlatform());

            // Prepend native path to environment path, to ensure the
            // right libs are being used.
            var path = Environment.GetEnvironmentVariable("PATH");
            path = nativePath + ";" + Path.Combine(nativePath, "plugins") + ";" + path;
            Environment.SetEnvironmentVariable("PATH", path);

            // Set the additional GDAL environment variables.
            var gdalData = Path.Combine(gdalPath, "data");
            Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
            Gdal.SetConfigOption("GDAL_DATA", gdalData);

            var driverPath = Path.Combine(nativePath, "plugins");
            Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", driverPath);
            Gdal.SetConfigOption("GDAL_DRIVER_PATH", driverPath);

            Environment.SetEnvironmentVariable("GEOTIFF_CSV", gdalData);
            Gdal.SetConfigOption("GEOTIFF_CSV", gdalData);

            var projSharePath = Path.Combine(gdalPath, "share");
            Environment.SetEnvironmentVariable("PROJ_LIB", projSharePath);
            Gdal.SetConfigOption("PROJ_LIB", projSharePath);
        }

        /// <summary>
        /// Method to ensure the static constructor is being called.
        /// </summary>
        /// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
        public static void ConfigureOgr()
        {
            if (_configuredOgr) return;

            // Register drivers
            Ogr.RegisterAll();
            _configuredOgr = true;

            PrintDriversOgr();
        }

        /// <summary>
        /// Method to ensure the static constructor is being called.
        /// </summary>
        /// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
        public static void ConfigureGdal()
        {
            if (_configuredGdal) return;

            // Register drivers
            Gdal.AllRegister();
            _configuredGdal = true;

            PrintDriversGdal();
        }

        private static void PrintDriversOgr()
        {
#if DEBUG
            var num = Ogr.GetDriverCount();
            for (var i = 0; i < num; i++)
            {
                var driver = Ogr.GetDriver(i);
                Console.WriteLine(string.Format("OGR {0}: {1}", i, driver.name));
            }
#endif
        }

        private static void PrintDriversGdal()
        {
#if DEBUG
            var num = Gdal.GetDriverCount();
            for (var i = 0; i < num; i++)
            {
                var driver = Gdal.GetDriver(i);
                Console.WriteLine(string.Format("GDAL {0}: {1}-{2}", i, driver.ShortName, driver.LongName));
            }
#endif
        }
    }
}
View Code

 

标签:Ogr,static,GDAL,使用,var,Path,Gdal,Winform
From: https://www.cnblogs.com/shiningleo007/p/18029214

相关文章

  • 9.Polly在NET中的使用,重试、熔断、超时、降级、限流简单用法
    Polly是一个.NET弹性和瞬态故障处理库,允许开发人员以Fluent和线程安全的方式来实现重试、断路、超时、隔离、限流和降级策略。文档: https://gitee.com/hubo/Polly重试(Retry)出现故障自动重试,这个是很常见的场景,如:当发生请求异常、网络错误、服务暂时不可用时,就应该重试。......
  • C# 使用证书发起请求
    privateHttpClientHandlergetHandler(){stringcacheKey="handlerChche";HttpClientHandlerhandler=null;//cache.GetCache<HttpClientHandler>(cacheKey);if(handler==null){handler=newHttpClientHandler......
  • R语言使用Bass模型进行手机市场产品周期预测|附代码数据
    原文链接:http://tecdat.cn/?p=17725最近我们被客户要求撰写关于Bass模型的研究报告,包括一些图形和统计输出。巴斯Bass扩散模型已成功地用于预测各种新推出的产品以及成熟产品的市场份额主要观点该模型的主要思想来自两个来源:消费者不受社会影响的产品意愿。因为其他人已经......
  • java中 Scanner类 使用相关情况
    1、Scanner类使用Scanner对象之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。◆基本语法:Scanners=newScanner(System.in);......
  • Redis分布式锁的正确使用姿势
    前言分布式锁在日常开发中,用处非常的多。包括但不限于抢红包,秒杀,支付下单,幂等,等等场景。分布式锁的实现方式有多种,包括redis实现,mysql实现,zookeeper实现等等。而其中redis非常适合作为分布式锁使用,并且在各个公司都大规模的使用。本文将由浅入深的探究Redis分布式锁的实现,最终......
  • 【Unity】ScriptableObject使用之后,重新打开Unity会报错
    导致问题出现的原因:ScriptableObject的类与其他类放一起了具体请见:unity2020.3官方文档ScriptableObject部分解决方法:为ScriptableObject单独创建一个c#脚本,并将c#名称与ScriptableObject类名保持一致。猜测出现这样的原因,如果哪里不对烦请大佬指正一下:Unity中基本上都是会通......
  • C# Winform 为控件添加鼠标悬浮时的提示消息
    https://www.cnblogs.com/whr2071/p/16453901.html 学习自C#鼠标悬浮时,提示信息_20180509很简单也很详细,可直接查看原贴。发在这里主要是记录一下,方便我自己查找。绑定你想要在其上悬浮显示的控件的鼠标悬浮事件在其中创建ToolTip,设置属性,并绑定到该控件---以下就是链接......
  • C# 与JAVA 的RSA 加密解密交互,C#使用BouncyCastle来实现私钥加密,公钥解密的方法
    因为一般C#的RSA加密解密都是公钥加密,私钥解密,没有私钥加密,公钥解密。在网上查了很多资料,终于看到有个博主的分享,关于私钥加密,公钥解密的解决方案,非常感谢(最下面有源网址)。此处就把简单应用的源码附上,需要的自己去完善。 1、私钥加密,公钥解密的源码usingOrg.BouncyCastle.......
  • Reactive UI 包的使用 [观察者模式]
     1ReactiveUI原版说明包---------  |一个高级的、可组合的、反应式的模型-视图-视图模型框架(reactiveui.net)   2ReactiveUI具体分析拆解的笔记--------- ReactiveUI使用笔记,教程总结–CodeQing3ReactiveX对于具体的关键字分析 ----------ReactiveX-随笔分......
  • fswatch 使用和参数说明
    常用:fswatch-0./xx/xx  监控./xxx/xxx的文件;每次变更的时候输出fswatch-1./xx/xx   监控./xxx/xxx的文件;只监控一次后,退出fswatchfswatch-o./xx/xx   监控./xxx/xxx的文件;输出一个变更事件数字fswatch-x./xx/xx  监控  ./xxx/xxx的文件;输出变......