将GDAL / OGR合并到iOS项目中 - 快速指南

时间:2012-09-28 16:29:45

标签: ios gdal ogr

问题在于: GDAL是一个非常棒的开源库,用于管理复杂的GIS数据,包括栅格和矢量。它完全编译为Mac OS(由William Kyngesburye提供)和其他平台,但不适用于iOS。

浏览网络时,您可以找到有关创建iOS库主题的一些(相对较旧的)信息,从3年前编写的pseudogreen着名脚本开始。堆栈溢出还有一些碎片,如GDAL / OGR on the iPhone,它们提供了额外的信息。

本文旨在涵盖我采取的所有步骤,这使我在使用iOS6和XCode 4.5.5的简单iOS应用程序中实现GDAL / OGR的全功能集成

2 个答案:

答案 0 :(得分:17)

注意

这个回复是在不久前编写的,并且mo可以在Xcode 6及更高版本上运行。请check this link获取此问题的最新答案。

简介

将GDAL合并到您的iOS应用中需要5个步骤:

  1. 从GDAL网站下载GDAL源代码
  2. 运行下面给出的configure / build / install脚本
  3. 将生成的静态库与包含文件
  4. 一起添加到iOS项目中
  5. 与iOS项目中的其他库链接
  6. 开始编码...... GDAL和OGR教程是很好的起点
  7. 正在下载GDAL

    GDAL是一个C ++开源库,可以从www.gdal.org web site下载。 在撰写本文时,最新版本是1.9.0。如果可能,您应该下载最新的稳定版本。

    运行脚本以编译GDAL for iOS和模拟器

    要在iOS项目中使用GDAL,您需要将源代码编译为静态库(.a)。使用最新的iOS6支持的体系结构,您应该为以下体系结构创建静态库:

    • i386用于模拟器
    • armv7 for iPhone 3GS to iPhone 4S
    • 适用于iPhone 5的armv7s

    为1架构构建的基本脚本

    以下脚本改编自pseudogreen,可以为单一架构编译源代码。

    将此代码复制粘贴到文本编辑器中,并将其另存为扩展名为.sh的文件:例如build_gdal_ios.sh。

    要使用它,请将脚本复制到您下载gdal源代码的目录中,并按如下方式运行:

    • 为模拟器构建库:

      `sh build_gdal_ios.sh -p "location where you want to save the resulting files" simulator`
      
    • 为设备构建它:

      `sh build_gdal_ios.sh -p "location where you want to save the resulting files" -a "architecture" device`
      

    您也可以输入sh build_gdal_ios.sh -h来获取帮助。

        #!/bin/bash
        ################################################################################
        #
        # Copyright (c) 2008-2009 Christopher J. Stawarz
        #
        # 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.
        #
        ################################################################################
    
    
    
        # Disallow undefined variables
        set -u
    
    
        default_gcc_version=4.2
        default_iphoneos_version=6.0
        default_macos_version=10.8
        default_architecture=armv7
        default_prefix="${HOME}/Documents/iOS_GDAL"
    
        GCC_VERSION="${GCC_VERSION:-$default_gcc_version}"
        export IPHONEOS_DEPLOYMENT_TARGET="${IPHONEOS_DEPLOYMENT_TARGET:-$default_iphoneos_version}"
        export MACOSX_DEPLOYMENT_TARGET="${MACOSX_DEPLOYMENT_TARGET:-$default_macos_version}"
        DEFAULT_ARCHITECTURE="${DEFAULT_ARCHITECTURE:-$default_architecture}"
        DEFAULT_PREFIX="${HOME}/Documents/iOS_GDAL"
    
        echo Default architecture: $DEFAULT_ARCHITECTURE
    
        usage ()
        {
            cat >&2 << EOF
        Usage: ${0##*/} [-ht] [-p prefix] [-a arch] target [configure_args]
            -h  Print help message
            -p  Installation prefix (default: \$HOME/Documents/iOS_GDAL...)
            -t  Use 16-bit Thumb instruction set (instead of 32-bit ARM)
            -a  Architecture target for compilation (default: armv7)
    
        The target must be "device" or "simulator".  Any additional arguments
        are passed to configure.
    
        The following environment variables affect the build process:
    
            GCC_VERSION (default: $default_gcc_version)
            IPHONEOS_DEPLOYMENT_TARGET  (default: $default_iphoneos_version)
            MACOSX_DEPLOYMENT_TARGET    (default: $default_macos_version)
            DEFAULT_PREFIX  (default: $default_prefix)
        EOF
        }
    
        prefix="${DEFAULT_PREFIX}"
    
        echo Prefix: $prefix
    
        while getopts ":hp:a:t" opt; do
            case $opt in
            h  ) usage ; exit 0 ;;
            p  ) prefix="$OPTARG" ;;
            t  ) thumb_opt=thumb ;;
            a  ) DEFAULT_ARCHITECTURE="$OPTARG" ;;
            \? ) usage ; exit 2 ;;
            esac
        done
        shift $(( $OPTIND - 1 ))
    
        if (( $# < 1 )); then
            usage
            exit 2
        fi
    
        target=$1
        shift
    
        case $target in
    
            device )
            arch="${DEFAULT_ARCHITECTURE}"
            platform=iPhoneOS
            extra_cflags="-m${thumb_opt:-no-thumb} -mthumb-interwork"
            ;;
    
            simulator )
            arch=i386
            platform=iPhoneSimulator
            extra_cflags="-D__IPHONE_OS_VERSION_MIN_REQUIRED=${IPHONEOS_DEPLOYMENT_TARGET%%.*}0000"
            ;;
    
            * )
            echo No target found!!!
            usage
            exit 2
    
        esac
    
    
        platform_dir="/Applications/Xcode.app/Contents/Developer/Platforms/${platform}.platform/Developer"
        platform_bin_dir="${platform_dir}/usr/llvm-gcc-${GCC_VERSION}/bin"
        platform_sdk_dir="${platform_dir}/SDKs/${platform}${IPHONEOS_DEPLOYMENT_TARGET}.sdk"
        prefix="${prefix}/${arch}/${platform}.platform/${platform}${IPHONEOS_DEPLOYMENT_TARGET}.sdk"
    
        echo library will be exported to $prefix
    
        export CC="${platform_bin_dir}/llvm-gcc-${GCC_VERSION}"
        export CFLAGS="-arch ${arch} -pipe -Os -gdwarf-2 -isysroot ${platform_sdk_dir} ${extra_cflags}"
        export LDFLAGS="-arch ${arch} -isysroot ${platform_sdk_dir}"
        export CXX="${platform_bin_dir}/llvm-g++-${GCC_VERSION}"
        export CXXFLAGS="${CFLAGS}"
        export CPP="${platform_bin_dir}/llvm-cpp-${GCC_VERSION}"
        export CXXCPP="${CPP}"
    
    
        ./configure \
            --prefix="${prefix}" \
            --host="${arch}-apple-darwin" \
            --disable-shared \
            --enable-static \
            --with-unix-stdio-64=no \
            "$@" || exit
    
        make install || exit
    
        cat >&2 << EOF
    
        Build succeeded!  Files were installed in
    
          $prefix
    
    
       EOF
    

    请注意,此脚本有一些默认参数,您可以更改这些参数以反映SDK或LLVM Apple编译器中的首选项或更改:

    • default_gcc_version = 4.2
    • default_iphoneos_version = 6.0
    • default_macos_version = 10.8
    • default_architecture = ARMv7的
    • default_prefix = “$ {HOME} /文档/ GDALLibrary”

    现在为多种架构构建

    使用前面的脚本(build_gdal_ios.sh)允许您一次构建一个体系结构...您需要编译3,然后将所有这些库集中在一个静态库文件下。

    以下脚本只允许(将其保存为另一个名称,例如build_gdal_all_ios.sh):

    #!/bin/bash
    make clean
    ./build_gdal_ios.sh -p ${HOME}/Documents/GDALLibrary -a armv7 device
    make clean
    ./build_gdal_ios.sh -p ${HOME}/Documents/GDALLibrary -a armv7s device
    make clean
    ./build_gdal_ios.sh -p ${HOME}/Documents/GDALLibrary simulator
    

    运行此脚本后,您的库将保存在子文件夹的$ {HOME} / Documents / GDALLibrary目录中:

    • $ {HOME} /Documents/GDALLibrary/i386/iPhoneSimulator.platform/iPhoneSimulator6.0.sdk/lib
    • $ {HOME} /Documents/GDALLibrary/armv7/iPhoneOS.platform/iPhoneSimulator6.0.sdk/lib
    • $ {HOME} /Documents/GDALLibrary/armv7s/iPhoneOS.platform/iPhoneSimulator6.0.sdk/lib

    您现在可以使用可执行脂肪(用于吸脂术)将3个库连接成一个,如下所示:

    lipo ${HOME}/Documents/GDALLibrary/i386/iPhoneSimulator.platform/iPhoneSimulator6.0.sdk/lib/libgdal.a ${HOME}/Documents/GDALLibrary/armv7/iPhoneOS.platform/iPhoneSimulator6.0.sdk/lib/libgdal.a ${HOME}/Documents/GDALLibrary/armv7s/iPhoneOS.platform/iPhoneSimulator6.0.sdk/lib/libgdal.a -output ${HOME}/Documents/GDALLibrary/libgdal.a -create
    

    ......你已经完成了......

    将静态库添加到XCode项目

    这一步非常简单:

    1. 在Xcode(4.5)中创建一个新项目,或者打开要将GDAL添加到
    2. 的项目
    3. 在文件资源管理器中右键单击并选择“将文件添加到项目”
    4. 选择上面创建的libgdal.a以及其中一个include目录中的include文件(3个目录包含相同的文件)
    5. 将以下库添加到XCode项目中(从项目框架列表中):
      • 的libstdc ++。6.0.9.dylib
      • libz.dylib
      • libiconv.dylib
      • libsqlite3.dylib
      • libxml2.dylib(如果架构armv7的未定义符号: “_xmlCatalogResolveSystem”等)
    6. 构建代码。所有人都应该毫无困难地编译。

      开始编码

      这里有一个技巧:您在Objective-C环境中使用C ++库(和头文件)。如果将一个GDAL头文件包含到.m文件中,XCode会抱怨C ++语法。

      这里有两个解决方案:

      1. 将所有GDAL代码写入.mm文件中,然后Xcode将其识别为Objective-C ++文件并编译
      2. 在Phil Jordan的优秀文章Mixing Objective-C++ and C++
      3. 中使用Objective-C文件中的类扩展名

        有一次,我将发布一些GDAL代码示例......但稍后......

答案 1 :(得分:0)

还可以使用lutraconsulting/input-sdk存储库,可以在其中下载或构建带有OSGeo库的预构建ios二进制文件。