def _demo(): """Code to run if in demo mode Requires: arcpytools fc_info, tweet """ tbl = "Table_tools.gdb/pnts_2k_normal" in_tbl = "/".join(script.split("/")[:-2] + [tbl]) # _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # in_fld = 'Unif' # 'Sequences2' #'Ys' del_fld = True out_fld = 'Result_fld' in_flds = [oid_fld, in_fld] # OBJECTID, plus another field in_arr = tbl_2_nparray(in_tbl, in_flds) c = np.array(['cumulative sum', 'diff from max', 'diff from mean', 'diff from median', 'diff from min', 'diff from value', 'percent', 'sequential diff', 'sequential number', 'z_score']) func = 'percent' #np.random.choice(c) xtend = False val = None return in_tbl, in_arr, in_fld, out_fld, del_fld, func, xtend, val
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, shp_fld] a = arcpy.da.FeatureClassToNumPyArray(in_fc, in_flds) a = a[shp_fld] return a
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) return a
def _tool(): """run when script is from a tool """ in_tbl = sys.argv[1] in_fld = sys.argv[2] out_fld = sys.argv[3] # output field name func = sys.argv[4] win_size = int(sys.argv[5]) # ---- main tool section desc = arcpy.da.Describe(in_tbl) # ---- main tool section _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # flds = [oid_fld, in_fld] tbl_path = desc['path'] fnames = [i.name for i in arcpy.ListFields(in_tbl)] if out_fld in fnames: out_fld += 'dup' out_fld = arcpy.ValidateFieldName(out_fld, tbl_path) args = [in_tbl, in_fld, out_fld, tbl_path] msg = "in_tbl {}\nin_fld {}\nout_fld {}\ntbl_path {}".format(*args) tweet(msg) # # ---- call section for processing function # _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # # ---- remove the selection by calling the table in_tbl = desc['catalogPath'] # flds = [oid_fld, in_fld] in_arr = tbl_2_nparray(in_tbl, flds) tweet("{!r:}".format(in_arr)) xtend = True return in_tbl, in_arr, in_fld, out_fld, func, win_size, xtend
def _demo(): """Code to run if in demo mode """ tbl = "Table_tools.gdb/pnts_2k_normal" in_tbl = "/".join(script.split("/")[:-2] + [tbl]) # _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # in_fld = 'Sequences' # 'Norm' # 'Ys' out_fld = 'Result_fld' in_flds = [oid_fld, in_fld] # OBJECTID, plus another field in_arr = tbl_2_nparray(in_tbl, in_flds) func = 'mean' #np.random.choice(c) win_size = 5 xtend = False return in_tbl, in_arr, in_fld, out_fld, func, win_size, xtend
def _tool(): """run when script is from a tool """ in_tbl = sys.argv[1] in_fld = sys.argv[2] stepsize = int(sys.argv[3]) out_tbl = sys.argv[4] # output field name # # ---- main tool section _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # flds = [oid_fld, in_fld] in_arr = arcpy.da.TableToNumPyArray(in_tbl, flds, skip_nulls=False, null_value=-1) a = in_arr[in_fld] # do stuff with array tweet("{!r:}".format(a)) return in_tbl, a, in_fld, stepsize, out_tbl
def _tool(): """run when script is from a tool """ in_tbl = sys.argv[1] in_fld = sys.argv[2] func = sys.argv[3] out_fld = sys.argv[4] # output field name del_fld = sys.argv[5] # # ---- main tool section _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # flds = [oid_fld, in_fld] in_arr = tbl_2_nparray(in_tbl, flds) tweet("{!r:}".format(in_arr)) xtend = True return in_tbl, in_arr, in_fld, out_fld, del_fld, func, xtend
def _tool(): """run when script is from a tool """ in_tbl = sys.argv[1] in_fld = sys.argv[2] func = sys.argv[3] out_fld = sys.argv[4] # output field name del_fld = sys.argv[5] val = sys.argv[6] # # ---- main tool section _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # flds = [oid_fld, in_fld] in_arr = tbl_2_nparray(in_tbl, flds) tweet("{!r:}".format(in_arr)) xtend = True return in_tbl, in_arr, in_fld, out_fld, del_fld, func, xtend, val
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 != "#": is_good = check_files(out_fc0) if not is_good: tweet("\nWrong path or filename?....{}\n".format(out_fc0)) return False, [] if out_fc1 != "#": 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]
def _demo(): """Code to run if in demo mode Requires: arcpytools fc_info, tweet """ tbl = "Table_tools.gdb/pnts_2k_normal" in_tbl = "/".join(script.split("/")[:-2] + [tbl]) # _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # in_fld = 'Text01' # 'Sequences2' #'Ys' del_fld = True out_fld = 'Result_fld' in_flds = [oid_fld, in_fld] # OBJECTID, plus another field in_arr = tbl_2_nparray(in_tbl, in_flds) # c = np.array(['sequential text']) func = 'sequential text' xtend = False return in_tbl, in_arr, in_fld, out_fld, del_fld, func, xtend
def _demo(): """Code to run if in demo mode Requires: arcpytools fc_info, tweet """ tbl = "Table_tools.gdb/pnts_2k_normal" in_tbl = "/".join(script.split("/")[:-2] + [tbl]) # _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # in_fld = 'SequenceTxt' #'Sequences2' # 'SequenceTxt' stepsize = 0 in_flds = [oid_fld, in_fld] # OBJECTID, plus another field a = arcpy.da.TableToNumPyArray(in_tbl, in_flds, skip_nulls=False, null_value=-1) # a = [1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 4, 4, 3, 3, 3, 2, 1] a = a[in_fld] out_tbl = None return in_tbl, a, in_fld, stepsize, out_tbl
def _tool(): """run when script is from a tool """ in_tbl = sys.argv[1] in_fld = sys.argv[2] out_fld = sys.argv[3] # output field name # ---- main tool section desc = arcpy.da.Describe(in_tbl) # ---- main tool section _, oid_fld, _, _ = fc_info(in_tbl, prn=False) # run fc_info # flds = [oid_fld, in_fld] tbl_path = desc['path'] fnames = [i.name for i in arcpy.ListFields(in_tbl)] if out_fld in fnames: out_fld += 'dup' out_fld = arcpy.ValidateFieldName(out_fld, tbl_path) args = [in_tbl, in_fld, out_fld, tbl_path] msg = "in_tbl {}\nin_fld {}\nout_fld {}\ntbl_path {}".format(*args) tweet(msg) # # ---- call section for processing function # #in_arr = arcpy.da.TableToNumPyArray(in_tbl, vals) # old in_arr = tbl_2_nparray(in_tbl, flds) # produce the table # tweet("{!r:}".format(in_arr)) # a0 = in_arr[in_fld] # # do stuff here ******************************** # sze = a0.dtype.str # ---- reassemble the table for extending dt = [('IDs', '<i8'), (out_fld, sze)] out_array = np.copy(in_arr.shape[0]) out_array[out_fld] = a0 # result goes here out_array.dtype = dt arcpy.da.ExtendTable(in_tbl, 'OBJECTID', out_array, 'IDs')
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, "*", "", 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
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, "*", "", 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
ft = {'bool': lambda x: repr(x.astype(np.int32)), 'float_kind': '{: 0.1f}'.format} np.set_printoptions(edgeitems=10, linewidth=100, precision=2, suppress=True, threshold=120, formatter=ft) np.ma.masked_print_option.set_display('-') script = sys.argv[0] # ---- Convert the featureclass to points returning base information ---- # ---- The 'Shape' field is changed to X and Y to facilitate sorting etc. in_fc = sys.argv[1] srt_order = sys.argv[2] ascend = sys.argv[3] out_fc = sys.argv[4] shp_fld, oid_fld, shp_type, SR = fc_info(in_fc) a = arcpy.da.FeatureClassToNumPyArray(in_fc, "*", "", SR) dt = [('X', '<f8'), ('Y', '<f8')] shps = np.array([tuple(i) for i in a[shp_fld]], dtype=dt) if srt_order == 'X': idx = np.argsort(shps, order=('X', 'Y')) else: idx = np.argsort(shps, order=('Y', 'X')) shps = a[idx] if not ascend: shps = shps[::-1] arcpy.da.NumPyArrayToFeatureClass(shps, out_fc, shp_fld, SR)
flder = "/".join(script.split("/")[:-2]) in_fc = flder + fc angle = 30.0 out_fc = flder + "/Point_tools.gdb/rot_std_dist2" 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 arcpy.da.NumPyArrayToFeatureClass(arr, out_fc, ['XYs']) msg = """ ------------------------------------- Input points..... {} Rotation angle... {} Output points.... {} """ tweet(msg.format(in_fc, angle, out_fc))
: dy = 2 : out_fc = r'C:\GIS\Table_tools\Table_tools.gdb\bb' """ import sys import numpy as np import arcpy from arcpytools 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 ----
: 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 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 ----