Select to view content in your preferred language

Create Feature Service with Feature Layer and Table from Shapefile and CSV

1923
3
Jump to solution
04-29-2022 07:09 AM
Clubdebambos
Frequent Contributor

I have a point shapefile with one field, a unique id called reg_no. I have a csv file with a reg_no column and other fields. I want to create a Feature Service in AGOL with the point geometry as a Feature Layer and the csv file as a Table. The goal is to do this purely with the ArcGIS API for Python. I do not want to use ArcMap/Pro or arcpy. I can add the shapefile and csv separately to AGOL and create separate Feature Services, but cannot get them in the same Feature Service. Once in the same Feature Service I will create a relationship between the Feature Layer and the Table so when you click on a point in a webmap you can access the Table data by selecting Show Related Records option in the popup.

I have tried adding the shapefile and the csv file to a zipped folder, adding to AGOL and publishing, as shown below, but this simply adds the shapefile as a point layer in the Feature Service. When looking at the contents I can see the shapefile item has been added from the zipped folder but nothing happens the csv file.

from arcgis import GIS

agol = GIS("home")
niah_zip = r"C:\Users\*****\Documents\Training\Data\NIAH.zip"
niah_agol = agol.content.add({}, niah_zip)
niah_fs = niah_agol.publish()
~ learn.finaldraftmapping.com
0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Frequent Contributor

Just to confirm that this workflow does work. First you get the table definition as a dictionary

hts = agol.content.get("***item_id***")
table = Table(hts.tables[0].url, agol)

table_def = dict(table.properties)

 

Then add that to the Feature Layer Collection 

fs = agol.content.get("***item_id***")

flc = FeatureLayerCollection(fs.url, agol)

flc.manager.add_to_definition({"tables" : [table_def]})

 

Use the Content Manager analyze function on the original CSV uploaded

csv = agol.content.analyze(
    item ="***item_id***",
    file_type = "csv",
    geocoding_service = None,
    location_type = None
)

 

And append in data...

print(tbl.append(
    item_id = "***item_id***",
    upload_format = "csv",
    source_info = csv,
    upsert = False
))

 

~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
3 Replies
HamishMorton
Esri Contributor

Hi @Clubdebambos,

Have you considered using the manager class of the FeatureLayerCollection object? You could add a table to the definition created when publishing your shapefile, then append your CSV data.

arcgis.features.managers module — arcgis 2.0.0 documentation

Hamish
Clubdebambos
Frequent Contributor

Thanks for the reply @HamishMorton, I did consider it, I was hoping there was a way to zip up shapefiles and CSVs and tell AGOL how to publish. Or even publish separately; the shapefile and the CSV as a table in two different feature services and combine easily. But it looks like you're correct, the best way is probably to add the structure/schema to the definition and then append in, and delete any items not required after that. Cheers.

~ learn.finaldraftmapping.com
0 Kudos
Clubdebambos
Frequent Contributor

Just to confirm that this workflow does work. First you get the table definition as a dictionary

hts = agol.content.get("***item_id***")
table = Table(hts.tables[0].url, agol)

table_def = dict(table.properties)

 

Then add that to the Feature Layer Collection 

fs = agol.content.get("***item_id***")

flc = FeatureLayerCollection(fs.url, agol)

flc.manager.add_to_definition({"tables" : [table_def]})

 

Use the Content Manager analyze function on the original CSV uploaded

csv = agol.content.analyze(
    item ="***item_id***",
    file_type = "csv",
    geocoding_service = None,
    location_type = None
)

 

And append in data...

print(tbl.append(
    item_id = "***item_id***",
    upload_format = "csv",
    source_info = csv,
    upsert = False
))

 

~ learn.finaldraftmapping.com
0 Kudos