Beispiel #1
0
def to_array(in_fc):
    """Extract the shapes and produce a coordinate array.
    """
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    in_flds = [oid_fld] + ['SHAPE@X', 'SHAPE@Y']
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, in_flds)
    a = a[['SHAPE@X', 'SHAPE@Y']]
    a = a.view(np.float64).reshape(a.shape[0], 2).copy()
    return a, SR
Beispiel #2
0
def to_array(in_fc):
    """Extract the shapes and produce a coordinate array.
    """
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    key_flds = ['SHAPE@X', 'SHAPE@Y']
    in_flds = [oid_fld] + key_flds
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, in_flds)
    uni, idx = np.unique(a[key_flds], True)
    uni_pnts = a[idx]
    #a = a[['SHAPE@X', 'SHAPE@Y']]
    a = uni.view(np.float64).reshape(uni.shape[0], 2)
    return a, uni_pnts, idx
Beispiel #3
0
def _demo():
    """A sample run demonstrating the principles and workflow"""
#    a = np.array([[0, 0], [0, 8], [10, 8], [10, 0], [3, 4], [7, 4]])
    pth = script.split("/")[:-2] + ['Point_tools.gdb', 'unsorted_pnts']
    in_fc = "/".join(pth)
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc,
                                          ['SHAPE@X', 'SHAPE@Y'], "", SR)
    a = a.view(np.dtype('float64')).reshape(a.shape[0], 2)
    idx, a_srt, d = dist_arr(a, prn=False)  # distance array and sorted pnts
    pairs = mst(d)                  # the orig-dest pairs for the mst
    o_d = connect(a_srt, d, pairs)  # produce an o-d structured array
    os = a_srt[pairs[:, 0]]
    ds = a_srt[pairs[:, 1]]
    fr_to = np.array(list(zip(os, ds)))
    return a, o_d, fr_to
Beispiel #4
0
def _demo():
    """A sample run demonstrating the principles and workflow"""
    #    a = np.array([[0, 0], [0, 8], [10, 8], [10, 0], [3, 4], [7, 4]])
    pth = script.split("/")[:-2] + ['Point_tools.gdb', 'unsorted_pnts']
    in_fc = "/".join(pth)
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, ['SHAPE@X', 'SHAPE@Y'], "",
                                          SR)
    a = a.view(np.dtype('float64')).reshape(a.shape[0], 2)
    idx, a_srt, d = dist_arr(a, prn=False)  # distance array and sorted pnts
    pairs = mst(d)  # the orig-dest pairs for the mst
    o_d = connect(a_srt, d, pairs)  # produce an o-d structured array
    os = a_srt[pairs[:, 0]]
    ds = a_srt[pairs[:, 1]]
    fr_to = np.array(list(zip(os, ds)))
    return a, o_d, fr_to
Beispiel #5
0
def test_envs(in_fc, cent, out_fc0, out_fc1):
    """ test the required parameters
    """
    # (1) ---- check input feature and for projected data
    if not arcpy.Exists(in_fc):
        tweet("\nThis file doesn't exist.\n")
        return False, []
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    #
    if SR.type != 'Projected':
        tweet("\nRadial sorts only make sense for projected data.\n")
        return False, []
    if shp_type != 'Point':
        tweet("\nYou need a point file.\n")
        return False, []
    #
    # (2) ---- check the output files
    if out_fc0 not in (None, 'None', " ", "", "#"):
        is_good = check_files(out_fc0)
        if not is_good:
            tweet("\nWrong path or filename?....{}\n".format(out_fc0))
            return False, []
    if out_fc1 not in (None, 'None', " ", "", "#"):
        is_good = check_files(out_fc1)
        if not is_good:
            tweet("\nWrong path or filename?....{}\n".format(out_fc1))
            return False, []
    #
    # (3) check the center ....
    if cent in (None, 'None', " ", "", "#"):
        cent = None
    elif isinstance(cent, str):
        for i in [", ", ",", ";"]:
            cent = cent.replace(i, " ")
        try:
            cent = [float(i.strip()) for i in cent.split(" ")]
            if len(cent) != 2:
                cent = [cent[0], cent[0]]
                tweet("\nBad center so I used... {} instead \n".format(cent))
        except ValueError:
            cent = None
            tweet("\nCenter used... {}\n".format(cent))
    # (4) all should be good
    return True, [out_fc0, out_fc1, cent, SR]
