Gdal-use in QT

Source: Internet
Author: User

Gdal is a well-known open-source GIS data operation library, which is used by software including ArcGIS.

The official website address is as follows: http://www.gdal.org/

Many formats are supported. Including basic images PNG, GIF, JPG, Tiff, and BMP

Remote Sensing Image Data of PCI, ERDAS, and ArcInfo

Vector files such as SHP, tab, and MIF

Oracle Spatial and postsql spatial databases

 

I plan to do some learning development with QT, and it takes a lot of time. It is a lot of trouble because it is intended to be developed by Microsoft.

The compiler uses mingw, and the version I use is 4.5, as follows: http://sourceforge.net/projects/mingw/files/

The latest version of gdal source code is 1.7.3, as follows: http://trac.osgeo.org/gdal/wiki/DownloadSource

After mingw is installed, use the mingw shell to enter the gdal source code root directory.

Run

./Configure -- prefix = $ path_to_install_gdal_root -- without-libtool -- without-Python

Note:

Path_to_install_gdal_root is a variable that indicates the directory to which the gdal is to be built.

-- Without-libtool indicates that the. A file is not generated. (If you want to generate a static library in Linux through cross-compilation, you can skip this option)

-- Without-Python indicates that no available Python files are generated.

The complete command is./configure -- prefix =/D/GIS/gdal

In this way, the makefile is generated. If no error is reported, continue running.

Make

I keep reporting errors during the process. After one day, I checked a lot of information to find out that the path in the gnumakefile. Opt file is wrong,

Replace "$ (gdal_root)" before 12 rows with a period "."

Make again, and finally generate a bunch of O files (equivalent to the OBJ files on the win Platform), a little longer (this compilation efficiency is far worse than. Net)

If all the way smoothly, continue to run

Make install

Some files will be generated under the D: \ GIS \ gdal directory, including some header files and a libgdal. DLL (more than 20 MB, which may be added to a mess during compilation, a big DLL)

And the gdal-1.7.3 directory itself also generated a lot of intermediate files, made the original M folder into several hundred m.

I don't know much about GCC or configure, so that all the intermediate files will stay there.

 

At this point, gdal can finally be used in QT.

I am using version 4.6.2 of the QT library. The latest version is 4.7.1. The new version includes some meego items.

IDE uses its own QT creator. The latest version is 2.0.

A console project is created. The code in Main. cpp is as follows:

 

 #include <QtCore/QCoreApplication>
#include <iostream>

#include "include/gdal/ogrsf_frmts.h"

using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

OGRRegisterAll();

OGRDataSource *poDS;

poDS = OGRSFDriverRegistrar::Open( "ZW0817P.shp", FALSE );
if( poDS == NULL )
{
printf( "Open failed.\n" );
exit( 1 );
}

OGRLayer *poLayer;

poLayer = poDS->GetLayerByName( "point" );

OGRFeature *poFeature;

poLayer->ResetReading();
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
int iField;

for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
{
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );

if( poFieldDefn->GetType() == OFTInteger )
printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
else if( poFieldDefn->GetType() == OFTReal )
printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
else if( poFieldDefn->GetType() == OFTString )
printf( "%s,", poFeature->GetFieldAsString(iField) );
else
printf( "%s,", poFeature->GetFieldAsString(iField) );
}

OGRGeometry *poGeometry;

poGeometry = poFeature->GetGeometryRef();
if( poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
{
OGRPoint *poPoint = (OGRPoint *) poGeometry;

printf( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );
}
else
{
printf( "no point geometry\n" );
}
OGRFeature::DestroyFeature( poFeature );
}

OGRDataSource::DestroyDataSource( poDS );


return a.exec();
}

Note:

# Include "include/gdal/ogrsf_frmts.h"

I copied the include and Lib files under the gdal compiled by make install to the project folder.

In addition to the include header file, add a line to the project's pro file.

Libs + = D: \ GIS \ gdal \ Lib \ libgdal. dll

This tells the compiler to link the DLL so that it can be built.

The generated EXE is under proname-Build-Desktop \ debug by default.

But the runtime will not report libstdc ++-6.dll, libgcc_s_dw2-1.dll, the two files in the bin directory of the mingw installation directory,

Then libgdal will be reported and cannot be found.

Then there are qtcore4.dll, qtcored4.dll, and mingw10.dll. These files are under the QT \ bin directory of the QT installation directory.

Copy all the six DLL files to the running directory.

If a line of config + = static is added to the project's pro file, the compiler will compile libgdal into the EXE file.

Mingw and qt dll, because they are not familiar with makefile writing, I have not found a way to statically compile them into the EXE file, otherwise, you need to copy these DLL files when running on machines without mingw and QT installed.

. One solution is to use the depends tool of VC to check which DLL files are required and copy them to the running directory.

 

In this way, it is much easier to use the gdal library as a tnnd, and compile it on VC :(

In the process of self-exploration, I read a lot of information and spent a lot of time. I want to say that the efficiency of self-study is really not high, and I haven't fully understood it yet. c ++ compilation.

 

Summary:

Mingw port GCC and GDB to the compiling environment of Windows, while cygwin and msys simulate the running environment of Linux and UNIX on Windows.

Not familiar with configure and make.

I am not familiar with static and dynamic compilation methods, so I am not clear about def, Lib, A, DLL, And so files.

Next, we will continue to learn about the OGR part of gdal to understand the storage model of GIS data.

 

Reference:

Use of the configure command: http://topic.csdn.net/t/20030607/01/1886493.html

The role and usage of libtool: http://socol.javaeye.com/blog/576506

How to compile gdal by mingw: http://cache.baidu.com/C? M = platinum & P = 882a93408b8600dd42be9b7e0f4fc9 & User = Baidu

Http://trac.osgeo.org/gdal/wiki/FAQInstallationAndBuilding#CanIbuildGDALwithCygwinorMinGW

Http://www.qtcn.org/bbs/read.php? Tid = 22937

Common Parameters of QT creator project file Pro:

Http://blog.163.com/yandong_8212/blog/static/132153914201072101642792/

Dependency http://bonfire.blogbus.com/tag/Qt/ for generating executable files using QT dynamic compilation

Re-static compilation of QT Methods http://www.qtcn.org/bbs/read.php? Tid = 17173

Gdal for C ++ API Entry Example http://www.gdal.org/ogr/ogr_apitut.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.