FileGDBAPI学习

来源:互联网 发布:淘宝流量一下子跌下来 编辑:程序博客网 时间:2024/06/05 05:09

FileGDBAPI学习

环境:vs2010 win7_64
  1. 首先可以在官网上下载对应版本的源码及例子
    这里贴出官网下载地址http://appsforms.esri.com/products/download/#File_Geodatabase_API_1.3(下载速度比较慢)
    这里我贴出我的环境对应的(点击下载)(密码:e4wl)
  2. 熟悉 并编译例子
    下载的压缩包解压后,可以看到如下文件链接:

    这里写图片描述
    其中 /bin目录下主要包含我们调用API函数需要的.dll文件 /bin64 64位程序使用
    /doc目录下包含完整的api介绍文档,建议详读
    /include目录下包含所需的头文件,我们的项目中需要添加引用
    /lib目录下主要包含需要使用的库文件
    /samples目录下是C++调用API的例子
    /samplesC#目录下是C#调用API的例子
    OpenTK.dll和OpenTK.GLControl.dll是C#例子中要添加引用的dll(我自己下载源码编译得到的,顺便就放过来了)
    用VS2010打开C++解决方案,解决方案如下图:
    这里写图片描述
    然后打开项目属性,确认包含目录和库目录是否正确:对应的include目录和lib目录
    这里写图片描述
    查看项目输出目录:
    这里写图片描述
    确保上面的提到的dll放入到该输出目录。
    然后就可以点击生成并运行:
    如果生成成功但运行时报以下错误:
    这里写图片描述
    是因为缺少vs2008运行时环境(点击下载),将其放在生成目录下,或者windows下的System32中
    运行成功后得到如下结果:
    这里写图片描述

  3. 利用API开发拷贝功能
    这里主要编写了一些简单的Copy功能,可能实现不够完善,希望能够有大神指导。
    拷贝整个FileGDB数据库,分两种方式
    1.通过文件夹拷贝方式,详情参见
    2.通过调用FileGDBAPI所提供的API:
    实现比较简单,大致想法是细化问题,先实现拷贝要素函数、利用该函数实现拷贝要素数据集函数,然后利用上述两个函数实现整个数据库的拷贝。
    这里贴出源码:

