def getcoords(self): """ Method creating a dict() of point coordinates: {a:(xa,ya), b:(xb,yb)...} """ # Create a new Map_info() object map = vect.pointer(vect.Map_info()) # Load the vector map to Map_info() object. Level should be 2 (with topology) vect.Vect_open_old2(map, self.layer, "", "-1") # Get number of point features (1) in the layer n_lines = vect.Vect_get_num_primitives(map, 1) # Create new line and categories structures line = vect.Vect_new_line_struct() cats = vect.Vect_new_cats_struct() # Make an empty list to store all feature cats and their coordinates in coordsdict = {} # Iterate through all features and write their coordinates to the list for i in xrange(0, n_lines): # Read next line from Map_info() vect.Vect_read_next_line(map, line, cats, 1) # Get line structure values, i.e. coordinates x = line.contents.x[0] y = line.contents.y[0] # Get the point category number cat = cats.contents.cat[0] coordsdict[cat] = (x, y) # Do some cleanup vect.Vect_destroy_line_struct(line) vect.Vect_destroy_cats_struct(cats) vect.Vect_close(map) # Return coordinate dictionary. Example: {1: (635185.6745587245, 6434401.869609355), 3: (634763.0860512792, 6437526.1793751), 4: (636855.7953351085, 6435785.045250954), 5: (636705.1202666728, 6432286.035328391), 6: (633607.9105266054, 6432286.035328391), 7: (632762.4559759387, 6435655.297275356)} return coordsdict
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