Select to view content in your preferred language

How to create FeatureLayer from scratch

3234
4
Jump to solution
01-03-2020 04:07 AM
JamesKwong1
Occasional Contributor

I'm trying to create new instance of [arcgis.features.FeatureLayer](https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayer) using python arcgis package.

I find it impossible, I'm traversing through [documentation](https://developers.arcgis.com/python/api-reference) and [examples](https://developers.arcgis.com/python/guide/appending-features/#Append-attribute-values-from-the-csv-...) trying different things and I find myself going round in circles and slowly I start pulling my hear out...

This is what I have:

import arcgis
import json
from arcgis.gis import GIS
g = GIS(username="my_uname", password="my_pwd")
prop = arcgis.features.FeatureCollection(dictdata={'attributes': {'foo': 'bar', 'lorem': 'ipsum'}})
prop_json=json.dumps({"featureCollection": {"layers": [dict(prop.properties)]}})
item_properties={"type": "Feature Collection", "title": "test_feature_collection_01", "text": prop_json}
it = g.content.add(item_properties=item_properties)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

At this point - I can't understand why `it.layers` yields empty results. I believe `item_properties` is malformed resulting in `arcgis` ignoring my layers definition... but I have nowhere to check what it should look like. I figured I'd like to use something from `arcgis` to generate layer definition for me rather than to handcraft JSON myself so it's future-proof.

What I want to do with that item later is this:

lr = arcgis.features.FeatureLayer.fromitem(item=it)

This fails with `TypeError: item must be a type of service, not Feature Collection`

So I figured I could publish item and use it here.

pit = it.publish()
lr = arcgis.features.FeatureLayer.fromitem(item=pit)

‍‍‍

I need `FeatureLayer` to be able to call `append` (so I can just throw extra data each time I have anything new I want to push)

But to my surprise I cannot even publish the item and I'm getting `Exception: Job failed.` (to my bigger surprise item actually gets published as I can see it through content manager website)

I also tried creating "CSV" item type:

import arcgis
import json
from arcgis.gis import GIS


g = GIS(username="my_uname", password="my_pwd")
item_properties={"type": "CSV", "title": "test_feature_collection_01"}
it = g.content.add(item_properties=item_properties, data="/tmp/foo.csv")
pit = it.publish()

lr = arcgis.features.FeatureLayer.fromitem(item=pit)

however this results in layer-less item, which results in unhandled exception `IndexError: list index out of range` (because method `arcgis` tries to get to layers which is empty...)

Please help...

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Looking at your original post and follow-up, I think what you want to use is Table.edit_features() since it allows passing a locally created feature set to the table as "adds".

James Kwong‌, I branched your follow-up question to https://community.esri.com/thread/246146-re-how-to-create-featurelayer-from-scratch 

View solution in original post

4 Replies
JamesKwong1
Occasional Contributor

I managed to get ***sort-of*** what I need... however I'm still trying to understand how to make `arcgis` to upload some data directly from my hard drive rather than from another item. Anyways here is the code:

import arcgis
import json
from arcgis.gis import GIS


g = GIS(username="my_uname", password="my_pwd")


item_properties_1={"type": "CSV", "title": "test_feature_collection_01"}
it_1 = g.content.add(item_properties=item_properties_1, data="/tmp/my_data.csv")
pit_1 = it_1.publish()
table = arcgis.features.Table.fromitem(item=pit_1)


# Now we want to throw some extra data, do we have to create another item to do that??


item_properties_2={"type": "CSV", "title": "test_feature_collection_02"}
it_2 = g.content.add(item_properties=item_properties_2, data="/tmp/more_data.csv")
source_info = g.content.analyze(file_path='/tmp/more_data.csv', file_type='csv', location_type='none')


# Data from `it_2` appears to be appended to `table` container (which is published item, not item itself)


table.append(item_id=it_2.id, upload_format='csv', source_info=source_info["publishParameters"], upsert=False)


So to append some data to existing item I have to create new item...

So the question now is how to append with data I have at hand without creating new item?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Regarding:

I need `FeatureLayer` to be able to call `append`

It does, if you are using ArcGIS Online.  Are you using ArcGIS Online or Portal?

JamesKwong1
Occasional Contributor

Hi Joshua,

Indeed, I have put myself in wrong way. I meant that I need `FeatureLayer` so I can call append. If you look at my recent reply you will notice that indeed I'm able to call `append`. The problem I'm left with is if it's possible to use `append` without having to create item with new data.

Many thanks,

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Looking at your original post and follow-up, I think what you want to use is Table.edit_features() since it allows passing a locally created feature set to the table as "adds".

James Kwong‌, I branched your follow-up question to https://community.esri.com/thread/246146-re-how-to-create-featurelayer-from-scratch