项目作者: AlexImmer

项目描述 :
PostGIS geometry objects in python
高级语言: Python
项目地址: git://github.com/AlexImmer/ppygis3.git
创建时间: 2017-03-09T13:19:25Z
项目社区:https://github.com/AlexImmer/ppygis3

开源协议:Other

下载


ppygis3

ppygis3 is a port of PPyGIS that works in Python>=3.
PPyGIS is an extension for psycopg2. PPyGIS adds support for PostGIS geometry objects by
translating them between PostGIS EWKB representation and python objects.

Installation

  1. pip install ppygis3

Usage

The module can be used exactly as described in the documentation of PPyGIS for Python2.
Simply use a different import statement, ppygis3 instead of PPyGIS.
The PPyGIS documentation can be found here

Basic Usage (Read, Write)

Adapted from the original documentation. By extending the psycopg2 module, geometry objects will be
automatically serialized when inserting and transformed to python objects
when loading.

  1. import psycopg2
  2. from ppygis3 import Point, LineString, Geometry
  3. # Connect to an existing spatially enabled database
  4. connection = psycopg2.connect(database='test', user = 'test')
  5. cursor = connection.cursor()
  6. cursor.execute('CREATE TABLE test(geometry GEOMETRY)')
  7. # Insert a point into the table
  8. cursor.execute('INSERT INTO test VALUES(%s)', (Point(1.0, 2.0),))
  9. # Retrieve the point from the table and print it
  10. cursor.execute('SELECT * FROM test')
  11. point = cursor.fetchone()[0]
  12. print(point)
  13. # Create a line and insert it into the table
  14. geometry = LineString((point, Point(4.0, 5.0, 6.0)))
  15. cursor.execute('INSERT INTO test VALUES(%s)', (geometry,))
  16. # Retrieve the table contents and print it
  17. cursor.execute('SELECT * FROM test')
  18. for row in cursor:
  19. print(row[0])
  20. # Disconnect from the database
  21. cursor.close()
  22. connection.close()

COPY FROM/TO

COPY FROM and TO allow for bulk uploads and downloads. The following shows example usage of this functionality.

  1. import psycopg2
  2. from ppygis3 import Point, LineString, Geometry
  3. from io import BytesIO
  4. # Connect to an existing spatially enabled database
  5. connection = psycopg2.connect(database='test', user = 'test')
  6. cursor = connection.cursor()
  7. cursor.execute('CREATE TABLE test(geometry GEOMETRY)')
  8. buffer = BytesIO()
  9. buffer.write(Point(1.0, 2.0).write_ewkb() + b'\n')
  10. buffer.write(LineString((Point(1.0, 2.0), Point(3.0, 4.0))).write_ewkb() + b'\n')
  11. buffer.seek(0)
  12. # Perform a bulk upload of data to the table
  13. cursor.copy_from(buffer, 'test')
  14. # Retrieve the table contents and print it
  15. cursor.execute('SELECT * FROM test')
  16. for row in cursor:
  17. print(row[0])
  18. # Perform a bulk download of data from the table
  19. buffer = BytesIO()
  20. cursor.copy_to(buffer, 'test')
  21. buffer.seek(0)
  22. # Print the data
  23. for line in buffer:
  24. print(Geometry.read_ewkb(line.strip()))
  25. # Disconnect from the database
  26. cursor.close()
  27. connection.close()