-
Notifications
You must be signed in to change notification settings - Fork 0
/
fc_to_pd_df.py
64 lines (53 loc) · 3.11 KB
/
fc_to_pd_df.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import arcpy
import numpy as np
import pandas as pd
def fc_to_pd_df(feature_class, field_list=None, skip_nulls=False, null_value=-999):
'''
This function converts a feature class to a pandas dataframe. The default returns all fields but you may supply a
list of fields to extract. Fields with the "Geometry" datatype like the "Shape" field of most FC are removed because
they are not 1-dimensional and Pandas can't deal with that data type.
Note that very large feature classes may not work due to memory limitations, especially if using 32bit python, which
applies to ArcMap users. You may try supplying a list of only the fields you require to get past the memory limitations.
ArcPro has 64bit python 3.0. This script has not been tested with that version.
Written: 7/17/2019 GD
:param feature_class: Input ArcGIS Feature Class
:param field_list: Fields for input (optional), default is all fields
:return: Pandas dataframe object
'''
# Generate a list of fields to import.
field_list_temp = []
all_fields = []
fields = arcpy.ListFields(feature_class)
for field in fields:
# If a list of fields is not supplied import all fields, check for and exclude geometry data types
if field_list is None:
if field.type != 'Geometry':
field_list_temp.append(field.name)
else:
print("Field \"{0}\" is of data type \"{1}\" and will not be imported into the pandas dataframe.".format(
field.name, field.type))
# If a list is supplied we will check if any of the requested fields are of geometry data type, remove, and warn user
else:
all_fields.append(field.name) # Append fields to a list that will be used to check if user requested fields exist in the feature class
if (field.type != 'Geometry') & (field.name in field_list):
field_list_temp.append(field.name)
elif (field.type == 'Geometry') & (field.name in field_list):
print("Field \"{0}\" is of data type \"{1}\" and will not be imported into the pandas dataframe.".format(
field.name, field.type))
# If field_list is set, check if requested fields exist in the FC
if field_list is not None:
for field in field_list:
if field.name not in all_fields:
raise ValueError("Requested field \"{0}\" was not found in the feature class!".format(field.name))
# Set field list to the list of verified field names to extract
field_list = field_list_temp
# Convert FC to numpy array with field list
np_array = arcpy.da.FeatureClassToNumPyArray(in_table=feature_class,
field_names=field_list,
skip_nulls=skip_nulls,
null_value=null_value)
return pd.DataFrame(np_array)
# Call example
if __name__ == '__main__':
fc_path = r'C:\\FeatureClass.gdb\FeatureClass'
df = fc_to_pd_df(fc_path)