Skip to content

Instantly share code, notes, and snippets.

@odoe
Created September 10, 2012 21:13
Show Gist options
  • 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. http://esritogeo.herokuapp.com/
# 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
# http://help.arcgis.com/en/arcgisdesktop/10.0/help/000v/000v00000153000000.htm
# 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
# http://help.arcgis.com/en/arcgisdesktop/10.0/help/0017/001700000035000000.htm
arcpy.CopyFeatures_management(geom, feature)
# refer to the AddField docs
# http://help.arcgis.com/en/arcgisdesktop/10.0/help/0017/001700000047000000.htm
arcpy.AddField_management(feature, "ID_FIELD", "TEXT", 12, "", "", "ID_FIELD", "NULLABLE")
# refer to the CalculateField docs
# http://help.arcgis.com/en/arcgisdesktop/10.0/help/0017/00170000004m000000.htm
try:
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
items.append(feature)
del geom
target = "all_features"
# refer to Merge docs
# http://help.arcgis.com/en/arcgisdesktop/10.0/help/0017/001700000055000000.htm
arcpy.Merge_management(items, target)
# then loop my list and
# delete my individual features
# http://help.arcgis.com/en/arcgisdesktop/10.0/help/0017/001700000052000000.htm
# 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.
@d-wasserman
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?
https://gist.github.com/Holisticnature/070ec800584d18a22e1b5a636ca183b7

@Dansio
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'
        else:
            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