def get(self, layer=1): """Return the first found category of given layer and the number of category found. :param layer: the number of layer :type layer: int """ cat = ctypes.c_int() n_cats = libvect.Vect_cat_get(self.c_cats, layer, ctypes.byref(cat)) return cat.value, n_cats
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
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 GetNearestNodeCat(e, n, layer, tresh, vectMap): if not haveCtypes: return -2 vectMapName, mapSet = ParseMapStr(vectMap) openedMap = pointer(vectlib.Map_info()) ret = vectlib.Vect_open_old(openedMap, c_char_p(encode(vectMapName)), c_char_p(encode(mapSet))) if ret == 1: vectlib.Vect_close(openedMap) if ret != 2: return -1 nodeNum = vectlib.Vect_find_node(openedMap, c_double(e), c_double(n), c_double(0), c_double(tresh), vectlib.WITHOUT_Z) if nodeNum > 0: e = c_double(0) n = c_double(0) vectlib.Vect_get_node_coor(openedMap, nodeNum, byref(e), byref(n), None) # z e = e.value n = n.value else: vectlib.Vect_close(openedMap) return -1 box = vectlib.bound_box() List = POINTER(vectlib.boxlist) List = vectlib.Vect_new_boxlist(c_int(0)) box.E = box.W = e box.N = box.S = n box.T = box.B = 0 vectlib.Vect_select_lines_by_box( openedMap, byref(box), vectlib.GV_POINT, List) found = 0 dcost = 0 Cats = POINTER(vectlib.line_cats) Cats = vectlib.Vect_new_cats_struct() cat = c_int(0) for j in range(List.contents.n_values): line = List.contents.id[j] type = vectlib.Vect_read_line(openedMap, None, Cats, line) if type != vectlib.GV_POINT: continue if vectlib.Vect_cat_get(Cats, c_int(layer), byref(cat)): found = 1 break if found: return cat.value return -1