pygrass.rpc package

Submodules

pygrass.rpc.base module

Fast and exit-safe interface to PyGRASS Raster and Vector layer using multiprocessing

(C) 2015 by the GRASS Development Team This program is free software under the GNU General Public License (>=v2). Read the file COPYING that comes with GRASS for details.

authors

Soeren Gebbert

class pygrass.rpc.base.RPCServerBase[source]

Bases: object

This is the base class for send and receive RPC server It uses a Pipe for IPC.

>>> import grass.script as gscript
>>> from grass.pygrass.rpc.base import RPCServerBase
>>> import time
>>> provider = RPCServerBase()
>>> provider.is_server_alive()
True
>>> provider.is_check_thread_alive()
True
>>> provider.stop()
>>> time.sleep(1)
>>> provider.is_server_alive()
False
>>> provider.is_check_thread_alive()
False
>>> provider = RPCServerBase()
>>> provider.is_server_alive()
True
>>> provider.is_check_thread_alive()
True

Kill the server process with an exception, it should restart

>>> provider.client_conn.send([1])
>>> provider.is_server_alive()
True
>>> provider.is_check_thread_alive()
True
check_server()[source]
is_check_thread_alive()[source]
is_server_alive()[source]
safe_receive(message)[source]

Receive the data and throw a FatalError exception in case the server process was killed and the pipe was closed by the checker thread

start_checker_thread()[source]
start_server()[source]

This function must be re-implemented in the subclasses

stop()[source]

Stop the check thread, the libgis server and close the pipe

This method should be called at exit using the package atexit

stop_checker_thread()[source]
thread_checker()[source]

Check every 200 micro seconds if the server process is alive

pygrass.rpc.base.dummy_server(lock, conn)[source]

Dummy server process

Parameters
  • lock – A multiprocessing.Lock

  • conn – A multiprocessing.Pipe

Module contents

Fast and exit-safe interface to PyGRASS Raster and Vector layer using multiprocessing

(C) 2015 by the GRASS Development Team This program is free software under the GNU General Public License (>=v2). Read the file COPYING that comes with GRASS for details.

authors

Soeren Gebbert

class pygrass.rpc.DataProvider[source]

Bases: pygrass.rpc.base.RPCServerBase

Fast and exit-safe interface to PyGRASS data delivery functions

get_raster_image_as_np(name, mapset=None, extent=None, color='RGB')[source]

Return the attribute table of a vector map as dictionary.

See documentation of: pygrass.raster.raster2numpy_img

Usage:

>>> from grass.pygrass.rpc import DataProvider
>>> import time
>>> provider = DataProvider()
>>> ret = provider.get_raster_image_as_np(name=test_raster_name)
>>> len(ret)
64

>>> extent = {
...     "north": 30,
...     "south": 10,
...     "east": 30,
...     "west": 10,
...     "rows": 2,
...     "cols": 2,
... }
>>> ret = provider.get_raster_image_as_np(name=test_raster_name, extent=extent)
>>> len(ret)
16

>>> extent = {"rows": 3, "cols": 1}
>>> ret = provider.get_raster_image_as_np(name=test_raster_name, extent=extent)
>>> len(ret)
12

>>> extent = {
...     "north": 100,
...     "south": 10,
...     "east": 30,
...     "west": 10,
...     "rows": 2,
...     "cols": 2,
... }
>>> ret = provider.get_raster_image_as_np(name=test_raster_name, extent=extent)

>>> provider.stop()
>>> time.sleep(1)

>>> extent = {"rows": 3, "cols": 1}
>>> ret = provider.get_raster_image_as_np(name=test_raster_name, extent=extent)
>>> len(ret)
12


..
get_vector_features_as_wkb_list(name, mapset=None, extent=None, feature_type='point', field=1)[source]

Return the features of a vector map as wkb list.

Parameters
  • extent – A dictionary of {“north”:double, “south”:double, “east”:double, “west”:double}

  • feature_type – point, centroid, line, boundary or area

See documentation: pygrass.vector.VectorTopo::features_to_wkb_list

pygrass.vector.VectorTopo::areas_to_wkb_list

Usage:

>>> from grass.pygrass.rpc import DataProvider
>>> provider = DataProvider()
>>> wkb = provider.get_vector_features_as_wkb_list(
...     name=test_vector_name, extent=None, feature_type="point"
... )
>>> for entry in wkb:
...     f_id, cat, string = entry
...     print(f_id, cat, len(string))
...
1 1 21
2 1 21
3 1 21

>>> extent = {"north": 6.6, "south": 5.5, "east": 14.5, "west": 13.5}
>>> wkb = provider.get_vector_features_as_wkb_list(
...     name=test_vector_name, extent=extent, feature_type="point"
... )
>>> for entry in wkb:
...     f_id, cat, string = entry
...     print(f_id, cat, len(string))
...
3 1 21

>>> wkb = provider.get_vector_features_as_wkb_list(
...     name=test_vector_name, extent=None, feature_type="line"
... )
>>> for entry in wkb:
...     f_id, cat, string = entry
...     print(f_id, cat, len(string))
...
4 2 57
5 2 57
6 2 57


>>> wkb = provider.get_vector_features_as_wkb_list(
...     name=test_vector_name, extent=None, feature_type="centroid"
... )
>>> for entry in wkb:
...     f_id, cat, string = entry
...     print(f_id, cat, len(string))
...
19 3 21
18 3 21
20 3 21
21 3 21

>>> wkb = provider.get_vector_features_as_wkb_list(
...     name=test_vector_name, extent=None, feature_type="area"
... )
>>> for entry in wkb:
...     f_id, cat, string = entry
...     print(f_id, cat, len(string))
...
1 3 225
2 3 141
3 3 93
4 3 141

>>> wkb = provider.get_vector_features_as_wkb_list(
...     name=test_vector_name, extent=None, feature_type="boundary"
... )
>>> for entry in wkb:
...     f_id, cat, string = entry
...     print(f_id, cat, len(string))
...
10 None 41
7 None 41
8 None 41
9 None 41
11 None 89
12 None 41
14 None 41
13 None 41
17 None 41
15 None 41
16 None 41

>>> provider.stop()

..
get_vector_table_as_dict(name, mapset=None, where=None)[source]

Return the attribute table of a vector map as dictionary.

See documentation of: pygrass.vector.VectorTopo::table_to_dict

Usage:

>>> from grass.pygrass.rpc import DataProvider
>>> provider = DataProvider()
>>> ret = provider.get_vector_table_as_dict(name=test_vector_name)
>>> ret["table"]
{1: [1, 'point', 1.0], 2: [2, 'line', 2.0], 3: [3, 'centroid', 3.0]}
>>> ret["columns"]
Columns([('cat', 'INTEGER'), ('name', 'varchar(50)'), ('value', 'double precision')])
>>> ret = provider.get_vector_table_as_dict(
...     name=test_vector_name, where="value > 1"
... )
>>> ret["table"]
{2: [2, 'line', 2.0], 3: [3, 'centroid', 3.0]}
>>> ret["columns"]
Columns([('cat', 'INTEGER'), ('name', 'varchar(50)'), ('value', 'double precision')])
>>> provider.get_vector_table_as_dict(name="no_map", where="value > 1")
>>> provider.stop()

..
start_server()[source]

This function must be re-implemented in the subclasses

class pygrass.rpc.RPCDefs[source]

Bases: object

GET_RASTER_IMAGE_AS_NP = 3
GET_VECTOR_FEATURES_AS_WKB = 2
GET_VECTOR_TABLE_AS_DICT = 1
G_FATAL_ERROR = 14
STOP = 0
pygrass.rpc.data_provider_server(lock, conn)[source]

The PyGRASS data provider server designed to be a target for multiprocessing.Process

Parameters
  • lock – A multiprocessing.Lock

  • conn – A multiprocessing.Pipe