zoukankan      html  css  js  c++  java
  • 扩展OGR格式驱动(一)无需外部依赖库

    GDAL/OGR官方网站中给了编写栅格和矢量数据新格式的教程,OGR Driver Implementation Tutorial,但是没有给出具体的编译过程,这篇博文记录如何将新建的格式编入gdal/ogr。

    一、继承实现Driver、DataSource、Layer

    (1) 实现部分参考OGR Driver Implementation Tutorial,这个教程很详细,另外也可以阅读ogr某些格式的源码了解这一过程。下面这个是一个更为简单的Open方法实现:

     1  int  OGRSPFDataSource::Open( const char *pszFilename, int bUpdate )
     2  {
     3  // -------------------------------------------------------------------- 
     4   //      Does this appear to be an .spf file?                           
     5   // --------------------------------------------------------------------
     6    if( !EQUAL( CPLGetExtension(pszFilename), "spf" ) )
     7       return FALSE;
     8    if( bUpdate )
     9    {
    10       CPLError( CE_Failure, CPLE_OpenFailed, 
    11                  "Update access not supported by the SPF driver." );
    12       return FALSE;
    13    }
    14    std::cout <<"OGRSPFDataSource::Open( const char *pszFilename, int bUpdate )"<<std::endl;
    15    std::cout <<"Trying to open the datasource "<<pszFilename<< " ."<<std::endl;
    16    return TRUE;
    17  }

    (2) 这一部分实现后应该形成ogr_spf.h、ogrspfdatasource.cpp、ogrspfdriver.cpp、ogrspflayer.cpp四个源文件,其中头文件ogr_spf.h中最好加上宏定义:

     1 #ifndef _OGR_SPF_H_INCLUDED
     2 #define _OGR_SPF_H_INCLUDED
     3  
     4 class OGRSPFDriver : public OGRSFDriver
     5 {
     6      ...
     7 }
     8    
     9 class OGRSPFDataSource : public OGRDataSource
    10 {
    11     ...
    12 }
    13   
    14 class OGRSPFLayer : public OGRLayer
    15 {
    16       ...
    17 }
    18  #endif /* ndef _OGR_SPF_H_INCLUDED */

    ogrspfdriver.cpp文件中则需要添加驱动注册函数定义: 

    1 void RegisterOGRSPF()
    2  {
    3     OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRSPFDriver );
    4  }

    (3) 修改完成后在ogr\ogrsf_frmts文件夹下创建spf文件夹,将这四个源文件放入其中。

    二、修改编译配置文件

    (1) 从其他格式文件夹下拷贝makefile.vc和GUNmakefile文件到spf目录下,使用VS或记事本打开修改:

    1 #makefile.vc文件
    2 
    3 OBJ = ogrspfdatasource.obj ogrspfdriver.obj ogrspflayer.obj
    4 EXTRAFLAGS =    -I.. -I..\..
    5 GDAL_ROOT    =    ..\..\..
    6 !INCLUDE $(GDAL_ROOT)\nmake.opt
    7 default:    $(OBJ)
    8 clean: 
    9     -del *.obj *.pdb
    1 #GUNmakefile文件
    2 include ../GDALmake.opt
    3 OBJ    =    ogrspfdatasource.o ogrspfdriver.o ogrspflayer.o
    4 CPPFLAGS    :=    -I.. -I../.. $(GDAL_INCLUDE) $(CPPFLAGS)
    5 default:    $(O_OBJ:.o=.$(OBJ_EXT))
    6 clean: 
    7     rm -f *.o $(O_OBJ)
    8 $(O_OBJ):    ogr_spf.h

    (2)修改ogrsf_frmts.h和ogrregisterall.cpp文件

    在ogrregisterall.cpp中void OGRRegisterAll()函数中添加下面的代码: 

    1 #ifdef SPF_ENABLED 
    2     RegisterOGRSPF(); 
    3 #endif

     在ogrsf_frmts.h中添加:

    void CPL_DLL RegisterOGRSPF();

    (3) 修改makefile.vc

    打开ogr\ogrsf_frmts\generic\makefile.vc,在BASEFORMATS中添加-DSPF_ENABLED;

    打开\ogr\ogrsf_frmts\makefile.vc,在DIRLIST中添加spf,在lib /out:中添加spf\*.obj;

    三、检验编译后结果

    保存所有修改重新编译gdal/ogr库,在bin下使用ogrinfo --formats命令查看:

    无标题

    无标题2

     转自:http://www.cnblogs.com/geosky/archive/2013/05/11/new_ogr_driver.html 

  • 相关阅读:
    【插件】博客园markdown编辑器自定义代码黑色背景高亮显示
    【MatrixSynapseChat】安装教程(一)基于centOS7.* 安装Synapse
    【Python3】python安装 matrix-synapse[all] 报错’Error: pg_config executable not found‘
    【python3】在CentOS7上升级SQLite,并让Python3使用新版SQLite
    【linux】CentOS7 升级sqlite3
    【Python3】Centos7 安装Python3.7
    面试官:兄弟,说说Java的static关键字吧
    六一儿童节,程序员写给女儿的一封信
    被缠上了,小王问我怎么在 Spring Boot 中使用 JDBC 连接 MySQL
    女生适合学编程吗?
  • 原文地址:https://www.cnblogs.com/geosky/p/new_ogr_driver.html
Copyright © 2011-2022 走看看