#include <Windows.h>#include <shellapi.h>#include <atlstr.h>#include "FileGDBCopyHelp.h"#include "FileGDBAPI.h"using namespace std;using namespace FileGDBAPI;//从源数据库中拷贝要素到目标数据库//sourceGdbFilePath 源数据库文件路径//targetGdbFIlePath 目标数据库文件路径//tablePath                 要素(表)路径//return                            -1 失败   0 成功int CopyRows(wstring sourceGdbFilePath, wstring targetGdbFIlePath, wstring tablePath){    fgdbError hr;    Geodatabase geodatabase;    //Open the  geodatabase    if ((hr = OpenGeodatabase(sourceGdbFilePath, geodatabase)) != S_OK)    {        wcout << "An error occurred while opening the geodatabase." << endl; return -1;    }    // Open the Cities table.    Table theTable;    if ((hr = geodatabase.OpenTable(tablePath, theTable)) != S_OK)    {        wcout << "An error occurred while opening the table." << endl; return -1;    }    //获取该表中所有要素    EnumRows avQueryResult;    if ((hr = theTable.Search(L"*", L"", false, avQueryResult)) != S_OK)    {        wcout << "An error occurred while performing attribute search." << endl; return -1;    }    //打开目标数据库    Geodatabase geodatabaseT;    if ((hr = OpenGeodatabase(targetGdbFIlePath, geodatabaseT)) != S_OK)    {        wcout << "An error occurred while opening the geodatabase." << endl; return -1;    }    Table theTableT;    //获取源表的属性    vector<FieldDef> fieldDefs;    if((hr = theTable.GetFields(fieldDefs)) != S_OK)    {        wcout << "An error occurred while getting table fields." << endl; return -1;    }    if((hr = geodatabaseT.CreateTable(tablePath, fieldDefs, L"DEFAULTS", theTableT)) != S_OK)    {        wcout << "An error occurred while creating table." << endl; return -1;    }    Row theRow;    while((hr = avQueryResult.Next(theRow)) == S_OK)    {        if ((hr = theTableT.Insert(theRow)) != S_OK)        {            wcout << "An error occurred while inserting a row." << endl; return -1;        }        else        {            wcout << "Inserted S" << endl;        }    }        if ((hr = geodatabaseT.CloseTable(theTableT)) != S_OK)    {        wcout << "An error occurred while closing Cities." << endl; return -1;    }    if ((hr = CloseGeodatabase(geodatabaseT)) != S_OK)    {        wcout << "An error occurred while closing the geodatabase." << endl; return -1;    }    // Close the table    if ((hr = geodatabase.CloseTable(theTable)) != S_OK)    {        wcout << "An error occurred while closing Cities." << endl; return -1;    }    if ((hr = CloseGeodatabase(geodatabase)) != S_OK)    {        wcout << "An error occurred while closing the geodatabase." << endl; return -1;    }    return 0;}//要素数据集拷贝  configurationkeywordint CopyDataSet(wstring sourceGdbFilePath, wstring targetGdbFIlePath, wstring dataSetPath, vector<wstring> tablePaths){    fgdbError hr;    wstring   errorText;    Geodatabase geodatabase;    //打开源数据库    if ((hr = OpenGeodatabase(sourceGdbFilePath, geodatabase)) != S_OK)    {        wcout << "An error occurred while opening the geodatabase." << endl; return -1;    }    Table theTable;    if ((hr = geodatabase.OpenTable(dataSetPath + tablePaths.back(), theTable)) != S_OK)//打开表    {        wcout << "An error occurred while opening the table." << endl; return -1;    }    vector<FieldDef> fieldDefs;    if((hr = theTable.GetFields(fieldDefs)) != S_OK)    //获取空间属性    {        wcout << "An error occurred while getting the field." << endl; return -1;    }    //获取空间属性列    FieldDef fieldDefGeo;    vector<FieldDef>::iterator iter = fieldDefs.begin();    vector<FieldDef>::iterator iter_end = fieldDefs.end();    for(; iter != iter_end; iter++)    {        FieldDef tempFieldDef = *iter;        FieldType fieldType;        if((hr = tempFieldDef.GetType(fieldType)) != S_OK)        {            wcout << "An error occurred while getting the fieldtype." << endl; return -1;        }        if(fieldType == fieldTypeGeometry)        {            fieldDefGeo = tempFieldDef;            break;        }    }    GeometryDef geometryDef;    if((hr = fieldDefGeo.GetGeometryDef(geometryDef)) != S_OK)    {        wcout << "An error occurred while getting the geometryDef." << endl; return -1;    }    SpatialReference spatialReference;    if((hr = geometryDef.GetSpatialReference(spatialReference)) != S_OK)    {        wcout << "An error occurred while getting the spatialReference." << endl; return -1;    }    //打开目标数据库    Geodatabase geodatabaseT;    if ((hr = OpenGeodatabase(targetGdbFIlePath, geodatabaseT)) != S_OK)    {        wcout << "An error occurred while opening the geodatabase." << endl; return -1;    }    if ((hr = geodatabaseT.CreateFeatureDataset(dataSetPath, spatialReference)) != S_OK)    {        wcout << "An error occurred while creating the feature dataset." << endl; return -1;    }    vector<wstring>::iterator tableIter = tablePaths.begin();    vector<wstring>::iterator tableIter_end = tablePaths.end();    for(; tableIter != tableIter_end; tableIter++)    {        Table theTable;        wstring tempPath = *tableIter;        if ((hr = geodatabase.OpenTable(dataSetPath + tempPath, theTable)) != S_OK)//打开表        {            wcout << "An error occurred while opening the table." << endl; return -1;        }        EnumRows avQueryResult;        if ((hr = theTable.Search(L"*", L"", false, avQueryResult)) != S_OK)        {            wcout << "An error occurred while performing attribute search." << endl; return -1;        }        vector<FieldDef> fieldDefs;        if((hr = theTable.GetFields(fieldDefs)) != S_OK)    //获取属性        {            wcout << "An error occurred while getting the field." << endl; return -1;        }        Table theTableT;        if((hr = geodatabaseT.CreateTable(dataSetPath + tempPath, fieldDefs, L"DEFAULTS", theTableT)) != S_OK)        {            wcout << "An error occurred while creating the table." << endl; return -1;        }        Row theRow;        while((hr = avQueryResult.Next(theRow)) == S_OK)        {            if ((hr = theTableT.Insert(theRow)) != S_OK)            {                wcout << "An error occurred while insertting a row." << endl; return -1;            }            else            {                wcout << "Inserted S" << endl;            }        }    }    if ((hr = CloseGeodatabase(geodatabase)) != S_OK)    {        wcout << "An error occurred while closing the geodatabase." << endl; return -1;    }    return 0;}//拷贝数据库 WinAPI方式//sourceGdbFilePath     源数据库文件路径//targetGdbFIlePath     目标数据库文件路径//return                                0int CopyGDBFileByWinAPI(CString sourceGdbFilePath, CString targetGdbFIlePath){    SHFILEOPSTRUCT fop;    ZeroMemory(&fop, sizeof(fop));    fop.wFunc = FO_COPY;    sourceGdbFilePath += '\0';    targetGdbFIlePath += '\0';    fop.pFrom = sourceGdbFilePath;    fop.pTo = targetGdbFIlePath;    SHFileOperation(&fop);    return 0;}//拷贝数据库 FileGDBAPI方式int CopyGDBFileByFileGDBAPI(wstring sourceGdbFilePath, wstring targetGdbFIlePath){    //在4中的类库项目中实现了    return 0;}int _tmain(int argc, _TCHAR* argv[]){           //test(L"E:\\02testData\\FeatureDatasetDemo.gdb");    //CopyRows(L"E:\\02testData\\Editing.gdb", L"E:\\aa.gdb", L"\\Cities");    //CopyDataSet(L"E:\\02testData\\FeatureDatasetDemo.gdb", L"E:\\a2.gdb", L"\\Transit", tablePaths);    //CopyGDBFileByFileAPI("E:\\Editing.gdb\\", "E:\\a1.gdb\\");    //CopyGDBFileByFileGDBAPI(L"E:\\02testData\\Editing.gdb", L"E:\\aa.gdb");    int hr = 0;    FileGDBCopyHelp *fgdbh = new FileGDBCopyHelp();    //hr = fgdbh->CopyRow(L"E:\\02testData\\Editing.gdb", L"E:\\aa.gdb", L"\\Cities");    //hr = fgdbh->CopyDataSet(L"E:\\02testData\\FeatureDatasetDemo.gdb", L"E:\\a2.gdb", L"\\Transit");    //hr = fgdbh->CopyGDBFileByWinAPI("E:\\02testData\\Editing.gdb", "E:\\a1.gdb");    hr = fgdbh->CopyGDBFileByFileGDBAPI(L"E:\\02testData\\FeatureDatasetDemo.gdb", L"E:\\a2.gdb");}

这里记得将FileGDBAPID.dll(Debug版)放到工程输出目录下,并且在引用目录中加入需要加入的.h文件,以及添加工程库目录并在输入中加入FileGDBAPID.lib(Debug版)。
4. 封装成类库项目 并编写demo调用
*

这里要用到C++类库项目开发的一些知识,详情见我的另一篇博客:

*
这里我贴出源码:

  1. 利用托管C++封装 并编写C#代码调用
0 0
原创粉丝点击