Last active
April 19, 2021 20:47
-
-
Save d-wasserman/070ec800584d18a22e1b5a636ca183b7 to your computer and use it in GitHub Desktop.
This gist is intended to share a sample worker function to write Geojson to an ArcGIS Feature Class using Arcpy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def write_geojson_to_records(geojson): | |
"""Will write geojson to a list of dictionaries with geometry keys holding coordinates and storing the properties | |
to dictionaries. | |
@param: geojson- geojson file to write to records.""" | |
gjson_data = json.load(geojson, encoding='utf-8') | |
records = [] | |
arcpy.AddMessage("Geojson being read by row...") | |
for feature in gjson_data["features"]: | |
try: | |
row = {} | |
row["geometry"] = feature["geometry"] | |
row["type"] = feature["geometry"]["type"] | |
feat_dict = feature["properties"] | |
for prop in feat_dict: | |
row[prop] = feat_dict[prop] | |
records.append(row) | |
except: | |
pass | |
return records | |
def write_geojson_to_feature_class(geojson, output_fc, projection=arcpy.SpatialReference(4326)): | |
"""This function will write a geojson file to an output feature class in the chosen projection. | |
@param: geojson- geojson file to read | |
@param: output_fc - output feature class to write | |
@param: projection - spatial reference of output file""" | |
arcpy.env.overwriteOutput = True | |
records = write_geojson_to_records(geojson) | |
path = os.path.split(output_fc)[0] | |
name = os.path.split(output_fc)[1] | |
type = records[0]["type"] | |
arctype = None | |
if type == "FeatureCollection": | |
arcpy.AddWarning("FeatureCollections break the point,line, and polygon models of feature classes. Attempting " | |
"to write features as points.") | |
arctype = "POINT" | |
elif type == "LineString": | |
arctype = "POLYLINE" | |
else: | |
arctype = str(type).upper() | |
arcpy.AddMessage("Creating new feature class...") | |
arcpy.CreateFeatureclass_management(path, name, arctype, spatial_reference=projection) | |
record_dict = records[0] | |
for key in record_dict: | |
if key == "geometry": | |
continue | |
element = record_dict[key] | |
field_type = "TEXT" | |
try: | |
num_element = float(element) | |
if isinstance(num_element, float): | |
field_type = "DOUBLE" | |
if isinstance(element, int): | |
field_type = "LONG" | |
except: | |
pass | |
arcpy.AddField_management(output_fc, arcpy.ValidateFieldName(key, path), field_type) | |
field_list = [f.name for f in arcpy.ListFields((output_fc)) if f.type not in ["OID", "Geometry"] | |
and f.name.lower() not in ["shape_area", "shape_length"]] | |
fields = ["SHAPE@"] + field_list | |
arcpy.AddMessage("Writing records to feature class...") | |
with arcpy.da.InsertCursor(output_fc, fields) as icursor: | |
for record in records: | |
new_row = [] | |
for field in fields: | |
if field == "SHAPE@": | |
try: | |
geom = arcpy.AsShape(record.setdefault("geometry", None)) | |
except: | |
geom = None | |
new_row.append(geom) | |
else: | |
new_row.append(record.setdefault(str(field), None)) | |
icursor.insertRow(new_row) | |
return output_fc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment