{"title":"安装 SpatiaLite","version":"4.0","locale":"zh-hans","docname":"ref/contrib/gis/install/spatialite","url":"/zh-hans/4.0/ref/contrib/gis/install/spatialite/","canonical":"https://djangodocs.dev/zh-hans/4.0/ref/contrib/gis/install/spatialite/","summary":"SpatiaLite 为 SQLite 增加了空间支持，使其成为一个全功能的空间数据库。 首先，检查是否可以从系统包或二进制文件中安装 SpatiaLite。 For example, on Debian-based distributions that package SpatiaLite 4.3+, try to…","html":"<h1>安装 SpatiaLite<a class=\"heading-anchor\" href=\"#installing-spatialite\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p><a class=\"reference external\" href=\"https://www.gaia-gis.it/fossil/libspatialite/index\">SpatiaLite</a> 为 SQLite 增加了空间支持，使其成为一个全功能的空间数据库。</p>\n<p>首先，检查是否可以从系统包或二进制文件中安装 SpatiaLite。</p>\n<p>For example, on Debian-based distributions that package SpatiaLite 4.3+, try to\ninstall the <code class=\"docutils literal notranslate\"><span class=\"pre\">libsqlite3-mod-spatialite</span></code> package. For older releases install\n<code class=\"docutils literal notranslate\"><span class=\"pre\">spatialite-bin</span></code>.</p>\n<p>对于 macOS，请按照 <a class=\"reference internal\" href=\"#spatialite-macos\"><span class=\"std std-ref\">下方的说明</span></a>。</p>\n<p>对于 Windows，你可以在 <a class=\"reference external\" href=\"https://www.gaia-gis.it/gaia-sins/\">Gaia-SINS</a> 主页上找到二进制文件。</p>\n<p>在任何情况下，你都应该能够 <a class=\"reference internal\" href=\"#spatialite-source\"><span class=\"std std-ref\">从源码安装</span></a>。</p>\n<section id=\"installing-from-source\">\n<span id=\"spatialite-source\"></span><h2>从源码安装<a class=\"heading-anchor\" href=\"#installing-from-source\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference internal\" href=\"/zh-hans/4.0/ref/contrib/gis/install/geolibs/\"><span class=\"doc\">GEOS and PROJ</span></a> should be installed\nprior to building SpatiaLite.</p>\n<section id=\"sqlite\">\n<h3>SQLite<a class=\"heading-anchor\" href=\"#sqlite\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>首先检查 SQLite 是否使用 <a class=\"reference external\" href=\"https://www.sqlite.org/rtree.html\">R*Tree 模块</a> 编译。运行 sqlite3 命令行界面，输入以下查询：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">sqlite</span><span class=\"o\">&gt;</span> <span class=\"n\">CREATE</span> <span class=\"n\">VIRTUAL</span> <span class=\"n\">TABLE</span> <span class=\"n\">testrtree</span> <span class=\"n\">USING</span> <span class=\"n\">rtree</span><span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"p\">,</span><span class=\"n\">minX</span><span class=\"p\">,</span><span class=\"n\">maxX</span><span class=\"p\">,</span><span class=\"n\">minY</span><span class=\"p\">,</span><span class=\"n\">maxY</span><span class=\"p\">);</span>\n</code></pre></div>\n<p>如果你获得一个错误，你将不得不从源码重新编译 SQLite。否则，请跳过本节。</p>\n<p>要从源码安装，请从 <a class=\"reference external\" href=\"https://www.sqlite.org/download.html\">SQLite 下载页面</a> 下载最新的合并源码档案，并解压：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ wget https://www.sqlite.org/YYYY/sqlite-amalgamation-XXX0000.zip\n$ unzip sqlite-amalgamation-XXX0000.zip\n$ cd sqlite-amalgamation-XXX0000\n</code></pre></div>\n<p>接下来，运行 <code class=\"docutils literal notranslate\"><span class=\"pre\">configure</span></code> 脚本 —— 不过 <code class=\"docutils literal notranslate\"><span class=\"pre\">CFLAGS</span></code> 环境变量需要自定义，以便 SQLite 知道如何构建 R*Tree 模块：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ CFLAGS=&quot;-DSQLITE_ENABLE_RTREE=1&quot; ./configure\n$ make\n$ sudo make install\n$ cd ..\n</code></pre></div>\n</section>\n<section id=\"spatialite-library-libspatialite\">\n<span id=\"spatialitebuild\"></span><h3>SpatiaLite 库（<code class=\"docutils literal notranslate\"><span class=\"pre\">libspatialite</span></code>）<a class=\"heading-anchor\" href=\"#spatialite-library-libspatialite\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>从 <a class=\"reference external\" href=\"https://www.gaia-gis.it/gaia-sins/libspatialite-sources/\">下载页面</a> 获取最新的 SpatiaLite 库源捆绑包：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ wget https://www.gaia-gis.it/gaia-sins/libspatialite-sources/libspatialite-X.Y.Z.tar.gz\n$ tar xaf libspatialite-X.Y.Z.tar.gz\n$ cd libspatialite-X.Y.Z\n$ ./configure\n$ make\n$ sudo make install\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>对于从源头构建的 macOS 用户来说，SpatiaLite 库 <em>和</em> 工具需要配置其 <code class=\"docutils literal notranslate\"><span class=\"pre\">target</span></code> ：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ ./configure --target=macosx\n</code></pre></div>\n</aside>\n</section>\n</section>\n<section id=\"macos-specific-instructions\">\n<span id=\"spatialite-macos\"></span><h2>macOS 特定说明<a class=\"heading-anchor\" href=\"#macos-specific-instructions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>要安装 SpatiaLite 库和工具，macOS 用户可以选择 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/contrib/gis/install/#kyngchaos\"><span class=\"std std-ref\">KyngChaos 包</span></a> 和 <a class=\"reference external\" href=\"https://brew.sh/\">Homebrew</a> 。</p>\n<section id=\"kyngchaos\">\n<h3>KyngChaos<a class=\"heading-anchor\" href=\"#kyngchaos\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>首先，按照 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/contrib/gis/install/#kyngchaos\"><span class=\"std std-ref\">KyngChaos 包</span></a> 部分的说明。</p>\n<p>在创建 SpatiaLite 数据库时，需要使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">spatialite</span></code> 程序。但是，不要试图从源头编译 SpatiaLite 工具，而是下载 MacOS 的 <a class=\"reference external\" href=\"https://www.kyngchaos.com/software/frameworks/\">SpatiaLite Tools</a> 包，并将 <code class=\"docutils literal notranslate\"><span class=\"pre\">spatialite</span></code> 安装到 <code class=\"docutils literal notranslate\"><span class=\"pre\">PATH</span></code> 中的可用位置。 例如：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ curl -O https://www.kyngchaos.com/files/software/frameworks/Spatialite_Tools-4.3.zip\n$ unzip Spatialite_Tools-4.3.zip\n$ cd Spatialite\\ Tools/tools\n$ sudo cp spatialite /Library/Frameworks/SQLite3.framework/Programs\n</code></pre></div>\n<p>最后，为了让 GeoDjango 能够找到 KyngChaos SpatiaLite 库，在你的 <code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code> 中添加以下内容：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">SPATIALITE_LIBRARY_PATH</span><span class=\"o\">=</span><span class=\"s1\">&#39;/Library/Frameworks/SQLite3.framework/SQLite3&#39;</span>\n</code></pre></div>\n</section>\n<section id=\"homebrew\">\n<h3>Homebrew<a class=\"heading-anchor\" href=\"#homebrew\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference external\" href=\"https://brew.sh/\">Homebrew</a> 代表你处理所有与 SpatiaLite 相关的软件包，包括 SQLite、SpatiaLite、PROJ 和 GEOS。按以下方式安装它们：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ brew update\n$ brew install spatialite-tools\n$ brew install gdal\n</code></pre></div>\n<p>最后，为了让 GeoDjango 能够找到 SpatiaLite 库，在你的 <code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code> 中添加以下内容：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">SPATIALITE_LIBRARY_PATH</span><span class=\"o\">=</span><span class=\"s1\">&#39;/usr/local/lib/mod_spatialite.dylib&#39;</span>\n</code></pre></div>\n</section>\n</section>","rootId":"installing-spatialite","toc":[{"title":"从源码安装","anchor":"installing-from-source","children":[{"title":"SQLite","anchor":"sqlite","children":[]},{"title":"SpatiaLite 库（libspatialite）","anchor":"spatialite-library-libspatialite","children":[]}]},{"title":"macOS 特定说明","anchor":"macos-specific-instructions","children":[{"title":"KyngChaos","anchor":"kyngchaos","children":[]},{"title":"Homebrew","anchor":"homebrew","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API 参考","url":"/zh-hans/4.0/ref/"},{"docname":"ref/contrib/index","title":"contrib 包","url":"/zh-hans/4.0/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/zh-hans/4.0/ref/contrib/gis/"},{"docname":"ref/contrib/gis/install/index","title":"GeoDjango 安装","url":"/zh-hans/4.0/ref/contrib/gis/install/"}],"prev":{"docname":"ref/contrib/gis/install/postgis","title":"安装 PostGIS","url":"/zh-hans/4.0/ref/contrib/gis/install/postgis/"},"next":{"docname":"ref/contrib/gis/model-api","title":"GeoDjango Model API","url":"/zh-hans/4.0/ref/contrib/gis/model-api/"},"formats":{"html":"/zh-hans/4.0/ref/contrib/gis/install/spatialite/","markdown":"/zh-hans/4.0/ref/contrib/gis/install/spatialite.md","json":"/zh-hans/4.0/ref/contrib/gis/install/spatialite.json"},"source":"https://github.com/django/django/blob/stable/4.0.x/docs/ref/contrib/gis/install/spatialite.txt","official":"https://docs.djangoproject.com/zh-hans/4.0/ref/contrib/gis/install/spatialite/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}