datasette-geojson 作者:eyeseast

星标

README 源代码

datasette-geojson

PyPI Changelog Tests License

为 datasette 查询添加 GeoJSON 作为输出选项。

安装

在与 Datasette 相同的环境中安装此插件。

datasette install datasette-geojson

用法

要渲染 GeoJSON,请在包含 geometry 列的任何查询 URL 后添加 .geojson 扩展名。该列应为有效的 GeoJSON 几何对象

例如,您可以使用 geojson-to-sqliteshapefile-to-sqlite邻里边界加载到 SQLite 数据库中。

wget -O neighborhoods.geojson https://opendata.arcgis.com/datasets/3525b0ee6e6b427f9aab5d0a1d0a1a28_0.geojson
geojson-to-sqlite boston.db neighborhoods neighborhoods.geojson --spatial-index # create a spatial index
datasette serve boston.db --load-extension spatialite

如果您使用的是 Spatialite,geometry 列将采用二进制格式。如果不是,请确保 geometry 列是格式正确的 GeoJSON 几何对象。如果您使用了 geojson-to-sqliteshapefile-to-sqlite,则应该一切就绪。

在 Datasette 中运行此查询,您将看到一个下载 GeoJSON 的链接

select
  rowid,
  OBJECTID,
  Name,
  Acres,
  Neighborhood_ID,
  SqMiles,
  ShapeSTArea,
  ShapeSTLength,
  geometry
from
  neighborhoods
order by
  rowid
limit
  101

请注意,geometry 列需要显式命名为 geometry,否则您将无法获得导出 GeoJSON 的选项。如果您想使用不同的列,请使用 AS 重命名它:SELECT other AS geometry FROM my_table

export geojson

开发

要在本地设置此插件,请先检出代码。然后创建一个新的虚拟环境

cd datasette-geojson
python3 -mvenv venv
source venv/bin/activate

或者如果您正在使用 pipenv

pipenv shell

现在安装依赖项和测试

pip install -e '.[test]'

运行测试

pytest