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

标签 ios gdal ogr

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

pseudogreen 的著名脚本开始,您可以浏览网络,找到有关创建 iOS 库主题的一些(相对较旧的)信息。这是 3 年前写的。堆栈溢出也有一些零碎的东西,例如 GDAL / OGR on the iPhone提供额外信息。

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

最佳答案

笔记

此响应是在一段时间前编写的,并且更适用于 Xcode 6 及更高版本。请check this link有关此问题的最新答案。

介绍

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

  • 从GDAL网站下载GDAL源代码
  • 运行下面给出的配置/构建/安装脚本
  • 将生成的静态库与包含文件一起添加到您的 iOS 项目中
  • 与 iOS 项目中的其他库链接
  • 开始编码... GDAL 和 OGR 教程是很好的起点

  • 下载GDAL

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

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

    为了在您的 iOS 项目中使用 GDAL,您需要将源代码编译为静态库 (.a)。使用最新的 iOS6 支持的架构,您应该为以下架构创建静态库:
  • i386 模拟器
  • armv7 适用于 iPhone 3GS 到 iPhone 4S
  • 适用于 iPhone 5 的 armv7s

  • 为 1 个架构构建的基本脚本

    以下脚本改编自 pseudogreen 's 可以为单个架构编译源代码。

    将此代码复制粘贴到文本编辑器中,并将其保存为扩展名为 .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
    

    请注意,此脚本有一些默认参数,您可以更改这些参数以反射(reflect)您的偏好或 SDK 或 LLVM Apple 编译器中的更改:
  • default_gcc_version=4.2
  • default_iphoneos_version=6.0
  • default_macos_version=10.8
  • default_architecture=armv7
  • default_prefix="${HOME}/Documents/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

  • 您现在可以使用可执行的 lipo(用于吸脂)将 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 项目

    这一步相当简单:
  • 在 Xcode (4.5) 中创建一个新项目或打开要添加 GDAL 的项目
  • 在文件资源管理器中右键单击并选择“将文件添加到项目”
  • 选择上面创建的 libgdal.a 以及包含目录之一中的包含文件(这 3 个目录包含相同的文件)
  • 将以下库添加到您的 XCode 项目(来自项目框架列表):
  • libstdc++.6.0.9.dylib
  • libz.dylib
  • libiconv.dylib
  • libsqlite3.dylib
  • libxml2.dylib(如果架构 armv7 的 undefined symbol :
    “_xmlCatalogResolveSystem”等)

  • 构建你的代码。一切都应该编译没有问题。

    开始编码

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

    在这里,您有两种解决方案:
  • 将所有 GDAL 代码写入 .mm 文件,然后 Xcode 会将其识别为 Objective-C++ 文件并编译
  • 按照 Phil Jordan 在他的优秀文章 Mixing Objective-C++ and C++ 中所述,在您的 Objective-C 文件中使用类扩展。

  • 有一次我会发布一些 GDAL 代码示例……但稍后……

    关于ios - 将 GDAL/OGR 合并到 iOS 项目中 - 快速指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12643898/

    相关文章:

    ios - 如何阻止我的证书始终在xcode中吊销?

    gdal - 如何将 GRIB 文件目录加载到 Dask 数组中

    c++ - 如何以编程方式为 Qt 中的当前进程设置环境变量?

    gdal - 将 .dbf .prj .shp .shx 转换为 GeoJson

    ios - 在 iOS 中实时更改播放轨道的速度(节奏)

    ios - 如何围绕其中心旋转 SwiftUI Path?

    objective-c - 在核心数据中传递对象

    ios - NSTask 或等效的 iPhone

    d3.js - D3 的美国邮政编码 TOPOJson

    readOGR无法打开图层错误