AnsweredAssumed Answered

How to create FeatureLayer from scratch

Question asked by unleashlive on Jan 3, 2020
Latest reply on Jan 6, 2020 by bixb0012
Branched to a new discussion

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-item-into-target-feature-layer-attribute-field) 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...

Outcomes