Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jwasilgeo/3b153df7806573a8e4e5b3b60142649b to your computer and use it in GitHub Desktop.
Save jwasilgeo/3b153df7806573a8e4e5b3b60142649b to your computer and use it in GitHub Desktop.
Convert features in an Esri GDB feature class to an Esri JSON-style list of features, represented as a list of feature dictionaries.
def convert_feature_class_to_features_list(source_feature_class):
# convert feature class features to Esri features list
# (JSON represented as a list of feature dictionaries)
# ignore 'Geometry' field type and manually ask for JSON-formatted geometry with 'SHAPE@JSON' below
# you could also add the 'OID' field type to ignore if you need to add these features to a hosted feature service
field_types_to_ignore = ['Geometry']
# establish the out fields, minus any fields that need to be ignored
out_fields = [
i.name for i
in arcpy.Describe(source_feature_class).fields
if i.type not in field_types_to_ignore
]
# and specifically ask for geometry info in JSON format
out_fields.append('SHAPE@JSON')
features = [
{
'attributes': {
out_fields[attribute_index]: attribute_value
for attribute_index, attribute_value
in enumerate(row[:-1])
},
'geometry': json.loads(row[-1])
} for row in arcpy.da.SearchCursor(
in_table=source_feature_class,
field_names=out_fields
)
]
return features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment