Skip to content

Instantly share code, notes, and snippets.

@RalucaNicola
Created November 6, 2024 15:14
Show Gist options
  • Save RalucaNicola/b3d2c68c943bb94f6d5e9d65dbd9169e to your computer and use it in GitHub Desktop.
Save RalucaNicola/b3d2c68c943bb94f6d5e9d65dbd9169e to your computer and use it in GitHub Desktop.
from arcgis.gis import GIS
gis = GIS("home")
item_title = 'geodesignTest'
#upload geojson
geojson_item = gis.content.add({
'type': 'GeoJson',
'title': item_title
}, data='/arcgis/home/igcTestData/brian-repro-polygons-lines.geojson')
# define fields and types
fields = [
{"name": "project_or_policy", "type": "esriFieldTypeString", "sqlType": "sqlTypeNVarchar"},
{"name": "diagram_name", "type": "esriFieldTypeString", "sqlType": "sqlTypeNVarchar"},
{"name": "color", "type": "esriFieldTypeString", "sqlType": "sqlTypeNVarchar"},
{"name": "diagram_id", "type": "esriFieldTypeInteger", "sqlType": "sqlTypeInteger"},
{"name": "tag_codes", "type": "esriFieldTypeString", "sqlType": "sqlTypeNVarchar"},
{"name": "start_date", "type": "esriFieldTypeDate", "sqlType": "sqlTypeDate"},
{"name": "end_date", "type": "esriFieldTypeDate", "sqlType": "sqlTypeDate"},
{"name": "notes", "type": "esriFieldTypeString", "sqlType": "sqlTypeNVarchar"},
{"name": "grid_location", "type": "esriFieldTypeString", "sqlType": "sqlTypeNVarchar"},
{"name": "system_name", "type": "esriFieldTypeString", "sqlType": "sqlTypeNVarchar"},
{"name": "ObjectID", "type": "esriFieldTypeOID", "sqlType": "sqlTypeOther"},
{"name": "Shape__Area", "type": "esriFieldTypeDouble", "sqlType": "sqlTypeDouble"},
{"name": "Shape__Length", "type": "esriFieldTypeDouble", "sqlType": "sqlTypeDouble"}
]
# transform to Esri field schema
def transform_field(field):
transformed_field = {
"name": field["name"],
"type": field["type"],
"alias": field["name"].replace("_", " ").capitalize(),
"sqlType": field["sqlType"],
"Noneable": True,
"editable": False,
"domain": None,
"defaultValue": None
}
if (field["type"] == "esriFieldTypeString"):
transformed_field["length"] = 255
if (field["type"] == "esriFieldTypeDate"):
transformed_field["length"] = 0
transformed_field["precision"] = 1
return transformed_field
esri_fields_schema = list(map(transform_field, fields))
# prepare publish parameters
publish_params = {
"type": "geojson",
"name": item_title,
"layers": [
{
"name": f"{item_title}_polygons",
"type": "Feature Layer",
"geometryType": "esriGeometryPolygon",
"objectIdField": "ObjectId",
"fields": esri_fields_schema
},
{
"name": f"{item_title}_points",
"type": "Feature Layer",
"geometryType": "esriGeometryPoint",
"objectIdField": "ObjectId",
"fields": esri_fields_schema
},
{
"name": f"{item_title}_lines",
"type": "Feature Layer",
"geometryType": "esriGeometryPolyline",
"objectIdField": "ObjectId",
"fields": esri_fields_schema
}
]
}
# publish as feature layers
feature_layer_item = geojson_item.publish(publish_parameters = publish_params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment