Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. def write_geojson_to_records(geojson):
  2. """Will write geojson to a list of dictionaries with geomtry keys holding coordinates and storing the properties
  3. to dictionaries."""
  4. gjson_data = json.loads(geojson, encoding='utf-8')
  5. records = []
  6. arc_print("Geojson being read by row...")
  7. for feature in gjson_data["features"]:
  8. try:
  9. row = {}
  10. row["geometry"] = feature["geometry"]
  11. row["type"] = feature["geometry"]["type"]
  12. feat_dict = feature["properties"]
  13. for prop in feat_dict:
  14. row[prop] = feat_dict[prop]
  15. records.append(row)
  16. except:
  17. pass
  18. return records
  19.  
  20. def write_geojson_to_feature_class(geojson, output_fc, projection=arcpy.SpatialReference(4326)):
  21. """This function will write a geojson file to an output feature class in the chosen projection. """
  22. records = write_geojson_to_records(geojson)
  23. path = os.path.split(output_fc)[0]
  24. name = os.path.split(output_fc)[1]
  25. type = records[0]["type"]
  26. arctype = None
  27. if type == "FeatureCollection":
  28. arcpy.AddWarning("FeatureCollections break the point,line, and polygon models of feature classes. Attempting "
  29. "to write features as points.")
  30. arctype = "POINT"
  31. elif type == "LineString":
  32. arctype = "POLYLINE"
  33. else:
  34. arctype = str(type).upper()
  35. arc_print("Creating new feature class...")
  36. arcpy.CreateFeatureclass_management(path, name, arctype, spatial_reference=projection)
  37. record_dict = records[0]
  38. for key in record_dict:
  39. if key == "geometry":
  40. continue
  41. element = record_dict[key]
  42. field_type = "TEXT"
  43. try:
  44. num_element = float(element)
  45. if isinstance(num_element, float):
  46. field_type = "DOUBLE"
  47. if isinstance(element, int):
  48. field_type = "LONG"
  49. except:
  50. pass
  51. add_new_field(output_fc, arcpy.ValidateFieldName(key, path), field_type)
  52. fields = ["SHAPE@"] + get_fields(output_fc)
  53. arc_print("Writing records to feature class...")
  54. with arcpy.da.InsertCursor(output_fc, fields) as icursor:
  55. for record in records:
  56. new_row = []
  57. for field in fields:
  58. if field == "SHAPE@":
  59. try:
  60. geom = arcpy.AsShape(record.setdefault("geometry", None))
  61. except:
  62. geom = None
  63. new_row.append(geom)
  64. else:
  65. new_row.append(record.setdefault(str(field), None))
  66. icursor.insertRow(new_row)
  67. return output_fc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement