Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Last active April 16, 2024 21:59
Show Gist options
  • Save AlexArcPy/d44b7a40c373386c229f14c4840e8400 to your computer and use it in GitHub Desktop.
Save AlexArcPy/d44b7a40c373386c229f14c4840e8400 to your computer and use it in GitHub Desktop.
Convert geodatabase feature class features into GeoJSON strings for loading into elastic search database (bulk api)
'''
Convert file geodatabase feature class features into GeoJSON and
then construct text file for loading into elastic search bulk api
'''
import json
import arcpy
arcpy.env.overwriteOutput = True
#create a geojson file
geo_json_cities_path = r'C:\GIS\Temp\Elastic\usa_cities.json'
arcpy.FeaturesToJSON_conversion(in_features=r'C:\Program Files (x86)\ArcGIS\Desktop10.5\TemplateData\TemplateData.gdb\USA\cities',
out_json_file=geo_json_cities_path, geoJSON=True)
with open(geo_json_cities_path) as f:
data = json.load(f)
#
geo_shapes = []
for feat in data['features']:
geo_shape = {}
geo_shape['name'] = feat['properties']['CITY_NAME']
geo_shape['state'] = feat['properties']['STATE_NAME']
geo_shape['pop'] = feat['properties']['POP1990']
geo_shape['location'] = feat['geometry']['coordinates']
geo_shapes.append(geo_shape)
with open(r'C:\GIS\Temp\Elastic\ready\usa_cities_elastic.json','wb') as f:
for city in geo_shapes:
f.write('{ "index": {"_index": "cities", "_type": "city"} }\n')
f.write(json.dumps(city))
f.write('\n')
#sample rows from output .json file:
#{ "index": {"_index": "cities", "_type": "city"} }
#{"state": "Washington", "name": "Bellingham", "pop": 52179, "location": [-122.46818353792992, 48.74387985436505]}
#{ "index": {"_index": "cities", "_type": "city"} }
#{"state": "Montana", "name": "Havre", "pop": 10201, "location": [-109.67985528815007, 48.54381826401885]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment