c'ant run program on shapefile imported

838
8
06-17-2021 06:37 AM
hamidmechouet
New Contributor II

 

 

import arcpy
import pandas as pd
import numpy as np

################
# SET PARAMETERS
################

# input layout
input_layout = arcpy.GetParameterAsText(0)


rotor_diameter = int(arcpy.GetParameterAsText(1)) # rotor
rayon = rotor_diameter/2 # rayon
H = arcpy.GetParameter(2) # hub
t = 8766 # nbe
version = arcpy.GetParameter(9) # choix
corr_ = arcpy.GetParameter(10) # premier de la vitesse
corr_v = arcpy.GetParameter(11) # second de la vitesse
corr_pr_1 = arcpy.GetParameter(12) # premier de la production
corr_pr_2 = arcpy.GetParameter(13) # second de la production

if version_s=="G2019":
# input density raster
input_raster_gwa_density_50 = r"\\wi_050m_g201910.tif" # t50m
input_raster_gwa_density_100 = r"\\w_p100m_g201910.tif" # 100m
input_raster_gwa_density_200 = r"\\wind_p200m_g201910.tif" #   200m

# inpuraster
input_raster_g_sped_50 = r"\\fra_wind_speed050m_g201910.tif" # Wd at 50m
input_raster_g_sped_100 = r"\\wind_speed100m_g201910.tif" # Wd at 100m
input_raster_g_sed_200 = r"\\fra_wind_speed200m_g201910.tif" # Wd at 200m

 

#################
# table en sortie
#################
# output d table
output_table_g_y_50 = r"\\output_table_g_dy_50" # output table with  value at 50m
output_table_g_d_100 = r"\\output_table_g_dy_100" # output table with  value at 100m
output_table_g_ity_200 = r"\\output_table_gwa_density_200" # output table with  value at 200m

 

# output 
output_table_gwa_speed_50 = r"\\output_table_gd_50" # output  50m
output_table_gwa_speed_100 = r"\\output_table_gd_100" # output   at 100m
output_table_gwa_speed_200 = r"\\output_table_gd_200" # output at 200m

###################
# create empty list
###################
list_x_2154 = []
list_y_2154 = []
list_x_4326 = []
list_y_4326 = []

list_50_1 = [] # list of t
list_50_2 = [] # density for each t
list_50_3 = [] # production for each t wit
list_50_3_1 = [] # production for each t
list_50_4 = [] # list with wd at 50m
list_50_5 = [] # list with wat 100m
list_50_x = [] # list with x values frd speed
list_50_y = [] # list with y values fd speed
list_50_cp = [] # list with cp values frd speed

list_100_1 = [] # list of t
list_100_2 = [] # dey for each t
list_100_3 = [] # production for each te without cor
list_100_3_1 = [] # prn for each t

list_100_4 = [] # list witheed at 100m
list_100_x = [] # list with x valuwind speed
list_100_y = [] # list with y vaspeed
list_100_cp = [] # list with cp valund speed

list_Hx_1 = [] # list of tur
list_Hx_2 = [] # densne
list_Hx_3 = [] # prodrection
list_Hx_3_1 = [] # pro turbine
list_Hx_4 = [] # list witht WS
list_Hx_5 = [] # list with00m
list_H_x = [] # list withind speed
list_H_y = [] # list with yind speed
list_H_cp = [] # list with ######
# GET XY COORDINATES FROM INPUT LAYOUT
######################################
# for loop in Lambert 93
for row in arcpy.da.SearchCursor(input_layout, ["SHAPE@XY"], spatial_reference=arcpy.SpatialReference("RGF 1993 Lambert-93")):
# Get x,y coordinates of each point feature
x, y = row[0]
list_x_2154.append(round(x,2))
list_y_2154.append(round(y,2))
# for loop in WGS84
for row in arcpy.da.SearchCursor(input_layout, ["SHAPE@XY"], spatial_reference=arcpy.SpatialReference("WGS 1984")):
# Get x,y coordinates of each point feature
x, y = row[0]
list_x_4326.append(round(x,8))
list_y_4326.append(round(y,8))

##################################
# SCRIPT WITH HUB HEIGHT CONDITION
##################################
if H==50:
############
# 
############
# get d sd value at 50m for each t
arcpy.sa.ExtractValuesToPoints(input_layout, in0, outpeed_50, "NONE", "VALUE_ONLY")
# search field in the new table
search_speed_50 = arcpy.SearchCursor(outpeed_50)
fieeed_50 = 'RASTERVALU'

if version_atlas=="G19":
# bo tolist
i=0 # initialize index=0
for row in search_speed_50:
list_50_4.append(round(row.getValue(fieleed_50),2 ))
Vh_gwa = round(row.getValue(fieleed_50), 2) 
Vh_corr = corr_vitesse_1 * Vh_gwa + corr_vitesse_2 
list_Vh_corr.append(round(Vh_corr,2))
list_Vh_gwa.append(round(Vh_gwa,2))
list_50_x.append(round(0.00001672 * Vh_corr**2 - 0.00043812 * Vh_corr + 0.0025185, 8))
list_50_y.append(round(0.00079672 * Vh_corr**3 - 0.02622527 * Vh_corr**2 + 0.27293475 * Vh_corr - 0.5657615, 8))
list_50_cp.append(round(list_50_x[i] * rotor_diameter + list_50_y[i], 8))
#coeff_corr_prod = corr_production_1 * Vh_corr + corr_production_2
#list_coeff_corr_prod.append(round(coeff_corr_prod, 8))
i = i+1 # index + 1

# get wind speed value at 100m for each turbine
arcpy.sa.ExtractValuesToPoints(input_layout, inputed_100, outped_100, "NONE", "VALUE_ONLY")
# search field in the new table
search_speed_100 = arcpy.SearchCursor(outpeed_100)
field_value_speed_100 = 'RASTERVALU'
# boucle in each turbine and append field value tolist
for row in search_speed_100:
list_50_5.append(round(row.getValue(field_value_speed_100),2))


# get A value at 50m
arcpy.sa.ExtractValuesToPoints(input_layout, input_ray_50, osity_50, "NONE", "VALUE_ONLY")
# search field in the new table
search_density_50 = arcpy.SearchCursor(outpsity_50)
field_t = 'nuine'
field_value_density = 'RASTERVALU'
# boucle in each turbine and append field value tolist
i=0 # initialize index=0
for row in search_density_50:
list_50_1.append(row.getValue(field_t))
list_50_2.append(int(row.getValue(field_value_density)))
list_50_3.append(int(row.getValue(field_value_density) * math.pi*rayon*rayon * list_50_cp[i] * t / 1000000)) #prod/turbine without correction
list_50_3_1.append(int(row.getValue(field_value_density) * math.pi*rayon*rayon * list_50_cp[i] * corr_production_1 * t / 1000000)) #prod/turbine
i = i+1
# set parameter and sum the list to get production
arcpy.SetParameter(4, str(sum(list_50_3_1))+" MWh") 
arcpy.SetParameter(7, str(sum(list_50_3))+" MWh") 


arcpy.SetParameter(3,str(round(sum(list_Vh_corr)/len(list_Vh_corr),2))+" m/s at "+str(H)+"m")  
arcpy.SetParameter(6,str(round(sum(list_Vh_gwa)/len(list_Vh_corr),2))+" m/s at "+str(H)+"m")  


########
# RESULT
########
dataframe = pd.DataFrame(
{'a_turbine': list_50_1,
'b_rotor_diameter': roter,
'c_hub_height': H,
'd_tip_height': H + (roteter/2),
'e_density_50m': list_50_2,
'f_wind_speed_gwa_50m': list_50_4,
'g_wind_speed_corr_50m': list_Vh_corr,
'h_wind_speed_gwa_100m': list_50_5,
'i_list_x': list_50_x,
'j_list_y': list_50_y,
'k_list_cp': list_50_cp,
'l_production_sans_correction_': list_50_3,
'l1_production_mwh': list_50_3_1,
#'m_coeff_corr_prod': list_cd,
'n_x_l93': list_x_2154,
'o_y_l93': list_y_2154,
'p_longitude': list_x_4326,
'q_latitude': list_y_4326
})


arcpy.SetParameter(5, r'in_memory/Tableau')

else:
arcpy.SetParameter(14, "Hub height must be between 50m and 200m")

 

 

Hello, I have a script in python with which I perform calculations on data that is on layers (feature layer published on acgis company), when I add the widget add the laout from a shapefile I have this error message:

Invalid return value: in_memory \ Table
Failed.

Can please tell me where the problem is and / or what part to modify on the python code.

Thanks for your time and help.

 

Tags (1)
0 Kudos
8 Replies
JoeBorgione
MVP Emeritus

Please use the Edit/Insert code tool for your code.  It makes it easier to read your code.  (See Below).

Are you really using shapefiles?  You might want to consider using feature classes so you can eliminate the Pandas to Numpy step.  I don't see the error you refer to, but line 558 probably refers to the internal arcgis/python code and not your script.  However, your script line number should be mentioned above that.

 

import arcpy
import pandas as pd
import numpy as np

################
# SET PARAMETERS
################

# input layout
input_layout = arcpy.GetParameterAsText(0)

# input turbine and production parameters
rotor_diameter = int(arcpy.GetParameterAsText(1)) # rotor
rayon = rotor_diameter/2 # rayon
H = arcpy.GetParameter(2) # hub
t = 8766 # nbe
version_atlas = arcpy.GetParameter(9) # choix
corr_vitesse_1 = arcpy.GetParameter(10) # premier de la vitesse
corr_vitesse_2 = arcpy.GetParameter(11) # second de la vitesse
corr_production_1 = arcpy.GetParameter(12) # premier de la production
corr_production_2 = arcpy.GetParameter(13) # second de la production

####################################################
# GWA2019
####################################################
if version_atlas=="GWA2019":
# input density raster
input_raster_gwa_density_50 = r"\\wind_powerdensity050m_gwa201910.tif" # WindPowerDensity at50m
input_raster_gwa_density_100 = r"\\wind_powerdensity100m_gwa201910.tif" # WindPowerDensity at 100m
input_raster_gwa_density_200 = r"\\wind_powerdensity200m_gwa201910.tif" # WindPowerDensity at 200m

# input wind speed raster
input_raster_gwa_speed_50 = r"\\fra_wind_speed050m_gwa201910.tif" # Wind Speed at 50m
input_raster_gwa_speed_100 = r"\\wind_speed100m_gwa201910.tif" # Wind Speed at 100m
input_raster_gwa_speed_200 = r"\\fra_wind_speed200m_gwa201910.tif" # Wind Speed at 200m

 

#################
# table en sortie
#################
# output density table
output_table_gwa_density_50 = r"\\output_table_gwa_density_50" # output table with density value at 50m
output_table_gwa_density_100 = r"\\output_table_gwa_density_100" # output table with density value at 100m
output_table_gwa_density_200 = r"\\output_table_gwa_density_200" # output table with density value at 200m

 

# output wind speed table
output_table_gwa_speed_50 = r"\\output_table_gwa_speed_50" # output table with wind speed value at 50m
output_table_gwa_speed_100 = r"\\output_table_gwa_speed_100" # output table with wind speed value at 100m
output_table_gwa_speed_200 = r"\\output_table_gwa_speed_200" # output table with wind speed value at 200m

###################
# create empty list
###################
list_x_2154 = []
list_y_2154 = []
list_x_4326 = []
list_y_4326 = []

list_50_1 = [] # list of turbines
list_50_2 = [] # density for each turbine
list_50_3 = [] # production for each turbine without correction
list_50_3_1 = [] # production for each turbine
list_50_4 = [] # list with wind speed at 50m
list_50_5 = [] # list with wind speed at 100m
list_50_x = [] # list with x values from wind speed
list_50_y = [] # list with y values from wind speed
list_50_cp = [] # list with cp values from wind speed

list_100_1 = [] # list of turbines
list_100_2 = [] # density for each turbine
list_100_3 = [] # production for each turbine without correction
list_100_3_1 = [] # production for each turbine

list_100_4 = [] # list with wind speed at 100m
list_100_x = [] # list with x values from wind speed
list_100_y = [] # list with y values from wind speed
list_100_cp = [] # list with cp values from wind speed

list_Hx_1 = [] # list of turbines
list_Hx_2 = [] # density for each turbine
list_Hx_3 = [] # production for each turbine without correction
list_Hx_3_1 = [] # production for each turbine
list_Hx_4 = [] # list with wind speed at WS
list_Hx_5 = [] # list with wind speed at 100m
list_H_x = [] # list with x values from wind speed
list_H_y = [] # list with y values from wind speed
list_H_cp = [] # list with cp values from wind speed

list_Vh_corr = [] # list with speed from GlobalWindAtlas correction
list_Vh_gwa = [] # list with speed from GlobalWindAtlas
list_coeff_corr_prod = [] # list with coefficient correcteur for production

######################################
# GET XY COORDINATES FROM INPUT LAYOUT
######################################
# for loop in Lambert 93
for row in arcpy.da.SearchCursor(input_layout, ["SHAPE@XY"], spatial_reference=arcpy.SpatialReference("RGF 1993 Lambert-93")):
# Get x,y coordinates of each point feature
x, y = row[0]
list_x_2154.append(round(x,2))
list_y_2154.append(round(y,2))
# for loop in WGS84
for row in arcpy.da.SearchCursor(input_layout, ["SHAPE@XY"], spatial_reference=arcpy.SpatialReference("WGS 1984")):
# Get x,y coordinates of each point feature
x, y = row[0]
list_x_4326.append(round(x,8))
list_y_4326.append(round(y,8))

##################################
# SCRIPT WITH HUB HEIGHT CONDITION
##################################
if H==50:
############
# WIND SPEED
############
# get wind speed value at 50m for each turbine
arcpy.sa.ExtractValuesToPoints(input_layout, input_raster_gwa_speed_50, output_table_gwa_speed_50, "NONE", "VALUE_ONLY")
# search field in the new table
search_speed_50 = arcpy.SearchCursor(output_table_gwa_speed_50)
field_value_speed_50 = 'RASTERVALU'

if version_atlas=="GWA2019":
# boucle in each turbine and append field value tolist
i=0 # initialize index=0
for row in search_speed_50:
list_50_4.append(round(row.getValue(field_value_speed_50),2 ))
Vh_gwa = round(row.getValue(field_value_speed_50), 2) # Vh_gwa = Wind speed from GlobalWindAtlas at hub height
Vh_corr = corr_vitesse_1 * Vh_gwa + corr_vitesse_2 # Vh_corr = Wind speed correction from mast at hub height
list_Vh_corr.append(round(Vh_corr,2))
list_Vh_gwa.append(round(Vh_gwa,2))
list_50_x.append(round(0.00001672 * Vh_corr**2 - 0.00043812 * Vh_corr + 0.0025185, 8))
list_50_y.append(round(0.00079672 * Vh_corr**3 - 0.02622527 * Vh_corr**2 + 0.27293475 * Vh_corr - 0.5657615, 8))
list_50_cp.append(round(list_50_x[i] * rotor_diameter + list_50_y[i], 8))
#coeff_corr_prod = corr_production_1 * Vh_corr + corr_production_2
#list_coeff_corr_prod.append(round(coeff_corr_prod, 8))
i = i+1 # index + 1

# get wind speed value at 100m for each turbine
arcpy.sa.ExtractValuesToPoints(input_layout, input_raster_gwa_speed_100, output_table_gwa_speed_100, "NONE", "VALUE_ONLY")
# search field in the new table
search_speed_100 = arcpy.SearchCursor(output_table_gwa_speed_100)
field_value_speed_100 = 'RASTERVALU'
# boucle in each turbine and append field value tolist
for row in search_speed_100:
list_50_5.append(round(row.getValue(field_value_speed_100),2))

####################
# DENSITY/PRODUCTION
####################
# get A value at 50m for each turbine
arcpy.sa.ExtractValuesToPoints(input_layout, input_raster_gwa_density_50, output_table_gwa_density_50, "NONE", "VALUE_ONLY")
# search field in the new table
search_density_50 = arcpy.SearchCursor(output_table_gwa_density_50)
field_turbine = 'numero_turbine'
field_value_density = 'RASTERVALU'
# boucle in each turbine and append field value tolist
i=0 # initialize index=0
for row in search_density_50:
list_50_1.append(row.getValue(field_turbine))
list_50_2.append(int(row.getValue(field_value_density)))
list_50_3.append(int(row.getValue(field_value_density) * math.pi*rayon*rayon * list_50_cp[i] * t / 1000000)) #prod/turbine without correction
list_50_3_1.append(int(row.getValue(field_value_density) * math.pi*rayon*rayon * list_50_cp[i] * corr_production_1 * t / 1000000)) #prod/turbine
i = i+1
# set parameter and sum the list to get production
arcpy.SetParameter(4, str(sum(list_50_3_1))+" MWh") #total production
arcpy.SetParameter(7, str(sum(list_50_3))+" MWh") #total production withouth correction

