Create SpatiaLite in Python

About this article

Notes on creating SpatiaLite in Python

Execution environment

OS Windows7-64bit Python 2.7.10

DLL download

[Here] Download the Windows version binary from [link-1] [link-1]:https://www.gaia-gis.it/spatialite-2.3.1/binaries.html

DLL copy

When you unzip it, there is a DLL in each bin folder, so copy it to a folder that passes the path

script

A simple script to create SpatiaLite containing POINT, LINESTRING, POLYGON The created sample.spatialite can be referenced with GIS tools such as QGIS

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()

Other

Recommended Posts

Create SpatiaLite in Python
Create a function in Python
Create a dictionary in Python
Create gif video in Python
Create a DI Container in Python
Create a binary file in Python
Create Gmail in Python without API
Create Python project documentation in Sphinx
Create a Kubernetes Operator in Python
Create a random string in Python
Create and read messagepacks in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Create your own Linux commands in Python
Create ScriptableObject in Python when building ADX2
[LLDB] Create your own command in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
Create Qt designer parts in Python (PyQt)
[GPS] Create a kml file in Python