Beispiel #6
0
def test_envs(in_fc, cent, out_fc0, out_fc1):
    """ test the required parameters
    """
    # (1) ---- check input feature and for projected data
    if not arcpy.Exists(in_fc):
        tweet("\nThis file doesn't exist.\n")
        return False, []
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    #
    if SR.type != 'Projected':
        tweet("\nRadial sorts only make sense for projected data.\n")
        return False, []
    if shp_type != 'Point':
        tweet("\nYou need a point file.\n")
        return False, []
    #
    # (2) ---- check the output files
    if out_fc0 not in (None, 'None', " ", "", "#"):
        is_good = check_files(out_fc0)
        if not is_good:
            tweet("\nWrong path or filename?....{}\n".format(out_fc0))
            return False, []
    if out_fc1 not in (None, 'None', " ", "", "#"):
        is_good = check_files(out_fc1)
        if not is_good:
            tweet("\nWrong path or filename?....{}\n".format(out_fc1))
            return False, []
    #
    # (3) check the center ....
    if cent in (None, 'None', " ", "", "#"):
        cent = None
    elif isinstance(cent, str):
        for i in [", ", ",", ";"]:
            cent = cent.replace(i, " ")
        try:
            cent = [float(i.strip()) for i in cent.split(" ")]
            if len(cent) != 2:
                cent = [cent[0], cent[0]]
                tweet("\nBad center so I used... {} instead \n".format(cent))
        except ValueError:
            cent = None
            tweet("\nCenter used... {}\n".format(cent))
    # (4) all should be good
    return True, [out_fc0, out_fc1, cent, SR]
Beispiel #7
0
def _tool():
    in_fc = sys.argv[1]
    out_fc = sys.argv[2]
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    out_flds = [oid_fld, shp_fld]
    frmt = """\nScript.... {}\nUsing..... {}\nSR...{}\n"""
    args = [script, in_fc, SR.name]
    msg = frmt.format(*args)
    tweet(msg)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, shp_fld, "", SR)
    if len(a) >= 2:
        z = np.zeros((a.shape[0], 2))
        z[:, 0] = a['Shape'][:, 0]
        z[:, 1] = a['Shape'][:, 1]
        idx, a_srt, d = dist_arr(z)
        pairs = mst(d)
        o_d = connect(a_srt, d, pairs)

        os = a_srt[pairs[:, 0]]
        ds = a_srt[pairs[:, 1]]

        fr_to = np.array(list(zip(os, ds)))
        s = []
        for pt in fr_to:
            s.append(
                arcpy.Polyline(arcpy.Array([arcpy.Point(*p) for p in pt]), SR))

        if arcpy.Exists(out_fc):
            arcpy.Delete_management(out_fc)
        arcpy.CopyFeatures_management(s, out_fc)
    else:
        msg2 = """
        |
        ---- Potential User error......
        Technically the script didn't fail.... but...
        You need at least 2 different points... make sure you don't have an
        incorrect selection
        ---- Try again
        |
        """
        tweet(dedent(msg2))
Beispiel #8
0
def _tool():
    in_fc = sys.argv[1]
    out_fc = sys.argv[2]
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    out_flds = [oid_fld, shp_fld]
    frmt = """\nScript.... {}\nUsing..... {}\nSR...{}\n"""
    args = [script, in_fc, SR.name]
    msg = frmt.format(*args)
    tweet(msg)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, shp_fld, "", SR)
    if len(a) >= 2:
        z = np.zeros((a.shape[0], 2))
        z[:, 0] = a['Shape'][:, 0]
        z[:, 1] = a['Shape'][:, 1]
        idx, a_srt, d = dist_arr(z)
        pairs = mst(d)
        o_d = connect(a_srt, d, pairs)

        os = a_srt[pairs[:, 0]]
        ds = a_srt[pairs[:, 1]]

        fr_to = np.array(list(zip(os, ds)))
        s = []
        for pt in fr_to:
            s.append(arcpy.Polyline(arcpy.Array([arcpy.Point(*p) for p in pt]), SR))

        if arcpy.Exists(out_fc):
            arcpy.Delete_management(out_fc)
        arcpy.CopyFeatures_management(s, out_fc)
    else:
        msg2 = """
        |
        ---- Potential User error......
        Technically the script didn't fail.... but...
        You need at least 2 different points... make sure you don't have an
        incorrect selection
        ---- Try again
        |
        """
        tweet(dedent(msg2))
Beispiel #9
0
def connect(in_fc, out_fc, N=1, testing=False):
    """Run the analysis to form the closest point pairs.
    :  Calls n_near to produce the nearest features.
    """
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, shp_fld, "", SR)
    dt = '<f8'
    b = np.array([tuple(i) for i in a[shp_fld]], dtype=dt)
    coords, dist, n_array = n_near(b, N, ordered=True)  # ---- run n_near ----
    fr_to = coords[:, :(N + 1) * 2]
    frum = fr_to[:, :2]
    twos = fr_to[:, 2:].reshape(-1, N, 2)
    r = []
    for i in range(len(frum)):
        f = frum[i]
        t = twos[i]
        for j in range(len(t)):
            r.append(np.array([f, t[j]]))
    rr = np.array(r)
    r0 = np.array([i[np.lexsort((i[:, 1], i[:, 0]))] for i in rr])  # slicesort
    r1 = r0.reshape(-1, 4)
    r2 = _uniq_by_row_col(r1, axis=0)  # use if np.version < 1.13
    # r2 = unique_2d(r1)
    r3 = r2[np.argsort(r2[..., 0])]
    r3 = r3.reshape(-1, 2, 2)
    if not testing:
        s = []
        for pt in r3:
            arr = arcpy.Array([arcpy.Point(*p) for p in pt])
            s.append(arcpy.Polyline(arr, SR))
        if arcpy.Exists(out_fc):
            arcpy.Delete_management(out_fc)
        arcpy.CopyFeatures_management(s, out_fc)
        return None
    else:
        return a, b, r0, r1, r2, r3
Beispiel #10
0
def connect(in_fc, out_fc, N=1, testing=False):
    """Run the analysis to form the closest point pairs.
    :  Calls n_near to produce the nearest features.
    """
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, shp_fld, "", SR)
    dt = '<f8'
    b = np.array([tuple(i) for i in a[shp_fld]], dtype=dt)
    coords, dist, n_array = n_near(b, N, ordered=True)  # ---- run n_near ----
    fr_to = coords[:, :(N+1)*2]
    frum = fr_to[:, :2]
    twos = fr_to[:, 2:].reshape(-1, N, 2)
    r = []
    for i in range(len(frum)):
        f = frum[i]
        t = twos[i]
        for j in range(len(t)):
            r.append(np.array([f, t[j]]))
    rr = np.array(r)
    r0 = np.array([i[np.lexsort((i[:, 1], i[:, 0]))] for i in rr])  # slicesort
    r1 = r0.reshape(-1, 4)
    r2 = _uniq_by_row_col(r1, axis=0)  # use if np.version < 1.13
    # r2 = unique_2d(r1)
    r3 = r2[np.argsort(r2[..., 0])]
    r3 = r3.reshape(-1, 2, 2)
    if not testing:
        s = []
        for pt in r3:
            arr = arcpy.Array([arcpy.Point(*p) for p in pt])
            s.append(arcpy.Polyline(arr, SR))
        if arcpy.Exists(out_fc):
            arcpy.Delete_management(out_fc)
        arcpy.CopyFeatures_management(s, out_fc)
        return None
    else:
        return a, b, r0, r1, r2, r3
Beispiel #11
0
    in_fc = flder + fc
    angle = 30.0
    out_fc = flder + "/Point_tools.gdb/rot_std_dist"

else:
    testing = False
    in_fc = sys.argv[1]
    angle = float(sys.argv[2])
    out_fc = sys.argv[3]


# ---- convert to array, shift and return ----
# Apparently, there can be problems writing directly to a featureclass
# so, write to in_memory changing the required field names, then copy out
#
shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
arr = arcpy.da.FeatureClassToNumPyArray(in_fc, "*", "", SR, True)
a = arr[shp_fld]
new_pnts = trans_rot(a, angle)
nms = ['Feat_id', 'XYs'] + [i for i in arr.dtype.names[2:]]
arr.dtype.names = nms
arr['XYs'] = new_pnts
if not testing:
    arcpy.da.NumPyArrayToFeatureClass(arr, out_fc, ['XYs'])
#
msg = """
-------------------------------------
Input points..... {}
Rotation angle... {}
Output points.... {}
-------------------------------------
Beispiel #12
0
    angle = 30.0
    out_fc = flder + "/Point_tools.gdb/mesh_pnts"
    dx = 250.0
    dy = 250.0
    top_down = True
else:
    testing = False
    create_output = True
    extent_fc = sys.argv[1]  # must be a featureclass
    dx = abs(float(sys.argv[2]))
    dy = abs(float(sys.argv[3]))
    top_down = sys.argv[4]
    out_fc = sys.argv[5]

arcpy.env.overwriteOutput = True
shp_fld, oid_fld, shp_type, SR = fc_info(extent_fc)
desc = arcpy.da.Describe(extent_fc)
xtent = (desc['extent'].__str__()).split(" ")[:4]
L, B, R, T = [float(i) for i in xtent]
fld_names = ['X', 'Y']
pnts = mesh_xy(L, B, R, T, dx, dy, as_rec=True, top_down=top_down)
# ---- create output
if create_output:
    array_fc(pnts, out_fc, fld_names, SR)
ln = "-" * 70
frmt = """\n
:{}:
:Script... {}
:Output to..... {}
:using ........ {}
:Processing extent specified...
Beispiel #13
0
    flder = "/".join(script.split("/")[:-2])
    in_fc = flder + fc
    angle = 30.0
    out_fc = flder + "/Point_tools.gdb/rot_std_dist"