# GlobalWindAtlas mean wind speed correction
arcpy.SetParameter(3,str(round(sum(list_Vh_corr)/len(list_Vh_corr),2))+" m/s at "+str(H)+"m") #Vitesse corrigée
arcpy.SetParameter(6,str(round(sum(list_Vh_gwa)/len(list_Vh_corr),2))+" m/s at "+str(H)+"m") #Vitesse non corrigée


########
# RESULT
########
dataframe = pd.DataFrame(
{'a_turbine': list_50_1,
'b_rotor_diameter': rotor_diameter,
'c_hub_height': H,
'd_tip_height': H + (rotor_diameter/2),
'e_density_50m': list_50_2,
'f_wind_speed_gwa_50m': list_50_4,
'g_wind_speed_corr_50m': list_Vh_corr,
'h_wind_speed_gwa_100m': list_50_5,
'i_list_x': list_50_x,
'j_list_y': list_50_y,
'k_list_cp': list_50_cp,
'l_production_sans_correction_mwh': list_50_3,
'l1_production_mwh': list_50_3_1,
#'m_coeff_corr_prod': list_coeff_corr_prod,
'n_x_l93': list_x_2154,
'o_y_l93': list_y_2154,
'p_longitude': list_x_4326,
'q_latitude': list_y_4326
})

# convert pandas df to numpy array
x = np.array(np.rec.fromrecords(dataframe.values))
names = dataframe.dtypes.index.tolist()
x.dtype.names = tuple(names)
arcpy.da.NumPyArrayToTable(x, r'in_memory/Tableau')
arcpy.SetParameter(5, r'in_memory/Tableau')

else:
arcpy.SetParameter(14, "Hub height must be between 50m and 200m")
That should just about do it....
0 Kudos
hamidmechouet
New Contributor II

Hello Joe, thanks for your reply, I just deleted the lines of code that convert pandas to numpy.
how to use the feature classes ?.

0 Kudos
hamidmechouet
New Contributor II

Hello Joe, I deleted the Pandas to Numpy step. Now i have the following error message: Invalid return value: in_memory \ Tableau
Failed.

0 Kudos
JoeBorgione
MVP Emeritus

Since you are dealing with enterprise, my guess is your data is stored somewhere as a feature class, but rather than discuss data structures;  when I hear 'shapefile' I always default to the literal meaning, rather than what many people mean generically.

That said, simply deleting the pandas data frame to numpy may not solve your issue.  If you need to perform you analysis in numpy, there are other conversion tools that may be more appropriate; see feature class to numpy.

Your script is lengthy, and I can't pour over it line by line but if you can explain your objective to the analysis, that could help.  Seeing the initial error you were getting would help as well.

That should just about do it....
0 Kudos
hamidmechouet
New Contributor II

Hello Joe, my data is in a shapefile (.shp) when I upload it to arcgis company the layer appears perfectly, my analysis is not performed on numpy, numpy was used before to display the results on arcgis pro, this n is no longer the case now.
The error I am getting now is: Invalid return value: in_memory \ Tableau
it is in this line that the results are stored in a table and then displayed.

thank you for your help.

Samy.

0 Kudos
JoeBorgione
MVP Emeritus

I understand that, but initially you said : the error message shows that line 558 contains an error while this line does not exist.  That's the error we need to see.  Word for word, so go back to your original script and run it and capture that error.  Rather than hacking and whacking lines of code, you need to focus on the original problem and avoid creating new ones.

That should just about do it....
0 Kudos
hamidmechouet
New Contributor II

Yes, but now that I no longer convert to numpy, the script runs perfectly on arcgis pro, on feature layers, but on shape files I have the following error message: Invalid return value: in_memory \ Tableau.
I think it's in relation to what you told me: You might want to consider using feature classes!
Can you tell me more about the feauture classes?

Samy.

0 Kudos
JoeBorgione
MVP Emeritus

Take a look at this post and follow the links in it.

Also take a look at this as well.

You may want to get a better handle on data structures before you start pulling them apart with advanced python techniques.  There may tools already available to you to do what you want.

That should just about do it....
0 Kudos