Esempio n. 1
0
 def __new__(cls,
             table_path: str,
             field,
             where_clause: _tp.Union[None, str, _q.Where] = None):
     # Populate the frozenset
     with _cursors.SearchCursor(table_path, field, where_clause) as rows:
         return super(ValueSet, cls).__new__(cls,
                                             (value for value, in rows))
Esempio n. 2
0
    def _populate(self, table_path, fields, where_clause=None, **kwargs):
        """ Populates the lookup with data, calling _process_row() on each row returned by the SearchCursor. """
        try:
            # Validate fields
            self._check_fields(fields, self._get_fields(table_path))

            # Validate row processor function (if any)
            row_func = kwargs.get(_ROWFUNC_ARG, self._process_row)
            has_self = self._has_self(row_func)

            with _cursors.SearchCursor(table_path, fields, where_clause) as rows:
                for row in rows:
                    failed = row_func(row, **kwargs) if has_self else row_func(self, row, **kwargs)
                    if failed:
                        raise Exception(failed)

        except Exception as e:
            raise RuntimeError('Failed to create {} for {}: {}'.format(self.__class__.__name__,
                                                                       _tu.to_repr(table_path), e))
Esempio n. 3
0
    def _populate(self, fc_path, where_clause, all_vertices):
        """ Populates the NodeSet with node keys. """

        field, all_vertices = self._fix_params(fc_path, all_vertices)

        # Iterate over all geometries and add keys
        with _cursors.SearchCursor(fc_path, field, where_clause) as rows:
            for shape, in rows:
                # If the geometry is a simple coordinate tuple, immediately add it
                if field.startswith(_const.FIELD_XY):
                    self.add(get_nodekey(*shape))
                    continue

                if all_vertices:
                    for coord in _geo.get_vertices(shape):
                        self.add(get_nodekey(*coord))
                    continue

                # When *all_vertices* is False (or the geometry is not a Multipoint), only get the start/end nodes
                self.add(get_nodekey(shape.firstPoint))
                self.add(get_nodekey(shape.lastPoint))
Esempio n. 4
0
    def num_rows(self,
                 where_clause: _tp.Union[str, _q.Where, None] = None) -> int:
        """
        Returns the number of rows for a table or feature class.

        If the current ``Describe`` object does not support this action or does not have any rows, 0 will be returned.

        :param where_clause:    An optional where clause to base the row count on.
        :type where_clause:     str, gpf.tools.queries.Where
        """
        field = None
        if where_clause:
            if isinstance(where_clause, str):
                field = _tu.unquote(where_clause.split()[0])
            elif hasattr(where_clause, 'fields'):
                field = where_clause.fields[0]
            else:
                raise ValueError(
                    'where_clause must be a string or Where instance')

        try:
            if field:
                # Iterate over the dataset rows, using the (first) field from the where_clause
                with _cursors.SearchCursor(self.catalogPath,
                                           field,
                                           where_clause=where_clause) as rows:
                    num_rows = sum(1 for _ in rows)
                del rows
            else:
                # Use the ArcPy GetCount() tool for the row count
                num_rows = int(
                    _arcpy.GetCount_management(self.catalogPath).getOutput(0))
        except Exception as e:
            _warn(str(e), DescribeWarning)
            num_rows = 0

        return num_rows