0

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
  • Print query, copy it and apply through gui to check if it's correct. If not, convert parameter to integer. – FelixIP Jul 02 '21 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 Jul 03 '21 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 Jul 03 '21 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 Jul 04 '21 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 Jul 04 '21 at 16:55
  • You are not reading comments to the end. – FelixIP Jul 04 '21 at 19:16

0 Answers0