I'm new to ArcPy and Python in general. I'm experimenting with Python within ArcGIS Pro, but I've encountered an issue, here is the code:
import arcpy
arcpy.env.workspace = "C:/Users/Nastgardaren/Desktop/Strangnas/Strangnas/Strangnas.gdb"
input_inkomst = arcpy.GetParameterAsText(0)
input_fastigheter = arcpy.GetParameterAsText(1)
inkomstklass = arcpy.GetParameterAsText(2)
klasstyp = arcpy.GetParameterAsText(3)
val_av_klass = arcpy.management.SelectLayerByAttribute(input_inkomst,'NEW_SELECTION',"'{}'='{}'".format(inkomstklass,klasstyp))
produkt = arcpy.management.SelectLayerByLocation(input_fastigheter, 'WITHIN', val_av_klass)
arcpy.CopyFeatures_management(produkt,'produkt2')
the idea with the project is to create a new layer, which represents polygons (a subset of the layer in input_fastigheter) within the containment of another vector layer (input_inkomst) based on values between 1-8 from a field in the input_inkomst layer (the layer is predetermined for the first parameter, but I made it a user defined parameter, just for the sake of the experiment). When I remove the fourth input parameter and assign a defined value between 1 and 8, then the script works fine, but when I run the script like I pasted here, then I get a empty layer as a result.
I have found a very similar question on this site: Get Parameter as Text and select layer by attribute
Although I tried to solve it the same way as the OP did, I wasn't succesful because the name of the field in the attribute table wasn't properly defined apparently according to the error i received. This lead me to create a fourth parameter in which the user writes the name of the field, but I'm still stuck on the issue of having an empty output.
-
Print query, copy it and apply through gui to check if it's correct. If not, convert parameter to integer.FelixIP– FelixIP2021年07月02日 20:17:39 +00:00Commented Jul 2, 2021 at 20:17
-
Do you mean that I should convert the "getparameterastext" parameters to integer inside of python, or should I change the parameter data type in arcgis to integer from string?Nastgardaren– Nastgardaren2021年07月03日 19:30:49 +00:00Commented Jul 3, 2021 at 19:30
-
Inside, because it will read it as text, no matter parameter type. Still compute query before select and add arcpy.AddMessage(query)FelixIP– FelixIP2021年07月03日 21:42:32 +00:00Commented Jul 3, 2021 at 21:42
-
Alright, when I tried to compute the code in pycharm directly, it said that the syntax is invalid, this was the case also in python within arcgis. I assume that I'm missing something about this, but the main issue is that the conversion from string to integer did not yield any results. I looked up how I should do and did it like this: klasstyp_str = int(arcpy.GetParameterAsText(3) klasstyp = int(klasstyp_str)Nastgardaren– Nastgardaren2021年07月04日 16:51:05 +00:00Commented Jul 4, 2021 at 16:51
-
and still, the result is an empty feature class. I realise why it didn't work before, the "get parameter as text" converts all input to string by default, but I don't understand why it didn't work this time if I converted the input to integer.Nastgardaren– Nastgardaren2021年07月04日 16:55:00 +00:00Commented Jul 4, 2021 at 16:55