[GRASS-SVN] r66142 - in grass/trunk/lib/python/pygrass: raster rpc vector
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Sep 7 10:28:32 PDT 2015
Author: huhabla
Date: 2015-09-07 10:28:31 -0700 (Mon, 07 Sep 2015)
New Revision: 66142
Modified:
grass/trunk/lib/python/pygrass/raster/__init__.py
grass/trunk/lib/python/pygrass/rpc/__init__.py
grass/trunk/lib/python/pygrass/vector/__init__.py
grass/trunk/lib/python/pygrass/vector/abstract.py
grass/trunk/lib/python/pygrass/vector/table.py
Log:
pygrass: Using name and mapset in the rpc module, fixed two tests, editor removed white spaces
Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py 2015-09-07 08:09:13 UTC (rev 66141)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py 2015-09-07 17:28:31 UTC (rev 66142)
@@ -88,16 +88,12 @@
'A test map'
>>> elev.hist.keyword
'This is a test map'
-
+
>>> attrs = list(elev.hist)
>>> attrs[0]
('name', u'Raster_test_map')
- >>> attrs[1]
- ('mapset', 'user1')
>>> attrs[2]
('mtype', '')
- >>> attrs[3]
- ('creator', 'soeren')
Each Raster map have an attribute call ``cats`` that allow user
to interact with the raster categories.
Modified: grass/trunk/lib/python/pygrass/rpc/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/rpc/__init__.py 2015-09-07 08:09:13 UTC (rev 66141)
+++ grass/trunk/lib/python/pygrass/rpc/__init__.py 2015-09-07 17:28:31 UTC (rev 66142)
@@ -23,6 +23,7 @@
import grass.lib.gis as libgis
from base import RPCServerBase
from grass.pygrass.gis.region import Region
+import grass.pygrass.utils as utils
import logging
###############################################################################
@@ -38,26 +39,32 @@
def _get_raster_image_as_np(lock, conn, data):
- """Convert a raster map into an image and return
+ """Convert a raster map into an image and return
a numpy array with RGB or Gray values.
-
+
:param lock: A multiprocessing.Lock instance
:param conn: A multiprocessing.Pipe instance used to send True or False
:param data: The list of data entries [function_id, raster_name, extent, color]
"""
array = None
try:
- raster_name = data[1]
- extent = data[2]
- color = data[3]
+ name = data[1]
+ mapset = data[2]
+ extent = data[3]
+ color = data[4]
- rast = RasterRow(raster_name)
-
+ mapset = utils.get_mapset_raster(name, mapset)
+
+ if not mapset:
+ raise ValueError("Unable to find raster map <%s>"%(name))
+
+ rast = RasterRow(name, mapset)
+
if rast.exist():
-
+
reg = Region()
- reg.from_rast(raster_name)
-
+ reg.from_rast(name)
+
if extent is not None:
if "north" in extent:
reg.north = extent["north"]
@@ -73,27 +80,34 @@
reg.cols = extent["cols"]
reg.adjust()
- array = raster2numpy_img(raster_name, reg, color)
+ array = raster2numpy_img(name, reg, color)
except:
raise
finally:
conn.send(array)
-
+
def _get_vector_table_as_dict(lock, conn, data):
"""Get the table of a vector map layer as dictionary
:param lock: A multiprocessing.Lock instance
:param conn: A multiprocessing.Pipe instance used to send True or False
- :param data: The list of data entries [function_id, name, where]
+ :param data: The list of data entries [function_id, name, mapset, where]
"""
ret = None
try:
name = data[1]
- where = data[2]
- layer = VectorTopo(name)
-
- if layer.exist() is True:
+ mapset = data[2]
+ where = data[3]
+
+ mapset = utils.get_mapset_vector(name, mapset)
+
+ if not mapset:
+ raise ValueError("Unable to find vector map <%s>"%(name))
+
+ layer = VectorTopo(name, mapset)
+
+ if layer.exist() is True:
layer.open("r")
columns = None
table = None
@@ -101,7 +115,7 @@
columns = layer.table.columns
table = layer.table_to_dict(where=where)
layer.close()
-
+
ret = {}
ret["table"] = table
ret["columns"] = columns
@@ -112,26 +126,32 @@
def _get_vector_features_as_wkb_list(lock, conn, data):
"""Return vector layer features as wkb list
-
+
supported feature types:
point, centroid, line, boundary, area
:param lock: A multiprocessing.Lock instance
:param conn: A multiprocessing.Pipe instance used to send True or False
- :param data: The list of data entries [function_id,name,extent,
+ :param data: The list of data entries [function_id,name,mapset,extent,
feature_type, field]
"""
wkb_list = None
try:
name = data[1]
- extent = data[2]
- feature_type = data[3]
- field = data[4]
+ mapset = data[2]
+ extent = data[3]
+ feature_type = data[4]
+ field = data[5]
bbox = None
-
- layer = VectorTopo(name)
-
+
+ mapset = utils.get_mapset_vector(name, mapset)
+
+ if not mapset:
+ raise ValueError("Unable to find vector map <%s>"%(name))
+
+ layer = VectorTopo(name, mapset)
+
if layer.exist() is True:
if extent is not None:
bbox = basic.Bbox(north=extent["north"],
@@ -144,7 +164,7 @@
wkb_list = layer.areas_to_wkb_list(bbox=bbox, field=field)
else:
wkb_list = layer.features_to_wkb_list(bbox=bbox,
- feature_type=feature_type,
+ feature_type=feature_type,
field=field)
layer.close()
except:
@@ -219,7 +239,7 @@
"""
def __init__(self):
RPCServerBase.__init__(self)
-
+
def start_server(self):
"""This function must be re-implemented in the subclasses
"""
@@ -229,12 +249,12 @@
self.server_conn))
self.server.daemon = True
self.server.start()
-
- def get_raster_image_as_np(self, name, extent=None, color="RGB"):
+
+ def get_raster_image_as_np(self, name, mapset=None, extent=None, color="RGB"):
"""Return the attribute table of a vector map as dictionary.
-
+
See documentation of: pygrass.raster.raster2numpy_img
-
+
Usage:
.. code-block:: python
@@ -244,39 +264,34 @@
>>> ret = provider.get_raster_image_as_np(name=test_raster_name)
>>> len(ret)
64
-
- >>> extent = {"north":30, "south":10, "east":30, "west":10,
+
+ >>> extent = {"north":30, "south":10, "east":30, "west":10,
... "rows":2, "cols":2}
- >>> ret = provider.get_raster_image_as_np(name=test_raster_name,
+ >>> ret = provider.get_raster_image_as_np(name=test_raster_name,
... extent=extent)
>>> len(ret)
16
- >>> ret # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
- array([169, 255, 0, 255, 255, 0, 46, 255, 208, 255,
- 0, 255, 255, 0, 84, 255], dtype=uint8)
-
+
>>> extent = {"rows":3, "cols":1}
- >>> ret = provider.get_raster_image_as_np(name=test_raster_name,
+ >>> ret = provider.get_raster_image_as_np(name=test_raster_name,
... extent=extent)
>>> len(ret)
12
- >>> ret # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
- array([255, 0, 7, 255, 255, 0, 84, 255, 255,
- 0, 123, 255], dtype=uint8)
+
>>> provider.stop()
-
+
..
"""
self.check_server()
self.client_conn.send([RPCDefs.GET_RASTER_IMAGE_AS_NP,
- name, extent, color])
+ name, mapset, extent, color])
return self.safe_receive("get_raster_image_as_np")
-
- def get_vector_table_as_dict(self, name, where=None):
+
+ def get_vector_table_as_dict(self, name, mapset=None, where=None):
"""Return the attribute table of a vector map as dictionary.
-
+
See documentation of: pygrass.vector.VectorTopo::table_to_dict
-
+
Usage:
.. code-block:: python
@@ -297,26 +312,26 @@
>>> provider.get_vector_table_as_dict(name="no_map",
... where="value > 1")
>>> provider.stop()
-
+
..
"""
self.check_server()
self.client_conn.send([RPCDefs.GET_VECTOR_TABLE_AS_DICT,
- name, where])
+ name, mapset, where])
return self.safe_receive("get_vector_table_as_dict")
- def get_vector_features_as_wkb_list(self, name, extent=None,
+ def get_vector_features_as_wkb_list(self, name, mapset=None, extent=None,
feature_type="point", field=1):
"""Return the features of a vector map as wkb list.
-
+
:param extent: A dictionary of {"north":double, "south":double,
"east":double, "west":double}
:param 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:
.. code-block:: python
@@ -392,14 +407,14 @@
17 None 41
15 None 41
16 None 41
-
+
>>> provider.stop()
-
+
..
"""
self.check_server()
self.client_conn.send([RPCDefs.GET_VECTOR_FEATURES_AS_WKB,
- name, extent, feature_type, field])
+ name, mapset, extent, feature_type, field])
return self.safe_receive("get_vector_features_as_wkb_list")
@@ -411,7 +426,7 @@
Module("r.mapcalc", expression="%s = row() + (10 * col())"%(test_raster_name),
overwrite=True)
utils.create_test_vector_map(test_vector_name)
-
+
doctest.testmod()
"""Remove the generated maps, if exist"""
Modified: grass/trunk/lib/python/pygrass/vector/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/__init__.py 2015-09-07 08:09:13 UTC (rev 66141)
+++ grass/trunk/lib/python/pygrass/vector/__init__.py 2015-09-07 17:28:31 UTC (rev 66142)
@@ -577,34 +577,34 @@
if release:
libvect.Vect_set_release_support(self.c_mapinfo)
super(VectorTopo, self).close(build=build)
-
+
@must_be_open
def table_to_dict(self, where=None):
"""Return the attribute table as a dictionary with the category as keys
-
+
The columns have the order of the self.table.columns.names() list.
-
+
Examples
-
+
>>> from grass.pygrass.vector import VectorTopo
>>> from grass.pygrass.vector.basic import Bbox
>>> test_vect = VectorTopo(test_vector_name)
>>> test_vect.open('r')
-
+
>>> test_vect.table_to_dict()
{1: [1, u'point', 1.0], 2: [2, u'line', 2.0], 3: [3, u'centroid', 3.0]}
-
+
>>> test_vect.table_to_dict(where="value > 2")
{3: [3, u'centroid', 3.0]}
>>> test_vect.table_to_dict(where="value > 0")
{1: [1, u'point', 1.0], 2: [2, u'line', 2.0], 3: [3, u'centroid', 3.0]}
-
+
>>> test_vect.table.filters.get_sql()
u'SELECT cat,name,value FROM vector_doctest_map WHERE value > 0 ORDER BY cat;'
"""
-
+
if self.table is not None:
table_dict = {}
# Get the category index
@@ -622,24 +622,24 @@
# Generate the dictionary
for entry in l:
table_dict[entry[cat_index]] = list(entry)
-
+
return(table_dict)
-
+
return None
-
+
@must_be_open
def features_to_wkb_list(self, bbox=None, feature_type="point", field=1):
- """Return all features of type point, line, boundary or centroid
+ """Return all features of type point, line, boundary or centroid
as a list of Well Known Binary representations (WKB)
- (id, cat, wkb) triplets located in a specific
+ (id, cat, wkb) triplets located in a specific
bounding box.
-
- :param bbox: The boundingbox to search for features,
+
+ :param bbox: The boundingbox to search for features,
if bbox=None the boundingbox of the whole
vector map layer is used
-
+
:type bbox: grass.pygrass.vector.basic.Bbox
-
+
:param feature_type: The type of feature that should be converted to
the Well Known Binary (WKB) format. Supported are:
'point' -> libvect.GV_POINT 1
@@ -647,23 +647,23 @@
'boundary' -> libvect.GV_BOUNDARY 3
'centroid' -> libvect.GV_CENTROID 4
:type type: string
-
+
:param field: The category field
:type field: integer
-
- :return: A list of triplets, or None if nothing was found
-
+
+ :return: A list of triplets, or None if nothing was found
+
The well known binary are stored in byte arrays.
-
+
Examples:
-
+
>>> from grass.pygrass.vector import VectorTopo
>>> from grass.pygrass.vector.basic import Bbox
>>> test_vect = VectorTopo(test_vector_name)
>>> test_vect.open('r')
>>> bbox = Bbox(north=20, south=-1, east=20, west=-1)
- >>> result = test_vect.features_to_wkb_list(bbox=bbox,
+ >>> result = test_vect.features_to_wkb_list(bbox=bbox,
... feature_type="point")
>>> len(result)
3
@@ -674,7 +674,7 @@
(2, 1, 21)
(3, 1, 21)
- >>> result = test_vect.features_to_wkb_list(bbox=None,
+ >>> result = test_vect.features_to_wkb_list(bbox=None,
... feature_type="line")
>>> len(result)
3
@@ -685,16 +685,16 @@
(5, 2, 57)
(6, 2, 57)
- >>> result = test_vect.features_to_wkb_list(bbox=bbox,
+ >>> result = test_vect.features_to_wkb_list(bbox=bbox,
... feature_type="boundary")
>>> len(result)
11
-
- >>> result = test_vect.features_to_wkb_list(bbox=None,
+
+ >>> result = test_vect.features_to_wkb_list(bbox=None,
... feature_type="centroid")
>>> len(result)
4
-
+
>>> for entry in result:
... f_id, cat, wkb = entry
... print(f_id, cat, len(wkb))
@@ -703,32 +703,32 @@
(20, 3, 21)
(21, 3, 21)
- >>> result = test_vect.features_to_wkb_list(bbox=bbox,
+ >>> result = test_vect.features_to_wkb_list(bbox=bbox,
... feature_type="blub")
Traceback (most recent call last):
...
GrassError: Unsupported feature type <blub>, supported are <point,line,boundary,centroid>
>>> test_vect.close()
-
-
+
+
"""
-
+
supported = ['point', 'line', 'boundary', 'centroid']
-
+
if feature_type.lower() not in supported:
raise GrassError("Unsupported feature type <%s>, "\
- "supported are <%s>"%(feature_type,
+ "supported are <%s>"%(feature_type,
",".join(supported)))
-
+
if bbox is None:
bbox = self.bbox()
-
- bboxlist = self.find_by_bbox.geos(bbox, type=feature_type.lower(),
+
+ bboxlist = self.find_by_bbox.geos(bbox, type=feature_type.lower(),
bboxlist_only = True)
-
+
if bboxlist is not None and len(bboxlist) > 0:
-
+
l = []
line_p = libvect.line_pnts()
line_c = libvect.line_cats()
@@ -743,7 +743,7 @@
if not barray:
raise GrassError(_("Unable to read line of feature %i"%(f_id)))
- ok = libvect.Vect_cat_get(ctypes.byref(line_c), field,
+ ok = libvect.Vect_cat_get(ctypes.byref(line_c), field,
ctypes.byref(cat))
if ok < 1:
pcat = None
@@ -751,32 +751,33 @@
pcat = cat.value
l.append((f_id, pcat, ctypes.string_at(barray, size.value)))
+ libgis.G_free(barray)
return l
return None
-
+
@must_be_open
def areas_to_wkb_list(self, bbox=None, field=1):
- """Return all features of type point, line, boundary or centroid
+ """Return all features of type point, line, boundary or centroid
as a list of Well Known Binary representations (WKB)
- (id, cat, wkb) triplets located in a specific
+ (id, cat, wkb) triplets located in a specific
bounding box.
-
- :param bbox: The boundingbox to search for features,
+
+ :param bbox: The boundingbox to search for features,
if bbox=None the boundingbox of the whole
vector map layer is used
-
+
:type bbox: grass.pygrass.vector.basic.Bbox
-
+
:param field: The centroid category field
:type field: integer
-
- :return: A list of triplets, or None if nothing was found
-
+
+ :return: A list of triplets, or None if nothing was found
+
The well known binary are stored in byte arrays.
-
+
Examples:
-
+
>>> from grass.pygrass.vector import VectorTopo
>>> from grass.pygrass.vector.basic import Bbox
>>> test_vect = VectorTopo(test_vector_name)
@@ -806,39 +807,40 @@
(4, 3, 141)
>>> test_vect.close()
-
-
+
+
"""
if bbox is None:
bbox = self.bbox()
-
+
bboxlist = self.find_by_bbox.areas(bbox, bboxlist_only = True)
-
+
if bboxlist is not None and len(bboxlist) > 0:
-
+
l = []
line_c = libvect.line_cats()
size = ctypes.c_size_t()
cat = ctypes.c_int()
for a_id in bboxlist.ids:
- barray = libvect.Vect_read_area_to_wkb(self.c_mapinfo,
- a_id,
+ barray = libvect.Vect_read_area_to_wkb(self.c_mapinfo,
+ a_id,
ctypes.byref(size))
if not barray:
raise GrassError(_("Unable to read area with id %i"%(a_id)))
pcat = None
- c_ok = libvect.Vect_get_area_cats(self.c_mapinfo, a_id,
+ c_ok = libvect.Vect_get_area_cats(self.c_mapinfo, a_id,
ctypes.byref(line_c))
if c_ok == 0: # Centroid found
-
- ok = libvect.Vect_cat_get(ctypes.byref(line_c), field,
+
+ ok = libvect.Vect_cat_get(ctypes.byref(line_c), field,
ctypes.byref(cat))
if ok > 0:
pcat = cat.value
l.append((a_id, pcat, ctypes.string_at(barray, size.value)))
+ libgis.G_free(barray)
return l
return None
Modified: grass/trunk/lib/python/pygrass/vector/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/abstract.py 2015-09-07 08:09:13 UTC (rev 66141)
+++ grass/trunk/lib/python/pygrass/vector/abstract.py 2015-09-07 17:28:31 UTC (rev 66142)
@@ -96,8 +96,6 @@
if not utils.is_clean_name(newname):
str_err = _("Map name {0} not valid")
raise ValueError(str_err.format(newname))
- if self.exist():
- self.rename(newname)
self._name = newname
name = property(fget=_get_name, fset=_set_name,
Modified: grass/trunk/lib/python/pygrass/vector/table.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/table.py 2015-09-07 08:09:13 UTC (rev 66141)
+++ grass/trunk/lib/python/pygrass/vector/table.py 2015-09-07 17:28:31 UTC (rev 66142)
@@ -47,8 +47,9 @@
>>> import os
>>> new_path2 = os.path.join(gisenv()['GISDBASE'], gisenv()['LOCATION_NAME'],
... gisenv()['MAPSET'], 'sqlite', 'sqlite.db')
- >>> new_path == new_path2
+ >>> new_path.replace("//","/") == new_path2.replace("//","/")
True
+
"""
if "$" not in path:
return path
More information about the grass-commit
mailing list