Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement