shapefile-to-sqlite 作者 simonw

星标

README 源代码

shapefile-to-sqlite

PyPI Changelog Tests License

将 shapefile 文件加载到 SQLite(可选 SpatiaLite)数据库中。

项目背景: 构建 shapefile-to-sqlite 时我学到的关于 shapefile 的知识

如何安装

$ pip install shapefile-to-sqlite

如何使用

你可以像这样针对 shapefile 文件运行此工具

$ shapefile-to-sqlite my.db features.shp

这会将几何数据以 GeoJSON 格式加载到文本列中。

结合 SpatiaLite 使用

如果你安装了 SpatiaLite,你可以像这样将它们加载为 SpatiaLite 几何数据

$ shapefile-to-sqlite my.db features.shp --spatialite

数据将被加载到一个名为 features 的表中——这个名称是基于 shapefile 的文件名。你可以使用 --table 指定一个替代的表名。

$ shapefile-to-sqlite my.db features.shp --table=places --spatialite

此工具将在以下位置搜索 SpatiaLite 模块

  • /usr/lib/x86_64-linux-gnu/mod_spatialite.so
  • /usr/local/lib/mod_spatialite.dylib

如果你将模块安装在其他位置,你可以使用 --spatialite_mod=xxx 选项来指定位置

$ shapefile-to-sqlite my.db features.shp \
    --spatialite_mod=/usr/lib/mod_spatialite.dylib

你可以使用 --spatial-index 选项在 geometry 列上创建空间索引

$ shapefile-to-sqlite my.db features.shp --spatial-index

如果你使用了 --spatialite-mod--spatial-index,则可以省略 --spatialite

投影

默认情况下,此工具会尝试将 shapefile 中的几何数据转换为 WGS 84 投影,以最好地符合 GeoJSON 规范

如果你想保留 shapefile 中使用的原始投影数据,请使用 --crs=keep 选项。

你可以通过将输出投影作为参数传递给 --crs 选项来转换数据。例如,要转换为 EPSG:2227(加州第 3 区),请使用 --crs=espg:2227

--crs 选项接受的完整格式列表在此处有详细文档。

提取列

如果你的数据包含一些列,其中有少量重复出现的值——例如负责土地块的特定机构的名称——你可以使用 -c 选项将这些列提取到单独的查找表中,通过外键引用。

$ shapefile-to-sqlite my.db features.shp -c agency

这将创建一个包含 idname 列的 agency 表,并在你的主表中创建一个名为 agency 的整数列,作为指向该表的外键引用。

-c 选项可以多次使用。

CPAD_2020a_Units 是一个使用 -c 选项创建的表示例。