从头开始创建FeatureLayer

问题描述 投票:0回答:1

我正在尝试使用python arcgis软件包创建arcgis.features.FeatureLayer的新实例。

[我发现这是不可能的,我遍历documentationexamplesmore examples,尝试了不同的尝试,发现自己转了一圈,慢慢地,我开始听到我的声音了...

这是我所拥有的:

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)

至此-我不明白为什么it.layers会产生空结果。我相信item_properties格式不正确,导致arcgis忽略了我的图层定义...但是我无处可查它的外观。我想我想使用arcgis中的某些东西来为我生成层定义,而不是亲自手工制作JSON,这样可以保证将来的发展。

稍后我要对该项目进行的操作是:

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

此操作失败,并显示TypeError: item must be a type of service, not Feature Collection

所以我认为我可以在这里发布并使用它。

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

我需要FeatureLayer才能调用append(这样,每次我要推送的新内容都可以抛出额外的数据)

但是令我惊讶的是,我什至无法发布该项目,并且得到了Exception: Job failed.(更令人惊讶的是,该项目实际上已发布,因为我可以通过Content Manager网站看到它)

我也尝试创建“ CSV”项目类型:

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)

但是这会导致缺少层的项目,从而导致未处理的异常IndexError: list index out of range(因为方法arcgis调用尝试进入到为空的层...]

请帮助...

python arcgis esri
1个回答
0
投票

我设法得到sort-of我所需要的...但是我仍在尝试了解如何使arcgis直接从我的硬盘而不是另一个硬盘上上传一些数据项目。无论如何,这里是代码:

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)

因此要将一些数据附加到现有项目中,我必须创建新项目...


编辑:

要上传数据而不必创建临时项目,请使用edit_features而不是append

# create table like shown in sample above

from arcgis import features
f = features.Feature.from_dict({"attributes": {"some_attribute": "value"}})
fs = features.FeatureSet(features=[f])
table.edit_features(adds=fs)

© www.soinside.com 2019 - 2024. All rights reserved.