Forked from d-wasserman/Write Geojson To Feature Class.py
Created
March 14, 2018 15:59
-
-
Save pyRobShrk/902b8ee02eec1c36b61d26539709ae5e 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 hidden or 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 geomtry keys holding coordinates and storing the properties | |
to dictionaries.""" | |
gjson_data = json.loads(geojson, encoding='utf-8') | |
records = [] | |
arc_print("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. """ | |
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() | |
arc_print("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 | |
add_new_field(output_fc, arcpy.ValidateFieldName(key, path), field_type) | |
fields = ["SHAPE@"] + get_fields(output_fc) | |
arc_print("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