Hello 🙂
I need to create random points. I was using the function to do this in ArcGIS but I got a problem. I can´t define a maximum distance among points.
I am trying to do so, starting with a fishnet points layer. I add fields that hold the values of the coordinates plus a random number between a minimum and a maximum.
The code works well.
However, I run into problems trying to create a new points layer. I used "FeatureClassToNumPyArray" and "NumPyArrayToFeatureClass" trying to replace the original shape@XY with the displaced points. I get this error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
In [104]:
Line 94: arcpy.da.NumPyArrayToFeatureClass(arr_random_points, Vpc,("RX","RY"))
RuntimeError: invalid table name
---------------------------------------------------------------------------
I do not understand why the array is not valid. Is there something wrong in my code?
This is the code:
# Creation of Random Points from a Fishnet inside a rectangle
vOx=arr_poly["OX"][0]
vOy=arr_poly["OY"][0]
vMx=arr_poly["MX"][0]
vMy=arr_poly["MY"][0]
Orig_coord=str(vOx) + " " + str(vOy)
print(Orig_coord)
Y_axis_c=str(vOx) + " " + str(vMy)
print(Y_axis_c)
arcpy.management.CreateFishnet("Fishnet_feat",Orig_coord,Y_axis_c,0.5,0.5,"","","","LABELS",Boundary_polygon,"POLYGON")
arcpy.management.AddFields(
"Fishnet_feat_label",
[['X', 'DOUBLE', 'X', "10", "2",""],
['Y', 'DOUBLE', 'Y', "10","2",""],
['RX', 'DOUBLE', 'RX', "10","","2"],
['RY', 'DOUBLE', 'RY', "10","2",""],
["New_Shape","TEXT","New_Shape",50,""]])
arcpy.management.CalculateGeometryAttributes("Fishnet_feat_label", [["X", "POINT_X"], ["Y", "POINT_Y"]])
arcpy.management.CalculateFields("Fishnet_feat_label", expression_type="PYTHON3", fields=[["RX", "round(!X! + random.uniform(0,0.9),2)"], ["RY", "round(!Y! + random.uniform(0,0.9),2)"], ["New_Shape", "str(!RX!) +\",\" + str(!RY!)"]], code_block="import random")
arr_random_points= arcpy.da.FeatureClassToNumPyArray("Fishnet_feat_label",["*"])
Vpc="test.shp"
arcpy.da.NumPyArrayToFeatureClass(arr_random_points, Vpc,("RX","RY"))
Print(Vpc)
Thank you!
Solved! Go to Solution.
ps.... Point Tools for Pro - Esri Community
has "spaced points"
which would probably do it... you can use it or examine the code with the toolbox to get more familiar with working with numpy... you might become a "numpathian pythonista" 😉
show a couple of rows of the array and its dtype
ie
yourarray[:5]
will provide both (obviously yourarray is your array
check your origin point as well for the featureclass to be constructed.
Hi Dan, thank you, these are some of the rows of the array:
print(arr_random_points[:5])
[(1, [-3.29999998e-01, -1.77633000e+03], -0.33, -1776.33, -0.21, -1776.14, '-0.21,-1776.14')
(2, [ 1.70000002e-01, -1.77633000e+03], 0.17, -1776.33, 0.39, -1775.56, '0.39,-1775.56')
(3, [ 6.70000002e-01, -1.77633000e+03], 0.67, -1776.33, 0.78, -1775.92, '0.78,-1775.92')
(4, [ 1.17000000e+00, -1.77633000e+03], 1.17, -1776.33, 1.28, -1775.71, '1.28,-1775.71')
(5, [ 1.67000000e+00, -1.77633000e+03], 1.67, -1776.33, 1.75, -1775.65, '1.75,-1775.65')]
The origin point comes from a rectangle. The fishnet looks like this:
I did not define dtypes....
using shape@XY is ok but a pain,
SR = ??? # specify your spatial reference or geometry will be "off-ish" .... long story
arr = arcpy.da.FeatureClassToNumPyArray(...your featureclass..., ('OID@', 'SHAPE@X', 'SHAPE@Y', whatever), spatial_reference=SR)
Then when you get "arr"
print(arr.dtype)
print(arr[:5])
There has to be a dtype since the objectid (OID@) is integer and some are float and some text(??)
Hi Dan,
Indeed, the dtype is as follows. I guess it comes from the feature class?
[('OID', '<i4'), ('Shape', '<f8', (2,)), ('X', '<f8'), ('Y', '<f8'), ('RX', '<f8'), ('RY', '<f8'), ('New_Shape', '<U50')]
[('OID', '<i4'), ('Shape', '<f8', (2,)), ('X', '<f8'), ('Y', '<f8'), ('RX', '<f8'), ('RY', '<f8'), ('New_Shape', '<U50')]
As I suggested, skip SHAPE@XY, it is a nested pair of coordinates, ... anyway...
NumPyArrayToFeatureClass (in_array, out_table, shape_fields, {spatial_reference})
out_table : your output featureclass... full path, raw encoded (eg r"c:\path_to\your.gdb\new_pnts")
shape_fields : if X and Y are your new points or 'RX, RY' are, specify those like ('X', 'Y') in brackets
Spatial reference .... make sure you use one!!!, you have been warned 😉
ps.... Point Tools for Pro - Esri Community
has "spaced points"
which would probably do it... you can use it or examine the code with the toolbox to get more familiar with working with numpy... you might become a "numpathian pythonista" 😉
Thank you Dan! I, for sure, want to become a "numpathian pythonista" 😄