Use numpy array for a quercy?

347
12
Jump to solution
04-02-2019 05:06 AM
JohannesBierer
Regular 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
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Try changing Lines #28-#31 with:

sc2 = "{} IN ({})".format(field, ",".join("'" + str(i) + "'" for i in x2))

View solution in original post

0 Kudos
12 Replies
JoeBorgione
MVP Esteemed Contributor

Try posting your code again: it didn't make the trip.

can't wait to retire....
JoshuaBixby
MVP Esteemed Contributor

You missed a critical part of string formatting on line 30:

sc = "%s = + %s" % (field, i)
JohannesBierer
Regular Contributor

Sorry, my mistake.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

My code snippet will address your TypeError, is the rest of the script working now?

0 Kudos
JohannesBierer
Regular Contributor

I changed the code in this, but have problems to code the selection quercy properly.

#!/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()
arcpy.env.overwriteOutput = True

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)
x1 = np.array(map(str, x))
x2 = x1.tolist()

field = 'FLURSTUECKSKENNZEICHEN'

sc = "'" + field + " IN ({})".format(x2) + "'"

sc1 = sc.replace("[", "")
sc2 = sc1.replace("]", "")

print sc2

arcpy.MakeFeatureLayer_management(fc, fl)

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

outFc = os.path.join(outP, "ALK_WK13.shp")

arcpy.CopyFeatures_management(fl, outFc)

Line 28 is somehow wrong. The print of sc2 looks like this:

'FLURSTUECKSKENNZEICHEN IN ('8334100002890000384', '8334100002890000384', '8334100002890000384', '8334100002890000384')'

but gives a error in Line 37? Invalid expression?

0 Kudos
JohannesBierer
Regular Contributor

With the right code formatting:

#!/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()
arcpy.env.overwriteOutput = True

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)
x1 = np.array(map(str, x))
x2 = x1.tolist()

field = 'FLURSTUECKSKENNZEICHEN'

sc = "'" + field + " IN ({})".format(x2) + "'"

sc1 = sc.replace("[", "")
sc2 = sc1.replace("]", "")

print sc2

arcpy.MakeFeatureLayer_management(fc, fl)

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

outFc = os.path.join(outP, "ALK_WK13.shp")

arcpy.CopyFeatures_management(fl, outFc)
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Is the field text or numeric, i.e., are those IN values supposed to be strings or numbers?

0 Kudos
JohannesBierer
Regular Contributor

not in the office. Can answer next monday.

0 Kudos
JohannesBierer
Regular Contributor

field is a text field and the in values should be text as well.

0 Kudos