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 nt = nn_kdtree(a, N=3, sorted_=True, to_tbl=True, as_cKD=True) tweet("\nnear table\n{}".format(nt)) #.reshape(nt.shape[0], 1))) arcpy.da.NumPyArrayToTable(nt, out_tbl)
def process(in_fc, id_fld, prn=True): """process the data for the tool or demo """ frmt = """ Group .... {} points in group {} center ... x = {:<8.2f} y = {:<10.2f} minimum .. x = {:<8.2f} y = {:<10.2f} maximum .. x = {:<8.2f} y = {:<10.2f} standard distance ... {:8.2f} distance stats .... mean:{:8.2f} min:{:8.2f} max:{:8.2f} std:{:8.2f} """ flds = ['SHAPE@X', 'SHAPE@Y', id_fld] a_in = arcpy.da.FeatureClassToNumPyArray(in_fc, flds) a_sort = np.sort(a_in, order=id_fld) a_split = np.split(a_sort, np.where(np.diff(a_sort[id_fld]))[0] + 1) msg = "" tbl = [] for i in range(len(a_split)): a0 = a_split[i][['SHAPE@X', 'SHAPE@Y']] a = a0.copy() a = a.view((a.dtype[0], len(a.dtype.names))) # art.geom._view_(a0) cent = np.mean(a, axis=0) min_ = np.min(a, axis=0) max_ = np.max(a, axis=0) var_x = np.var(a[:, 0]) var_y = np.var(a[:, 1]) stand_dist = np.sqrt(var_x + var_y) dm = _e_dist(a) dm_result = np.tril(dm, -1) vals = dm_result[np.nonzero(dm_result)] stats = [vals.mean(), vals.min(), vals.max(), vals.std()] # hdr = "Distance matrix...({}) ".format(i) # m = form_(dm_result, deci=1, wdth=80, title=hdr, prn=False) args = (i, len(a), *cent, *min_, *max_, stand_dist, *stats) #, m] tbl.append(args) msg += dedent(frmt).format(*args) if prn: tweet(msg) flds = ['ID', 'N_pnts', 'CentX', 'CentY', 'MinX', 'MinY', 'MaxX', 'MaxY', 'Stand Dist', 'Mean_dist', 'Min_dist', 'Max_dist', 'Std_dist'] dts = ['<i4', '<i4', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8'] tbl = np.array(tbl) tbl = np.core.records.fromarrays(tbl.transpose(), names=flds, formats=dts) return a_in, a_split, msg, tbl
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))
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))
if len(sys.argv) == 1: testing = True pth = "/".join(script.split("/")[:-2]) + "/Data/Near_testing.gdb" orig_fc = pth + "/orig_0" dest_fc = pth + "/dest_0" N = 1 # out_fc = r"C:\GIS\A_Tools_scripts\PointTools\Data\Near_testing.gdb\a2b" out_fc = None args = [script, testing, orig_fc, dest_fc, N, out_fc] else: args = _tool() tweet(frmt.format(*args)) # call tweet __, testing, orig_fc, dest_fc, N, out_fc = args returned = connect_od(orig_fc, dest_fc, out_fc, N=N, testing=testing) # call connect orig, dest, pnts, n_array = returned # --------------------------------------------------------------------- if __name__ == "__main__": """Main section... """ # print("Script... {}".format(script)) """ in_fc = r"C:\GIS\array_projects\data\Pro_base.gdb\small" out_fc = r"C:\GIS\array_projects\data\Pro_base.gdb\ft3" N = 1 testing = True a, b, r0, r1, r2, r3 = connect(in_fc, out_fc, N=N, testing=True) """
array_fc(pnts, out_fc, fld_names, SR) ln = "-" * 70 frmt = """\n :{}: :Script... {} :Output to..... {} :using ........ {} :Processing extent specified... : L {}, B {}, R {}, T {} :X spacing...{} :Y spacing...{} :Points...... {!r:} : :{}:" """ args = [ln, script, out_fc, SR.name, L, B, R, T, dx, dy, pnts, ln] msg = frmt.format(*args) tweet(msg) # ---------------------------------------------------------------------- # __main__ .... code section if __name__ == "__main__": """Optionally... : - print the script source name. : - run the _demo """ # print("Script... {}".format(script)) # pnts, mesh = _demo()
---- Concave/convex hull ---- script {} Testing {} in_fc {} group_by {} k_factor {} hull_type {} out_type {} out_fc {} ----------------------------------------------------------------------- """ args = [ script, testing, in_fc, group_by, k_factor, hull_type, out_type, out_fc ] tweet(msg.format(*args)) desc = arcpy.da.Describe(in_fc) SR = desc['spatialReference'] # # (1) ---- get the points out_flds = ['OID@', 'SHAPE@X', 'SHAPE@Y'] + [group_by] a = arcpy.da.FeatureClassToNumPyArray(in_fc, out_flds, "", SR, True) # # (2) ---- determine the unique groupings of the points uniq, idx, rev = np.unique(a[group_by], True, True) groups = [a[np.where(a[group_by] == i)[0]] for i in uniq] # # (3) ---- for each group, perform the concave hull hulls = [] for i in range(0, len(groups)):
else: return a, b, r0, r1, r2, r3 # ---- Run the analysis ---- frmt = """\n :Running ... {} :Using ..... {} :Finding ... {} closest points and forming connections :Producing.. {}\n """ 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 ret = connect(in_fc, out_fc, N=N, testing=False) # call connect # --------------------------------------------------------------------- if __name__ == "__main__": """Main section... """ # print("Script... {}".format(script)) """ in_fc = r"C:\GIS\array_projects\data\Pro_base.gdb\small" out_fc = r"C:\GIS\array_projects\data\Pro_base.gdb\ft3" N = 1 testing = True a, b, r0, r1, r2, r3 = connect(in_fc, out_fc, N=N, testing=True) """
# 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.... {} ------------------------------------- """ tweet(msg.format(in_fc, angle, out_fc)) # ---- the end ---- # ---------------------------------------------------------------------- # __main__ .... code section if __name__ == "__main__": """Optionally... : - print the script source name. : - run the _demo """ fc = "/Point_tools.gdb/std_dist_center" flder = "/".join(script.split("/")[:-2]) in_fc = flder + fc
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: tweet("\nPassed all checks-------------\n") # # ---- 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 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]
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"
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)