Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created September 10, 2012 21:13
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save odoe/3693897 to your computer and use it in GitHub Desktop.
Save odoe/3693897 to your computer and use it in GitHub Desktop.
ArcPy script to Convert GeoJSON to Feature Classes
# This is a script that I use to convert geojson to
# features in a file gdb
# Step 1. Use the REST page of an ArcGIS Map Service to
# get the esri json results of the data you want.
# Step 2. I used my EsriJSON to GeoJSON app to convert
# the results to geojson.
# Step 3. In ArcMap, use the python window to create a
# python dictionary of your geojson.
# Step4. Use the following script to convert that geojson
# into featureclasses and then merge them.
arcpy.env.workspace = r'C:\WorkSpace\target.gdb'
# I'm going to save my features to a list for merge later
items = []
for g in geojson["features"]:
# Refer to AsShape docs
# AsShape only creates a geometry object, so it's up to you
# to use the AddField/Calculate field tools if you want to get the
# attribute data across.
geom = arcpy.AsShape(g["geometry"])
# Save an id I'll use to calculate my field
my_id = str(g["properties"]["ID_FIELD"])
feature = "_" + str(my_id)
# refer to the Copy Features docs
arcpy.CopyFeatures_management(geom, feature)
# refer to the AddField docs
arcpy.AddField_management(feature, "ID_FIELD", "TEXT", 12, "", "", "ID_FIELD", "NULLABLE")
# refer to the CalculateField docs
arcpy.CalculateField_management(feature, "ID_FIELD", '"' + my_id + '"', "PYTHON")
except Exception, e:
print e
arcpy.CalculateField_management(feature, "ID_FIELD, 0, "PYTHON")
# save my features to a list
del geom
target = "all_features"
# refer to Merge docs
arcpy.Merge_management(items, target)
# then loop my list and
# delete my individual features
# This only deleted them from my map project, not the file gdb
for i in items:
arcpy.Delete_management(i, "")
del items
# If someone knows of a more efficient way to do this, I'd love to hear about it.
# This script isn't exactly a speed demon.
Copy link

I did get some inspiration from your script and went with an insert cursor approach. Do you see any edge cases I miss?

Copy link

Dansio commented May 8, 2023

Here is a script that I am using for this type of conversion. I was not sure how to solve the geometry_type differently so I added Line and Point into the names while I was exporting my GeoJSON files.
import arcpy
import os

# Get input parameters
inDirectory = arcpy.GetParameterAsText(0)  #directory that contains the GeoJSON files
outputGDB = arcpy.GetParameterAsText(1) #export database, set the parameter in the toolbox as Workspace

# Set workspace to output geodatabase
arcpy.env.workspace = outputGDB

# Loop through all the files in the input directory
for filename in os.listdir(inDirectory):
    if filename.endswith(".geojson"):
        # Get the full path to the input GeoJSON file
        inputGeoJSON = os.path.join(inDirectory, filename)

        # Get the name of the output feature class
        feature_class_name = os.path.splitext(filename)[0].replace(' ', '_').replace('-', '_')

        # Determine the geometry type based on the file name
        if 'point' in filename.lower():
            geometry_type = 'POINT'
        elif 'line' in filename.lower():
            geometry_type = 'POLYLINE'
            geometry_type = 'POLYGON'

        # Convert GeoJSON to features and save to output geodatabase
        arcpy.conversion.JSONToFeatures(inputGeoJSON, os.path.join(outputGDB, feature_class_name), geometry_type=geometry_type)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment