def tool(): """run the tool """ in_fc = sys.argv[1] N = int(sys.argv[2]) out_fc = sys.argv[3] args = [script, in_fc, N, out_fc] tweet(frmt.format(*args)) # call tweet connect(in_fc, out_fc, N=N, testing=False) # call connect
def tool(): """ run the tool""" in_fc = sys.argv[1] N = int(sys.argv[2]) out_tbl = sys.argv[3] args = [script, in_fc, N, out_tbl] tweet(frmt.format(*args)) # call tweet a = to_array(in_fc) # call to_array nt = near_tbl(a, b=None, N=N) # call near_tbl tweet("\nnear table\n{}".format(nt.reshape(nt.shape[0], 1))) arcpy.da.NumPyArrayToTable(nt, out_tbl)
def form_output(in_tbl, in_arr, out_fld="Result_", del_fld=True, vals=None, idx=0, xtend=False): """Form the output table given a field name and join field Requires: --------- tbl : input table fld_name : output field names, should contain OBJECTID and desired output field vals : values for output field sze : string representation of output field idx : index to start values from... usually 0 or 1 (ie for sequential) """ desc = arcpy.da.Describe(in_tbl) tbl_path = desc['path'] oid_fld = desc['OIDFieldName'] # 'OBJECTID' fnames = [i.name for i in arcpy.ListFields(in_tbl)] if del_fld in ('True', 'true', True, 1): del_fld = True else: del_fld = False if out_fld not in fnames: out_fld = out_fld elif out_fld in fnames and del_fld: arcpy.DeleteField_management(in_tbl, out_fld) tweet("\nDeleting field {}".format(out_fld)) else: out_fld += 'dup' out_fld = arcpy.ValidateFieldName(out_fld, tbl_path) # sze = vals.dtype.str dt = [('IDs', '<i4'), (out_fld, sze)] # ie '<f8' out_array = np.zeros((in_arr.shape[0], ), dtype=dt) out_array['IDs'] = in_arr[oid_fld] out_array[out_fld][idx:] = vals if xtend: arcpy.da.ExtendTable(in_tbl, oid_fld, out_array, 'IDs') return out_array
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 form_output(in_tbl, in_arr, out_fld="Result_", vals=None, idx=0, xtend=False): """Form the output table given a field name and join field Requires: --------- tbl : input table fld_name : output field names, should contain OBJECTID and desired output field vals : values for output field sze : string representation of output field idx : index to start values from... usually 0 or 1 (ie for sequential) """ desc = arcpy.da.Describe(in_tbl) tbl_path = desc['path'] oid_fld = desc['OIDFieldName'] # 'OBJECTID' del_fld = True fnames = [i.name for i in arcpy.ListFields(in_tbl)] if del_fld in ('True', 'true', True, 1): del_fld = True else: del_fld = False if out_fld not in fnames: out_fld = out_fld elif out_fld in fnames and del_fld: arcpy.DeleteField_management(in_tbl, out_fld) tweet("\nDeleting field {}".format(out_fld)) else: out_fld += 'dup' out_fld = arcpy.ValidateFieldName(out_fld, tbl_path) # sze = vals.dtype.str dt = [('IDs', '<i4'), (out_fld, sze)] # ie '<f8' out_array = np.zeros((in_arr.shape[0],), dtype=dt) out_array['IDs'] = in_arr[oid_fld] out_array[out_fld][idx:] = vals if xtend: arcpy.da.ExtendTable(in_tbl, oid_fld, out_array, 'IDs') return out_array
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 _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 _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
tweet("{!r:}".format(in_arr)) xtend = True return in_tbl, in_arr, in_fld, out_fld, func, win_size, xtend # ---------------------------------------------------------------------- # .... final code section producing the featureclass and extendtable if len(sys.argv) == 1: testing = True in_tbl, in_arr, in_fld, out_fld, func, win_size, xtend = _demo() else: testing = False in_tbl, in_arr, in_fld, out_fld, func, win_size, xtend = _tool() # if not testing: tweet('Some message here...') # ---- reassemble the table for extending ---- a = in_arr[in_fld] result = strided_func(a, step=win_size, func=func) out_array = form_output(in_tbl, in_arr, out_fld=out_fld, vals=result, idx=0, xtend=xtend) # ---------------------------------------------------------------------- # __main__ .... code section if __name__ == "__main__":
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) # frmt = """\n\nScript.... {}\nUsing..... {}\nSR...{}\nSorting by... {}, ascending... {}\nProducing ... {}\n""" args = [script, in_fc, SR.name, srt_order, ascend, out_fc] tweet(frmt.format(*args)) # ------------------------------------------------------------------------- if __name__ == "__main__": """ No demo """ # in_fc = r"C:\GIS\Geometry_projects\Spiral_sort\Polygons\Parcels.shp"
:Producing.. {}\n """ def tool(): """run the tool """ in_fc = sys.argv[1] N = int(sys.argv[2]) out_fc = sys.argv[3] args = [script, in_fc, N, out_fc] tweet(frmt.format(*args)) # call tweet connect(in_fc, out_fc, N=N, testing=False) # call connect if len(sys.argv) == 1: in_fc = r"C:\GIS\array_projects\data\Pro_base.gdb\small" # out_fc = r"C:\GIS\array_projects\data\Pro_base.gdb\ft3" out_fc = None N = 2 testing = True a, b, r0, r1, r2, r3 = connect(in_fc, out_fc, N=N, testing=True) args = [script, in_fc, N, out_fc] tweet(frmt.format(*args)) else: tool() # --------------------------------------------------------------------- if __name__ == "__main__": """Main section... """ # print("Script... {}".format(script))
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)) # ---- the end ----
min_space = int(sys.argv[2]) num = int(sys.argv[3]) SR = sys.argv[4] out_fc = sys.argv[5] frmt = """\n AOI extent for points... {} Minimum spacing.... {} Number of points... {} Spatial reference.. {} Output featureclass.. {}\n """ args = [aoi, min_space, num, SR, out_fc] msg = frmt.format(*args) tweet(msg) # ---- perform the point creation ---- aoi = aoi.split(" ")[:4] # extent is returned as a string ext = [round(float(i)) for i in aoi] L, B, R, T = ext a = n_spaced(L, B, R, T, min_space, num, verbose=False) all_flds = ['X', 'Y', 'x_coord', 'y_coord'] xy_flds = all_flds[:2] xy_dt = ['<f8', '<f8', 'float', 'float'] a = np.c_[(a, a)] z = array_struct(a, fld_names=all_flds, dt=xy_dt) # z = np.zeros((len(a)), dtype=[('X', '<f8'), ('Y', '<f8')]) # fld_names = ('X', 'Y') # z['X'] = a[:, 0] # z['Y'] = a[:, 1] out_fc = array_fc(z, out_fc, xy_flds, SR)
result = seq_text(a) idx = 0 else: result = seq_text(a) idx = 1 # # ---- reassemble the table for extending ---- out_array = form_output(in_tbl, in_arr, out_fld=out_fld, del_fld=del_fld, vals=result, idx=idx, xtend=xtend) msg = """ Processing... {} function..... {} input field.. {} output field. {} """ tweet(msg.format(in_tbl, func, in_fld, out_fld)) # ---------------------------------------------------------------------- # __main__ .... code section if __name__ == "__main__": """Optionally... : - print the script source name. : - run the _demo """ # print("Script... {}".format(script))
in_fc = gdb_pth + r"/radial_pnts" cent = None out_fc0 = gdb_pth + r"/radial" out_fc1 = gdb_pth + r"/OD_01" else: testing = False in_fc, from_north, cent, out_fc0, out_fc1 = _tool() # # (1) Run the test to see whether to continue # results = test_envs(in_fc, cent, out_fc0, out_fc1) cleared, vals = results # if not cleared: tweet(dedent(msg0).format(script)) else: print("\nPassed all checks-------------") # # ---- Process section ------------------------------ # pnts_out, plys_out, cent, SR = vals desc = _describe(in_fc) arcpy.env.workspace = desc['path'] # set the workspace to the gdb arr = _xyID(in_fc, to_pnts=True) indx = arr['IDs'] pnts = arr[['Xs', 'Ys']] pnts = pnts.view(np.float64).reshape(pnts.shape[0], 2) if cent is None: cent = np.mean(pnts, axis=0).tolist() #
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]
elif func == 'diff from median': result = median_diff(a) idx = 0 elif func == 'diff from min': result = min_diff(a) idx = 0 elif func == 'diff from value': idx = 0 val_orig = val try: val = int(val) except: val = 0 try: val = float(val) except: val = 0 finally: frmt = "Difference value entered... {!r:}... Value used... {!r:}" tweet(frmt.format(val_orig, val)) pass result = val_diff(a, val) elif func == 'percent': result = percent(a) idx = 0 elif func == 'sequential diff': result = seq_diff(a) # sequential diff call idx = 1 elif func == 'sequential number': result = seq_number(a) idx = 0 elif func == 'z_score': result = z_score(a) idx = 0 else:
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 # ---------------------------------------------------------------------- # .... final code section producing the featureclass and extendtable if len(sys.argv) == 1: testing = True in_tbl, in_arr, in_fld, out_fld, func, win_size, xtend = _demo() else: testing = False in_tbl, in_arr, in_fld, out_fld, func, win_size, xtend = _tool() # if not testing: tweet('Some message here...') # ---- reassemble the table for extending ---- a = in_arr[in_fld] result = strided_func(a, step=win_size, func=func) out_array = form_output(in_tbl, in_arr, out_fld=out_fld, vals=result, idx=0, xtend=xtend) # ---------------------------------------------------------------------- # __main__ .... code section if __name__ == "__main__":
arcpy.da.ExtendTable(in_tbl, 'OBJECTID', out_array, 'IDs') # ---------------------------------------------------------------------- # .... final code section producing the featureclass and extendtable if len(sys.argv) == 1: testing = True idx, a0, a, d0, d, ft = _demo() frmt = """ Testing... unsorted... {} sorted..... {} """ args = [a0, a] tweet(dedent(frmt).format(a0, a)) tweet(form_(d0, prn=False)) tweet(form_(d, title="Sorted...", prn=False)) tweet(frmt_rec(ft, prn=False)) else: testing = False _tool() # if not testing: tweet('Some message here...') # ---------------------------------------------------------------------- # __main__ .... code section if __name__ == "__main__": """Optionally...
result = min_diff(a) idx = 0 elif func == 'diff from value': idx = 0 val_orig = val try: val = int(val) except: val = 0 try: val = float(val) except: val = 0 finally: frmt = "Difference value entered... {!r:}... Value used... {!r:}" tweet(frmt.format(val_orig, val)) pass result = val_diff(a, val) elif func == 'percent': result = percent(a) idx = 0 elif func == 'sequential diff': result = seq_diff(a) # sequential diff call idx = 1 elif func == 'sequential number': result = 'seq_number' idx = 0 elif func == 'z_score': result = z_score(a) idx = 0 else:
# ---------------------------------------------------------------------- # .... final code section producing the featureclass and extendtable if len(sys.argv) == 1: testing = True idx, a0, a, d0, d, ft = _demo() frmt = """ Testing... unsorted... {} sorted..... {} """ args = [a0, a] tweet(dedent(frmt).format(a0, a)) tweet(form_(d0, prn=False)) tweet(form_(d, title="Sorted...", prn=False)) tweet(frmt_rec(ft, prn=False)) else: testing = False _tool() # if not testing: tweet('Some message here...') # ---------------------------------------------------------------------- # __main__ .... code section if __name__ == "__main__": """Optionally... : - print the script source name.
msg = """ ---- sequences ------------------------------------------------------ Processing ... {} input field .. {} step size ... {} (difference between adjacent values) output table . {} ---- Value : value in the field Count : number of observations in that sequence From_ : start location of the sequence (includes this index) To_ : end location of the sequence (up to but not including) NoData: -1 """ tweet(msg.format(in_tbl, in_fld, stepsize, out_tbl)) out = sequences(a, stepsize=0) if out_tbl not in ("#", "", " ", None, 'None'): arcpy.da.NumPyArrayToTable(out, out_tbl) prn = frmt_rec(out[:50], 0, True, False) tweet(prn) # ## ---- reassemble the table for extending ---- # ---------------------------------------------------------------------- # __main__ .... code section if __name__ == "__main__": """Optionally... : - print the script source name.