def next(self): """:: >>> mun = Vector('boundary_municp_sqlite') >>> mun.open() >>> mun.next() Boundary(v_id=None) >>> mun.next() Boundary(v_id=None) >>> mun.close() .. """ v_id = self.c_mapinfo.contents.next_line v_id = v_id if v_id != 0 else None c_points = ctypes.pointer(libvect.line_pnts()) c_cats = ctypes.pointer(libvect.line_cats()) ftype = libvect.Vect_read_next_line(self.c_mapinfo, c_points, c_cats) if ftype == -2: raise StopIteration() if ftype == -1: raise #if GV_TYPE[ftype]['obj'] is not None: return GV_TYPE[ftype]['obj'](v_id=v_id, c_mapinfo=self.c_mapinfo, c_points=c_points, c_cats=c_cats)
def __init__(self, c_mapinfo, v_id, c_cats=None): self.c_mapinfo = c_mapinfo self.id = v_id if c_cats is not None: self.c_cats = c_cats else: self.c_cats = ctypes.pointer(libvect.line_cats()) self.get_area_cats()
def __init__(self, v_id=None, c_mapinfo=None, c_points=None, c_cats=None): self.id = v_id # vector id self.c_mapinfo = c_mapinfo # set c_points if c_points is None: self.c_points = ctypes.pointer(libvect.line_pnts()) else: self.c_points = c_points # set c_cats if c_cats is None: self.c_cats = ctypes.pointer(libvect.line_cats()) else: self.c_cats = c_cats
def read(self, feature_id): """Return a geometry object given the feature id. :: >>> mun = VectTopo('boundary_municp_sqlite') >>> mun.open() >>> feature1 = mun.read(0) #doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: The index must be >0, 0 given. >>> feature1 = mun.read(1) >>> feature1 Boundary(v_id=1) >>> feature1.length() 1415.3348048582038 >>> mun.read(-1) Centoid(649102.382010, 15945.714502) >>> len(mun) 8707 >>> mun.read(8707) Centoid(649102.382010, 15945.714502) >>> mun.read(8708) #doctest: +ELLIPSIS Traceback (most recent call last): ... IndexError: Index out of range >>> mun.close() .. """ if feature_id < 0: # Handle negative indices feature_id += self.__len__() + 1 if feature_id >= (self.__len__() + 1): raise IndexError('Index out of range') if feature_id > 0: c_points = ctypes.pointer(libvect.line_pnts()) c_cats = ctypes.pointer(libvect.line_cats()) ftype = libvect.Vect_read_line(self.c_mapinfo, c_points, c_cats, feature_id) if GV_TYPE[ftype]['obj'] is not None: return GV_TYPE[ftype]['obj'](v_id=feature_id, c_mapinfo=self.c_mapinfo, c_points=c_points, c_cats=c_cats) else: raise ValueError('The index must be >0, %r given.' % feature_id)
def __init__(self, c_cats=None): self.c_cats = c_cats if c_cats else ctypes.pointer(libvect.line_cats())
def areas_to_wkb_list(self, bbox=None, field=1): """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 bounding box. :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 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.areas_to_wkb_list(bbox=bbox) >>> len(result) 4 >>> for entry in result: ... a_id, cat, wkb = entry ... print((a_id, cat, len(wkb))) (1, 3, 225) (2, 3, 141) (3, 3, 93) (4, 3, 141) >>> result = test_vect.areas_to_wkb_list() >>> len(result) 4 >>> for entry in result: ... a_id, cat, wkb = entry ... print((a_id, cat, len(wkb))) (1, 3, 225) (2, 3, 141) (3, 3, 93) (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, 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, ctypes.byref(line_c)) if c_ok == 0: # Centroid found 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
def features_to_wkb_list(self, bbox=None, feature_type="point", field=1): """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 bounding box. :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 'line' -> libvect.GV_LINE 2 '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 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, ... feature_type="point") >>> len(result) 3 >>> for entry in result: ... f_id, cat, wkb = entry ... print((f_id, cat, len(wkb))) (1, 1, 21) (2, 1, 21) (3, 1, 21) >>> result = test_vect.features_to_wkb_list(bbox=None, ... feature_type="line") >>> len(result) 3 >>> for entry in result: ... f_id, cat, wkb = entry ... print((f_id, cat, len(wkb))) (4, 2, 57) (5, 2, 57) (6, 2, 57) >>> result = test_vect.features_to_wkb_list(bbox=bbox, ... feature_type="boundary") >>> len(result) 11 >>> 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))) (19, 3, 21) (18, 3, 21) (20, 3, 21) (21, 3, 21) >>> 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, ",".join(supported))) if bbox is None: bbox = self.bbox() 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() size = ctypes.c_size_t() cat = ctypes.c_int() error = ctypes.c_int() for f_id in bboxlist.ids: barray = libvect.Vect_read_line_to_wkb(self.c_mapinfo, ctypes.byref(line_p), ctypes.byref(line_c), f_id, ctypes.byref(size), ctypes.byref(error)) if not barray: if error == -1: raise GrassError(_("Unable to read line of feature %i"%(f_id))) if error == -2: print("Empty feature %i"%(f_id)) continue ok = libvect.Vect_cat_get(ctypes.byref(line_c), field, ctypes.byref(cat)) if ok < 1: pcat = None else: pcat = cat.value l.append((f_id, pcat, ctypes.string_at(barray, size.value))) libgis.G_free(barray) return l return None