Problem using NumPyArrayToFeatureClass to modify coordinates of a points layer

1003
7
Jump to solution
07-10-2021 04:07 PM
mlopezr95984
New Contributor III

 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!

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

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"  😉

 


... sort of retired...

View solution in original post

7 Replies
DanPatterson
MVP Esteemed Contributor

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.

 

 


... sort of retired...
0 Kudos
mlopezr95984
New Contributor III

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:

fishnet.JPG

I did not define dtypes....

0 Kudos
DanPatterson
MVP Esteemed Contributor

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(??)


... sort of retired...
0 Kudos
mlopezr95984
New Contributor III

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')]

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

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 😉


... sort of retired...
0 Kudos
DanPatterson
MVP Esteemed Contributor

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"  😉

 


... sort of retired...
mlopezr95984
New Contributor III

Thank you Dan! I, for sure, want to become a "numpathian pythonista" 😄