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.5k29 gold badges115 silver badges349 bronze badges
asked Jul 1, 2021 at 20:37
6
  • Print query, copy it and apply through gui to check if it's correct. If not, convert parameter to integer. Commented 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? Commented 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) Commented 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) Commented 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. Commented Jul 4, 2021 at 16:55

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.