Select to view content in your preferred language

Use numpy array for a quercy?

1765
12
Jump to solution
04-02-2019 05:06 AM
JohannesBierer
Frequent Contributor

I would like to use numpy array from a excel file for a selection of features. Do I have to convert the array to a list first, and how to do that? With the following code I get the following error? Or some better ideas, many files to select?

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# 

import numpy as np
import arcpy
import pandas as pd
import sys, os

# Encoding der Standardausgabe herausfinden
stdout_encoding = sys.stdout.encoding or sys.getfilesystemencoding()

inTable = r"R:\Karto\Bierer2019\FFH_MEKA_FACT\Tabellen\wk13.xlsx"
outP = r"R:\Karto\Bierer2019\FFH_MEKA_FACT\Shape"
fc = r"\\RPKSF001\GIS_DATA\ArcUISTools15\Data\UIS_ALk.gdb\UIS_0100000047100001"
fl = "ALK"

# Tabelle
Flrstkz = pd.read_excel(inTable, index_col=None, na_values=['NA'], parse_cols = "A")

x = np.array(Flrstkz)

arcpy.MakeFeatureLayer_management(fc, fl)

field = 'FLURSTUECKSKENNZEICHEN'

for i in x:
    print i
        
    sc = "%s = + %s"(field, i)
    print sc

    arcpy.SelectLayerByAttribute_management (fl, "NEW_SELECTION", sc)

    outFc = os.path.join(outP, "WK13_"+ i + ".shp")

    arcpy.CopyFeatures_management(fl, outFc)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

[8334100002890000384]

Traceback (most recent call last):
  File "R:/Karto/zGIS/Python_2019/NumPy/NumPY_MEKA_FAKT.py", line 30, in <module>
    sc = "%s = + %s"(field, i)
TypeError: 'str' object is not callable

Better would be the "quercy" in (np.array)?

0 Kudos
12 Replies
JoshuaBixby
MVP Esteemed Contributor

Try changing Lines #28-#31 with:

sc2 = "{} IN ({})".format(field, ",".join("'" + str(i) + "'" for i in x2))
0 Kudos
LukeWebb
Frequent Contributor
Here is a function I use to build where cluases from lists, I use it like this.

values = ['test', 'test2']
field_name = 'query_field'
sql_where = build_where_clause_from_list (field_name, values)

values = [11, 27]
field_name = 'query_field'
sql_where = build_where_clause_from_list (field_name, values, field_type='Number')



def
build_where_clause_from_list(field_name, value_list, field_type="String"):
"""Build a SQL In Where clause using a list

e.g. field_name = 'Cheese', value_list=['Edam', 'Cheddar', 'Stinky Bishop']

output: "Cheese" in ('Edam', 'Cheddar', 'Stinky Bishop')
"""
where = """ "%s" in (""" % field_name
if field_type == 'String':
where+= "'"

if field_type == 'String':
joinTxt = "','"
else:
joinTxt = ","

where += joinTxt.join(map(str, value_list))
if field_type == 'String':
where += "'"
where += ")"
return where
JoshuaBixby
MVP Esteemed Contributor

Luke Webb‌, below is a slight modification you might want to consider:

def build_where_clause_from_list(field_name, value_list, force_string=False):
    import sys
    
    string_type = (str, bytes) if sys.version_info >= (3,0,0) else basestring
    
    fmt = lambda x: (
        "'{}'".format(x) 
        if force_string or isinstance(x, string_type) else
        "{}".format(x)
    )
    sql = '"{}" IN ({})'.format(field_name, ",".join(fmt(i) for i in value_list))
    
    return sql

The code above works in Python 2 and Python 3.  By default, it will enclose strings in single quotes and leave numbers as is, unless force_string is set.