Skip to content

ESRI Upload and update via api

Claiton edited this page Sep 14, 2023 · 2 revisions
#
gis = GIS(url_argis,username,password)

# Cria folder
folder_osm='filtered2osm'
gis.content.create_folder(folder_osm,owner=username)

# UPload shapefile
## metadata
item_properties = {"type":"Shapefile","title" : "EC-F-Azogues - Address points", "snippet" : "This layer contains the address points in the EC-F-Azogues.", "description" : "Azogues is  city  of the state of Cañar, located in Ecuador.", "licenseInfo" : "..", "tags" : "address_points, address", "accessInformation" : "San Francisco de Peleusí de Azogues; AddresForAll"}

## Url do shapefile zipado
data_location = 'http://dl.digital-guard.org/out/a4a_ec_f_azogues_geoaddress_21800000101101.zip'

## Upload ESRI
shp_item = gis.content.add(item_properties, data=data_location, folder=folder_osm)

## Cria thumbnail
shp_item.create_thumbnail(True)

## Atualiza permissões
shp_item.share(org = True, allow_members_to_edit = True)

shp_item

## ID do shapefile na ESRI
item_load_id = shp_item.id

## Categoriza o shapefile
gis.content.categories.assign_to_items(items = [{item_load_id : {"categories" : ["/Categories/Country/Ecuador","/Categories/Feature type/Address points"]}}])

# Publicação, cria um feature service
publish_item = shp_item.publish()

## Atualiza permissões
publish_item.share(org = True, groups = ["82b5c771d36f47a6939b95f1a8ae8f81"])

## ID do feature service
item_publish_id = publish_item.id

## Categoriza o feature service
gis.content.categories.assign_to_items(items = [{item_publish_id : {"categories" : ["/Categories/Country/Ecuador","/Categories/Feature type/Address points"]}}])

## Atualiza metadata do layer
feature_layer = publish_item.layers[0]
update_dict = {'displayField':'','name':'address_points', 'description':'This layer contains the address points in the EC-F-Azogues.','copyrightText':'San Francisco de Peleusí de Azogues; AddresForAll'}
feature_layer.manager.update_definition(update_dict)
# ALIAS. Tradução A4A -> OSM

## Dicionário de tradução
tr_dict = {}
tr_dict['address_points'] = {'hnum': 'addr:housenumber', 'via': 'addr:street'}
tr_dict['city_blocks']    = {}
tr_dict['road_network']   = {}
tr_dict['land_parcels']   = {}
tr_dict['neighborhood_boundaries'] = {}
tr_dict['building_footprints']     = {}

## Inicializa listas/dict auxiliares
old_fields = [dict(field) for field in feature_layer.properties.fields if field["name"] in (tr_dict[feature_layer.properties.name]).keys()]
new_fields = []

# Append listas/dict
for idx, old_field in enumerate(old_fields):
    print(old_field)
    expressions.append({"field": tr_dict[feature_layer.properties.name][old_field["name"]],"sqlExpression": old_field["name"],})
    delete_fields["fields"].append({"name": old_field["name"]})
    new_field = deepcopy(old_field)
    new_field['alias'] = tr_dict[feature_layer.properties.name][old_field["name"]]
    new_fields.append(new_field)

# Update campos
new_fields_add = feature_layer.manager.update_definition({"fields": new_fields})
print(new_fields_add)
# TROCA NOME Tradução A4A -> OSM

## Dicionário de tradução
tr_dict = {}
tr_dict['address_points'] = {'hnum': 'addr:housenumber', 'via': 'addr:street'}
tr_dict['city_blocks']    = {}
tr_dict['road_network']   = {}
tr_dict['land_parcels']   = {}
tr_dict['neighborhood_boundaries'] = {}
tr_dict['building_footprints']     = {}

## Inicializa listas/dict auxiliares
old_fields = [dict(field) for field in feature_layer.properties.fields if field["name"] in (tr_dict[feature_layer.properties.name]).keys()]
delete_fields = {"fields": []}
new_fields    = []
expressions   = []

# Append listas/dict
for idx, old_field in enumerate(old_fields):
    print(old_field)
    expressions.append({"field": tr_dict[feature_layer.properties.name][old_field["name"]],"sqlExpression": old_field["name"],})
    delete_fields["fields"].append({"name": old_field["name"]})
    new_field = deepcopy(old_field)
    new_field['name']  = tr_dict[feature_layer.properties.name][old_field["name"]]
    new_field['alias'] = tr_dict[feature_layer.properties.name][old_field["name"]]
    new_fields.append(new_field)


# Adiciona novos campos
new_fields_add = feature_layer.manager.add_to_definition({"fields": new_fields})
print(new_fields_add)

# Copia valores dos campos antigos para os novos
copy_values = feature_layer.calculate(where="1=1", calc_expression=expressions)

# Apaga campos antigos
print(feature_layer.manager.delete_from_definition(delete_fields))


feature_layer._refresh()