Exemple #1
0
def pnts_arr(in_fc, as_struct=True, shp_fld=None, SR=None):
    """Create points from an array.

    Requires:
    --------

    `in_fc` :
        input featureclass

    `as_struct` : boolean
        - True, returns a structured array with Id, X, Y fields
        - False, returns an ndarray with dtype='<f8'

    `shp_fld` & `SR` :
        if unspecified, it will be determined using fc_info
        to return featureclass information.
    """
    if shp_fld is None or SR is None:
        shp_fld, oid_fld, SR, shp_type = fc_info(in_fc)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, "*", "", SR)
    dt = [('Id', '<i4'), ('X', '<f8'), ('Y', '<f8')]
    if as_struct:
        shps = np.array([i for i in a[[oid_fld, shp_fld]]], dtype=dt)
    else:
        shps = a[shp_fld]
    return shps
Exemple #2
0
def _id_geom_array(in_fc):
    """The main code segment which gets the id and shape information and
    explodes the geometry to individual points
    """
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    a_flds = [oid_fld, shp_fld]
    a = arcpy.da.FeatureClassToNumPyArray(in_fc,
                                          field_names=a_flds,
                                          explode_to_points=True,
                                          spatial_reference=SR)
    dt = [('Idx', '<i4'), ('Xs', '<f8'), ('Ys', '<f8')]
    a.dtype = dt
    return a
Exemple #3
0
def _two_arrays(in_fc, both=True, split=True):
    """Send to a numpy structured/array and split it into a geometry array
    and an attribute array.  They can be joined back later if needed.

    Note
    ----
        The geometry array is returned as an object array.  See the
        main documentation

    Requires:
    --------

    functions:
        _xyID
            function to get geometry array
        _ndarray
            function to get the x, y, id array and attribute array
        fc_info(in_fc)
            function needed to return fc properties
    parameters:
        both
            True, to return both arrays, False to return just geometry
        split
            True, split points by their geometry groups as an object array;
            False, a sequential array with shape = (N,)
    variables:

    >>> dt_a = [('IDs', '<i4'), ('Xs', '<f8'), ('Ys', '<f8')]
    >>> dt_b = [('IDs', '<i4'), ('Xc', '<f8'), ('Yc', '<f8')]
    >>> dt_b.extend(b.dtype.descr[2:])

        Extend the dtype using the attribute dtype minus geometry and id
    """
    a = _xyID(in_fc, to_pnts=True)
    shp_fld, oid_fld, SR, shp_type = fc_info(in_fc)
    dt_a = [('IDs', '<i4'), ('Xs', '<f8'), ('Ys', '<f8')]
    dt_b = [('IDs', '<i4'), ('Xc', '<f8'), ('Yc', '<f8')]
    a.dtype = dt_a
    b = None
    if split:
        ids = np.unique(a['IDs'])
        w = np.where(np.diff(a['IDs']))[0] + 1
        a = np.split(a, w)
        a = np.array([[ids[i], a[i][['Xs', 'Ys']]] for i in range(len(ids))])
    if both:
        b = _ndarray(in_fc, to_pnts=False, flds=None, SR=None)
        dt_b.extend(b.dtype.descr[2:])
        b.dtype = dt_b
    return a, b
Exemple #4
0
def _two_arrays(in_fc, both=True, split=True):
    """Create structured/arrays split into a geometry and an attribute array.
    They can be joined back later if needed. The geometry array is returned as
    an object array.  See the main documentation.

    Parameters
    ----------
    both : boolean
        True, to return both arrays. False, to return just geometry
    split : boolean
        True, split points by their geometry groups as an object array.
        False, a sequential array with shape = (N,)

    Notes
    -----
    Requires the functions
    - _xyID, function to get geometry array
    - _ndarray, function to get the x, y, id array and attribute array
    - fc_info(in_fc), function needed to return fc properties

    >>> dt_a = [('IDs', '<i4'), ('Xs', '<f8'), ('Ys', '<f8')]
    >>> dt_b = [('IDs', '<i4'), ('Xc', '<f8'), ('Yc', '<f8')]
    >>> dt_b.extend(b.dtype.descr[2:])

    Extend the dtype using the attribute dtype minus geometry and id
    """
    a = _xyID(in_fc, to_pnts=True)
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    dt_a = [('IDs', '<i4'), ('Xs', '<f8'), ('Ys', '<f8')]
    dt_b = [('IDs', '<i4'), ('Xc', '<f8'), ('Yc', '<f8')]
    a.dtype = dt_a
    b = None
    if split:
        ids = np.unique(a['IDs'])
        w = np.where(np.diff(a['IDs']))[0] + 1
        a = np.split(a, w)
        a = np.array([[ids[i], a[i][['Xs', 'Ys']]] for i in range(len(ids))])
    if both:
        b = _ndarray(in_fc, to_pnts=False, flds=None, SR=SR)
        dt_b = dt_b + b.dtype.descr[3:]
        b.dtype = dt_b
    return a, b
Exemple #5
0
def fc_array(in_fc, flds="*", allpnts=True):
    """Convert a featureclass to an ndarray...with optional fields besides the
    FID/OIDName and Shape fields.

    Parameters:
    -----------
    in_fc : text
        Full path to the geodatabase and the featureclass name
    flds : text or list
        - ``''   : just an object id and shape field``
        - ``'*'  : all fields in the featureclass or``
        - ``list : specific fields ['OBJECTID','Shape','SomeClass', etc]``
    allpnts : boolean
        - True `explodes` geometry to individual points.
        - False returns the centroid

    Requires:
    ---------
        fc_info(in_fc) function

    See also:
    ---------
        FeatureClassToNumPyArray, ListFields for more information in current
        arcpy documentation
    """
    out_flds = []
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)  # get the base information
    flds_all = arcpy.ListFields(in_fc)
    flds_oth = [f for f in flds_all if f.type not in ('OID', 'Geometry')]
    fld_names = [f.name for f in flds_oth]
    oid_geom = [oid_fld, 'SHAPE@X', 'SHAPE@Y']
    nulls = {
        'Double': np.nan,
        'Single': np.nan,
        'Short': np.iinfo(np.int16).min,
        'SmallInteger': np.iinfo(np.int16).min,
        'Long': np.iinfo(np.int32).min,
        'Float': np.nan,
        'Integer': np.iinfo(np.int32).min,
        'String': str(None),
        'Text': str(None)
    }
    fld_dict = {i.name: i.type for i in flds_oth}
    null_dict = {f: nulls[fld_dict[f]] for f in fld_names}
    if flds == "":  # return just OID and Shape values
        out_flds = oid_geom  # FID and Shape X, Y
    elif flds == "*":  # all fields
        out_flds = oid_geom + fld_names
    else:
        out_flds = [oid_fld, 'SHAPE@X', 'SHAPE@Y']
        for f in flds_oth:
            if f.name in flds:
                out_flds.append(f.name)
    frmt = """\nRunning 'fc_array' with ....
    \nfeatureclass... {}\nFields... {}\nAll pnts... {}\nSR... {}
    """
    args = [in_fc, out_flds, allpnts, SR.name]
    msg = dedent(frmt).format(*args)
    tweet(msg)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc,
                                          field_names=out_flds,
                                          where_clause="",
                                          spatial_reference=SR,
                                          explode_to_points=allpnts,
                                          skip_nulls=False,
                                          null_value=null_dict)
    # out it goes in array format
    return a