VS2015下GDAL(C++版本)配置

一、准备

1.1)VS2015
1.2) Gdal-2.3.1 链接:https://pan.baidu.com/s/1LCQuufs9xIBiCIx36r9Olg 密码:xiqh

二.步骤

2.1)解压文件,使用 这里写图片描述 进入到解压文件的目录。

其中本来需要修改解压文件目录下的 makefile.opt 文件,具体看如下链接:配置makefile.opt ,但是为了避免出现不必要的错误;我将自己能生成的打包了。GDAL_HOME 我设置在 F:\warmerda\bld 。也就是会在该目录下生成编译的Gdal。

这里写图片描述

2.2)依次输入三个命令:

nmake /f makefile.vc                  (编译)
nmake /f makefile.vc install            (生成bin/html/data文件夹)
nmake /f makefile.vc devinstall            (生成lib/include文件夹)

2.3)①新建C++ 控制台 项目,右键项目->属性->VC++目录 ,设置包含目录,与库目录。
这里写图片描述

和②链接器:
这里写图片描述

③将F:\warmerda\bld\bin 下的gdal203.dll 放入项目的debug/bin 下 ,注意生成解决方案平台与放入的目录平台一致。如我的都是: Debug/x64
这里写图片描述

2.4) 运行测试

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <ogrsf_frmts.h>
#include <gdal_priv.h>
#include <ogr_geometry.h>
#include <iostream> 
//生成shp文件
int main()
{
    const char *pszDriverName = "ESRI Shapefile";
    GDALDriver *poDriver;
    CPLSetConfigOption("SHAPE_ENCODING", "UTF-8"); //避免乱码
    GDALAllRegister();
    poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName);
    if (poDriver == NULL)
    {
        printf("%s driver not available.\n", pszDriverName);
        return 0;
    }
    GDALDataset *poDS;

    poDS = poDriver->Create("d:/newShp.shp", 0, 0, 0, GDT_Unknown, NULL); //创建shp文件
    if (poDS == NULL)
    {
        printf("Creation of output file failed.\n");
        return 0;
    }
    OGRLayer *poLayer;
    poLayer = poDS->CreateLayer("point_out", NULL, wkbPoint, NULL);
    if (poLayer == NULL)
    {
        printf("Layer creation failed.\n");
        return 0;
    }
    OGRFieldDefn idField("ID", OFTReal);
    OGRFieldDefn firstField("NAME", OFTInteger);
    OGRFieldDefn secondField("X", OFTReal);
    OGRFieldDefn thirdField("Y", OFTString);
    idField.SetWidth(32);
    firstField.SetWidth(32);
    secondField.SetWidth(32);

    poLayer->CreateField(&idField);
    poLayer->CreateField(&firstField);
    poLayer->CreateField(&secondField);
    poLayer->CreateField(&thirdField);


    int x, y;
    int a = 10, b = 100;
    for (int i = 1; i <= 10; i++)
    {
        OGRFeature *poFeature;
        poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());
        poFeature->SetField("ID", i);
        poFeature->SetField("NAME", i);
        x = (rand() % (b - a)) + a;
        y = (rand() % (b - a)) + a;
        poFeature->SetField("X", x);
        poFeature->SetField("Y","你好,弟中弟");
        OGRPoint pt;
        pt.setX(x);
        pt.setY(y);
        poFeature->SetGeometry(&pt);
        if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
        {
            printf("Failed to create feature in shapefile.\n");
            return 0;
        }
        OGRFeature::DestroyFeature(poFeature);
    }
    GDALClose(poDS);
    return 1;
}

在D盘生成 一个newShp.shp,成功。
案例下载:链接:https://pan.baidu.com/s/1k8MIs-TmS8DHlUh7CSQ9XA 密码:bv1s

三.参考资料
https://www.baidu.com/link?url=an-323_fKNwRLqEJYWI73Qdd6H6zQGEdpE3sNevIZH1-DymMSMa06mIbWvgDPZb6VXN1oqCOle7SPj96h5q11q&wd=&eqid=dce3c5f50000e1bf000000065b88f91c

http://www.cnblogs.com/sansan/p/3394636.html?utm_source=tuicool&utm_medium=referral

http://www.mamicode.com/info-detail-1188582.html

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值