示例#1
0
    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)
示例#2
0
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
示例#3
0
 def read(self):
     """Read and set the coordinates of the centroid from the vector map,
     using the centroid_id and calling the Vect_read_line C function"""
     libvect.Vect_read_line(self.c_mapinfo, self.c_points, self.c_cats,
                            self.id)