else:
    testing = False
    in_fc = sys.argv[1]
    angle = float(sys.argv[2])
    out_fc = sys.argv[3]

# ---- convert to array, shift and return ----
# Apparently, there can be problems writing directly to a featureclass
# so, write to in_memory changing the required field names, then copy out
#
shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
arr = arcpy.da.FeatureClassToNumPyArray(in_fc, "*", "", SR, True)
a = arr[shp_fld]
new_pnts = trans_rot(a, angle)
nms = ['Feat_id', 'XYs'] + [i for i in arr.dtype.names[2:]]
arr.dtype.names = nms
arr['XYs'] = new_pnts
if not testing:
    arcpy.da.NumPyArrayToFeatureClass(arr, out_fc, ['XYs'])
#
msg = """
-------------------------------------
Input points..... {}
Rotation angle... {}
Output points.... {}
-------------------------------------
Beispiel #14
0
    out_fc = flder + "/Point_tools.gdb/mesh_pnts"
    dx = 250.0
    dy = 250.0
    top_down = True
else:
    testing = False
    create_output = True
    extent_fc = sys.argv[1]  # must be a featureclass
    dx = abs(float(sys.argv[2]))
    dy = abs(float(sys.argv[3]))
    top_down = sys.argv[4]
    out_fc = sys.argv[5]


arcpy.env.overwriteOutput = True
shp_fld, oid_fld, shp_type, SR = fc_info(extent_fc)
desc = arcpy.da.Describe(extent_fc)
xtent = (desc['extent'].__str__()).split(" ")[:4]
L, B, R, T = [float(i) for i in xtent]
fld_names = ['X', 'Y']
pnts = mesh_xy(L, B, R, T, dx, dy, as_rec=True, top_down=top_down)
# ---- create output
if create_output:
  array_fc(pnts, out_fc, fld_names, SR)
ln = "-"*70
frmt = """\n
:{}:
:Script... {}
:Output to..... {}
:using ........ {}
:Processing extent specified...
Beispiel #15
0
: dx = 2
: dy = 2
: out_fc = r'C:\GIS\Table_tools\Table_tools.gdb\bb'
"""
import sys
import numpy as np
import arcpy
from arcpytools_pnt import fc_info, tweet

arcpy.env.overwriteOutput = True

# ---- input parameters ----
in_fc = sys.argv[1]
dx = float(sys.argv[2])
dy = float(sys.argv[3])
out_fc = sys.argv[4]
xy_shift = np.array([dx, dy], dtype="<f8")
shp_field, OIDField, shp_type, SR = fc_info(in_fc)
# ---- convert to array, shift and return ----
# Apparently, there can be problems writing directly to a featureclass
# so, write to in_memory changing the required field names, then copy out
arr = arcpy.da.FeatureClassToNumPyArray(in_fc, "*", "", SR, True)
arr[shp_field] = arr[shp_field] + xy_shift
nms = ['Feat_id', 'XYs'] + [i for i in arr.dtype.names[2:]]
arr.dtype.names = nms
temp_out = "in_memory/temp2"
arcpy.da.NumPyArrayToFeatureClass(arr, temp_out, ['XYs'])
arcpy.CopyFeatures_management(temp_out, out_fc)
del temp_out
# ---- the end ----
Beispiel #16
0
: dy = 2
: out_fc = r'C:\GIS\Table_tools\Table_tools.gdb\bb'
"""
import sys
import numpy as np
import arcpy
from arcpytools_pnt import fc_info, tweet

arcpy.env.overwriteOutput = True


# ---- input parameters ----
in_fc = sys.argv[1]
dx = float(sys.argv[2])
dy = float(sys.argv[3])
out_fc = sys.argv[4]
xy_shift = np.array([dx, dy], dtype="<f8")
shp_field, OIDField, shp_type, SR = fc_info(in_fc)
# ---- convert to array, shift and return ----
# Apparently, there can be problems writing directly to a featureclass
# so, write to in_memory changing the required field names, then copy out
arr = arcpy.da.FeatureClassToNumPyArray(in_fc, "*", "", SR, True)
arr[shp_field] = arr[shp_field] + xy_shift
nms = ['Feat_id', 'XYs'] + [i for i in arr.dtype.names[2:]]
arr.dtype.names = nms
temp_out = "in_memory/temp2"
arcpy.da.NumPyArrayToFeatureClass(arr, temp_out, ['XYs'])
arcpy.CopyFeatures_management(temp_out, out_fc)
del temp_out
# ---- the end ----