Python spatialite library app (sqlite3 extension)

Source: Internet
Author: User

Problem:

You need to read the content from the Spatialite library using Python, there is a Geometry field, and what is read is a string of binary bytes. The original code is attached:

ImportSqlite3conn= Sqlite3.connect ('./20059363.db') Cursor=Conn.execute ("SELECT ROWID, Object_pid, MESH, Trafsign_shape, Sign_type, GEOMETRY from Had_object_traffic_sign WHERE Trafsign_shap E = = 2"    "ORDER by ROWID") forRowinchcursor:Print(row[0])Print(row[1])    Print(Row[5])

The output is:

11833191183319b"\x00\x01\x00\x00\x00\x00\xeb\x1agy\x1c\xbc! A ' f\xef\xd4\x81\x00ha\x1d\xb0\xaby\x1f\xbc! a7\xfcnv\x82\x00ha|\xeb\x03\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x1d\xb0\xaby\x1f\xbc! A7\xfcnv\x82\x00ha\x07\xb5\xdf\xda\[email protected]\xeb\x1agy\x1c\xbc! A ' F\xef\xd4\x81\x00ha\x07\xb5\xdf\xda\[email protected]\xeb\x1agy\x1c\xbc! A ' F\xef\xd4\x81\x00ha\xd8}\xc7\xf0\xd8\[email protected]\x1d\xb0\xaby\x1f\xbc! A7\xfcnv\x82\x00ha\xd8}\xc7\xf0\xd8\[email protected]\x1d\xb0\xaby\x1f\xbc! A7\xfcnv\x82\x00ha\x07\xb5\xdf\xda\[email protected]\xfe"

This third binary byte string is obviously problematic.

Process:

Ask a colleague later, say is a field that holds geographic information, need special method to decode.

Later query to a Astext method, you can read to the field's text? (In fact, I don't know what to read, the main field I don't understand what's written in it ...) ),

The SQL statement is like this:

" SELECT ROWID, Object_pid, MESH, Trafsign_shape, Sign_type, astext(GEOMETRY) from had_object_traffic_sign< c3/>ORDER by ROWID")

However, there is no Astest method in Sqlite3, so it needs to be extended.

This tutorial was found online: http://www.jianshu.com/p/5bc7d8b7b429, using the Python +spatialite + window solution, but didn't succeed ...

Later thought of a simple rough method, or according to this tutorial, the corresponding DLL, and then directly into the Python installation directory in the DLLs, and then modify the code:

ImportSqlite3conn= Sqlite3.connect ('./20059363.db') conn.enable_load_extension (True) conn.load_extension ('Mod_spatialite') Cursor=Conn.execute ("SELECT ROWID, Object_pid, MESH, Trafsign_shape, Sign_type, Astext (GEOMETRY) from Had_object_traffic_sign WHERE Trafs Ign_shape = = 2"    "ORDER by ROWID") forRowinchcursor:Print(row[0])Print(row[1])    Print(Row[5])

Output:

11833191183319POLYGON Z ((581135.737638 3145988.675262 105.977164, 581134.236871 3145987.663552 105.977164, 581134.236871 3145987.663552 104.060116, 581135.737638 3145988.675262 104.060116, 581135.737638 3145988.675262 105.977164))

Get.

The following is attached to a python3,64-bit DLL package, convenient for everyone to use.

Link: Http://pan.baidu.com/s/1gfioKZD Password: m23o

Python spatialite library app (sqlite3 extension)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.