3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでSpatiaLiteを作成

Posted at

この記事について

PythonでSpatialiteを作成した時のメモ

実行環境

OS Windows7-64bit
Python 2.7.10

DLLダウンロード

[ここ][link-1]からWindows版バイナリをダウンロード
[link-1]:https://www.gaia-gis.it/spatialite-2.3.1/binaries.html

  • libspatialite-win-x86-2.3.1.zip
  • libiconv-win-x86-1.9.2.zip
  • proj-win-x86-4.6.1.zip

DLLコピー

zip展開すると各binフォルダ内にDLLがあるのでパスが通ったフォルダへコピー

  • libspatialite-1.dll
  • libiconv2.dll
  • libproj-0.dll
    ※DBの作成だけであればgeos*.dllは必要ないようです

スクリプト

POINT,LINESTRING,POLYGONを格納したSpatialiteを作成する簡単なスクリプト
作成されたsample.spatialiteはQGISなどのGISツールで参照出来ます

sample.py
# -*- coding: utf-8 -*- 
import sys
import os
import sqlite3

if __name__ == "__main__":
  conn = sqlite3.connect("sample.spatialite")
  if conn:
    print 'connect success'
  else:
    print 'connect failes'
    sys.exit()
  conn.enable_load_extension(True)
  conn.execute('SELECT load_extension("libspatialite-1.dll")')
  conn.execute('SELECT InitSpatialMetaData()')
  conn.execute("INSERT INTO spatial_ref_sys (srid, auth_name, auth_srid, ref_sys_name, proj4text) VALUES (4326, 'epsg', 4326, 'WGS 84', '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')")

  #POINT
  conn.execute('CREATE TABLE "point" ("OBJECTID" INTEGER PRIMARY KEY AUTOINCREMENT)')
  conn.execute('Select AddGeometryColumn ("point", "Geometry", 4326, "POINT", 2)')
  conn.execute('INSERT INTO point (Geometry) VALUES(GeomFromText("POINT(139.69 35.679)",4326))')

  #LINESTRING
  conn.execute('CREATE TABLE "line" ("OBJECTID" INTEGER PRIMARY KEY AUTOINCREMENT)')
  conn.execute('Select AddGeometryColumn ("line", "Geometry", 4326, "LINESTRING", 2)')
  conn.execute('INSERT INTO line (Geometry) VALUES(GeomFromText("LINESTRING(139.69 35.68, 139.691 35.681, 139.692 35.68)",4326))')

  #POLYGON
  conn.execute('CREATE TABLE "polygon" ("OBJECTID" INTEGER PRIMARY KEY AUTOINCREMENT)')
  conn.execute('Select AddGeometryColumn ("polygon", "Geometry", 4326, "POLYGON", 2)')
  #Exterior CCW - Interior:CW
  conn.execute('INSERT INTO polygon (Geometry) VALUES(GeomFromText("POLYGON((139.69 35.682, 139.69 35.681, 139.691 35.681, 139.691 35.682, 139.69 35.682),(139.6902 35.6818, 139.6908 35.6818, 139.6908 35.6812, 139.6902 35.6812, 139.6902 35.6818))",4326))')

  conn.commit()
  conn.close()

その他

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?